1 5 package com.tctest; 6 7 import com.tc.logging.CustomerLogging; 8 import com.tc.logging.LogLevel; 9 import com.tc.logging.TCAppender; 10 import com.tc.logging.TCLogging; 11 import com.tc.object.config.ConfigVisitor; 12 import com.tc.object.config.DSOClientConfigHelper; 13 import com.tc.object.config.TransparencyClassSpec; 14 import com.tc.simulator.app.ApplicationConfig; 15 import com.tc.simulator.listener.ListenerProvider; 16 import com.tctest.runner.AbstractErrorCatchingTransparentApp; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 public class NonPortableGraphTest extends TransparentTestBase { 22 23 private static final int NODE_COUNT = 1; 24 25 public void doSetUp(TransparentTestIface t) throws Exception { 26 t.getTransparentAppConfig().setClientCount(NODE_COUNT); 27 t.initializeTestRunner(); 28 } 29 30 protected Class getApplicationClass() { 31 return NonPortableGraphTestApp.class; 32 } 33 34 public static class NonPortableGraphTestApp extends AbstractErrorCatchingTransparentApp { 35 private Map map = new HashMap (); 36 private Portable portable; 37 private NonPortable non_portable; 38 private LogAppender dsoLogs; 39 40 public NonPortableGraphTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 41 super(appId, cfg, listenerProvider); 42 dsoLogs = new LogAppender(); 43 44 TCLogging.addAppender(CustomerLogging.getDSORuntimeLogger().getName(), dsoLogs); 45 } 46 47 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 48 49 String testClass = NonPortableGraphTestApp.class.getName(); 50 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 51 52 config.addIncludePattern(testClass); 53 config.addIncludePattern(testClass + "$Portable"); 54 55 String methodExpression = "* " + testClass + "*.*(..)"; 56 config.addWriteAutolock(methodExpression); 57 58 spec.addRoot("map", "map"); 59 spec.addRoot("portable", "portable"); 60 spec.addRoot("non_portable", "non_portable"); 61 } 62 63 protected void runTest() throws Throwable { 64 testNonportableObjectGraph(); 65 } 66 67 public void testNonportableObjectGraph() { 68 try { 69 synchronized (map) { 70 map.put("mmkay", new NonPortable()); 71 } 72 throw new Exception ("1. Expecting TCNonPortableObjectError"); 73 } catch (Throwable e) { 74 if (!e.getClass().getName().equals("com.tc.exception.TCNonPortableObjectError")) throw new RuntimeException (e); 75 } 76 77 assertTrue(dsoLogs.getCurrentLogEvent().replaceAll("\\s", "").indexOf(log1.replaceAll("\\s", "")) > 0); 78 79 try { 80 portable = new Portable(new NonPortable()); 81 throw new Exception ("2. Expecting TCNonPortableObjectError"); 82 } catch (Throwable e) { 83 if (!e.getClass().getName().equals("com.tc.exception.TCNonPortableObjectError")) throw new RuntimeException (e); 84 } 85 86 assertTrue(dsoLogs.getCurrentLogEvent().replaceAll("\\s", "").indexOf(log2.replaceAll("\\s", "")) > 0); 87 88 try { 89 non_portable = new NonPortable(); 90 throw new Exception ("3. Expecting TCNonPortableObjectError"); 91 } catch (Throwable e) { 92 if (!e.getClass().getName().equals("com.tc.exception.TCNonPortableObjectError")) throw new RuntimeException (e); 93 } 94 95 assertTrue(dsoLogs.getCurrentLogEvent().replaceAll("\\s", "").indexOf(log1.replaceAll("\\s", "")) > 0); 96 } 97 98 private static class NonPortable { 99 Map map = new HashMap (); 100 101 public NonPortable() { 102 map.put("self", this); 103 } 104 } 105 106 private static class Portable { 107 Object obj; 108 109 public Portable(Object obj) { 110 this.obj = obj; 111 } 112 } 113 114 private static class LogAppender implements TCAppender { 115 StringBuffer buffer = new StringBuffer (); 116 117 public void append(LogLevel arg0, Object arg1, Throwable arg2) { 118 buffer.append(arg1 + "\n"); 119 } 120 121 public String getCurrentLogEvent() { 122 String s = buffer.toString(); 123 buffer = new StringBuffer (); 124 return s; 125 } 126 127 } 128 129 private static final String log1 = "!! com.tctest.NonPortableGraphTest$NonPortableGraphTestApp$NonPortable (id 0)" 130 + " Map map = (HashMap, id 1)" 131 + " [entry 0]" 132 + " key = \"self\"" 133 + "!! value = (ref id 0)"; 134 135 private static final String log2 = " com.tctest.NonPortableGraphTest$NonPortableGraphTestApp$Portable (id 0)" 136 + "!! Object obj = (com.tctest.NonPortableGraphTest$NonPortableGraphTestApp$NonPortable, id 2)" 137 + " Map map = (HashMap, id 3)" 138 + " [entry 0]" 139 + " key = \"self\"" 140 + "!! value = (ref id 2)"; 141 } 142 } 143 | Popular Tags |