KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ReflectionArrayTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest;
6
7 import com.tc.object.config.ConfigVisitor;
8 import com.tc.object.config.DSOClientConfigHelper;
9 import com.tc.object.config.TransparencyClassSpec;
10 import com.tc.object.config.spec.CyclicBarrierSpec;
11 import com.tc.object.tx.ReadOnlyException;
12 import com.tc.simulator.app.ApplicationConfig;
13 import com.tc.simulator.listener.ListenerProvider;
14 import com.tc.util.Assert;
15
16 import java.lang.reflect.Array JavaDoc;
17
18 public class ReflectionArrayTestApp extends GenericTestApp {
19   public ReflectionArrayTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
20     super(appId, cfg, listenerProvider, DataRoot.class);
21   }
22
23   protected Object JavaDoc getTestObject(String JavaDoc testName) {
24     DataRoot dataRoot = (DataRoot) sharedMap.get("dataRoot");
25     return dataRoot;
26   }
27
28   protected void setupTestObject(String JavaDoc testName) {
29     sharedMap.put("dataRoot", new DataRoot());
30   }
31
32   void testCreateLongArray(DataRoot root, boolean validate) {
33     if (validate) {
34       Assert.assertEquals(20, Array.getLength(root.getLongArray()));
35     } else {
36       synchronized (root) {
37         root.setLongArray((long[]) Array.newInstance(Long.TYPE, 20));
38       }
39     }
40   }
41
42   void testModifyLongArray(DataRoot root, boolean validate) {
43     if (validate) {
44       Assert.assertEquals(Long.MAX_VALUE, Array.getLong(root.getLongArray(), 0));
45     } else {
46       synchronized (root) {
47         Array.setLong(root.getLongArray(), 0, Long.MAX_VALUE);
48       }
49     }
50   }
51
52   void testModifyIntArray(DataRoot root, boolean validate) {
53     if (validate) {
54       Assert.assertEquals(Integer.MAX_VALUE, Array.getInt(root.getIntArray(), 0));
55     } else {
56       synchronized (root) {
57         Array.setInt(root.getIntArray(), 0, Integer.MAX_VALUE);
58       }
59     }
60   }
61
62   void testModiyShortArray(DataRoot root, boolean validate) {
63     if (validate) {
64       Assert.assertEquals(Short.MAX_VALUE, Array.getShort(root.getShortArray(), 0));
65     } else {
66       synchronized (root) {
67         Array.setShort(root.getShortArray(), 0, Short.MAX_VALUE);
68       }
69     }
70   }
71
72   void testModiyByteArray(DataRoot root, boolean validate) {
73     if (validate) {
74       Assert.assertEquals(Byte.MAX_VALUE, Array.getByte(root.getByteArray(), 0));
75     } else {
76       synchronized (root) {
77         Array.setByte(root.getByteArray(), 0, Byte.MAX_VALUE);
78       }
79     }
80   }
81
82   void testModiyBooleanArray(DataRoot root, boolean validate) {
83     if (validate) {
84       Assert.assertTrue(Boolean.TRUE.booleanValue() == Array.getBoolean(root.getBooleanArray(), 0));
85     } else {
86       synchronized (root) {
87         Array.setBoolean(root.getBooleanArray(), 0, Boolean.TRUE.booleanValue());
88       }
89     }
90   }
91
92   void testModifyDoubleArray(DataRoot root, boolean validate) {
93     if (validate) {
94       Assert.assertEquals(Double.MAX_VALUE, Array.getDouble(root.getDoubleArray(), 0));
95     } else {
96       synchronized (root) {
97         Array.setDouble(root.getDoubleArray(), 0, Double.MAX_VALUE);
98       }
99     }
100   }
101
102   void testModifyFloatArray(DataRoot root, boolean validate) {
103     if (validate) {
104       Assert.assertEquals(Float.MAX_VALUE, Array.getFloat(root.getFloatArray(), 0));
105     } else {
106       synchronized (root) {
107         Array.setFloat(root.getFloatArray(), 0, Float.MAX_VALUE);
108       }
109     }
110   }
111
112   void testModifyCharArray(DataRoot root, boolean validate) {
113     if (validate) {
114       Assert.assertEquals(Character.MAX_VALUE, Array.getChar(root.getCharArray(), 0));
115     } else {
116       synchronized (root) {
117         Array.setChar(root.getCharArray(), 0, Character.MAX_VALUE);
118       }
119     }
120   }
121
122   void testSetReferenceArrayElementToNull(DataRoot root, boolean validate) {
123     if (validate) {
124       Assert.assertNull(root.getInstanceArray()[0]);
125     } else {
126       synchronized (root) {
127         Array.set(root.getInstanceArray(), 0, null);
128       }
129
130       // test on a non-shared array too (for good measure)
131
Object JavaDoc[] nonSharedArray = new Object JavaDoc[] { this };
132       Array.set(nonSharedArray, 0, null);
133     }
134   }
135
136   void testWideningModifyByteArray1(DataRoot root, boolean validate) {
137     if (validate) {
138       Assert.assertEquals(Byte.MAX_VALUE, Array.getShort(root.getShortArray(), 0));
139     } else {
140       synchronized (root) {
141         Array.setByte(root.getShortArray(), 0, Byte.MAX_VALUE);
142       }
143     }
144   }
145
146   void testWideningModifyByteArray2(DataRoot root, boolean validate) {
147     if (validate) {
148       Assert.assertEquals(Byte.MAX_VALUE, Array.getInt(root.getIntArray(), 0));
149     } else {
150       synchronized (root) {
151         Array.setByte(root.getIntArray(), 0, Byte.MAX_VALUE);
152       }
153     }
154   }
155
156   void testWideningModifyByteArray3(DataRoot root, boolean validate) {
157     if (validate) {
158       Assert.assertEquals(Byte.MAX_VALUE, Array.getLong(root.getLongArray(), 0));
159     } else {
160       synchronized (root) {
161         Array.setByte(root.getLongArray(), 0, Byte.MAX_VALUE);
162       }
163     }
164   }
165
166   void testWideningModifyByteArray4(DataRoot root, boolean validate) {
167     if (validate) {
168       Assert.assertEquals(Byte.MAX_VALUE, Array.getFloat(root.getFloatArray(), 0));
169     } else {
170       synchronized (root) {
171         Array.setByte(root.getFloatArray(), 0, Byte.MAX_VALUE);
172       }
173     }
174   }
175
176   void testWideningModifyByteArray5(DataRoot root, boolean validate) {
177     if (validate) {
178       Assert.assertEquals(Byte.MAX_VALUE, Array.getDouble(root.getDoubleArray(), 0));
179     } else {
180       synchronized (root) {
181         Array.setByte(root.getDoubleArray(), 0, Byte.MAX_VALUE);
182       }
183     }
184   }
185
186   void testWideningModifyCharArray1(DataRoot root, boolean validate) {
187     if (validate) {
188       Assert.assertEquals(Character.MAX_VALUE, Array.getInt(root.getIntArray(), 0));
189     } else {
190       synchronized (root) {
191         Array.setChar(root.getIntArray(), 0, Character.MAX_VALUE);
192       }
193     }
194   }
195
196   void testWideningModifyCharArray2(DataRoot root, boolean validate) {
197     if (validate) {
198       Assert.assertEquals(Character.MAX_VALUE, Array.getLong(root.getLongArray(), 0));
199     } else {
200       synchronized (root) {
201         Array.setChar(root.getLongArray(), 0, Character.MAX_VALUE);
202       }
203     }
204   }
205
206   void testWideningModifyCharArray3(DataRoot root, boolean validate) {
207     if (validate) {
208       Assert.assertEquals(Character.MAX_VALUE, Array.getFloat(root.getFloatArray(), 0));
209     } else {
210       synchronized (root) {
211         Array.setChar(root.getFloatArray(), 0, Character.MAX_VALUE);
212       }
213     }
214   }
215
216   void testWideningModifyCharArray4(DataRoot root, boolean validate) {
217     if (validate) {
218       Assert.assertEquals(Character.MAX_VALUE, Array.getDouble(root.getDoubleArray(), 0));
219     } else {
220       synchronized (root) {
221         Array.setChar(root.getDoubleArray(), 0, Character.MAX_VALUE);
222       }
223     }
224   }
225
226   void testInstanceResolve(DataRoot root, boolean validate) {
227     if (validate) {
228       Instance[] array = root.getInstanceArray();
229       Assert.assertEquals(2, Array.getLength(array));
230       Assert.assertNotNull(Array.get(array, 0));
231       Assert.assertNotNull(Array.get(array, 1));
232     } else {
233       // nothing
234
}
235   }
236
237   /*
238    * // Comment out JDK 1.5 Generic specific test. // JDK 1.5 Generic construct specific test. void
239    * testJDK15GenericLongArray(DataRoot root, boolean validate) { if (validate) { Assert.assertEquals(20,
240    * Array.getLength(root.getGenericArray())); } else { synchronized (root) { GenericTestObject<Integer>[] genericArray =
241    * (GenericTestObject<Integer>[]) Array .newInstance(GenericTestObject.class, 20);
242    * root.setGenericArray(genericArray); } } }
243    */

244
245   // ReadOnly tests.
246
void testReadOnlyModifyLongArray(DataRoot root, boolean validate) {
247     if (validate) {
248       Assert.assertEquals(0, root.getLongArray()[0]);
249     } else {
250       synchronized (root) {
251         try {
252           Array.setLong(root.getLongArray(), 0, Long.MAX_VALUE);
253         } catch (ReadOnlyException roe) {
254           // ignore ReadOnlyException in test.
255
}
256       }
257     }
258   }
259
260   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
261     String JavaDoc testClass = ReflectionArrayTestApp.class.getName();
262     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
263
264     String JavaDoc writeAllowedMethodExpression = "* " + testClass + "*.*(..)";
265     config.addWriteAutolock(writeAllowedMethodExpression);
266     String JavaDoc readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)";
267     config.addReadAutolock(readOnlyMethodExpression);
268
269     config.addRoot(testClass, "dataRoot", "dataRoot", true);
270     spec.addRoot("barrier", "barrier");
271     spec.addRoot("barrier2", "barrier2");
272     config.addIncludePattern(DataRoot.class.getName());
273     config.addIncludePattern(Instance.class.getName());
274     new CyclicBarrierSpec().visit(visitor, config);
275   }
276
277   private static class Instance {
278     //
279
}
280
281   private static class DataRoot {
282     private final double[] doubleArray = new double[2];
283     private final float[] floatArray = new float[2];
284     private long[] longArray = new long[2];
285     private final int[] intArray = new int[2];
286     private final short[] shortArray = new short[2];
287     private final byte[] byteArray = new byte[2];
288     private final boolean[] booleanArray = new boolean[2];
289     private final char[] charArray = new char[2];
290     private final Instance[] instanceArray = new Instance[] { new Instance(), new Instance() };
291
292     // private GenericTestObject<Integer>[] genericArray;
293

294     public DataRoot() {
295       super();
296     }
297
298     public Instance[] getInstanceArray() {
299       return instanceArray;
300     }
301
302     public void setLongArray(long[] ls) {
303       this.longArray = ls;
304     }
305
306     public double[] getDoubleArray() {
307       return doubleArray;
308     }
309
310     public float[] getFloatArray() {
311       return floatArray;
312     }
313
314     public long[] getLongArray() {
315       return longArray;
316     }
317
318     public int[] getIntArray() {
319       return intArray;
320     }
321
322     public short[] getShortArray() {
323       return shortArray;
324     }
325
326     public byte[] getByteArray() {
327       return byteArray;
328     }
329
330     public boolean[] getBooleanArray() {
331       return booleanArray;
332     }
333
334     public char[] getCharArray() {
335       return charArray;
336     }
337
338     /*
339      * public GenericTestObject<Integer>[] getGenericArray() { return genericArray; } public void
340      * setGenericArray(GenericTestObject<Integer>[] genericArray) { this.genericArray = genericArray; }
341      */

342
343   }
344
345   /*
346    * private static class GenericTestObject<T> { T obj; public GenericTestObject(T o) { this.obj = o; } public T
347    * getObj() { return obj; } public String toString() { return obj.toString(); } }
348    */

349 }
350
Popular Tags