Skip to content

Commit 9d33b16

Browse files
committed
separating ui from main file and adding more tests
1 parent 9ea484e commit 9d33b16

File tree

5 files changed

+182
-149
lines changed

5 files changed

+182
-149
lines changed

bloC/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,10 @@
3636
<artifactId>assertj-core</artifactId>
3737
<scope>test</scope>
3838
</dependency>
39+
<dependency>
40+
<groupId>org.mockito</groupId>
41+
<artifactId>mockito-inline</artifactId>
42+
<scope>test</scope>
43+
</dependency>
3944
</dependencies>
4045
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.iluwatar.bloc;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.Font;
5+
import javax.swing.JButton;
6+
import javax.swing.JFrame;
7+
import javax.swing.JLabel;
8+
import javax.swing.SwingConstants;
9+
import javax.swing.WindowConstants;
10+
11+
/**
12+
* The BlocUI class handles the creation and management of the UI components.
13+
*/
14+
public class BlocUi {
15+
16+
/**
17+
* Creates and shows the UI.
18+
*/
19+
public void createAndShowUi() {
20+
// Create a Bloc instance to manage the state
21+
final Bloc bloc = new Bloc();
22+
23+
// setting up a frame window with a title
24+
JFrame frame = new JFrame("BloC example");
25+
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
26+
frame.setSize(400, 300);
27+
28+
// label to display the counter value
29+
JLabel counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER);
30+
counterLabel.setFont(new Font("Arial", Font.BOLD, 20));
31+
32+
// buttons for increment, decrement, and toggling listener
33+
JButton decrementButton = new JButton("Decrement");
34+
JButton toggleListenerButton = new JButton("Disable Listener");
35+
JButton incrementButton = new JButton("Increment");
36+
37+
frame.setLayout(new BorderLayout());
38+
frame.add(counterLabel, BorderLayout.CENTER);
39+
frame.add(incrementButton, BorderLayout.NORTH);
40+
frame.add(decrementButton, BorderLayout.SOUTH);
41+
frame.add(toggleListenerButton, BorderLayout.EAST);
42+
43+
// making a state listener to update the counter label when the state changes
44+
StateListener<State> stateListener = state -> counterLabel.setText("Counter: " + state.getValue());
45+
46+
// adding the listener to the Bloc instance
47+
bloc.addListener(stateListener);
48+
49+
toggleListenerButton.addActionListener(e -> {
50+
if (bloc.getListeners().contains(stateListener)) {
51+
bloc.removeListener(stateListener);
52+
toggleListenerButton.setText("Enable Listener");
53+
} else {
54+
bloc.addListener(stateListener);
55+
toggleListenerButton.setText("Disable Listener");
56+
}
57+
});
58+
59+
incrementButton.addActionListener(e -> bloc.increment());
60+
decrementButton.addActionListener(e -> bloc.decrement());
61+
62+
frame.setVisible(true);
63+
}
64+
}
Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,18 @@
11
package com.iluwatar.bloc;
2-
import java.awt.BorderLayout;
3-
import java.awt.Font;
4-
import javax.swing.JButton;
5-
import javax.swing.JFrame;
6-
import javax.swing.JLabel;
7-
import javax.swing.SwingConstants;
8-
import javax.swing.WindowConstants;
9-
10-
11-
122

133
/**
144
* The Main class demonstrates the use of the Bloc pattern in a simple GUI application.
15-
* It creates a JFrame with buttons to increment, decrement, and toggle a listener
16-
* that updates the counter value on the screen.
5+
* It initializes the UI and sets up actions for the buttons and listener management.
176
*/
187
public class Main {
198

209
/**
21-
* The entry point of the application. Initializes the GUI and sets up actions
22-
* for the buttons and listener management.
10+
* The entry point of the application. Initializes the GUI.
2311
*
2412
* @param args command-line arguments (not used in this example)
2513
*/
2614
public static void main(String[] args) {
27-
// Create a Bloc instance to manage the state
28-
29-
30-
// Create and set up the JFrame
31-
JFrame frame = new JFrame("BloC example");
32-
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
33-
frame.setSize(400, 300);
34-
35-
// Create a label to display the counter value
36-
JLabel counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER);
37-
counterLabel.setFont(new Font("Arial", Font.BOLD, 20));
38-
39-
// Create buttons for increment, decrement, and toggling listener
40-
JButton incrementButton = new JButton("Increment");
41-
JButton decrementButton = new JButton("Decrement");
42-
JButton toggleListenerButton = new JButton("Disable Listener");
43-
44-
// Set layout and add components to the frame
45-
frame.setLayout(new BorderLayout());
46-
frame.add(counterLabel, BorderLayout.CENTER);
47-
frame.add(incrementButton, BorderLayout.NORTH);
48-
frame.add(decrementButton, BorderLayout.SOUTH);
49-
frame.add(toggleListenerButton, BorderLayout.EAST);
50-
Bloc bloC = new Bloc();
51-
// Create a state listener to update the counter label when the state changes
52-
StateListener<State> stateListener = state -> counterLabel.setText("Counter: " + state.getValue());
53-
54-
// Add the listener to the Bloc instance
55-
bloC.addListener(stateListener);
56-
57-
// Set action listeners for buttons
58-
toggleListenerButton.addActionListener(e -> {
59-
if (bloC.getListeners().contains(stateListener)) {
60-
bloC.removeListener(stateListener);
61-
toggleListenerButton.setText("Enable Listener");
62-
} else {
63-
bloC.addListener(stateListener);
64-
toggleListenerButton.setText("Disable Listener");
65-
}
66-
});
67-
68-
incrementButton.addActionListener(e -> bloC.increment());
69-
decrementButton.addActionListener(e -> bloC.decrement());
70-
71-
// Make the frame visible
72-
frame.setVisible(true);
15+
BlocUi blocUi = new BlocUi();
16+
blocUi.createAndShowUi();
7317
}
74-
}
18+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.iluwatar.bloc;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import javax.swing.*;
8+
import java.awt.*;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class BlocUiTest {
13+
14+
private JFrame frame;
15+
private JLabel counterLabel;
16+
private JButton incrementButton;
17+
private JButton decrementButton;
18+
private JButton toggleListenerButton;
19+
private Bloc bloc;
20+
private StateListener<State> stateListener;
21+
22+
@Before
23+
public void setUp() {
24+
bloc = new Bloc(); // Re-initialize the Bloc for each test
25+
26+
frame = new JFrame("BloC example");
27+
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
28+
frame.setSize(400, 300);
29+
30+
counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER);
31+
counterLabel.setFont(new Font("Arial", Font.BOLD, 20));
32+
33+
incrementButton = new JButton("Increment");
34+
decrementButton = new JButton("Decrement");
35+
toggleListenerButton = new JButton("Disable Listener");
36+
37+
frame.setLayout(new BorderLayout());
38+
frame.add(counterLabel, BorderLayout.CENTER);
39+
frame.add(incrementButton, BorderLayout.NORTH);
40+
frame.add(decrementButton, BorderLayout.SOUTH);
41+
frame.add(toggleListenerButton, BorderLayout.EAST);
42+
43+
stateListener = state -> counterLabel.setText("Counter: " + state.getValue());
44+
bloc.addListener(stateListener);
45+
46+
incrementButton.addActionListener(e -> bloc.increment());
47+
decrementButton.addActionListener(e -> bloc.decrement());
48+
toggleListenerButton.addActionListener(e -> {
49+
if (bloc.getListeners().contains(stateListener)) {
50+
bloc.removeListener(stateListener);
51+
toggleListenerButton.setText("Enable Listener");
52+
} else {
53+
bloc.addListener(stateListener);
54+
toggleListenerButton.setText("Disable Listener");
55+
}
56+
});
57+
58+
frame.setVisible(true);
59+
}
60+
61+
@After
62+
public void tearDown() {
63+
frame.dispose();
64+
bloc = new Bloc(); // Reset Bloc state after each test to avoid state carryover
65+
}
66+
67+
68+
@Test
69+
public void testIncrementButton() {
70+
simulateButtonClick(incrementButton);
71+
assertEquals("Counter: 1", counterLabel.getText());
72+
}
73+
74+
@Test
75+
public void testDecrementButton() {
76+
simulateButtonClick(decrementButton);
77+
assertEquals("Counter: -1", counterLabel.getText());
78+
}
79+
80+
@Test
81+
public void testToggleListenerButton() {
82+
// Disable listener
83+
simulateButtonClick(toggleListenerButton);
84+
simulateButtonClick(incrementButton);
85+
assertEquals("Counter: 0", counterLabel.getText()); // Listener is disabled
86+
87+
// Enable listener
88+
simulateButtonClick(toggleListenerButton);
89+
simulateButtonClick(incrementButton);
90+
assertEquals("Counter: 2", counterLabel.getText()); // Listener is re-enabled
91+
}
92+
93+
private void simulateButtonClick(JButton button) {
94+
for (var listener : button.getActionListeners()) {
95+
listener.actionPerformed(null);
96+
}
97+
}
98+
}
Lines changed: 10 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,20 @@
11
package com.iluwatar.bloc;
22

3-
import org.junit.After;
4-
import org.junit.Before;
5-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
64

7-
import javax.swing.*;
8-
import java.awt.*;
5+
import static org.mockito.Mockito.mockStatic;
96

10-
import static org.junit.Assert.assertEquals;
117

12-
public class MainTest {
13-
14-
private JFrame frame;
15-
private JLabel counterLabel;
16-
private JButton incrementButton;
17-
private JButton decrementButton;
18-
private JButton toggleListenerButton;
19-
private Bloc bloc;
20-
private StateListener<State> stateListener;
21-
22-
@Before
23-
public void setUp() {
24-
bloc = new Bloc(); // Re-initialize the Bloc for each test
25-
26-
frame = new JFrame("BloC example");
27-
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
28-
frame.setSize(400, 300);
29-
30-
counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER);
31-
counterLabel.setFont(new Font("Arial", Font.BOLD, 20));
32-
33-
incrementButton = new JButton("Increment");
34-
decrementButton = new JButton("Decrement");
35-
toggleListenerButton = new JButton("Disable Listener");
36-
37-
frame.setLayout(new BorderLayout());
38-
frame.add(counterLabel, BorderLayout.CENTER);
39-
frame.add(incrementButton, BorderLayout.NORTH);
40-
frame.add(decrementButton, BorderLayout.SOUTH);
41-
frame.add(toggleListenerButton, BorderLayout.EAST);
42-
43-
stateListener = state -> counterLabel.setText("Counter: " + state.getValue());
44-
bloc.addListener(stateListener);
45-
46-
incrementButton.addActionListener(e -> bloc.increment());
47-
decrementButton.addActionListener(e -> bloc.decrement());
48-
toggleListenerButton.addActionListener(e -> {
49-
if (bloc.getListeners().contains(stateListener)) {
50-
bloc.removeListener(stateListener);
51-
toggleListenerButton.setText("Enable Listener");
52-
} else {
53-
bloc.addListener(stateListener);
54-
toggleListenerButton.setText("Disable Listener");
55-
}
56-
});
57-
58-
frame.setVisible(true);
59-
}
60-
61-
@After
62-
public void tearDown() {
63-
frame.dispose();
64-
bloc = new Bloc(); // Reset Bloc state after each test to avoid state carryover
65-
}
66-
67-
68-
@Test
69-
public void testIncrementButton() {
70-
simulateButtonClick(incrementButton);
71-
assertEquals("Counter: 1", counterLabel.getText());
72-
}
8+
class MainTest {
739

7410
@Test
75-
public void testDecrementButton() {
76-
simulateButtonClick(decrementButton);
77-
assertEquals("Counter: -1", counterLabel.getText());
78-
}
79-
80-
@Test
81-
public void testToggleListenerButton() {
82-
// Disable listener
83-
simulateButtonClick(toggleListenerButton);
84-
simulateButtonClick(incrementButton);
85-
assertEquals("Counter: 0", counterLabel.getText()); // Listener is disabled
86-
87-
// Enable listener
88-
simulateButtonClick(toggleListenerButton);
89-
simulateButtonClick(incrementButton);
90-
assertEquals("Counter: 2", counterLabel.getText()); // Listener is re-enabled
91-
}
11+
void testMain() {
12+
try (var mockedBlocUi = mockStatic(BlocUi.class)) {
13+
// Call the main method
14+
Main.main(new String[]{});
9215

93-
private void simulateButtonClick(JButton button) {
94-
for (var listener : button.getActionListeners()) {
95-
listener.actionPerformed(null);
16+
// Verify that createAndShowUi was called
17+
mockedBlocUi.verify(() -> new BlocUi().createAndShowUi());
9618
}
9719
}
98-
}
20+
}

0 commit comments

Comments
 (0)