1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.dependencyfinder.web; |
34 | |
|
35 | |
import java.io.*; |
36 | |
import java.util.*; |
37 | |
|
38 | |
import javax.servlet.*; |
39 | |
|
40 | |
import junit.framework.*; |
41 | |
|
42 | |
import org.apache.log4j.*; |
43 | |
|
44 | |
import com.meterware.httpunit.*; |
45 | |
import com.meterware.servletunit.*; |
46 | |
|
47 | |
import com.jeantessier.dependency.*; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 72 | public abstract class TestBase extends TestCase { |
53 | |
private static final String NO_GRAPH_MESSAGE = "There is no dependency graph at this time."; |
54 | |
|
55 | |
protected String fooPackageName; |
56 | |
protected String fooClassName; |
57 | |
protected String fooFeatureName; |
58 | |
protected String foo2ClassName; |
59 | |
protected String foo2FeatureName; |
60 | |
protected String barPackageName; |
61 | |
protected String barClassName; |
62 | |
protected String barFeatureName; |
63 | |
protected String bazPackageName; |
64 | |
protected String bazClassName; |
65 | |
protected String bazFeatureName; |
66 | |
protected String leftPackageName; |
67 | |
protected String leftClassName; |
68 | |
protected String leftFeatureName; |
69 | |
protected String rightPackageName; |
70 | |
protected String rightClassName; |
71 | |
protected String rightFeatureName; |
72 | |
|
73 | |
protected NodeFactory factory; |
74 | |
protected ServletUnitClient client; |
75 | |
protected WebRequest request; |
76 | |
protected InvocationContext context; |
77 | |
|
78 | |
protected String label; |
79 | |
|
80 | |
protected void setUp() throws Exception { |
81 | 72 | super.setUp(); |
82 | |
|
83 | 72 | Logger.getLogger(getClass()).setLevel(Level.ALL); |
84 | |
|
85 | 72 | Random random = new Random(); |
86 | |
|
87 | 72 | fooPackageName = "foo" + random.nextLong(); |
88 | 72 | fooClassName = fooPackageName + ".Foo" + random.nextLong(); |
89 | 72 | fooFeatureName = fooClassName + ".foo" + random.nextLong(); |
90 | 72 | foo2ClassName = fooPackageName + ".Foo2" + random.nextLong(); |
91 | 72 | foo2FeatureName = foo2ClassName + ".foo2" + random.nextLong(); |
92 | 72 | barPackageName = "bar" + random.nextLong(); |
93 | 72 | barClassName = barPackageName + ".Bar" + random.nextLong(); |
94 | 72 | barFeatureName = barClassName + ".bar" + random.nextLong(); |
95 | 72 | bazPackageName = "baz" + random.nextLong(); |
96 | 72 | bazClassName = bazPackageName + ".Baz" + random.nextLong(); |
97 | 72 | bazFeatureName = bazClassName + ".baz" + random.nextLong(); |
98 | 72 | leftPackageName = "left" + random.nextLong(); |
99 | 72 | leftClassName = leftPackageName + ".Left" + random.nextLong(); |
100 | 72 | leftFeatureName = leftClassName + ".left" + random.nextLong(); |
101 | 72 | rightPackageName = "right" + random.nextLong(); |
102 | 72 | rightClassName = rightPackageName + ".Right" + random.nextLong(); |
103 | 72 | rightFeatureName = rightClassName + ".right" + random.nextLong(); |
104 | |
|
105 | 72 | factory = new NodeFactory(); |
106 | 72 | FeatureNode foo = factory.createFeature(fooFeatureName); |
107 | 72 | FeatureNode bar = factory.createFeature(barFeatureName); |
108 | 72 | FeatureNode baz = factory.createFeature(bazFeatureName); |
109 | 72 | FeatureNode left = factory.createFeature(leftFeatureName); |
110 | 72 | FeatureNode right = factory.createFeature(rightFeatureName); |
111 | |
|
112 | 72 | foo.addDependency(bar); |
113 | 72 | bar.addDependency(baz); |
114 | 72 | left.addDependency(right); |
115 | 72 | right.addDependency(left); |
116 | |
|
117 | 72 | ServletRunner runner = new ServletRunner(new File("web/WEB-INF/web.xml"), "/web"); |
118 | 72 | client = runner.newClient(); |
119 | 72 | request = new GetMethodWebRequest(getStartUrl()); |
120 | 72 | context = client.newInvocation(request); |
121 | |
|
122 | 72 | label = "label " + random.nextLong(); |
123 | |
|
124 | 72 | getApplication().setAttribute("label", label); |
125 | 72 | getApplication().setAttribute("factory", factory); |
126 | 72 | } |
127 | |
|
128 | |
public void testNoLabel() throws Exception { |
129 | 10 | getApplication().removeAttribute("label"); |
130 | |
|
131 | 10 | context.service(); |
132 | 10 | WebResponse response = client.getResponse(request); |
133 | 10 | assertEquals("name", "Dependency Finder", response.getElementWithID("name").getText()); |
134 | 10 | assertNull("label", response.getElementWithID("label")); |
135 | 10 | } |
136 | |
|
137 | |
public void testLabel() throws Exception { |
138 | 10 | context.service(); |
139 | 10 | WebResponse response = client.getResponse(request); |
140 | 10 | assertEquals("name", "Dependency Finder", response.getElementWithID("name").getText()); |
141 | 10 | assertEquals("label", label, response.getElementWithID("label").getText()); |
142 | 10 | } |
143 | |
|
144 | |
public void testNoDependencyGraph() throws Exception { |
145 | 10 | getApplication().removeAttribute("factory"); |
146 | |
|
147 | 10 | context.service(); |
148 | 10 | WebResponse response = client.getResponse(request); |
149 | 10 | assertTrue("Missing text \"" + NO_GRAPH_MESSAGE + "\"", response.getText().contains(NO_GRAPH_MESSAGE)); |
150 | 10 | } |
151 | |
|
152 | |
public void testEmptyDependencyGraph() throws Exception { |
153 | 10 | getApplication().setAttribute("factory", new NodeFactory()); |
154 | |
|
155 | 10 | context.service(); |
156 | 10 | WebResponse response = client.getResponse(request); |
157 | 10 | assertFalse("Unexpected text \"" + NO_GRAPH_MESSAGE + "\"", response.getText().contains(NO_GRAPH_MESSAGE)); |
158 | 10 | } |
159 | |
|
160 | |
public void testTestDependencyGraph() throws Exception { |
161 | 10 | context.service(); |
162 | 10 | WebResponse response = client.getResponse(request); |
163 | 10 | assertFalse("Unexpected text \"" + NO_GRAPH_MESSAGE + "\"", response.getText().contains(NO_GRAPH_MESSAGE)); |
164 | 10 | } |
165 | |
|
166 | |
protected ServletContext getApplication() { |
167 | 176 | return client.getSession(true).getServletContext(); |
168 | |
} |
169 | |
|
170 | |
protected abstract String getStartUrl(); |
171 | |
} |