1
2
3
4
5
6
7
8
9
10
11
12
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
29
30
31
32
33
34 public class UtilitiesTest extends BaseTestCase {
35
36
37 private static Logger log = Logger.getLogger(UtilitiesTest.class.toString());
38
39
40
41
42
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
72
73
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
96
97
98
99 public void testDetermineSizeOfString() throws Exception {
100
101 log.info("Running testDetermineSizeOfString()...");
102
103
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
119
120
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
149
150
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
176 Utilities.printTime("");
177 Utilities.printLookAndFeels();
178 }
179
180
181
182
183
184
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 }