1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.plugin.http;
23
24 import static java.util.Collections.singleton;
25
26 import java.net.InetAddress;
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.Set;
30
31 import junit.framework.TestCase;
32 import net.grinder.testutility.RandomStubFactory;
33 import net.grinder.util.BlockingClassLoader;
34 import net.grinder.util.Sleeper;
35 import HTTPClient.HTTPConnection;
36 import HTTPClient.StubHTTPConnection;
37 import HTTPClient.HTTPConnection.BandwidthLimiterFactory;
38
39
40
41
42
43
44
45 public class TestHTTPConnectionWrapper extends TestCase {
46
47 public void testHTTPConnectionWrapper() throws Exception {
48
49 final RandomStubFactory<Sleeper> sleeperStubFactory =
50 RandomStubFactory.create(Sleeper.class);
51 final Sleeper sleeper = sleeperStubFactory.getStub();
52
53 final StubHTTPConnection connection = new StubHTTPConnection("foo");
54
55 final BandwidthLimiterFactory defaultBWLimiterFactory =
56 connection.getBandwithLimiterFactoryForTest();
57
58 assertModule(connection, "HTTPClient.RedirectionModule", true);
59 assertModule(connection, "HTTPClient.CookieModule", true);
60 assertModule(connection, "HTTPClient.ContentEncodingModule", true);
61 assertModule(connection, "HTTPClient.TransferEncodingModule", true);
62 assertModule(connection, "HTTPClient.AuthorizationModule", true);
63 assertTrue(connection.getAllowUserInteraction());
64 assertFalse(connection.getTestConnectionHealthWithBlockingRead());
65
66 final HTTPPluginConnectionDefaults defaults =
67 new HTTPPluginConnectionDefaults();
68
69 final HTTPConnectionWrapper wrapper =
70 new HTTPConnectionWrapper(connection, defaults, sleeper);
71
72 assertSame(connection, wrapper.getConnection());
73 assertFalse(connection.getAllowUserInteraction());
74 assertTrue(connection.getTestConnectionHealthWithBlockingRead());
75
76 assertModule(connection, "HTTPClient.RedirectionModule", false);
77 defaults.setFollowRedirects(true);
78 new HTTPConnectionWrapper(connection, defaults, sleeper);
79 assertModule(connection, "HTTPClient.RedirectionModule", true);
80 wrapper.setFollowRedirects(false);
81 assertModule(connection, "HTTPClient.RedirectionModule", false);
82
83 assertModule(connection, "HTTPClient.CookieModule", true);
84 defaults.setUseCookies(false);
85 new HTTPConnectionWrapper(connection, defaults, sleeper);
86 assertModule(connection, "HTTPClient.CookieModule", false);
87 wrapper.setUseCookies(true);
88 assertModule(connection, "HTTPClient.CookieModule", true);
89
90 assertModule(connection, "HTTPClient.ContentEncodingModule", false);
91 defaults.setUseContentEncoding(true);
92 new HTTPConnectionWrapper(connection, defaults, sleeper);
93 assertModule(connection, "HTTPClient.ContentEncodingModule", true);
94 wrapper.setUseContentEncoding(false);
95 assertModule(connection, "HTTPClient.ContentEncodingModule", false);
96
97 assertModule(connection, "HTTPClient.TransferEncodingModule", false);
98 defaults.setUseTransferEncoding(true);
99 new HTTPConnectionWrapper(connection, defaults, sleeper);
100 assertModule(connection, "HTTPClient.TransferEncodingModule", true);
101 wrapper.setUseTransferEncoding(false);
102 assertModule(connection, "HTTPClient.TransferEncodingModule", false);
103
104 assertModule(connection, "HTTPClient.AuthorizationModule", false);
105 defaults.setUseAuthorizationModule(true);
106 new HTTPConnectionWrapper(connection, defaults, sleeper);
107 assertModule(connection, "HTTPClient.AuthorizationModule", true);
108 wrapper.setUseAuthorizationModule(false);
109 assertModule(connection, "HTTPClient.AuthorizationModule", false);
110
111 try {
112 wrapper.setLocalAddress("unknown host");
113 fail("Expected URLException");
114 }
115 catch (URLException e) {
116 }
117
118 wrapper.setLocalAddress(InetAddress.getLocalHost().getHostName());
119
120 assertEquals(InetAddress.getLocalHost(),
121 connection.getLocalAddressForTest());
122
123 assertSame(defaultBWLimiterFactory,
124 connection.getBandwithLimiterFactoryForTest());
125 wrapper.setBandwidthLimit(100);
126 assertNotSame(defaultBWLimiterFactory,
127 connection.getBandwithLimiterFactoryForTest());
128 wrapper.setBandwidthLimit(0);
129 assertSame(defaultBWLimiterFactory,
130 connection.getBandwithLimiterFactoryForTest());
131 }
132
133 private void assertModule(HTTPConnection connection,
134 String className,
135 boolean present) {
136 final Class<?>[] modules = connection.getModules();
137 final Set<String> classNames = new HashSet<String>();
138
139 for (Class<?> module : modules) {
140 classNames.add(module.getName());
141 }
142
143 assertEquals(present, classNames.contains(className));
144 }
145
146
147 public void testClassInitialisation() throws Exception {
148
149 final String wrapperName = HTTPConnectionWrapper.class.getName();
150
151 final ClassLoader blockingLoader =
152 new BlockingClassLoader(
153 singleton("HTTPClient.AuthorizationModule"),
154 singleton(wrapperName),
155 Collections.<String>emptySet(),
156 false);
157
158 try {
159 Class.forName(wrapperName,
160 true,
161 blockingLoader);
162 fail("Expected ExceptionInInitializerError");
163 }
164 catch (ExceptionInInitializerError e) {
165 assertTrue(e.getCause() instanceof ClassNotFoundException);
166 }
167 }
168 }