001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.object.config.ConfigVisitor;
008: import com.tc.object.config.DSOClientConfigHelper;
009: import com.tc.object.config.TransparencyClassSpec;
010: import com.tc.object.config.Root;
011: import com.tc.object.config.spec.CyclicBarrierSpec;
012: import com.tc.object.util.ReadOnlyException;
013: import com.tc.simulator.app.ApplicationConfig;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tc.util.Assert;
016:
017: import java.lang.reflect.Array;
018:
019: public class ReflectionArrayTestApp extends GenericTestApp {
020: public ReflectionArrayTestApp(String appId, ApplicationConfig cfg,
021: ListenerProvider listenerProvider) {
022: super (appId, cfg, listenerProvider, DataRoot.class);
023: }
024:
025: protected Object getTestObject(String testName) {
026: DataRoot dataRoot = (DataRoot) sharedMap.get("dataRoot");
027: return dataRoot;
028: }
029:
030: protected void setupTestObject(String testName) {
031: sharedMap.put("dataRoot", new DataRoot());
032: }
033:
034: void testCreateLongArray(DataRoot root, boolean validate) {
035: if (validate) {
036: Assert.assertEquals(20, Array
037: .getLength(root.getLongArray()));
038: } else {
039: synchronized (root) {
040: root.setLongArray((long[]) Array.newInstance(Long.TYPE,
041: 20));
042: }
043: }
044: }
045:
046: void testModifyLongArray(DataRoot root, boolean validate) {
047: if (validate) {
048: Assert.assertEquals(Long.MAX_VALUE, Array.getLong(root
049: .getLongArray(), 0));
050: } else {
051: synchronized (root) {
052: Array.setLong(root.getLongArray(), 0, Long.MAX_VALUE);
053: }
054: }
055: }
056:
057: void testModifyIntArray(DataRoot root, boolean validate) {
058: if (validate) {
059: Assert.assertEquals(Integer.MAX_VALUE, Array.getInt(root
060: .getIntArray(), 0));
061: } else {
062: synchronized (root) {
063: Array.setInt(root.getIntArray(), 0, Integer.MAX_VALUE);
064: }
065: }
066: }
067:
068: void testModiyShortArray(DataRoot root, boolean validate) {
069: if (validate) {
070: Assert.assertEquals(Short.MAX_VALUE, Array.getShort(root
071: .getShortArray(), 0));
072: } else {
073: synchronized (root) {
074: Array
075: .setShort(root.getShortArray(), 0,
076: Short.MAX_VALUE);
077: }
078: }
079: }
080:
081: void testModiyByteArray(DataRoot root, boolean validate) {
082: if (validate) {
083: Assert.assertEquals(Byte.MAX_VALUE, Array.getByte(root
084: .getByteArray(), 0));
085: } else {
086: synchronized (root) {
087: Array.setByte(root.getByteArray(), 0, Byte.MAX_VALUE);
088: }
089: }
090: }
091:
092: void testModiyBooleanArray(DataRoot root, boolean validate) {
093: if (validate) {
094: Assert.assertTrue(Boolean.TRUE.booleanValue() == Array
095: .getBoolean(root.getBooleanArray(), 0));
096: } else {
097: synchronized (root) {
098: Array.setBoolean(root.getBooleanArray(), 0,
099: Boolean.TRUE.booleanValue());
100: }
101: }
102: }
103:
104: void testModifyDoubleArray(DataRoot root, boolean validate) {
105: if (validate) {
106: Assert.assertEquals(Double.MAX_VALUE, Array.getDouble(root
107: .getDoubleArray(), 0));
108: } else {
109: synchronized (root) {
110: Array.setDouble(root.getDoubleArray(), 0,
111: Double.MAX_VALUE);
112: }
113: }
114: }
115:
116: void testModifyFloatArray(DataRoot root, boolean validate) {
117: if (validate) {
118: Assert.assertEquals(Float.MAX_VALUE, Array.getFloat(root
119: .getFloatArray(), 0));
120: } else {
121: synchronized (root) {
122: Array
123: .setFloat(root.getFloatArray(), 0,
124: Float.MAX_VALUE);
125: }
126: }
127: }
128:
129: void testModifyCharArray(DataRoot root, boolean validate) {
130: if (validate) {
131: Assert.assertEquals(Character.MAX_VALUE, Array.getChar(root
132: .getCharArray(), 0));
133: } else {
134: synchronized (root) {
135: Array.setChar(root.getCharArray(), 0,
136: Character.MAX_VALUE);
137: }
138: }
139: }
140:
141: void testSetReferenceArrayElementToNull(DataRoot root,
142: boolean validate) {
143: if (validate) {
144: Assert.assertNull(root.getInstanceArray()[0]);
145: } else {
146: synchronized (root) {
147: Array.set(root.getInstanceArray(), 0, null);
148: }
149:
150: // test on a non-shared array too (for good measure)
151: Object[] nonSharedArray = new Object[] { this };
152: Array.set(nonSharedArray, 0, null);
153: }
154: }
155:
156: void testWideningModifyByteArray1(DataRoot root, boolean validate) {
157: if (validate) {
158: Assert.assertEquals(Byte.MAX_VALUE, Array.getShort(root
159: .getShortArray(), 0));
160: } else {
161: synchronized (root) {
162: Array.setByte(root.getShortArray(), 0, Byte.MAX_VALUE);
163: }
164: }
165: }
166:
167: void testWideningModifyByteArray2(DataRoot root, boolean validate) {
168: if (validate) {
169: Assert.assertEquals(Byte.MAX_VALUE, Array.getInt(root
170: .getIntArray(), 0));
171: } else {
172: synchronized (root) {
173: Array.setByte(root.getIntArray(), 0, Byte.MAX_VALUE);
174: }
175: }
176: }
177:
178: void testWideningModifyByteArray3(DataRoot root, boolean validate) {
179: if (validate) {
180: Assert.assertEquals(Byte.MAX_VALUE, Array.getLong(root
181: .getLongArray(), 0));
182: } else {
183: synchronized (root) {
184: Array.setByte(root.getLongArray(), 0, Byte.MAX_VALUE);
185: }
186: }
187: }
188:
189: void testWideningModifyByteArray4(DataRoot root, boolean validate) {
190: if (validate) {
191: Assert.assertEquals(Byte.MAX_VALUE, Array.getFloat(root
192: .getFloatArray(), 0));
193: } else {
194: synchronized (root) {
195: Array.setByte(root.getFloatArray(), 0, Byte.MAX_VALUE);
196: }
197: }
198: }
199:
200: void testWideningModifyByteArray5(DataRoot root, boolean validate) {
201: if (validate) {
202: Assert.assertEquals(Byte.MAX_VALUE, Array.getDouble(root
203: .getDoubleArray(), 0));
204: } else {
205: synchronized (root) {
206: Array.setByte(root.getDoubleArray(), 0, Byte.MAX_VALUE);
207: }
208: }
209: }
210:
211: void testWideningModifyCharArray1(DataRoot root, boolean validate) {
212: if (validate) {
213: Assert.assertEquals(Character.MAX_VALUE, Array.getInt(root
214: .getIntArray(), 0));
215: } else {
216: synchronized (root) {
217: Array.setChar(root.getIntArray(), 0,
218: Character.MAX_VALUE);
219: }
220: }
221: }
222:
223: void testWideningModifyCharArray2(DataRoot root, boolean validate) {
224: if (validate) {
225: Assert.assertEquals(Character.MAX_VALUE, Array.getLong(root
226: .getLongArray(), 0));
227: } else {
228: synchronized (root) {
229: Array.setChar(root.getLongArray(), 0,
230: Character.MAX_VALUE);
231: }
232: }
233: }
234:
235: void testWideningModifyCharArray3(DataRoot root, boolean validate) {
236: if (validate) {
237: Assert.assertEquals(Character.MAX_VALUE, Array.getFloat(
238: root.getFloatArray(), 0));
239: } else {
240: synchronized (root) {
241: Array.setChar(root.getFloatArray(), 0,
242: Character.MAX_VALUE);
243: }
244: }
245: }
246:
247: void testWideningModifyCharArray4(DataRoot root, boolean validate) {
248: if (validate) {
249: Assert.assertEquals(Character.MAX_VALUE, Array.getDouble(
250: root.getDoubleArray(), 0));
251: } else {
252: synchronized (root) {
253: Array.setChar(root.getDoubleArray(), 0,
254: Character.MAX_VALUE);
255: }
256: }
257: }
258:
259: void testInstanceResolve(DataRoot root, boolean validate) {
260: if (validate) {
261: Instance[] array = root.getInstanceArray();
262: Assert.assertEquals(2, Array.getLength(array));
263: Assert.assertNotNull(Array.get(array, 0));
264: Assert.assertNotNull(Array.get(array, 1));
265: } else {
266: // nothing
267: }
268: }
269:
270: /*
271: * // Comment out JDK 1.5 Generic specific test. // JDK 1.5 Generic construct specific test. void
272: * testJDK15GenericLongArray(DataRoot root, boolean validate) { if (validate) { Assert.assertEquals(20,
273: * Array.getLength(root.getGenericArray())); } else { synchronized (root) { GenericTestObject<Integer>[] genericArray =
274: * (GenericTestObject<Integer>[]) Array .newInstance(GenericTestObject.class, 20);
275: * root.setGenericArray(genericArray); } } }
276: */
277:
278: // ReadOnly tests.
279: void testReadOnlyModifyLongArray(DataRoot root, boolean validate) {
280: if (validate) {
281: Assert.assertEquals(0, root.getLongArray()[0]);
282: } else {
283: synchronized (root) {
284: try {
285: Array.setLong(root.getLongArray(), 0,
286: Long.MAX_VALUE);
287: } catch (ReadOnlyException roe) {
288: // ignore ReadOnlyException in test.
289: }
290: }
291: }
292: }
293:
294: public static void visitL1DSOConfig(ConfigVisitor visitor,
295: DSOClientConfigHelper config) {
296: String testClass = ReflectionArrayTestApp.class.getName();
297: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
298:
299: String writeAllowedMethodExpression = "* " + testClass
300: + "*.*(..)";
301: config.addWriteAutolock(writeAllowedMethodExpression);
302: String readOnlyMethodExpression = "* " + testClass
303: + "*.*ReadOnly*(..)";
304: config.addReadAutolock(readOnlyMethodExpression);
305:
306: config.addRoot(new Root(testClass, "dataRoot", "dataRoot"),
307: true);
308: spec.addRoot("barrier", "barrier");
309: spec.addRoot("barrier2", "barrier2");
310: config.addIncludePattern(DataRoot.class.getName());
311: config.addIncludePattern(Instance.class.getName());
312: new CyclicBarrierSpec().visit(visitor, config);
313: }
314:
315: private static class Instance {
316: //
317: }
318:
319: private static class DataRoot {
320: private final double[] doubleArray = new double[2];
321: private final float[] floatArray = new float[2];
322: private long[] longArray = new long[2];
323: private final int[] intArray = new int[2];
324: private final short[] shortArray = new short[2];
325: private final byte[] byteArray = new byte[2];
326: private final boolean[] booleanArray = new boolean[2];
327: private final char[] charArray = new char[2];
328: private final Instance[] instanceArray = new Instance[] {
329: new Instance(), new Instance() };
330:
331: // private GenericTestObject<Integer>[] genericArray;
332:
333: public DataRoot() {
334: super ();
335: }
336:
337: public Instance[] getInstanceArray() {
338: return instanceArray;
339: }
340:
341: public void setLongArray(long[] ls) {
342: this .longArray = ls;
343: }
344:
345: public double[] getDoubleArray() {
346: return doubleArray;
347: }
348:
349: public float[] getFloatArray() {
350: return floatArray;
351: }
352:
353: public long[] getLongArray() {
354: return longArray;
355: }
356:
357: public int[] getIntArray() {
358: return intArray;
359: }
360:
361: public short[] getShortArray() {
362: return shortArray;
363: }
364:
365: public byte[] getByteArray() {
366: return byteArray;
367: }
368:
369: public boolean[] getBooleanArray() {
370: return booleanArray;
371: }
372:
373: public char[] getCharArray() {
374: return charArray;
375: }
376:
377: /*
378: * public GenericTestObject<Integer>[] getGenericArray() { return genericArray; } public void
379: * setGenericArray(GenericTestObject<Integer>[] genericArray) { this.genericArray = genericArray; }
380: */
381:
382: }
383:
384: /*
385: * private static class GenericTestObject<T> { T obj; public GenericTestObject(T o) { this.obj = o; } public T
386: * getObj() { return obj; } public String toString() { return obj.toString(); } }
387: */
388: }
|