001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.exception.TCNonPortableObjectError;
008: import com.tc.logging.CustomerLogging;
009: import com.tc.logging.LogLevel;
010: import com.tc.logging.TCAppender;
011: import com.tc.logging.TCLogging;
012: import com.tc.object.config.ConfigVisitor;
013: import com.tc.object.config.DSOClientConfigHelper;
014: import com.tc.object.config.TransparencyClassSpec;
015: import com.tc.simulator.app.ApplicationConfig;
016: import com.tc.simulator.listener.ListenerProvider;
017: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: public class NonPortableGraphTest extends TransparentTestBase {
023:
024: private static final int NODE_COUNT = 1;
025:
026: public NonPortableGraphTest() {
027: //
028: }
029:
030: public void doSetUp(TransparentTestIface t) throws Exception {
031: t.getTransparentAppConfig().setClientCount(NODE_COUNT);
032: t.initializeTestRunner();
033: }
034:
035: protected Class getApplicationClass() {
036: return NonPortableGraphTestApp.class;
037: }
038:
039: public static class NonPortableGraphTestApp extends
040: AbstractErrorCatchingTransparentApp {
041: private Map map = new HashMap();
042: private Portable portable;
043: private NonPortable non_portable;
044: private LogAppender dsoLogs;
045:
046: public NonPortableGraphTestApp(String appId,
047: ApplicationConfig cfg, ListenerProvider listenerProvider) {
048: super (appId, cfg, listenerProvider);
049: dsoLogs = new LogAppender();
050:
051: TCLogging.addAppender(CustomerLogging.getDSORuntimeLogger()
052: .getName(), dsoLogs);
053: }
054:
055: public static void visitL1DSOConfig(ConfigVisitor visitor,
056: DSOClientConfigHelper config) {
057:
058: String testClass = NonPortableGraphTestApp.class.getName();
059: TransparencyClassSpec spec = config
060: .getOrCreateSpec(testClass);
061:
062: config.addIncludePattern(testClass);
063: config.addIncludePattern(testClass + "$Portable");
064:
065: String methodExpression = "* " + testClass + "*.*(..)";
066: config.addWriteAutolock(methodExpression);
067:
068: spec.addRoot("map", "map");
069: spec.addRoot("portable", "portable");
070: spec.addRoot("non_portable", "non_portable");
071: }
072:
073: protected void runTest() throws Throwable {
074: testNonportableObjectGraph();
075: }
076:
077: public void testNonportableObjectGraph() {
078: try {
079: synchronized (map) {
080: map.put("mmkay", new NonPortable());
081: }
082: throw new AssertionError(
083: "1. Expecting TCNonPortableObjectError");
084: } catch (TCNonPortableObjectError e) {
085: // expected
086: }
087:
088: assertTrue(dsoLogs.getCurrentLogEvent().replaceAll("\\s",
089: "").indexOf(log1.replaceAll("\\s", "")) > 0);
090:
091: try {
092: portable = new Portable(new NonPortable());
093: throw new AssertionError(
094: "2. Expecting TCNonPortableObjectError");
095: } catch (TCNonPortableObjectError e) {
096: // expected
097: }
098:
099: assertTrue(dsoLogs.getCurrentLogEvent().replaceAll("\\s",
100: "").indexOf(log2.replaceAll("\\s", "")) > 0);
101:
102: try {
103: non_portable = new NonPortable();
104: throw new AssertionError(
105: "3. Expecting TCNonPortableObjectError");
106: } catch (TCNonPortableObjectError e) {
107: // expected
108: }
109:
110: assertTrue(dsoLogs.getCurrentLogEvent().replaceAll("\\s",
111: "").indexOf(log1.replaceAll("\\s", "")) > 0);
112: }
113:
114: private static class NonPortable {
115: Map map = new HashMap();
116:
117: public NonPortable() {
118: map.put("self", this );
119: }
120: }
121:
122: private static class Portable {
123: Object obj;
124:
125: public Portable(Object obj) {
126: this .obj = obj;
127: }
128: }
129:
130: private static class LogAppender implements TCAppender {
131: StringBuffer buffer = new StringBuffer();
132:
133: public void append(LogLevel arg0, Object arg1,
134: Throwable arg2) {
135: buffer.append(arg1 + "\n");
136: }
137:
138: public String getCurrentLogEvent() {
139: String s = buffer.toString();
140: buffer = new StringBuffer();
141: return s;
142: }
143:
144: }
145:
146: private static final String log1 = "!! com.tctest.NonPortableGraphTest$NonPortableGraphTestApp$NonPortable (id 0)"
147: + " Map map = (HashMap, id 1)"
148: + " [entry 0]"
149: + " key = \"self\""
150: + "!! value = (ref id 0)";
151:
152: private static final String log2 = " com.tctest.NonPortableGraphTest$NonPortableGraphTestApp$Portable (id 0)"
153: + "!! Object obj = (com.tctest.NonPortableGraphTest$NonPortableGraphTestApp$NonPortable, id 1)"
154: + " Map map = (HashMap, id 2)"
155: + " [entry 0]"
156: + " key = \"self\""
157: + "!! value = (ref id 1)";
158: }
159: }
|