KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > JavaUtilArraysTestApp


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

4 package com.tctest;
5
6 import org.apache.commons.lang.ArrayUtils;
7
8 import EDU.oswego.cs.dl.util.concurrent.Latch;
9
10 import com.tc.object.config.ConfigVisitor;
11 import com.tc.object.config.DSOClientConfigHelper;
12 import com.tc.object.config.TransparencyClassSpec;
13 import com.tc.simulator.app.ApplicationConfig;
14 import com.tc.simulator.listener.ListenerProvider;
15 import com.tc.util.Assert;
16 import com.tctest.runner.AbstractTransparentApp;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * Makes sure that the methods in java.util.Arrays work with DSO managed arrays
24  */

25 public class JavaUtilArraysTestApp extends AbstractTransparentApp {
26
27   private final List JavaDoc data = new ArrayList JavaDoc();
28   private final Latch latch;
29
30   public JavaUtilArraysTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
31     super(appId, cfg, listenerProvider);
32     if (getParticipantCount() != 2) { throw new RuntimeException JavaDoc("invalid participant count " + getParticipantCount()); }
33     this.latch = new Latch();
34   }
35
36   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
37     String JavaDoc testClass = JavaUtilArraysTestApp.class.getName();
38     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
39     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
40     config.addWriteAutolock(methodExpression);
41     spec.addRoot("latch", "latch");
42     spec.addRoot("data", "data");
43     config.addIncludePattern(Data.class.getName());
44     String JavaDoc latchClassName = Latch.class.getName();
45     config.addIncludePattern(latchClassName);
46     // config.addWriteAutolock("* " + latchClassName + "*.*(..)");
47
}
48
49   public void run() {
50     final boolean firstNode;
51
52     synchronized (data) {
53       firstNode = data.size() == 0;
54       if (firstNode) {
55         data.add("temp");
56       }
57     }
58
59     if (!firstNode) {
60       try {
61         synchronized (latch) {
62           latch.acquire();
63         }
64       } catch (InterruptedException JavaDoc e) {
65         throw new RuntimeException JavaDoc(e);
66       }
67       validateData();
68     } else {
69       setupData();
70       synchronized (latch) {
71         latch.release();
72       }
73     }
74   }
75
76   private String JavaDoc expected(Object JavaDoc theWayItShouldBe, Object JavaDoc theWayItIs) {
77     return "expected " + ArrayUtils.toString(theWayItShouldBe) + ", but found: " + ArrayUtils.toString(theWayItIs);
78   }
79
80   private void validateData() {
81     Data d;
82
83     synchronized (data) {
84       d = (Data) data.remove(0);
85     }
86
87     synchronized (d) {
88       byte[] byteFillCompare = new byte[10];
89       Arrays.fill(byteFillCompare, (byte) 1);
90       if (!Arrays.equals(byteFillCompare, d.filledByte)) {
91         // formatting
92
throw new RuntimeException JavaDoc("filled bytes " + expected(byteFillCompare, d.filledByte));
93       }
94       byte[] sortedByte = new byte[] { Byte.MIN_VALUE, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, Byte.MAX_VALUE };
95       if (!Arrays.equals(sortedByte, d.sortedByte)) {
96         // formatting
97
throw new RuntimeException JavaDoc("sorted bytes: " + expected(sortedByte, d.sortedByte));
98       }
99
100       char[] charFillCompare = new char[10];
101       Arrays.fill(charFillCompare, 'z');
102       if (!Arrays.equals(charFillCompare, d.filledChar)) {
103         // formatting
104
throw new RuntimeException JavaDoc("filled chars: " + expected(charFillCompare, d.filledChar));
105       }
106       char[] sortedChar = new char[] { Character.MIN_VALUE, 'c', 'e', 'f', 'k', 'm', 'u', Character.MAX_VALUE };
107       if (!Arrays.equals(sortedChar, d.sortedChar)) {
108         // formatting
109
throw new RuntimeException JavaDoc("sorted chars: " + expected(sortedChar, d.sortedChar));
110       }
111
112       double[] doubleFillCompare = new double[10];
113       Arrays.fill(doubleFillCompare, Math.PI);
114       if (!Arrays.equals(doubleFillCompare, d.filledDouble)) {
115         // formatting
116
throw new RuntimeException JavaDoc("filled doubles: " + expected(doubleFillCompare, d.filledDouble));
117       }
118       double[] sortedDouble = new double[] { -Double.MAX_VALUE, -1.43D, -Double.MIN_VALUE, 0D, Double.MIN_VALUE,
119           Math.E, Math.PI, Double.MAX_VALUE };
120       if (!Arrays.equals(sortedDouble, d.sortedDouble)) {
121         // formatting
122
throw new RuntimeException JavaDoc("sorted doubles: " + expected(sortedDouble, d.sortedDouble));
123       }
124
125       float[] floatFillCompare = new float[10];
126       Arrays.fill(floatFillCompare, 8.1F);
127       if (!Arrays.equals(floatFillCompare, d.filledFloat)) {
128         // formatting
129
throw new RuntimeException JavaDoc("filled floats: " + expected(floatFillCompare, d.filledFloat));
130       }
131       float[] sortedFloat = new float[] { -123F, 0F, Float.MIN_VALUE, 3.14F, Float.MAX_VALUE };
132       if (!Arrays.equals(sortedFloat, d.sortedFloat)) {
133         // formatting
134
throw new RuntimeException JavaDoc("sorted floats: " + expected(sortedFloat, d.sortedFloat));
135       }
136
137       int[] intFillCompare = new int[10];
138       Arrays.fill(intFillCompare, 1);
139       if (!Arrays.equals(intFillCompare, d.filledInt)) {
140         // formatting
141
throw new RuntimeException JavaDoc("filled ints: " + expected(intFillCompare, d.filledInt));
142       }
143       int[] sortedInt = new int[] { Integer.MIN_VALUE, 1, 2, 7, 8, Integer.MAX_VALUE };
144       if (!Arrays.equals(sortedInt, d.sortedInt)) {
145         // formatting
146
throw new RuntimeException JavaDoc("sorted ints: " + expected(sortedInt, d.sortedInt));
147       }
148
149       long[] longFillCompare = new long[10];
150       Arrays.fill(longFillCompare, 42L);
151       if (!Arrays.equals(longFillCompare, d.filledLong)) {
152         // formatting
153
throw new RuntimeException JavaDoc("filled longs: " + expected(longFillCompare, d.filledLong));
154       }
155       long[] sortedLong = new long[] { Long.MIN_VALUE, 1L, 5L, 1000L, Long.MAX_VALUE };
156       if (!Arrays.equals(sortedLong, d.sortedLong)) {
157         // formatting
158
throw new RuntimeException JavaDoc("sorted longs: " + expected(sortedLong, d.sortedLong));
159       }
160
161       short[] shortFillCompare = new short[10];
162       Arrays.fill(shortFillCompare, (short) 69);
163       if (!Arrays.equals(shortFillCompare, d.filledShort)) {
164         // formatting
165
throw new RuntimeException JavaDoc("filled shorts: " + expected(shortFillCompare, d.filledShort));
166       }
167       short[] sortedShort = new short[] { Short.MIN_VALUE, 0, 2, Short.MAX_VALUE };
168       if (!Arrays.equals(sortedShort, d.sortedShort)) {
169         // formatting
170
throw new RuntimeException JavaDoc("sorted shorts: " + expected(sortedShort, d.sortedShort));
171       }
172
173       Object JavaDoc[] objectFillCompare = new Object JavaDoc[10];
174       Arrays.fill(objectFillCompare, new Integer JavaDoc(1));
175       if (!Arrays.equals(objectFillCompare, d.filledObject)) {
176         // formatting
177
throw new RuntimeException JavaDoc("filled objects: " + expected(objectFillCompare, d.filledObject));
178       }
179       Object JavaDoc[] sortedObject = new Object JavaDoc[] { new Integer JavaDoc(-4), new Integer JavaDoc(3) };
180       if (!Arrays.equals(sortedObject, d.sortedObject)) {
181         // formatting
182
throw new RuntimeException JavaDoc("sorted objects: " + expected(sortedObject, d.sortedObject));
183       }
184
185       boolean[] booleanFillCompare = new boolean[10];
186       Arrays.fill(booleanFillCompare, false);
187       if (!Arrays.equals(booleanFillCompare, d.filledBoolean)) {
188         // formatting
189
throw new RuntimeException JavaDoc("filled boolean: " + expected(booleanFillCompare, d.filledBoolean));
190       }
191
192       List JavaDoc list = d.asList;
193       if (list.size() != 2) { throw new RuntimeException JavaDoc("list wrong size: " + list.size()); }
194       if (!list.get(0).equals(new Integer JavaDoc(-4))) { throw new RuntimeException JavaDoc("wrong data"); }
195       if (!list.get(1).equals(new Integer JavaDoc(3))) { throw new RuntimeException JavaDoc("wrong data"); }
196
197       Object JavaDoc[] array = d.dataBehindAsList2;
198       if (array.length != 4) { throw new RuntimeException JavaDoc("array size wrong: " + array.length); }
199       Assert.assertEquals("put", array[0]);
200       Assert.assertEquals("the", array[1]);
201       Assert.assertEquals("rap", array[2]);
202       Assert.assertEquals("down", array[3]);
203     }
204   }
205
206   private void setupData() {
207     Data d = new Data();
208
209     synchronized (data) {
210       data.clear();
211       data.add(d);
212     }
213
214     d.sort();
215     d.fill();
216     d.modifyAsList2();
217   }
218
219   // NOTE: There is no such thing as sorted booleans
220
private static final byte[] unsortedByte = new byte[] { 0, 6, 3, 7, Byte.MAX_VALUE, 4, 2, 9, 8, 1, 5,
221       Byte.MIN_VALUE };
222   private static final char[] unsortedChar = new char[] { 'f', Character.MAX_VALUE, 'e', 'u', 'k', 'c', 'm',
223       Character.MIN_VALUE };
224   private static final double[] unsortedDouble = new double[] { Math.PI, Double.MIN_VALUE, -Double.MAX_VALUE, Math.E,
225       Double.MAX_VALUE, 0D, -Double.MIN_VALUE, -1.43D };
226   private static final float[] unsortedFloat = new float[] { Float.MAX_VALUE, 0F, Float.MIN_VALUE, -123F, 3.14F };
227   private static final int[] unsortedInt = new int[] { 2, Integer.MAX_VALUE, 7, Integer.MIN_VALUE, 8, 1 };
228   private static final long[] unsortedLong = new long[] { 1000L, 5L, 1L, Long.MIN_VALUE, Long.MAX_VALUE };
229   private static final short[] unsortedShort = new short[] { Short.MAX_VALUE, 2, Short.MIN_VALUE, 0 };
230   private static final Object JavaDoc[] unsortedObject = new Object JavaDoc[] { new Integer JavaDoc(3), new Integer JavaDoc(-4) };
231
232   private static class Data {
233
234     private boolean[] filledBoolean;
235     // NOTE: There is no such thing as sorted booleans
236

237     private final byte[] filledByte;
238     private final byte[] sortedByte;
239
240     private final char[] filledChar;
241     private final char[] sortedChar;
242
243     private final double[] filledDouble;
244     private final double[] sortedDouble;
245
246     private final float[] filledFloat;
247     private final float[] sortedFloat;
248
249     private final int[] filledInt;
250     private final int[] sortedInt;
251
252     private final long[] filledLong;
253     private final long[] sortedLong;
254
255     private final short[] filledShort;
256     private final short[] sortedShort;
257
258     private final Object JavaDoc[] filledObject;
259     private final Object JavaDoc[] sortedObject;
260
261     private final List JavaDoc asList;
262     private final List JavaDoc asList2;
263
264     private final Object JavaDoc[] dataBehindAsList2;
265
266     Data() {
267       this.filledBoolean = new boolean[10];
268       Arrays.fill(filledBoolean, true);
269
270       this.filledByte = new byte[10];
271       this.sortedByte = (byte[]) unsortedByte.clone();
272
273       this.filledChar = new char[10];
274       this.sortedChar = (char[]) unsortedChar.clone();
275
276       this.filledDouble = new double[10];
277       this.sortedDouble = (double[]) unsortedDouble.clone();
278
279       this.filledInt = new int[10];
280       this.sortedInt = (int[]) unsortedInt.clone();
281
282       this.filledFloat = new float[10];
283       this.sortedFloat = (float[]) unsortedFloat.clone();
284
285       this.filledLong = new long[10];
286       this.sortedLong = (long[]) unsortedLong.clone();
287
288       this.filledShort = new short[10];
289       this.sortedShort = (short[]) unsortedShort.clone();
290
291       this.filledObject = new Object JavaDoc[10];
292       this.sortedObject = (Object JavaDoc[]) unsortedObject.clone();
293
294       // the underlying array will be sorted(), so we should expect the List to come out sorted on the other side
295
this.asList = Arrays.asList(this.sortedObject);
296
297       this.dataBehindAsList2 = new Object JavaDoc[4];
298       this.dataBehindAsList2[0] = "put";
299       this.dataBehindAsList2[1] = "the";
300       this.dataBehindAsList2[2] = "smack";
301       this.dataBehindAsList2[3] = "down";
302
303       this.asList2 = Arrays.asList(this.dataBehindAsList2);
304     }
305
306     synchronized void fill() {
307       Arrays.fill(this.filledBoolean, false);
308       Arrays.fill(this.filledByte, (byte) 1);
309       Arrays.fill(this.filledChar, 'z');
310       Arrays.fill(this.filledDouble, Math.PI);
311       Arrays.fill(this.filledFloat, 8.1F);
312       Arrays.fill(this.filledInt, 1);
313       Arrays.fill(this.filledLong, 42L);
314       Arrays.fill(this.filledShort, (short) 69);
315       Arrays.fill(this.filledObject, new Integer JavaDoc(1));
316     }
317
318     synchronized void sort() {
319       Arrays.sort(this.sortedByte);
320       // There is no boolean natural sort
321
Arrays.sort(this.sortedChar);
322       Arrays.sort(this.sortedDouble);
323       Arrays.sort(this.sortedFloat);
324       Arrays.sort(this.sortedInt);
325       Arrays.sort(this.sortedLong);
326       Arrays.sort(this.sortedShort);
327       Arrays.sort(this.sortedObject);
328     }
329
330     synchronized void modifyAsList2() {
331       // test that the modifications made through the asList() makes it into the backend DSO managed array
332
this.asList2.set(2, "rap");
333     }
334
335   }
336
337   // private static class Latch {
338
// private boolean set = false;
339
//
340
// synchronized void acquire() {
341
// while (!set) {
342
// try {
343
// wait();
344
// } catch (InterruptedException e) {
345
// throw new RuntimeException(e);
346
// }
347
// }
348
// }
349
//
350
// synchronized void release() {
351
// set = true;
352
// notifyAll();
353
// }
354
// }
355

356 }
357
Popular Tags