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.util;
16  
17  import java.awt.*;
18  import java.awt.image.BufferedImage;
19  import java.util.logging.Logger;
20  import javax.swing.*;
21  
22  import net.curre.prefcount.gui.aa.AAJTextField;
23  import net.curre.prefcount.test.BaseTestCase;
24  import static net.curre.prefcount.util.Utilities.FieldType.*;
25  import static net.curre.prefcount.util.Utilities.PlatformType.*;
26  
27  /**
28   * This is a junit test for testing <code>Utilities</code> class.
29   * <p/>
30   * Created date: Nov 25, 2007
31   *
32   * @author Yevgeny Nyden
33   */
34  public class UtilitiesTest extends BaseTestCase {
35  
36    /** Private class logger. */
37    private static Logger log = Logger.getLogger(UtilitiesTest.class.toString());
38  
39    /**
40     * Tests the testValidateTextField() method.
41     *
42     * @throws Exception on error.
43     */
44    public void testValidateTextField() throws Exception {
45  
46      log.info("Running testValidateTextField()...");
47      JTextField field = new AAJTextField();
48  
49      field.setText("123");
50      boolean result = Utilities.validateTextField(field, INTEGER);
51      assertTrue("String \"123\" should be tested as a valid integer", result);
52  
53      field.setText("123X");
54      result = Utilities.validateTextField(field, INTEGER);
55      assertFalse("String \"123X\" should not be tested as a valid integer", result);
56  
57      field.setText(" \t123 \n");
58      result = Utilities.validateTextField(field, INTEGER);
59      assertTrue("String \" \t123 \n\" should be tested as a valid integer", result);
60  
61      field.setText("123");
62      try {
63        result = Utilities.validateTextField(field, UNDEFINED);
64        assertFalse("False must be returned for an unknown type", result);
65      } catch (Exception e) {
66        fail("Exception must not be thrown for an unknown type");
67      }
68    }
69  
70    /**
71     * Tests the getFirstLetterFromField() method.
72     *
73     * @throws Exception on error.
74     */
75    public void testGetFirstLetterFromField() throws Exception {
76  
77      log.info("Running testGetFirstLetterFromField()...");
78      JTextField field = new AAJTextField();
79  
80      String letter = Utilities.getFirstLetterFromField(field);
81      assertNull("Null is expected for a null field", letter);
82  
83      field.setText("  \t");
84      letter = Utilities.getFirstLetterFromField(field);
85      assertNull("Null is expected for an empty field", letter);
86  
87      field.setText("  dmitry ");
88      letter = Utilities.getFirstLetterFromField(field);
89      assertNotNull("Return value must not be null for a non-empty string", letter);
90      assertFalse("Returned letter must be capitalized", "d".equals(letter));
91      assertEquals("Returned letter is wrong", "D", letter);
92    }
93  
94    /**
95     * Tests the determineSizeOfString() method.
96     *
97     * @throws Exception on error.
98     */
99    public void testDetermineSizeOfString() throws Exception {
100 
101     log.info("Running testDetermineSizeOfString()...");
102 
103     // creating a dummy image to obtain a graphics object
104     BufferedImage img = new BufferedImage(10, 10, 10);
105     Graphics2D g2 = (Graphics2D) img.getGraphics();
106     Dimension size = Utilities.determineSizeOfString(g2, "");
107     assertNotNull("Returned dimension is null", size);
108     assertEquals("The width of an empty string should be 0", 0D, size.getWidth());
109     assertTrue("The height of the string should be greater than 0", size.getHeight() > 0D);
110 
111     size = Utilities.determineSizeOfString(g2, "abc");
112     assertNotNull("Returned dimension is null", size);
113     assertTrue("The width of the string should be greater than 0", size.getWidth() > 0D);
114     assertTrue("The height of the string should be greater than 0", size.getHeight() > 0D);
115   }
116 
117   /**
118    * Tests miscellaneous Utilities methods.
119    *
120    * @throws Exception on error.
121    */
122   public void testGetPlatformType() throws Exception {
123     log.info("Running testMiscellaneous()...");
124     final String currOs = System.getProperty("os.name");
125     final String currMrj = System.getProperty("mrj.version");
126     try {
127       System.clearProperty("mrj.version");
128       checkPlatform(MAC_OS, "MAC OS");
129       checkPlatform(LINUX, "linux");
130       checkPlatform(LINUX, "LINUX");
131       checkPlatform(WINDOWS, "windows");
132       checkPlatform(UNKNOWN, "nothing");
133       System.setProperty("mrj.version", "something");
134       checkPlatform(MAC_OS, "linux");
135       checkPlatform(MAC_OS, "nothing");
136 
137     } finally {
138       System.setProperty("os.name", currOs);
139       if (currMrj == null) {
140         System.clearProperty("mrj.version");
141       } else {
142         System.setProperty("mrj.version", currMrj);
143       }
144     }
145   }
146 
147   /**
148    * Tests miscellaneous Utilities methods.
149    *
150    * @throws Exception on error.
151    */
152   public void testMiscellaneous() throws Exception {
153 
154     log.info("Running testMiscellaneous()...");
155 
156     ImageIcon image = Utilities.createImage("ru");
157     assertNotNull("Returned image must not be null", image);
158     image = Utilities.createImage("us");
159     assertNotNull("Returned image must not be null", image);
160 
161     String str = Utilities.underlineLetter("012345", 1);
162     assertNotNull("Returned string must not be null", str);
163     str = str.toUpperCase();
164     assertTrue("Returned string must start with <HTML>", str.startsWith("<HTML>"));
165     assertEquals("Wrong index of <U>", 7, str.indexOf("<U>"));
166     assertEquals("Wrong index of </U>", 11, str.indexOf("</U>"));
167 
168     Color color = new Color(100, 100, 100);
169     Color c = Utilities.createDarkerColor(color, 10);
170     assertNotNull("Returned color is null", c);
171     assertEquals("Wrong value for red", 90, c.getRed());
172     assertEquals("Wrong value for green", 90, c.getGreen());
173     assertEquals("Wrong value for blue", 90, c.getBlue());
174 
175     // the following methods are just tested for not throwing exceptions
176     Utilities.printTime("");
177     Utilities.printLookAndFeels();
178   }
179 
180   /**
181    * Checks platform type.
182    *
183    * @param expectedType Type to expect.
184    * @param osNameToSet Os string name to set.
185    */
186   private static void checkPlatform(Utilities.PlatformType expectedType,
187                                     String osNameToSet) {
188     System.setProperty("os.name", osNameToSet);
189     Utilities.PlatformType type = Utilities.getPlatformType();
190     assertNotNull("Platform type is null", type);
191     assertEquals("Wrong platform type", expectedType, type);
192   }
193 
194 }