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;
005:
006: import com.tc.object.config.ConfigVisitor;
007: import com.tc.object.config.DSOClientConfigHelper;
008: import com.tc.simulator.app.ApplicationConfig;
009: import com.tc.simulator.listener.ListenerProvider;
010: import com.tc.util.Assert;
011:
012: import java.io.IOException;
013: import java.net.MalformedURLException;
014: import java.net.URL;
015: import java.net.URLConnection;
016: import java.net.URLStreamHandler;
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: public class GenericURLTestApp extends GenericTestApp {
021: public final static String URL_SPEC1 = "https://www.terracotta.org/path?param1=val1¶m2=val2;test#reference";
022: public final static String URL_SPEC2 = "http://www.terracottatech.com:8081";
023: public final static String URL_SPEC3 = "http://user:pass@www.apple.com#ref";
024:
025: public final static String URL_SPEC1_NEW = "https://dso.www.terracotta.org/path/tim?param1=val1¶m2=val2;test#reference";
026: public final static String URL_SPEC2_NEW = "http://dso.www.terracottatech.com:8081/tim";
027: public final static String URL_SPEC3_NEW = "http://dso.www.apple.com/tim#ref";
028:
029: public GenericURLTestApp(String appId, ApplicationConfig cfg,
030: ListenerProvider listenerProvider) {
031: super (appId, cfg, listenerProvider, URL.class);
032: }
033:
034: protected Object getTestObject(String testName) {
035: if ("SharedURL".equals(testName)) {
036: return ((List) sharedMap.get("list1")).iterator();
037: } else if ("URLWithCustomHandler".equals(testName)) {
038: return ((List) sharedMap.get("list2")).iterator();
039: }
040:
041: return null;
042: }
043:
044: protected void setupTestObject(String testName) {
045: List list1 = new ArrayList();
046: list1.add(createURL(URL_SPEC1));
047: list1.add(createURL(URL_SPEC2));
048: list1.add(createURL(URL_SPEC3));
049:
050: List list2 = new ArrayList();
051: list2.add(createURLWithCustomHandler(URL_SPEC1));
052: list2.add(createURLWithCustomHandler(URL_SPEC2));
053: list2.add(createURLWithCustomHandler(URL_SPEC3));
054:
055: sharedMap.put("list1", list1);
056: sharedMap.put("list2", list2);
057: }
058:
059: private URL createURL(String url) {
060: try {
061: return new URL(url);
062: } catch (MalformedURLException e) {
063: throw new RuntimeException(e);
064: }
065: }
066:
067: private URL createURLWithCustomHandler(String url) {
068: try {
069: return new URL(null, url, new DummyURLStreamHandler());
070: } catch (MalformedURLException e) {
071: throw new RuntimeException(e);
072: }
073: }
074:
075: void testSharedURL(URL url, boolean validate) throws Exception {
076: if (validate) {
077: if (createURL(URL_SPEC1).equals(url)) {
078: Assert.assertEquals(createURL(URL_SPEC1)
079: .toExternalForm(), url.toExternalForm());
080: } else if (createURL(URL_SPEC2).equals(url)) {
081: Assert.assertEquals(createURL(URL_SPEC2)
082: .toExternalForm(), url.toExternalForm());
083: } else if (createURL(URL_SPEC3).equals(url)) {
084: Assert.assertEquals(createURL(URL_SPEC3)
085: .toExternalForm(), url.toExternalForm());
086: } else {
087: Assert.fail();
088: }
089: } else {
090: synchronized (url) {
091: System.out.println("SharedURL : " + url);
092: }
093: }
094: }
095:
096: void testURLWithCustomHandler(URL url, boolean validate)
097: throws Exception {
098: if (validate) {
099: Assert.assertTrue(URL_SPEC1_NEW
100: .equals(url.toExternalForm())
101: || URL_SPEC2_NEW.equals(url.toExternalForm())
102: || URL_SPEC3_NEW.equals(url.toExternalForm()));
103: } else {
104: synchronized (url) {
105: url.openConnection();
106: System.out.println("URLWithCustomHandler : " + url);
107: }
108: }
109: }
110:
111: public static void visitL1DSOConfig(ConfigVisitor visitor,
112: DSOClientConfigHelper config) {
113: String testClass = GenericURLTestApp.class.getName();
114: config.getOrCreateSpec(testClass);
115: String writeAllowedMethodExpression = "* " + testClass
116: + "*.*(..)";
117: config.addWriteAutolock(writeAllowedMethodExpression);
118: String readOnlyMethodExpression = "* " + testClass
119: + "*.*ReadOnly*(..)";
120: config.addReadAutolock(readOnlyMethodExpression);
121: }
122:
123: public static class DummyURLStreamHandler extends URLStreamHandler {
124: protected URLConnection openConnection(URL u)
125: throws IOException {
126: setURL(u, u.getProtocol(), "dso." + u.getHost(), u
127: .getPort(), u.getFile(), u.getRef());
128: setURL(u, u.getProtocol(), u.getHost(), u.getPort(), u
129: .getAuthority(), u.getUserInfo(), u.getPath()
130: + "/tim", u.getQuery(), u.getRef());
131: return null;
132: }
133: }
134: }
|