1   /**
2    * This program is free software: you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
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   * This is a junit test for testing
28   * dialog inner panel functionality.
29   * <p/>
30   * Created date: Dec 9, 2007
31   *
32   * @author Yevgeny Nyden
33   */
34  public class DialogInnerPanelTest extends BaseTestCase {
35  
36    /** Private class logger. */
37    private static Logger log = Logger.getLogger(DialogInnerPanelTest.class.toString());
38  
39    /**
40     * Tests PlayersNamesPanel panel methods.
41     *
42     * @throws Exception on error.
43     */
44    public void testPlayersNamesPanel() throws Exception {
45  
46      log.info("Running testPlayersNamesPanel()...");
47  
48      // testing 3 player case
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      // testing 4 player case
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     * Tests PlayerDataPanel panel methods.
78     *
79     * @throws Exception on error.
80     */
81    public void testPlayerDataPanel() throws Exception {
82  
83      log.info("Running testPlayerDataPanel()...");
84  
85      // testing 3 player case
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      // testing 4 player case
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    * Tests LastInputPanel panel methods.
111    *
112    * @throws Exception on error.
113    */
114   public void testLastInputPanel() throws Exception {
115 
116     log.info("Running testLastInputPanel()...");
117 
118     // testing 3 player case
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     // testing 4 player case
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   /** Private methods ***********************/
139 
140   /**
141    * Tests the panel validation for a given numeric field.
142    * This method assumes the panel is valid at start.
143    *
144    * @param panel  Panel to test.
145    * @param fields Fields to test.
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    * Tests the last input panel validation.
159    *
160    * @param panel Last input panel to test.
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    * Tests the isFirstPanel(), isLastPanel(),
179    * and getHeaderMessage() DialogInnerPanel methods.
180    *
181    * @param panel   DialogInnerPanel panel.
182    * @param isLast  Expected isLast value.
183    * @param isFirst Expected isFirst value.
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 }