KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > NullLiteralArrayElementRegressionTestApp


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 EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
7
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.config.TransparencyClassSpec;
11 import com.tc.object.config.spec.CyclicBarrierSpec;
12 import com.tc.simulator.app.ApplicationConfig;
13 import com.tc.simulator.listener.ListenerProvider;
14 import com.tctest.runner.AbstractTransparentApp;
15
16 public class NullLiteralArrayElementRegressionTestApp extends AbstractTransparentApp {
17
18   private final TestObject root = new TestObject();
19   private final CyclicBarrier barrier;
20
21   public NullLiteralArrayElementRegressionTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
22     super(appId, cfg, listenerProvider);
23
24     if (getParticipantCount() != 3) {
25       // must have 3 nodes for this test to work
26
throw new RuntimeException JavaDoc("wrong number of nodes: " + getParticipantCount());
27     }
28
29     barrier = new CyclicBarrier(getParticipantCount());
30   }
31
32   public void run() {
33     try {
34       test();
35     } catch (Throwable JavaDoc t) {
36       notifyError(t);
37     }
38   }
39
40   private void test() throws Exception JavaDoc {
41     // Get the root object paged into each node (and strongly held) before creating the array
42
TestObject obj = root;
43
44     final boolean creator;
45     synchronized (obj) {
46       if (!obj.hasArray()) {
47         creator = true;
48         obj.setArray(new Object JavaDoc[10]);
49       } else {
50         creator = false;
51       }
52     }
53
54     barrier.barrier();
55
56     if (creator) {
57       synchronized (obj) {
58         obj.setElement(5, 42L);
59       }
60     }
61
62     barrier.barrier();
63
64     Object JavaDoc value = obj.getElement(5);
65
66     if (value == null) { throw new NullPointerException JavaDoc("element is null"); }
67   }
68
69   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
70     String JavaDoc testClass = NullLiteralArrayElementRegressionTestApp.class.getName();
71     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
72
73     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
74     config.addWriteAutolock(methodExpression);
75     spec.addRoot("barrier", "barrier");
76     spec.addRoot("root", "root");
77     config.addIncludePattern(TestObject.class.getName());
78
79     new CyclicBarrierSpec().visit(visitor, config);
80   }
81
82   private static class TestObject {
83     private Object JavaDoc[] array;
84
85     boolean hasArray() {
86       return this.array != null;
87     }
88
89     void setArray(Object JavaDoc[] a) {
90       this.array = a;
91     }
92
93     Object JavaDoc getElement(int index) {
94       return this.array[index];
95     }
96
97     // This method takes a long b/c that is a "literal" type, but stored in an Object array
98
void setElement(int index, long value) {
99       array[index] = new Long JavaDoc(value);
100     }
101
102   }
103
104 }
105
Popular Tags