KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > NonPortableInstancesTest


1 /*
2  * All content copyright (c) 2003-2007 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 org.apache.commons.io.IOUtils;
8 import org.apache.commons.lang.ClassUtils;
9
10 import com.tc.exception.TCNonPortableObjectError;
11 import com.tc.logging.CustomerLogging;
12 import com.tc.logging.LogLevel;
13 import com.tc.logging.TCAppender;
14 import com.tc.logging.TCLogging;
15 import com.tc.object.bytecode.Manageable;
16 import com.tc.object.config.ConfigVisitor;
17 import com.tc.object.config.DSOClientConfigHelper;
18 import com.tc.object.config.TransparencyClassSpec;
19 import com.tc.simulator.app.ApplicationConfig;
20 import com.tc.simulator.listener.ListenerProvider;
21 import com.tc.util.Assert;
22 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.net.ServerSocket JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 public class NonPortableInstancesTest extends TransparentTestBase {
35
36   private static final int NODE_COUNT = 1;
37
38   public void doSetUp(TransparentTestIface t) throws Exception JavaDoc {
39     t.getTransparentAppConfig().setClientCount(NODE_COUNT);
40     t.initializeTestRunner();
41   }
42
43   protected Class JavaDoc getApplicationClass() {
44     return App.class;
45   }
46
47   public static class App extends AbstractErrorCatchingTransparentApp {
48
49     private final Object JavaDoc[] array = new Object JavaDoc[1];
50     private final Ref physicalObject = new Ref();
51     private final List JavaDoc logicalObject = new ArrayList JavaDoc();
52     private Object JavaDoc nonPortableRoot;
53
54     private final Map JavaDoc map = new HashMap JavaDoc();
55
56     private final LogAppender logEvents;
57
58     public App(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
59       super(appId, cfg, listenerProvider);
60
61       logEvents = new LogAppender();
62
63       TCLogging.addAppender(CustomerLogging.getDSORuntimeLogger().getName(), logEvents);
64     }
65
66     public Object JavaDoc getNonPortableRoot() {
67       // this method here to silence compiler warning
68
return nonPortableRoot;
69     }
70
71     protected void runTest() throws Throwable JavaDoc {
72
73       // array elements are checked for portability before traversing
74
try {
75         synchronized (array) {
76           array[0] = new NotPortable();
77         }
78         throw new AssertionError JavaDoc();
79       } catch (TCNonPortableObjectError e) {
80         // expected
81
}
82       validate(1);
83
84       // field sets are checked for portability before traversing
85
try {
86         synchronized (physicalObject) {
87           physicalObject.setRef(new NotPortable());
88         }
89         throw new AssertionError JavaDoc();
90       } catch (TCNonPortableObjectError e) {
91         // expected
92
}
93       validate(1);
94
95       // params to methods on logical types are checked for portability before traversing
96
try {
97         synchronized (logicalObject) {
98           logicalObject.add(new NotPortable());
99         }
100         throw new AssertionError JavaDoc();
101       } catch (TCNonPortableObjectError e) {
102         // expected
103
}
104       validate(1);
105
106       // root values are checked for portability before traversing
107
try {
108         nonPortableRoot = new NotPortable();
109         throw new AssertionError JavaDoc();
110       } catch (TCNonPortableObjectError e) {
111         // expected
112
}
113       validate(1);
114
115       // This test will pass the initial portability checks (both params to put() are portable), but the value object
116
// contains a reference to a non-portable type
117
try {
118         synchronized (map) {
119           map.put("key", new Portable());
120         }
121         throw new AssertionError JavaDoc();
122       } catch (TCNonPortableObjectError e) {
123         // expected
124
}
125       validate(2);
126
127     }
128
129     private void validate(int i) throws IOException JavaDoc {
130       String JavaDoc expect = getExpected(i);
131       String JavaDoc actual = logEvents.takeLoggedMessages();
132
133       expect = expect.replaceAll("\r", "");
134       actual = actual.replaceAll("\r", "");
135
136       Assert.assertEquals(expect, actual);
137     }
138
139     private String JavaDoc getExpected(int i) throws IOException JavaDoc {
140       String JavaDoc resource = ClassUtils.getShortClassName(getClass()) + "-dump" + i + ".txt";
141       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
142
143       InputStream JavaDoc in = null;
144       try {
145         in = getClass().getResourceAsStream(resource);
146         if (in == null) {
147           fail("missing resource: " + resource);
148         }
149         IOUtils.copy(in, baos);
150       } finally {
151         if (in != null) {
152           try {
153             in.close();
154           } catch (Exception JavaDoc e) {
155             throw new RuntimeException JavaDoc(e);
156           }
157         }
158       }
159
160       baos.flush();
161       return new String JavaDoc(baos.toByteArray());
162     }
163
164     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
165       config.getOrCreateSpec(Ref.class.getName());
166       TransparencyClassSpec spec = config.getOrCreateSpec(Portable.class.getName());
167       spec.setHonorTransient(true);
168       spec.addTransient("ss");
169
170       String JavaDoc testClass = App.class.getName();
171       spec = config.getOrCreateSpec(testClass);
172       String JavaDoc methodExpr = "* " + testClass + "*.*(..)";
173       config.addWriteAutolock(methodExpr);
174
175       spec.addRoot("logicalObject", "logicalObject");
176       spec.addRoot("array", "array");
177       spec.addRoot("physicalObject", "physicalObject");
178       spec.addRoot("nonPortableRoot", "nonPortableRoot");
179
180       spec.addRoot("map", "map");
181     }
182
183   }
184
185   private static class Ref {
186     private Object JavaDoc ref;
187
188     Object JavaDoc getRef() {
189       return ref;
190     }
191
192     void setRef(Object JavaDoc ref) {
193       this.ref = ref;
194     }
195
196   }
197
198   private static class Portable {
199     Ref ref = new Ref();
200
201     transient Runtime JavaDoc honeredTransient = Runtime.getRuntime();
202     ServerSocket JavaDoc ss; // transient by DSO config
203

204     Portable() {
205       ref.setRef(makeGraphWithNonPortableNodes(new NotPortable()));
206       try {
207         ss = new ServerSocket JavaDoc();
208       } catch (IOException JavaDoc e) {
209         throw new AssertionError JavaDoc(e);
210       }
211     }
212   }
213
214   private static class NotPortable {
215     final Map JavaDoc m = makeGraphWithNonPortableNodes(this);
216
217     Thread JavaDoc t = Thread.currentThread();
218
219     NotPortable() {
220       if (this instanceof Manageable) { throw new AssertionError JavaDoc("this type should not be portable"); }
221     }
222   }
223
224   private static Map JavaDoc makeGraphWithNonPortableNodes(Object JavaDoc nonPortable) {
225     Map JavaDoc m = new HashMap JavaDoc();
226     Ref ref = new Ref();
227     Ref r2 = new Ref();
228     r2.setRef(nonPortable);
229     ref.setRef(r2);
230
231     m.put("ref", ref);
232
233     Object JavaDoc[][] a = new Object JavaDoc[][] { { null }, { new Ref() } };
234     ((Ref) a[1][0]).setRef(m);
235
236     return m;
237   }
238
239   private static class LogAppender implements TCAppender {
240
241     private final List JavaDoc events = new ArrayList JavaDoc();
242
243     public void append(LogLevel level, Object JavaDoc message, Throwable JavaDoc throwable) {
244       events.add(new Event(level, message, throwable));
245     }
246
247     String JavaDoc takeLoggedMessages() {
248       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
249       for (Iterator JavaDoc iter = events.iterator(); iter.hasNext();) {
250         Event event = (Event) iter.next();
251         buf.append(event.getMessage() + "\n");
252       }
253
254       events.clear();
255
256       return buf.toString();
257     }
258
259   }
260
261   static class Event {
262     private final LogLevel level;
263     private final Object JavaDoc message;
264     private final Throwable JavaDoc throwable;
265
266     Event(LogLevel level, Object JavaDoc message, Throwable JavaDoc throwable) {
267       this.level = level;
268       this.message = message;
269       this.throwable = throwable;
270     }
271
272     public LogLevel getLevel() {
273       return level;
274     }
275
276     public Object JavaDoc getMessage() {
277       return message;
278     }
279
280     public Throwable JavaDoc getThrowable() {
281       return throwable;
282     }
283
284     public String JavaDoc toString() {
285       return "[" + level + "] " + message + ((throwable == null) ? "" : throwable.getMessage());
286     }
287   }
288
289 }
290
Popular Tags