001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest.transparency;
005:
006: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
007:
008: import com.tc.object.config.ConfigVisitor;
009: import com.tc.object.config.DSOClientConfigHelper;
010: import com.tc.object.config.TransparencyClassSpec;
011: import com.tc.object.config.spec.CyclicBarrierSpec;
012: import com.tc.simulator.app.ApplicationConfig;
013: import com.tc.simulator.listener.ListenerProvider;
014: import com.tc.util.Assert;
015: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
016:
017: import java.awt.AWTException;
018: import java.io.FileNotFoundException;
019: import java.io.IOException;
020: import java.io.PrintWriter;
021: import java.io.StringWriter;
022: import java.util.ArrayList;
023: import java.util.ConcurrentModificationException;
024: import java.util.List;
025: import java.util.NoSuchElementException;
026:
027: public class ShareExceptionsTestApp extends
028: AbstractErrorCatchingTransparentApp {
029:
030: private static final int INITIAL = 0;
031: private static final int INTERMEDIATE = 1;
032: private static final int END = 2;
033:
034: final List root = new ArrayList();
035: final CyclicBarrier barrier;
036:
037: public ShareExceptionsTestApp(String appId, ApplicationConfig cfg,
038: ListenerProvider listenerProvider) {
039: super (appId, cfg, listenerProvider);
040: barrier = new CyclicBarrier(getParticipantCount());
041: }
042:
043: public static void visitL1DSOConfig(ConfigVisitor visitor,
044: DSOClientConfigHelper config) {
045: String testClass = ShareExceptionsTestApp.class.getName();
046: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
047: String methodExpression = "* " + testClass + "*.*(..)";
048: config.addWriteAutolock(methodExpression);
049: spec.addRoot("root", "root");
050: spec.addRoot("barrier", "barrier");
051:
052: CyclicBarrierSpec cbspec = new CyclicBarrierSpec();
053: cbspec.visit(visitor, config);
054:
055: // Include everything to be instrumented.
056: config.addIncludePattern("*..*", false);
057: }
058:
059: protected void runTest() throws Throwable {
060: moveToStage(INITIAL);
061: List localCopy = createVariousExceptions();
062: int n = barrier.barrier();
063: if (n == 0) {
064: add2Root(localCopy);
065: moveToStage(INTERMEDIATE);
066: } else {
067: moveToStageAndWait(INTERMEDIATE);
068: synchronized (root) {
069: verify(localCopy, root);
070: }
071: }
072: moveToStage(END);
073: }
074:
075: private void verify(List localCopy, List actual) {
076: Assert.assertEquals(localCopy.size(), actual.size());
077: for (int i = 0; i < actual.size(); i++) {
078: Throwable tl = (Throwable) localCopy.get(i);
079: Throwable ta = (Throwable) actual.get(i);
080: verify(tl, ta);
081: }
082: }
083:
084: private void verify(Throwable tl, Throwable ta) {
085: if (tl == null) {
086: Assert.assertTrue(ta == null);
087: return;
088: }
089: System.err.println(" Local Copy = " + tl);
090: tl.printStackTrace();
091: System.err.println(" Actual = " + ta);
092: ta.printStackTrace();
093: if (tl instanceof MyLocalException) {
094: Assert.assertTrue(ta instanceof MyLocalException);
095: Assert
096: .assertTrue(((MyLocalException) tl).localInt == ((MyLocalException) ta).localInt);
097: }
098: Assert.assertEquals(tl.getMessage(), ta.getMessage());
099: Assert.assertEquals(tl.getLocalizedMessage(), ta
100: .getLocalizedMessage());
101: if (tl.getCause() != tl) {
102: Assert.assertTrue(ta.getCause() != ta);
103: verify(tl.getCause(), ta.getCause());
104: }
105: Assert.assertEquals(tl.toString(), ta.toString());
106: Assert.assertEquals(getPrintString(tl), getPrintString(ta));
107: verify(tl.getStackTrace(), ta.getStackTrace());
108: }
109:
110: private void verify(StackTraceElement[] stackTrace1,
111: StackTraceElement[] stackTrace2) {
112: Assert.assertEquals(stackTrace1.length, stackTrace2.length);
113: for (int i = 0; i < stackTrace1.length; i++) {
114: Assert.assertEquals(stackTrace1[i], stackTrace2[i]);
115: }
116: }
117:
118: private String getPrintString(Throwable t) {
119: StringWriter sw = new StringWriter();
120: PrintWriter pw = new PrintWriter(sw);
121: t.printStackTrace(pw);
122: pw.close();
123: return sw.toString();
124: }
125:
126: private void add2Root(List localCopy) {
127: synchronized (root) {
128: root.addAll(localCopy);
129: }
130: }
131:
132: private List createVariousExceptions() {
133: List l = new ArrayList();
134: addThrownException(l);
135: addThrownAndPrintedException(l);
136: addNewlyCreatedException(l);
137: addNewlyCreatedAndStackTraceComputedException(l);
138: addInitCausedException(l);
139: addSetStackTraceException(l);
140: addSomeRandomExceptions(l);
141: return l;
142: }
143:
144: // TODO:: ADD MORE (ALL) EXCEPTIONS HERE
145: private void addSomeRandomExceptions(List l) {
146: l.add(new Exception("666"));
147: l.add(new RuntimeException("This is a runtime exception"));
148: l.add(new AWTException("This is awt exception"));
149: l.add(new IOException("This is IO exception"));
150: l.add(new FileNotFoundException("C:\\windows.sucks"));
151: l.add(new Error("Serious Error"));
152: l.add(new ConcurrentModificationException(
153: "Who touched my list ?"));
154: l.add(new NoSuchElementException("No one named saro"));
155:
156: }
157:
158: private void addSetStackTraceException(List l) {
159: Throwable t1 = getException1();
160: Throwable t2 = getException2();
161: t1.setStackTrace(t2.getStackTrace());
162: l.add(t1);
163: l.add(t2);
164: }
165:
166: private Throwable getException1() {
167: return new MyLocalException("MyException1", count++);
168: }
169:
170: private void addInitCausedException(List l) {
171: Throwable t = new MyLocalException("InitCausedException",
172: count++);
173: t.initCause(new Exception("Hello Sollu"));
174: l.add(t);
175: }
176:
177: private void addNewlyCreatedAndStackTraceComputedException(List l) {
178: Throwable t = new MyLocalException(
179: "NewlyCreatedAndStackTraceComputedException", count++);
180: t.getStackTrace();
181: l.add(t);
182: }
183:
184: private void addNewlyCreatedException(List l) {
185: l.add(new MyLocalException("NewlyCreatedException", count++));
186: }
187:
188: private void addThrownException(List l) {
189: try {
190: getSomeException();
191: } catch (Throwable t) {
192: l.add(t);
193: }
194: }
195:
196: private void addThrownAndPrintedException(List l) {
197: try {
198: getSomeException();
199: } catch (Throwable t) {
200: t.printStackTrace();
201: l.add(t);
202: }
203: }
204:
205: int count = 0;
206:
207: private void getSomeException() throws Throwable {
208: throw new MyLocalException("My Local Exception", count++);
209: }
210:
211: private Throwable getException2() {
212: return new MyLocalException("MyException2", count++);
213: }
214:
215: public static class MyLocalException extends Throwable {
216:
217: int localInt;
218:
219: public MyLocalException(String message, int i) {
220: super (message + " - " + i);
221: localInt = i;
222: }
223:
224: public MyLocalException(String message, Throwable cause) {
225: super (message, cause);
226: }
227:
228: public boolean equals(Object o) {
229: System.err.println("Equals Method called on "
230: + this .getMessage() + " and " + o);
231: if (o instanceof MyLocalException) {
232: MyLocalException other = (MyLocalException) o;
233: boolean b = this .localInt == other.localInt;
234: if (!b)
235: return b;
236: b = this .getMessage().equals(other.getMessage());
237: if (!b)
238: return b;
239: return true;
240: }
241: return false;
242: }
243:
244: }
245: }
|