-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separating ui from main file and adding more tests
- Loading branch information
1 parent
9ea484e
commit 9d33b16
Showing
5 changed files
with
182 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.iluwatar.bloc; | ||
|
||
import java.awt.BorderLayout; | ||
import java.awt.Font; | ||
import javax.swing.JButton; | ||
import javax.swing.JFrame; | ||
import javax.swing.JLabel; | ||
import javax.swing.SwingConstants; | ||
import javax.swing.WindowConstants; | ||
|
||
/** | ||
* The BlocUI class handles the creation and management of the UI components. | ||
*/ | ||
public class BlocUi { | ||
|
||
/** | ||
* Creates and shows the UI. | ||
*/ | ||
public void createAndShowUi() { | ||
// Create a Bloc instance to manage the state | ||
final Bloc bloc = new Bloc(); | ||
|
||
// setting up a frame window with a title | ||
JFrame frame = new JFrame("BloC example"); | ||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | ||
frame.setSize(400, 300); | ||
|
||
// label to display the counter value | ||
JLabel counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER); | ||
counterLabel.setFont(new Font("Arial", Font.BOLD, 20)); | ||
|
||
// buttons for increment, decrement, and toggling listener | ||
JButton decrementButton = new JButton("Decrement"); | ||
JButton toggleListenerButton = new JButton("Disable Listener"); | ||
JButton incrementButton = new JButton("Increment"); | ||
|
||
frame.setLayout(new BorderLayout()); | ||
frame.add(counterLabel, BorderLayout.CENTER); | ||
frame.add(incrementButton, BorderLayout.NORTH); | ||
frame.add(decrementButton, BorderLayout.SOUTH); | ||
frame.add(toggleListenerButton, BorderLayout.EAST); | ||
|
||
// making a state listener to update the counter label when the state changes | ||
StateListener<State> stateListener = state -> counterLabel.setText("Counter: " + state.getValue()); | ||
|
||
// adding the listener to the Bloc instance | ||
bloc.addListener(stateListener); | ||
|
||
toggleListenerButton.addActionListener(e -> { | ||
if (bloc.getListeners().contains(stateListener)) { | ||
bloc.removeListener(stateListener); | ||
toggleListenerButton.setText("Enable Listener"); | ||
} else { | ||
bloc.addListener(stateListener); | ||
toggleListenerButton.setText("Disable Listener"); | ||
} | ||
}); | ||
|
||
incrementButton.addActionListener(e -> bloc.increment()); | ||
decrementButton.addActionListener(e -> bloc.decrement()); | ||
|
||
frame.setVisible(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,18 @@ | ||
package com.iluwatar.bloc; | ||
import java.awt.BorderLayout; | ||
import java.awt.Font; | ||
import javax.swing.JButton; | ||
import javax.swing.JFrame; | ||
import javax.swing.JLabel; | ||
import javax.swing.SwingConstants; | ||
import javax.swing.WindowConstants; | ||
|
||
|
||
|
||
|
||
/** | ||
* The Main class demonstrates the use of the Bloc pattern in a simple GUI application. | ||
* It creates a JFrame with buttons to increment, decrement, and toggle a listener | ||
* that updates the counter value on the screen. | ||
* It initializes the UI and sets up actions for the buttons and listener management. | ||
*/ | ||
public class Main { | ||
|
||
/** | ||
* The entry point of the application. Initializes the GUI and sets up actions | ||
* for the buttons and listener management. | ||
* The entry point of the application. Initializes the GUI. | ||
* | ||
* @param args command-line arguments (not used in this example) | ||
*/ | ||
public static void main(String[] args) { | ||
// Create a Bloc instance to manage the state | ||
|
||
|
||
// Create and set up the JFrame | ||
JFrame frame = new JFrame("BloC example"); | ||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | ||
frame.setSize(400, 300); | ||
|
||
// Create a label to display the counter value | ||
JLabel counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER); | ||
counterLabel.setFont(new Font("Arial", Font.BOLD, 20)); | ||
|
||
// Create buttons for increment, decrement, and toggling listener | ||
JButton incrementButton = new JButton("Increment"); | ||
JButton decrementButton = new JButton("Decrement"); | ||
JButton toggleListenerButton = new JButton("Disable Listener"); | ||
|
||
// Set layout and add components to the frame | ||
frame.setLayout(new BorderLayout()); | ||
frame.add(counterLabel, BorderLayout.CENTER); | ||
frame.add(incrementButton, BorderLayout.NORTH); | ||
frame.add(decrementButton, BorderLayout.SOUTH); | ||
frame.add(toggleListenerButton, BorderLayout.EAST); | ||
Bloc bloC = new Bloc(); | ||
// Create a state listener to update the counter label when the state changes | ||
StateListener<State> stateListener = state -> counterLabel.setText("Counter: " + state.getValue()); | ||
|
||
// Add the listener to the Bloc instance | ||
bloC.addListener(stateListener); | ||
|
||
// Set action listeners for buttons | ||
toggleListenerButton.addActionListener(e -> { | ||
if (bloC.getListeners().contains(stateListener)) { | ||
bloC.removeListener(stateListener); | ||
toggleListenerButton.setText("Enable Listener"); | ||
} else { | ||
bloC.addListener(stateListener); | ||
toggleListenerButton.setText("Disable Listener"); | ||
} | ||
}); | ||
|
||
incrementButton.addActionListener(e -> bloC.increment()); | ||
decrementButton.addActionListener(e -> bloC.decrement()); | ||
|
||
// Make the frame visible | ||
frame.setVisible(true); | ||
BlocUi blocUi = new BlocUi(); | ||
blocUi.createAndShowUi(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.iluwatar.bloc; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class BlocUiTest { | ||
|
||
private JFrame frame; | ||
private JLabel counterLabel; | ||
private JButton incrementButton; | ||
private JButton decrementButton; | ||
private JButton toggleListenerButton; | ||
private Bloc bloc; | ||
private StateListener<State> stateListener; | ||
|
||
@Before | ||
public void setUp() { | ||
bloc = new Bloc(); // Re-initialize the Bloc for each test | ||
|
||
frame = new JFrame("BloC example"); | ||
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | ||
frame.setSize(400, 300); | ||
|
||
counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER); | ||
counterLabel.setFont(new Font("Arial", Font.BOLD, 20)); | ||
|
||
incrementButton = new JButton("Increment"); | ||
decrementButton = new JButton("Decrement"); | ||
toggleListenerButton = new JButton("Disable Listener"); | ||
|
||
frame.setLayout(new BorderLayout()); | ||
frame.add(counterLabel, BorderLayout.CENTER); | ||
frame.add(incrementButton, BorderLayout.NORTH); | ||
frame.add(decrementButton, BorderLayout.SOUTH); | ||
frame.add(toggleListenerButton, BorderLayout.EAST); | ||
|
||
stateListener = state -> counterLabel.setText("Counter: " + state.getValue()); | ||
bloc.addListener(stateListener); | ||
|
||
incrementButton.addActionListener(e -> bloc.increment()); | ||
decrementButton.addActionListener(e -> bloc.decrement()); | ||
toggleListenerButton.addActionListener(e -> { | ||
if (bloc.getListeners().contains(stateListener)) { | ||
bloc.removeListener(stateListener); | ||
toggleListenerButton.setText("Enable Listener"); | ||
} else { | ||
bloc.addListener(stateListener); | ||
toggleListenerButton.setText("Disable Listener"); | ||
} | ||
}); | ||
|
||
frame.setVisible(true); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
frame.dispose(); | ||
bloc = new Bloc(); // Reset Bloc state after each test to avoid state carryover | ||
} | ||
|
||
|
||
@Test | ||
public void testIncrementButton() { | ||
simulateButtonClick(incrementButton); | ||
assertEquals("Counter: 1", counterLabel.getText()); | ||
} | ||
|
||
@Test | ||
public void testDecrementButton() { | ||
simulateButtonClick(decrementButton); | ||
assertEquals("Counter: -1", counterLabel.getText()); | ||
} | ||
|
||
@Test | ||
public void testToggleListenerButton() { | ||
// Disable listener | ||
simulateButtonClick(toggleListenerButton); | ||
simulateButtonClick(incrementButton); | ||
assertEquals("Counter: 0", counterLabel.getText()); // Listener is disabled | ||
|
||
// Enable listener | ||
simulateButtonClick(toggleListenerButton); | ||
simulateButtonClick(incrementButton); | ||
assertEquals("Counter: 2", counterLabel.getText()); // Listener is re-enabled | ||
} | ||
|
||
private void simulateButtonClick(JButton button) { | ||
for (var listener : button.getActionListeners()) { | ||
listener.actionPerformed(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,20 @@ | ||
package com.iluwatar.bloc; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import static org.mockito.Mockito.mockStatic; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class MainTest { | ||
|
||
private JFrame frame; | ||
private JLabel counterLabel; | ||
private JButton incrementButton; | ||
private JButton decrementButton; | ||
private JButton toggleListenerButton; | ||
private Bloc bloc; | ||
private StateListener<State> stateListener; | ||
|
||
@Before | ||
public void setUp() { | ||
bloc = new Bloc(); // Re-initialize the Bloc for each test | ||
|
||
frame = new JFrame("BloC example"); | ||
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | ||
frame.setSize(400, 300); | ||
|
||
counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER); | ||
counterLabel.setFont(new Font("Arial", Font.BOLD, 20)); | ||
|
||
incrementButton = new JButton("Increment"); | ||
decrementButton = new JButton("Decrement"); | ||
toggleListenerButton = new JButton("Disable Listener"); | ||
|
||
frame.setLayout(new BorderLayout()); | ||
frame.add(counterLabel, BorderLayout.CENTER); | ||
frame.add(incrementButton, BorderLayout.NORTH); | ||
frame.add(decrementButton, BorderLayout.SOUTH); | ||
frame.add(toggleListenerButton, BorderLayout.EAST); | ||
|
||
stateListener = state -> counterLabel.setText("Counter: " + state.getValue()); | ||
bloc.addListener(stateListener); | ||
|
||
incrementButton.addActionListener(e -> bloc.increment()); | ||
decrementButton.addActionListener(e -> bloc.decrement()); | ||
toggleListenerButton.addActionListener(e -> { | ||
if (bloc.getListeners().contains(stateListener)) { | ||
bloc.removeListener(stateListener); | ||
toggleListenerButton.setText("Enable Listener"); | ||
} else { | ||
bloc.addListener(stateListener); | ||
toggleListenerButton.setText("Disable Listener"); | ||
} | ||
}); | ||
|
||
frame.setVisible(true); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
frame.dispose(); | ||
bloc = new Bloc(); // Reset Bloc state after each test to avoid state carryover | ||
} | ||
|
||
|
||
@Test | ||
public void testIncrementButton() { | ||
simulateButtonClick(incrementButton); | ||
assertEquals("Counter: 1", counterLabel.getText()); | ||
} | ||
class MainTest { | ||
|
||
@Test | ||
public void testDecrementButton() { | ||
simulateButtonClick(decrementButton); | ||
assertEquals("Counter: -1", counterLabel.getText()); | ||
} | ||
|
||
@Test | ||
public void testToggleListenerButton() { | ||
// Disable listener | ||
simulateButtonClick(toggleListenerButton); | ||
simulateButtonClick(incrementButton); | ||
assertEquals("Counter: 0", counterLabel.getText()); // Listener is disabled | ||
|
||
// Enable listener | ||
simulateButtonClick(toggleListenerButton); | ||
simulateButtonClick(incrementButton); | ||
assertEquals("Counter: 2", counterLabel.getText()); // Listener is re-enabled | ||
} | ||
void testMain() { | ||
try (var mockedBlocUi = mockStatic(BlocUi.class)) { | ||
// Call the main method | ||
Main.main(new String[]{}); | ||
|
||
private void simulateButtonClick(JButton button) { | ||
for (var listener : button.getActionListeners()) { | ||
listener.actionPerformed(null); | ||
// Verify that createAndShowUi was called | ||
mockedBlocUi.verify(() -> new BlocUi().createAndShowUi()); | ||
} | ||
} | ||
} | ||
} |