001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependencyfinder.web;
034:
035: import java.util.*;
036: import java.io.*;
037:
038: import junit.framework.*;
039: import org.apache.log4j.*;
040: import com.jeantessier.dependency.*;
041: import com.meterware.servletunit.*;
042: import com.meterware.httpunit.*;
043:
044: /**
045: * Sets up a dependency graph like the one in <a href="graph.xml">graph.xml</a>.
046: */
047: public abstract class TestBase extends TestCase {
048: private static final String NO_GRAPH_MESSAGE = "There is no dependency graph at this time.";
049:
050: protected String fooPackageName;
051: protected String fooClassName;
052: protected String fooFeatureName;
053: protected String foo2ClassName;
054: protected String foo2FeatureName;
055: protected String barPackageName;
056: protected String barClassName;
057: protected String barFeatureName;
058: protected String bazPackageName;
059: protected String bazClassName;
060: protected String bazFeatureName;
061: protected String leftPackageName;
062: protected String leftClassName;
063: protected String leftFeatureName;
064: protected String rightPackageName;
065: protected String rightClassName;
066: protected String rightFeatureName;
067:
068: protected NodeFactory factory;
069: protected ServletUnitClient client;
070: protected WebRequest request;
071: protected InvocationContext context;
072:
073: protected void setUp() throws Exception {
074: super .setUp();
075:
076: Logger.getLogger(getClass()).setLevel(Level.ALL);
077:
078: Random random = new Random();
079:
080: fooPackageName = "foo" + random.nextLong();
081: fooClassName = fooPackageName + ".Foo" + random.nextLong();
082: fooFeatureName = fooClassName + ".foo" + random.nextLong();
083: foo2ClassName = fooPackageName + ".Foo2" + random.nextLong();
084: foo2FeatureName = foo2ClassName + ".foo2" + random.nextLong();
085: barPackageName = "bar" + random.nextLong();
086: barClassName = barPackageName + ".Bar" + random.nextLong();
087: barFeatureName = barClassName + ".bar" + random.nextLong();
088: bazPackageName = "baz" + random.nextLong();
089: bazClassName = bazPackageName + ".Baz" + random.nextLong();
090: bazFeatureName = bazClassName + ".baz" + random.nextLong();
091: leftPackageName = "left" + random.nextLong();
092: leftClassName = leftPackageName + ".Left" + random.nextLong();
093: leftFeatureName = leftClassName + ".left" + random.nextLong();
094: rightPackageName = "right" + random.nextLong();
095: rightClassName = rightPackageName + ".Right"
096: + random.nextLong();
097: rightFeatureName = rightClassName + ".right"
098: + random.nextLong();
099:
100: factory = new NodeFactory();
101: FeatureNode foo = factory.createFeature(fooFeatureName);
102: FeatureNode bar = factory.createFeature(barFeatureName);
103: FeatureNode baz = factory.createFeature(bazFeatureName);
104: FeatureNode left = factory.createFeature(leftFeatureName);
105: FeatureNode right = factory.createFeature(rightFeatureName);
106:
107: foo.addDependency(bar);
108: bar.addDependency(baz);
109: left.addDependency(right);
110: right.addDependency(left);
111:
112: ServletRunner runner = new ServletRunner(new File(
113: "web/WEB-INF/web.xml"), "/web");
114: client = runner.newClient();
115: request = new GetMethodWebRequest(getStartUrl());
116: context = client.newInvocation(request);
117:
118: client.getSession(true).getServletContext().setAttribute(
119: "factory", factory);
120: }
121:
122: public void testNoDependencyGraph() throws Exception {
123: client.getSession(true).getServletContext().removeAttribute(
124: "factory");
125:
126: context.service();
127: WebResponse response = client.getResponse(request);
128: assertTrue("Missing text \"" + NO_GRAPH_MESSAGE + "\"",
129: response.getText().contains(NO_GRAPH_MESSAGE));
130: }
131:
132: public void testEmptyDependencyGraph() throws Exception {
133: client.getSession(true).getServletContext().setAttribute(
134: "factory", new NodeFactory());
135:
136: context.service();
137: WebResponse response = client.getResponse(request);
138: assertFalse("Unexpected text \"" + NO_GRAPH_MESSAGE + "\"",
139: response.getText().contains(NO_GRAPH_MESSAGE));
140: }
141:
142: public void testTestDependencyGraph() throws Exception {
143: context.service();
144: WebResponse response = client.getResponse(request);
145: assertFalse("Unexpected text \"" + NO_GRAPH_MESSAGE + "\"",
146: response.getText().contains(NO_GRAPH_MESSAGE));
147: }
148:
149: protected abstract String getStartUrl();
150: }
|