KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > NonPortableGraphTest


1 /*
2  * All content copyright (c) 2003-2006 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 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 JavaDoc;
19 import java.util.Map JavaDoc;
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 JavaDoc {
26     t.getTransparentAppConfig().setClientCount(NODE_COUNT);
27     t.initializeTestRunner();
28   }
29
30   protected Class JavaDoc getApplicationClass() {
31     return NonPortableGraphTestApp.class;
32   }
33
34   public static class NonPortableGraphTestApp extends AbstractErrorCatchingTransparentApp {
35     private Map JavaDoc map = new HashMap JavaDoc();
36     private Portable portable;
37     private NonPortable non_portable;
38     private LogAppender dsoLogs;
39
40     public NonPortableGraphTestApp(String JavaDoc 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 JavaDoc testClass = NonPortableGraphTestApp.class.getName();
50       TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
51
52       config.addIncludePattern(testClass);
53       config.addIncludePattern(testClass + "$Portable");
54
55       String JavaDoc 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 JavaDoc {
64       testNonportableObjectGraph();
65     }
66
67     public void testNonportableObjectGraph() {
68       try {
69         synchronized (map) {
70           map.put("mmkay", new NonPortable());
71         }
72         throw new Exception JavaDoc("1. Expecting TCNonPortableObjectError");
73       } catch (Throwable JavaDoc e) {
74         if (!e.getClass().getName().equals("com.tc.exception.TCNonPortableObjectError")) throw new RuntimeException JavaDoc(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 JavaDoc("2. Expecting TCNonPortableObjectError");
82       } catch (Throwable JavaDoc e) {
83         if (!e.getClass().getName().equals("com.tc.exception.TCNonPortableObjectError")) throw new RuntimeException JavaDoc(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 JavaDoc("3. Expecting TCNonPortableObjectError");
91       } catch (Throwable JavaDoc e) {
92         if (!e.getClass().getName().equals("com.tc.exception.TCNonPortableObjectError")) throw new RuntimeException JavaDoc(e);
93       }
94
95       assertTrue(dsoLogs.getCurrentLogEvent().replaceAll("\\s", "").indexOf(log1.replaceAll("\\s", "")) > 0);
96     }
97
98     private static class NonPortable {
99       Map JavaDoc map = new HashMap JavaDoc();
100
101       public NonPortable() {
102         map.put("self", this);
103       }
104     }
105
106     private static class Portable {
107       Object JavaDoc obj;
108
109       public Portable(Object JavaDoc obj) {
110         this.obj = obj;
111       }
112     }
113
114     private static class LogAppender implements TCAppender {
115       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
116
117       public void append(LogLevel arg0, Object JavaDoc arg1, Throwable JavaDoc arg2) {
118         buffer.append(arg1 + "\n");
119       }
120
121       public String JavaDoc getCurrentLogEvent() {
122         String JavaDoc s = buffer.toString();
123         buffer = new StringBuffer JavaDoc();
124         return s;
125       }
126
127     }
128
129     private static final String JavaDoc 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 JavaDoc 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