001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.debugger;
043:
044: import java.awt.Rectangle;
045: import java.util.Arrays;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.Collections;
049: import java.util.HashMap;
050: import java.util.Map;
051: import junit.framework.TestCase;
052:
053: /**
054: * Test of the Properties class
055: *
056: * @author Martin Entlicher
057: */
058: public class PropertiesTest extends TestCase {
059:
060: public PropertiesTest(String testName) {
061: super (testName);
062: }
063:
064: /** Tests just the basic get/set methods. */
065: public void testGetSet() throws Exception {
066: String prop = "get/set";
067: Properties p = Properties.getDefault();
068: for (int i = 0; i < 10; i++) {
069: int j = 0;
070: String app = Integer.toHexString(j);
071: testGetSet(p, prop + i + app, true);
072: app = Integer.toHexString(++j);
073: testGetSet(p, prop + i + app, (byte) i);
074: app = Integer.toHexString(++j);
075: testGetSet(p, prop + i + app, (short) i);
076: app = Integer.toHexString(++j);
077: testGetSet(p, prop + i + app, (int) 100 * i - 2000);
078: app = Integer.toHexString(++j);
079: testGetSet(p, prop + i + app, (long) 1000000000000000L * i
080: - 12345678987654321L);
081: app = Integer.toHexString(++j);
082: testGetSet(p, prop + i + app,
083: (float) (1234.1234 * i - 5678));
084: app = Integer.toHexString(++j);
085: testGetSet(p, prop + i + app,
086: (double) (1234.1234e200 * i * Math.sin(i)));
087: app = Integer.toHexString(++j);
088: testGetSet(p, prop + i + app, Integer
089: .toBinaryString(i * 1234));
090: app = Integer.toHexString(++j);
091: //testGetSet(p, prop + i, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
092: app = Integer.toHexString(++j);
093: //testGetSet(p, prop + i, new double[] { 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.1 });
094: app = Integer.toHexString(++j);
095: testGetSet(p, prop + i + app, new String[] { "0", "1", "2",
096: "3", "4", "5", "6", "7", "8", "9" });
097: app = Integer.toHexString(++j);
098: //testGetSet(p, prop + i + app, new String[][] { { "0", "1" }, { "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } });
099: //app = Integer.toHexString(++j);
100: //testGetSet(p, prop + i + app, new Rectangle[] { new Rectangle(), new Rectangle(10, 20), new Rectangle(234, -432) });
101: //app = Integer.toHexString(++j);
102: //testGetSet(p, prop + i + app, new ArrayList(Arrays.asList(new Integer[] { new Integer(i) })));
103: testGetSet(p, prop + i + app, new ArrayList(Arrays
104: .asList(new String[] { Integer.toString(i) })));
105: //testGetSet(p, prop + i + app, Collections.singleton(new Integer(i)));
106: app = Integer.toHexString(++j);
107: testGetSet(p, prop + i + app, new ArrayList(Arrays
108: .asList(new Object[] { Integer.toHexString(i),
109: "String " + i })));
110: //testGetSet(p, prop + i + app, Arrays.asList(new Object[] { Integer.toHexString(i), "String "+i }));
111: //testGetSet(p, prop + i + app, Arrays.asList(new Object[] { new Integer(i), "String "+i }));
112: app = Integer.toHexString(++j);
113: //testGetSet(p, prop + i + app, Collections.singletonMap(new Integer(i), "i = "+i));
114: testGetSet(p, prop + i + app, new HashMap(Collections
115: .singletonMap(Integer.toString(i), "i = " + i)));
116: }
117: }
118:
119: public void testReader() throws Exception {
120: Properties p = Properties.getDefault();
121: ((Properties.PropertiesImpl) p).addReader(new TestReader());
122: assertNull(p.getObject("rect 1", null));
123: assertNull(p.getObject("rect 2", null));
124: assertNull(p.getObject("test 1", null));
125: assertNull(p.getObject("test 2", null));
126: assertNull(p.getObject("test 3", null));
127: Rectangle r1 = new Rectangle(9876, 1234);
128: Rectangle r2 = new Rectangle(987654321, 123456789);
129: p.setObject("rect 1", r1);
130: p.setObject("rect 2", r2);
131: TestObject t1 = new TestObject(12345678);
132: TestObject t2 = new TestObject(999999999965490L);
133: TestObject t3 = new TestObject(999999999999999999L);
134: p.setObject("test 1", t1);
135: p.setObject("test 2", t2);
136: p.setObject("test 3", t3);
137:
138: assertEquals(r1, p.getObject("rect 1", null));
139: assertEquals(r2, p.getObject("rect 2", null));
140: assertEquals(t1, p.getObject("test 1", null));
141: assertEquals(t2, p.getObject("test 2", null));
142: assertEquals(t3, p.getObject("test 3", null));
143: }
144:
145: /** Stress test of multi-threaded get/set */
146: public void testStressGetSet() throws Exception {
147: Properties p = Properties.getDefault();
148: ((Properties.PropertiesImpl) p).addReader(new TestReader());
149: int n = 5;
150: ConcurrentGetSet[] cgs = new ConcurrentGetSet[n];
151: Thread[] t = new Thread[n];
152: for (int i = 0; i < n; i++) {
153: cgs[i] = new ConcurrentGetSet(p, "CGS " + i);
154: t[i] = new Thread(cgs[i]);
155: t[i].start();
156: }
157: for (int i = 0; i < n; i++) {
158: t[i].join();
159: if (cgs[i].getException() != null) {
160: throw cgs[i].getException();
161: }
162: }
163: }
164:
165: private static class ConcurrentGetSet implements Runnable {
166:
167: private Properties p;
168: private String prop;
169: private TestObject[] t;
170: private Exception ex;
171:
172: public ConcurrentGetSet(Properties p, String prop) {
173: this .p = p;
174: this .prop = prop;
175: t = new TestObject[3];
176: t[0] = new TestObject(12345678);
177: t[1] = new TestObject(999999999965490L);
178: t[2] = new TestObject(999999999999999999L);
179: }
180:
181: public void run() {
182: cycle: for (int i = 0; i < 10000; i++) {
183: for (int k = 0; k < t.length; k++) {
184: p.setObject(prop + k, t[k]);
185: }
186: try {
187: if ((i % 1000) == 0) {
188: Thread.currentThread().sleep(
189: 1 + (prop.hashCode() % 10));
190: }
191: } catch (InterruptedException iex) {
192: }
193: for (int k = 0; k < t.length; k++) {
194: try {
195: assertEquals(t[k], p.getObject(prop + k, null));
196: } catch (Exception ex) {
197: this .ex = ex;
198: break cycle;
199: }
200: }
201: }
202: }
203:
204: public Exception getException() {
205: return ex;
206: }
207: }
208:
209: private void testGetSet(Properties p, String name, boolean obj) {
210: // suppose that the property is not defined
211: assertEquals(true, p.getBoolean(name, true));
212: assertEquals(false, p.getBoolean(name, false));
213: // check set/get:
214: p.setBoolean(name, obj);
215: assertEquals(obj, p.getBoolean(name, !obj));
216: }
217:
218: private void testGetSet(Properties p, String name, byte obj) {
219: // suppose that the property is not defined
220: assertEquals((byte) 10, p.getByte(name, (byte) 10));
221: assertEquals((byte) 20, p.getByte(name, (byte) 20));
222: // check set/get:
223: p.setByte(name, obj);
224: assertEquals(obj, p.getByte(name, (byte) 0));
225: }
226:
227: private void testGetSet(Properties p, String name, short obj) {
228: // suppose that the property is not defined
229: assertEquals((short) 10, p.getShort(name, (short) 10));
230: assertEquals((short) 20, p.getShort(name, (short) 20));
231: // check set/get:
232: p.setShort(name, obj);
233: assertEquals(obj, p.getShort(name, (short) 0));
234: }
235:
236: private void testGetSet(Properties p, String name, int obj) {
237: // suppose that the property is not defined
238: assertEquals((int) 10, p.getInt(name, (int) 10));
239: assertEquals((int) 20, p.getInt(name, (int) 20));
240: // check set/get:
241: p.setInt(name, obj);
242: assertEquals(obj, p.getInt(name, (int) 0));
243: }
244:
245: private void testGetSet(Properties p, String name, long obj) {
246: // suppose that the property is not defined
247: assertEquals((long) 10, p.getLong(name, (long) 10));
248: assertEquals((long) 20, p.getLong(name, (long) 20));
249: // check set/get:
250: p.setLong(name, obj);
251: assertEquals(obj, p.getLong(name, (long) 0));
252: }
253:
254: private void testGetSet(Properties p, String name, float obj) {
255: // suppose that the property is not defined
256: assertEquals((float) 10, p.getFloat(name, (float) 10), 0);
257: assertEquals((float) 20, p.getFloat(name, (float) 20), 0);
258: // check set/get:
259: p.setFloat(name, obj);
260: assertEquals(obj, p.getFloat(name, (float) 0), 0);
261: }
262:
263: private void testGetSet(Properties p, String name, double obj) {
264: // suppose that the property is not defined
265: assertEquals((double) 10, p.getDouble(name, (double) 10), 0);
266: assertEquals((double) 20, p.getDouble(name, (double) 20), 0);
267: // check set/get:
268: p.setDouble(name, obj);
269: assertEquals(obj, p.getDouble(name, (double) 0), 0);
270: }
271:
272: private void testGetSet(Properties p, String name, String obj) {
273: // suppose that the property is not defined
274: assertNull(p.getString(name, null));
275: assertEquals("10", p.getString(name, "10"));
276: assertEquals("20", p.getString(name, "20"));
277: // check set/get:
278: p.setString(name, obj);
279: assertEquals(obj, p.getString(name, ""));
280: }
281:
282: private void testGetSet(Properties p, String name, Object[] obj) {
283: // suppose that the property is not defined
284: assertNull(p.getArray(name, null));
285: assertTrue(Arrays.deepEquals(new String[] { "10" }, p.getArray(
286: name, new String[] { "10" })));
287: assertTrue(Arrays.deepEquals(new String[] { "20" }, p.getArray(
288: name, new String[] { "20" })));
289: // check set/get:
290: p.setArray(name, obj);
291: //assertEquals(obj, p.getArray(name, new Object[]{}));
292: Object[] ret = p.getArray(name, new Object[] {});
293: assertTrue("Expecting: " + Arrays.asList(obj) + "\nbut got: "
294: + Arrays.asList(ret), Arrays.deepEquals(obj, ret));
295: }
296:
297: private void testGetSet(Properties p, String name, Collection obj) {
298: // suppose that the property is not defined
299: assertNull(p.getCollection(name, null));
300: assertEquals(Collections.singleton("10"), p.getCollection(name,
301: Collections.singleton("10")));
302: assertEquals(Collections.singletonList("20"), p.getCollection(
303: name, Collections.singletonList("20")));
304: // check set/get:
305: p.setCollection(name, obj);
306: assertEquals(obj, p.getCollection(name, Collections.emptySet()));
307: }
308:
309: private void testGetSet(Properties p, String name, Map obj) {
310: // suppose that the property is not defined
311: assertNull(p.getMap(name, null));
312: assertEquals(Collections.singletonMap("10", "20"), p.getMap(
313: name, Collections.singletonMap("10", "20")));
314: // check set/get:
315: p.setMap(name, obj);
316: assertEquals(obj, p.getMap(name, Collections.emptyMap()));
317: }
318:
319: private void testGetSet(Properties p, String name, Object obj) {
320: // suppose that the property is not defined
321: assertNull(p.getObject(name, null));
322: assertEquals("10", p.getObject(name, "10"));
323: assertEquals("20", p.getObject(name, "20"));
324: // check set/get:
325: p.setObject(name, obj);
326: assertEquals(obj, p.getObject(name, ""));
327: }
328:
329: private static class TestReader implements Properties.Reader {
330:
331: public String[] getSupportedClassNames() {
332: return new String[] { "java.awt.Rectangle",
333: "org.netbeans.api.debugger.PropertiesTest$TestObject" };
334: }
335:
336: public Object read(String className, Properties properties) {
337: if (className.equals("java.awt.Rectangle")) {
338: int w = properties.getInt("Rectangle.width", 0);
339: int h = properties.getInt("Rectangle.height", 0);
340: return new Rectangle(w, h);
341: }
342: if (className
343: .equals("org.netbeans.api.debugger.PropertiesTest$TestObject")) {
344: return new TestObject(properties);
345: }
346: throw new IllegalArgumentException(className);
347: }
348:
349: public void write(Object object, Properties properties) {
350: if (object instanceof Rectangle) {
351: properties.setInt("Rectangle.width",
352: ((Rectangle) object).width);
353: properties.setInt("Rectangle.height",
354: ((Rectangle) object).height);
355: return;
356: }
357: if (object instanceof TestObject) {
358: ((TestObject) object).write(properties);
359: return;
360: }
361: throw new IllegalArgumentException(object.toString());
362: }
363: }
364:
365: private static class TestObject {
366:
367: private boolean boo;
368: private char c;
369: private int l;
370: private String[] strings;
371: private Rectangle r;
372: private String binaryState;
373:
374: public TestObject(long state) {
375: boo = (state & 1) == 1;
376: c = (char) (state & 255);
377: l = (int) ((state << 8) & 255);
378: strings = new String[l];
379: for (int i = 0; i < l; i++) {
380: int d = i % 64;
381: strings[i] = "arr[i] = " + ((state << d) & 1);
382: }
383: r = new Rectangle((int) (state & 65535),
384: (int) ((state << 16) & 65535));
385: binaryState = Long.toBinaryString(state);
386: }
387:
388: public TestObject(Properties p) {
389: boo = p.getBoolean("boo", false);
390: c = p.getChar("char", (char) 0);
391: l = p.getInt("length", 0);
392: for (int i = 0; i < l; i++) {
393: strings[i] = p.getString("string " + i, null);
394: }
395: r = (Rectangle) p.getObject("rectangle", null);
396: binaryState = p.getString("binaryState", null);
397: }
398:
399: public void write(Properties p) {
400: p.setBoolean("boo", boo);
401: p.setChar("char", c);
402: p.setInt("length", l);
403: for (int i = 0; i < l; i++) {
404: p.setString("string " + i, strings[i]);
405: }
406: p.setObject("rectangle", r);
407: p.setString("binaryState", binaryState);
408: }
409:
410: public boolean equals(Object obj) {
411: if (!(obj instanceof TestObject)) {
412: return false;
413: }
414: TestObject t = (TestObject) obj;
415: if (boo != t.boo)
416: return false;
417: if (c != t.c)
418: return false;
419: if (l != t.l)
420: return false;
421: for (int i = 0; i < l; i++) {
422: if (!strings[i].equals(t.strings[i]))
423: return false;
424: }
425: if (!r.equals(t.r))
426: return false;
427: if (!binaryState.equals(t.binaryState))
428: return false;
429: return true;
430: }
431:
432: public int hashCode() {
433: return 1234 + r.width + r.height;
434: }
435:
436: public String toString() {
437: return binaryState;
438: }
439: }
440:
441: }
|