KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > transparency > ShareExceptionsTestApp


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.transparency;
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.tc.util.Assert;
15 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
16
17 import java.awt.AWTException JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.ConcurrentModificationException JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26
27 public class ShareExceptionsTestApp extends AbstractErrorCatchingTransparentApp {
28
29   private static final int INITIAL = 0;
30   private static final int INTERMEDIATE = 1;
31   private static final int END = 2;
32
33   final List JavaDoc root = new ArrayList JavaDoc();
34   final CyclicBarrier barrier;
35
36   public ShareExceptionsTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
37     super(appId, cfg, listenerProvider);
38     barrier = new CyclicBarrier(getParticipantCount());
39   }
40
41   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
42     String JavaDoc testClass = ShareExceptionsTestApp.class.getName();
43     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
44     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
45     config.addWriteAutolock(methodExpression);
46     spec.addRoot("root", "root");
47     spec.addRoot("barrier", "barrier");
48
49     CyclicBarrierSpec cbspec = new CyclicBarrierSpec();
50     cbspec.visit(visitor, config);
51
52     // Include everything to be instrumented.
53
config.addIncludePattern("*..*", false);
54   }
55
56   protected void runTest() throws Throwable JavaDoc {
57     moveToStage(INITIAL);
58     List JavaDoc localCopy = createVariousExceptions();
59     int n = barrier.barrier();
60     if (n == 0) {
61       add2Root(localCopy);
62       moveToStage(INTERMEDIATE);
63     } else {
64       moveToStageAndWait(INTERMEDIATE);
65       synchronized (root) {
66         verify(localCopy, root);
67       }
68     }
69     moveToStage(END);
70   }
71
72   private void verify(List JavaDoc localCopy, List JavaDoc actual) {
73     Assert.assertEquals(localCopy.size(), actual.size());
74     for (int i = 0; i < actual.size(); i++) {
75       Throwable JavaDoc tl = (Throwable JavaDoc) localCopy.get(i);
76       Throwable JavaDoc ta = (Throwable JavaDoc) actual.get(i);
77       verify(tl, ta);
78     }
79   }
80
81   private void verify(Throwable JavaDoc tl, Throwable JavaDoc ta) {
82     if (tl == null) {
83       Assert.assertTrue(ta == null);
84       return;
85     }
86     System.err.println(" Local Copy = " + tl);
87     tl.printStackTrace();
88     System.err.println(" Actual = " + ta);
89     ta.printStackTrace();
90     if(tl instanceof MyLocalException) {
91       Assert.assertTrue(ta instanceof MyLocalException);
92       Assert.assertTrue(((MyLocalException)tl).localInt == ((MyLocalException)ta).localInt);
93     }
94     Assert.assertEquals(tl.getMessage(), ta.getMessage());
95     Assert.assertEquals(tl.getLocalizedMessage(), ta.getLocalizedMessage());
96     if (tl.getCause() != tl) {
97       Assert.assertTrue(ta.getCause() != ta);
98       verify(tl.getCause(), ta.getCause());
99     }
100     Assert.assertEquals(tl.toString(), ta.toString());
101     Assert.assertEquals(getPrintString(tl), getPrintString(ta));
102     verify(tl.getStackTrace(), ta.getStackTrace());
103   }
104
105   private void verify(StackTraceElement JavaDoc[] stackTrace1, StackTraceElement JavaDoc[] stackTrace2) {
106     Assert.assertEquals(stackTrace1.length, stackTrace2.length);
107     for (int i = 0; i < stackTrace1.length; i++) {
108       Assert.assertEquals(stackTrace1[i], stackTrace2[i]);
109     }
110   }
111
112   private String JavaDoc getPrintString(Throwable JavaDoc t) {
113     StringWriter JavaDoc sw = new StringWriter JavaDoc();
114     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
115     t.printStackTrace(pw);
116     pw.close();
117     return sw.toString();
118   }
119
120   private void add2Root(List JavaDoc localCopy) {
121     synchronized (root) {
122       root.addAll(localCopy);
123     }
124   }
125
126   private List JavaDoc createVariousExceptions() {
127     List JavaDoc l = new ArrayList JavaDoc();
128     addThrownException(l);
129     addThrownAndPrintedException(l);
130     addNewlyCreatedException(l);
131     addNewlyCreatedAndStackTraceComputedException(l);
132     addInitCausedException(l);
133     addSetStackTraceException(l);
134     addSomeRandomExceptions(l);
135     return l;
136   }
137
138   // TODO:: ADD MORE (ALL) EXCEPTIONS HERE
139
private void addSomeRandomExceptions(List JavaDoc l) {
140     l.add(new Exception JavaDoc("666"));
141     l.add(new RuntimeException JavaDoc("This is a runtime exception"));
142     l.add(new AWTException JavaDoc("This is awt exception"));
143     l.add(new IOException JavaDoc("This is IO exception"));
144     l.add(new FileNotFoundException JavaDoc("C:\\windows.sucks"));
145     l.add(new Error JavaDoc("Serious Error"));
146     l.add(new ConcurrentModificationException JavaDoc("Who touched my list ?"));
147     l.add(new NoSuchElementException JavaDoc("No one named saro"));
148
149   }
150
151   private void addSetStackTraceException(List JavaDoc l) {
152     Throwable JavaDoc t1 = getException1();
153     Throwable JavaDoc t2 = getException2();
154     t1.setStackTrace(t2.getStackTrace());
155     l.add(t1);
156     l.add(t2);
157   }
158
159   private Throwable JavaDoc getException1() {
160     return new MyLocalException("MyException1", count++);
161   }
162
163   private void addInitCausedException(List JavaDoc l) {
164     Throwable JavaDoc t = new MyLocalException("InitCausedException", count++);
165     t.initCause(new Exception JavaDoc("Hello Sollu"));
166     l.add(t);
167   }
168
169   private void addNewlyCreatedAndStackTraceComputedException(List JavaDoc l) {
170     Throwable JavaDoc t = new MyLocalException("NewlyCreatedAndStackTraceComputedException", count++);
171     t.getStackTrace();
172     l.add(t);
173   }
174
175   private void addNewlyCreatedException(List JavaDoc l) {
176     l.add(new MyLocalException("NewlyCreatedException", count++));
177   }
178
179   private void addThrownException(List JavaDoc l) {
180     try {
181       getSomeException();
182     } catch (Throwable JavaDoc t) {
183       l.add(t);
184     }
185   }
186
187   private void addThrownAndPrintedException(List JavaDoc l) {
188     try {
189       getSomeException();
190     } catch (Throwable JavaDoc t) {
191       t.printStackTrace();
192       l.add(t);
193     }
194   }
195
196   int count = 0;
197
198   private void getSomeException() throws Throwable JavaDoc {
199     throw new MyLocalException("My Local Exception", count++);
200   }
201
202   private Throwable JavaDoc getException2() {
203     return new MyLocalException("MyException2", count++);
204   }
205
206   public static class MyLocalException extends Throwable JavaDoc {
207
208     int localInt;
209
210     public MyLocalException(String JavaDoc message, int i) {
211       super(message + " - " + i);
212       localInt = i;
213     }
214
215     public MyLocalException(String JavaDoc message, Throwable JavaDoc cause) {
216       super(message, cause);
217     }
218
219     public boolean equals(Object JavaDoc o) {
220       System.err.println("Equals Method called on " + this.getMessage() + " and " + o);
221       if (o instanceof MyLocalException) {
222         MyLocalException other = (MyLocalException) o;
223         boolean b = this.localInt == other.localInt;
224         if (!b) return b;
225         b = this.getMessage().equals(other.getMessage());
226         if (!b) return b;
227         return true;
228       }
229       return false;
230     }
231
232   }
233 }
234
Popular Tags