1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.curre.prefcount.gui;
16
17 import java.util.logging.Logger;
18 import javax.swing.*;
19
20 import net.curre.prefcount.PrefCountRegistry;
21 import net.curre.prefcount.gui.aa.AAJTextField;
22 import net.curre.prefcount.test.BaseTestCase;
23
24 import org.apache.commons.lang.StringUtils;
25
26
27
28
29
30
31
32
33
34 public class DialogInnerPanelTest extends BaseTestCase {
35
36
37 private static Logger log = Logger.getLogger(DialogInnerPanelTest.class.toString());
38
39
40
41
42
43
44 public void testPlayersNamesPanel() throws Exception {
45
46 log.info("Running testPlayersNamesPanel()...");
47
48
49 PrefCountRegistry.getInstance().setCurrentLocale(PrefCountRegistry.DEFAULT_LOCALE_ID);
50 MainWindow window = new MainWindow(false);
51 PrefCountRegistry.getInstance().setMainWindow(window);
52 window.startPlayerDialog(3);
53
54 PlayersNamesPanel panel = new PlayersNamesPanel(window.playerDialogFrame, 3);
55 checkDialogInnerPanel(panel, false, true);
56 for (JTextField field : panel.playersFields) {
57 assertFalse("Validation should not succeed", panel.validateFields());
58 field.setText("A");
59 }
60 assertTrue("Validation should succeed", panel.validateFields());
61
62
63 window = new MainWindow(false);
64 PrefCountRegistry.getInstance().setMainWindow(window);
65 window.startPlayerDialog(4);
66
67 panel = new PlayersNamesPanel(window.playerDialogFrame, 4);
68 checkDialogInnerPanel(panel, false, true);
69 for (JTextField field : panel.playersFields) {
70 assertFalse("Validation should not succeed", panel.validateFields());
71 field.setText("A");
72 }
73 assertTrue("Validation should succeed", panel.validateFields());
74 }
75
76
77
78
79
80
81 public void testPlayerDataPanel() throws Exception {
82
83 log.info("Running testPlayerDataPanel()...");
84
85
86 PrefCountRegistry.getInstance().setCurrentLocale(PrefCountRegistry.DEFAULT_LOCALE_ID);
87 MainWindow window = new MainWindow(false);
88 PrefCountRegistry.getInstance().setMainWindow(window);
89 window.startPlayerDialog(3);
90
91 PlayerDataPanel panel = new PlayerDataPanel(window.playerDialogFrame, 3, 0);
92 checkDialogInnerPanel(panel, false, false);
93 checkIntegerValidation(panel, panel.mountField);
94 checkIntegerValidation(panel, panel.bulletField);
95 checkIntegerValidation(panel, panel.fieldVists.toArray(new AAJTextField[0]));
96
97
98 window = new MainWindow(false);
99 PrefCountRegistry.getInstance().setMainWindow(window);
100 window.startPlayerDialog(4);
101
102 panel = new PlayerDataPanel(window.playerDialogFrame, 4, 0);
103 checkDialogInnerPanel(panel, false, false);
104 checkIntegerValidation(panel, panel.mountField);
105 checkIntegerValidation(panel, panel.bulletField);
106 checkIntegerValidation(panel, panel.fieldVists.toArray(new AAJTextField[0]));
107 }
108
109
110
111
112
113
114 public void testLastInputPanel() throws Exception {
115
116 log.info("Running testLastInputPanel()...");
117
118
119 PrefCountRegistry.getInstance().setCurrentLocale(PrefCountRegistry.DEFAULT_LOCALE_ID);
120 MainWindow window = new MainWindow(false);
121 PrefCountRegistry.getInstance().setMainWindow(window);
122 window.startPlayerDialog(3);
123
124 LastInputPanel panel = new LastInputPanel(window.playerDialogFrame);
125 checkDialogInnerPanel(panel, true, false);
126 checkIntegerValidation(panel);
127
128
129 window = new MainWindow(false);
130 PrefCountRegistry.getInstance().setMainWindow(window);
131 window.startPlayerDialog(4);
132
133 panel = new LastInputPanel(window.playerDialogFrame);
134 checkDialogInnerPanel(panel, true, false);
135 checkIntegerValidation(panel);
136 }
137
138
139
140
141
142
143
144
145
146
147 private void checkIntegerValidation(DialogInnerPanel panel, JTextField... fields) {
148 for (JTextField field : fields) {
149 assertTrue("Validation should succeed", panel.validateFields());
150 field.setText("12Ax");
151 assertFalse("Validation should not succeed", panel.validateFields());
152 field.setText("12");
153 assertTrue("Validation should succeed", panel.validateFields());
154 }
155 }
156
157
158
159
160
161
162 private void checkIntegerValidation(LastInputPanel panel) {
163 panel.setTargetBulletText(" ");
164 assertFalse("Validation should not succeed", panel.validateFields());
165 panel.setTargetBulletText("12Ax");
166 assertFalse("Validation should not succeed", panel.validateFields());
167 panel.setTargetBulletText("0");
168 assertFalse("Validation should not succeed", panel.validateFields());
169 panel.setTargetBulletText("-1");
170 assertFalse("Validation should not succeed", panel.validateFields());
171 panel.setTargetBulletText("12");
172 assertTrue("Validation should succeed", panel.validateFields());
173 panel.setTargetBulletText(" 12 ");
174 assertTrue("Validation should succeed", panel.validateFields());
175 }
176
177
178
179
180
181
182
183
184
185 private void checkDialogInnerPanel(DialogInnerPanel panel, boolean isLast, boolean isFirst) {
186 assertEquals("Panel isLast flag is wrong,", isLast, panel.isLastPanel());
187 assertEquals("Panel isFirst flag is wrong,", isFirst, panel.isFirstPanel());
188 String msg = panel.getHeaderMessage();
189 assertNotNull("Panel header message is null", msg);
190 assertFalse("Panel header message is empty", StringUtils.isBlank(msg));
191 }
192
193 }