1 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 ; 18 import java.io.FileNotFoundException ; 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.util.ArrayList ; 23 import java.util.ConcurrentModificationException ; 24 import java.util.List ; 25 import java.util.NoSuchElementException ; 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 root = new ArrayList (); 34 final CyclicBarrier barrier; 35 36 public ShareExceptionsTestApp(String 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 testClass = ShareExceptionsTestApp.class.getName(); 43 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 44 String 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 config.addIncludePattern("*..*", false); 54 } 55 56 protected void runTest() throws Throwable { 57 moveToStage(INITIAL); 58 List 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 localCopy, List actual) { 73 Assert.assertEquals(localCopy.size(), actual.size()); 74 for (int i = 0; i < actual.size(); i++) { 75 Throwable tl = (Throwable ) localCopy.get(i); 76 Throwable ta = (Throwable ) actual.get(i); 77 verify(tl, ta); 78 } 79 } 80 81 private void verify(Throwable tl, Throwable 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 [] stackTrace1, StackTraceElement [] 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 getPrintString(Throwable t) { 113 StringWriter sw = new StringWriter (); 114 PrintWriter pw = new PrintWriter (sw); 115 t.printStackTrace(pw); 116 pw.close(); 117 return sw.toString(); 118 } 119 120 private void add2Root(List localCopy) { 121 synchronized (root) { 122 root.addAll(localCopy); 123 } 124 } 125 126 private List createVariousExceptions() { 127 List l = new ArrayList (); 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 private void addSomeRandomExceptions(List l) { 140 l.add(new Exception ("666")); 141 l.add(new RuntimeException ("This is a runtime exception")); 142 l.add(new AWTException ("This is awt exception")); 143 l.add(new IOException ("This is IO exception")); 144 l.add(new FileNotFoundException ("C:\\windows.sucks")); 145 l.add(new Error ("Serious Error")); 146 l.add(new ConcurrentModificationException ("Who touched my list ?")); 147 l.add(new NoSuchElementException ("No one named saro")); 148 149 } 150 151 private void addSetStackTraceException(List l) { 152 Throwable t1 = getException1(); 153 Throwable t2 = getException2(); 154 t1.setStackTrace(t2.getStackTrace()); 155 l.add(t1); 156 l.add(t2); 157 } 158 159 private Throwable getException1() { 160 return new MyLocalException("MyException1", count++); 161 } 162 163 private void addInitCausedException(List l) { 164 Throwable t = new MyLocalException("InitCausedException", count++); 165 t.initCause(new Exception ("Hello Sollu")); 166 l.add(t); 167 } 168 169 private void addNewlyCreatedAndStackTraceComputedException(List l) { 170 Throwable t = new MyLocalException("NewlyCreatedAndStackTraceComputedException", count++); 171 t.getStackTrace(); 172 l.add(t); 173 } 174 175 private void addNewlyCreatedException(List l) { 176 l.add(new MyLocalException("NewlyCreatedException", count++)); 177 } 178 179 private void addThrownException(List l) { 180 try { 181 getSomeException(); 182 } catch (Throwable t) { 183 l.add(t); 184 } 185 } 186 187 private void addThrownAndPrintedException(List l) { 188 try { 189 getSomeException(); 190 } catch (Throwable t) { 191 t.printStackTrace(); 192 l.add(t); 193 } 194 } 195 196 int count = 0; 197 198 private void getSomeException() throws Throwable { 199 throw new MyLocalException("My Local Exception", count++); 200 } 201 202 private Throwable getException2() { 203 return new MyLocalException("MyException2", count++); 204 } 205 206 public static class MyLocalException extends Throwable { 207 208 int localInt; 209 210 public MyLocalException(String message, int i) { 211 super(message + " - " + i); 212 localInt = i; 213 } 214 215 public MyLocalException(String message, Throwable cause) { 216 super(message, cause); 217 } 218 219 public boolean equals(Object 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
|