12
12
*/
13
13
package org .asynchttpclient .extras .registry ;
14
14
15
+ import java .io .IOException ;
15
16
import java .lang .reflect .InvocationTargetException ;
16
17
17
18
import junit .extensions .PA ;
@@ -34,7 +35,7 @@ public abstract class AbstractAsyncHttpClientFactoryTest {
34
35
public static final String TEST_CLIENT_CLASS_NAME = "org.asynchttpclient.extras.registry.TestAsyncHttpClient" ;
35
36
public static final String BAD_CLIENT_CLASS_NAME = "org.asynchttpclient.extras.registry.BadAsyncHttpClient" ;
36
37
public static final String NON_EXISTENT_CLIENT_CLASS_NAME = "org.asynchttpclient.extras.registry.NonExistentAsyncHttpClient" ;
37
-
38
+
38
39
private Server server ;
39
40
private int port ;
40
41
@@ -62,9 +63,7 @@ public void tearDown() throws Exception {
62
63
}
63
64
64
65
/**
65
- * If the property is not found via the system property or properties file
66
- * the default instance of AsyncHttpClient should be returned.
67
- * @throws Exception
66
+ * If the property is not found via the system property or properties file the default instance of AsyncHttpClient should be returned.
68
67
*/
69
68
// ================================================================================================================
70
69
@ Test (groups = "standalone" )
@@ -94,104 +93,107 @@ public void testGetAsyncHttpClientProvider() throws Exception {
94
93
// ==================================================================================================================================
95
94
96
95
/**
97
- * If the class is specified via a system property then that class should be
98
- * returned
96
+ * If the class is specified via a system property then that class should be returned
99
97
*/
100
98
// ===================================================================================================================================
101
99
@ Test (groups = "standalone" )
102
- public void testFactoryWithSystemProperty () {
100
+ public void testFactoryWithSystemProperty () throws IOException {
103
101
System .setProperty (AsyncImplHelper .ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY , TEST_CLIENT_CLASS_NAME );
104
102
AsyncHttpClientConfigHelper .reloadProperties ();
105
- Assert .assertTrue (AsyncHttpClientFactory .getAsyncHttpClient ().getClass ().equals (TestAsyncHttpClient .class ));
103
+ try (AsyncHttpClient ahc = AsyncHttpClientFactory .getAsyncHttpClient ()) {
104
+ Assert .assertTrue (ahc .getClass ().equals (TestAsyncHttpClient .class ));
105
+ }
106
106
}
107
107
108
108
@ Test (groups = "standalone" )
109
- public void testGetAsyncHttpClientConfigWithSystemProperty () {
109
+ public void testGetAsyncHttpClientConfigWithSystemProperty () throws IOException {
110
110
System .setProperty (AsyncImplHelper .ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY , TEST_CLIENT_CLASS_NAME );
111
111
AsyncHttpClientConfigHelper .reloadProperties ();
112
- AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory .getAsyncHttpClient ();
113
- Assert .assertTrue (asyncHttpClient .getClass ().equals (TestAsyncHttpClient .class ));
112
+ try (AsyncHttpClient ahc = AsyncHttpClientFactory .getAsyncHttpClient ()) {
113
+ Assert .assertTrue (ahc .getClass ().equals (TestAsyncHttpClient .class ));
114
+ }
114
115
}
115
116
116
117
@ Test (groups = "standalone" )
117
- public void testGetAsyncHttpClientProviderWithSystemProperty () {
118
+ public void testGetAsyncHttpClientProviderWithSystemProperty () throws IOException {
118
119
System .setProperty (AsyncImplHelper .ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY , TEST_CLIENT_CLASS_NAME );
119
120
AsyncHttpClientConfigHelper .reloadProperties ();
120
- AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory .getAsyncHttpClient ();
121
- Assert .assertTrue (asyncHttpClient .getClass ().equals (TestAsyncHttpClient .class ));
121
+ try (AsyncHttpClient ahc = AsyncHttpClientFactory .getAsyncHttpClient ()) {
122
+ Assert .assertTrue (ahc .getClass ().equals (TestAsyncHttpClient .class ));
123
+ }
122
124
}
123
125
124
126
// ===================================================================================================================================
125
127
126
128
/**
127
- * If any of the constructors of the class fail then a
128
- * AsyncHttpClientException is thrown.
129
+ * If any of the constructors of the class fail then a AsyncHttpClientException is thrown.
129
130
*/
130
131
// ===================================================================================================================================
131
132
@ Test (groups = "standalone" , expectedExceptions = BadAsyncHttpClientException .class )
132
- public void testFactoryWithBadAsyncHttpClient () {
133
+ public void testFactoryWithBadAsyncHttpClient () throws IOException {
133
134
System .setProperty (AsyncImplHelper .ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY , BAD_CLIENT_CLASS_NAME );
134
135
AsyncHttpClientConfigHelper .reloadProperties ();
135
- AsyncHttpClientFactory .getAsyncHttpClient ();
136
- Assert .fail ("BadAsyncHttpClientException should have been thrown before this point" );
136
+ try (AsyncHttpClient ahc = AsyncHttpClientFactory .getAsyncHttpClient ()) {
137
+ Assert .fail ("BadAsyncHttpClientException should have been thrown before this point" );
138
+ }
137
139
}
138
140
139
141
@ Test (groups = "standalone" )
140
- public void testGetAsyncHttpClientConfigWithBadAsyncHttpClient () {
142
+ public void testGetAsyncHttpClientConfigWithBadAsyncHttpClient () throws IOException {
141
143
System .setProperty (AsyncImplHelper .ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY , BAD_CLIENT_CLASS_NAME );
142
144
AsyncHttpClientConfigHelper .reloadProperties ();
143
- try {
144
- AsyncHttpClientFactory . getAsyncHttpClient ();
145
+ try ( AsyncHttpClient ahc = AsyncHttpClientFactory . getAsyncHttpClient ()) {
146
+ //
145
147
} catch (AsyncHttpClientImplException e ) {
146
148
assertException (e );
147
149
}
148
- //Assert.fail("AsyncHttpClientImplException should have been thrown before this point");
150
+ // Assert.fail("AsyncHttpClientImplException should have been thrown before this point");
149
151
}
150
152
151
153
@ Test (groups = "standalone" )
152
- public void testGetAsyncHttpClientProviderWithBadAsyncHttpClient () {
154
+ public void testGetAsyncHttpClientProviderWithBadAsyncHttpClient () throws IOException {
153
155
System .setProperty (AsyncImplHelper .ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY , BAD_CLIENT_CLASS_NAME );
154
156
AsyncHttpClientConfigHelper .reloadProperties ();
155
- try {
156
- AsyncHttpClientFactory . getAsyncHttpClient ();
157
+ try ( AsyncHttpClient ahc = AsyncHttpClientFactory . getAsyncHttpClient ()) {
158
+ //
157
159
} catch (AsyncHttpClientImplException e ) {
158
160
assertException (e );
159
161
}
160
- //Assert.fail("AsyncHttpClientImplException should have been thrown before this point");
162
+ // Assert.fail("AsyncHttpClientImplException should have been thrown before this point");
161
163
}
162
164
163
165
// ===================================================================================================================================
164
166
165
167
/*
166
- * If the system property exists instantiate the class else if the class is
167
- * not found throw an AsyncHttpClientException.
168
+ * If the system property exists instantiate the class else if the class is not found throw an AsyncHttpClientException.
168
169
*/
169
170
@ Test (groups = "standalone" , expectedExceptions = AsyncHttpClientImplException .class )
170
- public void testFactoryWithNonExistentAsyncHttpClient () {
171
+ public void testFactoryWithNonExistentAsyncHttpClient () throws IOException {
171
172
System .setProperty (AsyncImplHelper .ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY , NON_EXISTENT_CLIENT_CLASS_NAME );
172
173
AsyncHttpClientConfigHelper .reloadProperties ();
173
- AsyncHttpClientFactory .getAsyncHttpClient ();
174
+ try (AsyncHttpClient ahc = AsyncHttpClientFactory .getAsyncHttpClient ()) {
175
+ //
176
+ }
174
177
Assert .fail ("AsyncHttpClientImplException should have been thrown before this point" );
175
178
}
176
179
177
180
/**
178
- * If property is specified but the class can’t be created or found for any
179
- * reason subsequent calls should throw an AsyncClientException.
181
+ * If property is specified but the class can’t be created or found for any reason subsequent calls should throw an AsyncClientException.
180
182
*/
181
183
@ Test (groups = "standalone" , expectedExceptions = AsyncHttpClientImplException .class )
182
- public void testRepeatedCallsToBadAsyncHttpClient () {
184
+ public void testRepeatedCallsToBadAsyncHttpClient () throws IOException {
183
185
boolean exceptionCaught = false ;
184
186
System .setProperty (AsyncImplHelper .ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY , NON_EXISTENT_CLIENT_CLASS_NAME );
185
187
AsyncHttpClientConfigHelper .reloadProperties ();
186
- try {
187
- AsyncHttpClientFactory . getAsyncHttpClient ();
188
+ try ( AsyncHttpClient ahc = AsyncHttpClientFactory . getAsyncHttpClient ()) {
189
+ //
188
190
} catch (AsyncHttpClientImplException e ) {
189
191
exceptionCaught = true ;
190
192
}
191
193
Assert .assertTrue (exceptionCaught , "Didn't catch exception the first time" );
192
194
exceptionCaught = false ;
193
- try {
194
- AsyncHttpClientFactory . getAsyncHttpClient ();
195
+ try ( AsyncHttpClient ahc = AsyncHttpClientFactory . getAsyncHttpClient ()) {
196
+ //
195
197
} catch (AsyncHttpClientImplException e ) {
196
198
exceptionCaught = true ;
197
199
}
0 commit comments