3333
3434import org .httprpc .*;
3535import org .httprpc .beans .BeanAdapter ;
36+ import org .junit .Assert ;
37+ import org .junit .Test ;
3638
3739public class WebServiceProxyTest extends AbstractTest {
3840 public interface TestService {
@@ -55,7 +57,7 @@ public interface AttachmentInfo {
5557
5658 @ RequestMethod ("GET" )
5759 public Map <String , Object > testGet (@ RequestParameter ("string" ) String text , List <String > strings , int number , boolean flag ,
58- Date date , LocalDate localDate , LocalTime localTime , LocalDateTime localDateTime ) throws IOException ;
60+ Date date , LocalDate localDate , LocalTime localTime , LocalDateTime localDateTime ) throws IOException ;
5961
6062 @ RequestMethod ("GET" )
6163 @ ResourcePath ("fibonacci" )
@@ -82,37 +84,22 @@ public interface TreeNode {
8284 public List <TreeNode > getChildren ();
8385 }
8486
85- private static Date date = new Date ();
87+ private Date date = new Date ();
8688
87- private static LocalDate localDate = LocalDate .now ();
88- private static LocalTime localTime = LocalTime .now ();
89- private static LocalDateTime localDateTime = LocalDateTime .now ();
89+ private LocalDate localDate = LocalDate .now ();
90+ private LocalTime localTime = LocalTime .now ();
91+ private LocalDateTime localDateTime = LocalDateTime .now ();
9092
9193 private static final int EOF = -1 ;
9294
93- public static void main (String [] args ) throws Exception {
94- testGet ();
95- testGetFibonnaci ();
96- testURLEncodedPost ();
97- testMultipartPost ();
98- testCustomPost ();
99- testPut ();
100- testDelete ();
101- testUnauthorized ();
102- testError ();
103- testTimeout ();
104- testMath ();
105- testMathService ();
106- testTree ();
107- }
108-
109- public static void testGet () throws Exception {
95+ @ Test
96+ public void testGet () throws Exception {
11097 TestService testService = WebServiceProxy .adapt (new URL ("http://localhost:8080/httprpc-test/test" ), TestService .class );
11198
11299 Map <String , ?> result = testService .testGet ("héllo+gøodbye" , listOf ("a" , "b" , "c" ), 123 , true ,
113100 date , localDate , localTime , localDateTime );
114101
115- validate ("GET" , result .get ("string" ).equals ("héllo+gøodbye" )
102+ Assert . assertTrue ("GET" , result .get ("string" ).equals ("héllo+gøodbye" )
116103 && result .get ("strings" ).equals (listOf ("a" , "b" , "c" ))
117104 && result .get ("number" ).equals (123L )
118105 && result .get ("flag" ).equals (true )
@@ -122,15 +109,17 @@ public static void testGet() throws Exception {
122109 && result .get ("localDateTime" ).equals (localDateTime .toString ()));
123110 }
124111
125- public static void testGetFibonnaci () throws Exception {
112+ @ Test
113+ public void testGetFibonnaci () throws Exception {
126114 TestService testService = WebServiceProxy .adapt (new URL ("http://localhost:8080/httprpc-test/test/" ), TestService .class );
127115
128116 List <Integer > fibonacci = testService .testGetFibonacci ();
129117
130- validate ("GET (Fibonacci)" , fibonacci .equals (listOf (1 , 2 , 3 , 5 , 8 , 13 )));
118+ Assert . assertTrue ("GET (Fibonacci)" , fibonacci .equals (listOf (1 , 2 , 3 , 5 , 8 , 13 )));
131119 }
132120
133- public static void testURLEncodedPost () throws Exception {
121+ @ Test
122+ public void testURLEncodedPost () throws Exception {
134123 WebServiceProxy webServiceProxy = new WebServiceProxy ("POST" , new URL ("http://localhost:8080/httprpc-test/test" ));
135124
136125 webServiceProxy .setArguments (mapOf (
@@ -146,7 +135,7 @@ public static void testURLEncodedPost() throws Exception {
146135
147136 Map <String , ?> result = webServiceProxy .invoke ();
148137
149- validate ("POST (URL-encoded)" , result .get ("string" ).equals ("héllo+gøodbye" )
138+ Assert . assertTrue ("POST (URL-encoded)" , result .get ("string" ).equals ("héllo+gøodbye" )
150139 && result .get ("strings" ).equals (listOf ("a" , "b" , "c" ))
151140 && result .get ("number" ).equals (123L )
152141 && result .get ("flag" ).equals (true )
@@ -157,7 +146,8 @@ public static void testURLEncodedPost() throws Exception {
157146 && result .get ("attachmentInfo" ).equals (listOf ()));
158147 }
159148
160- public static void testMultipartPost () throws Exception {
149+ @ Test
150+ public void testMultipartPost () throws Exception {
161151 URL textTestURL = WebServiceProxyTest .class .getResource ("test.txt" );
162152 URL imageTestURL = WebServiceProxyTest .class .getResource ("test.jpg" );
163153
@@ -167,7 +157,7 @@ public static void testMultipartPost() throws Exception {
167157 date , localDate , localTime , localDateTime ,
168158 listOf (textTestURL , imageTestURL ));
169159
170- validate ("POST (multipart)" , response .getString ().equals ("héllo+gøodbye" )
160+ Assert . assertTrue ("POST (multipart)" , response .getString ().equals ("héllo+gøodbye" )
171161 && response .getStrings ().equals (listOf ("a" , "b" , "c" ))
172162 && response .getNumber () == 123
173163 && response .getFlag () == true
@@ -181,7 +171,8 @@ public static void testMultipartPost() throws Exception {
181171 && response .getAttachmentInfo ().get (1 ).getChecksum () == 1038036 );
182172 }
183173
184- public static void testCustomPost () throws Exception {
174+ @ Test
175+ public void testCustomPost () throws Exception {
185176 WebServiceProxy webServiceProxy = new WebServiceProxy ("POST" , new URL ("http://localhost:8080/httprpc-test/test" ));
186177
187178 URL imageTestURL = WebServiceProxyTest .class .getResource ("test.jpg" );
@@ -203,10 +194,11 @@ public static void testCustomPost() throws Exception {
203194 return ImageIO .read (inputStream );
204195 });
205196
206- validate ("POST (custom)" , image != null );
197+ Assert . assertTrue ("POST (custom)" , image != null );
207198 }
208199
209- public static void testPut () throws Exception {
200+ @ Test
201+ public void testPut () throws Exception {
210202 WebServiceProxy webServiceProxy = new WebServiceProxy ("PUT" , new URL ("http://localhost:8080/httprpc-test/test" ));
211203
212204 URL textTestURL = WebServiceProxyTest .class .getResource ("test.txt" );
@@ -237,10 +229,11 @@ public static void testPut() throws Exception {
237229 return textBuilder .toString ();
238230 });
239231
240- validate ("PUT" , text != null );
232+ Assert . assertTrue ("PUT" , text != null );
241233 }
242234
243- public static void testDelete () throws Exception {
235+ @ Test
236+ public void testDelete () throws Exception {
244237 WebServiceProxy webServiceProxy = new WebServiceProxy ("DELETE" , new URL ("http://localhost:8080/httprpc-test/test" ));
245238
246239 webServiceProxy .setArguments (mapOf (
@@ -249,10 +242,11 @@ public static void testDelete() throws Exception {
249242
250243 webServiceProxy .invoke ();
251244
252- validate ("DELETE" , true );
245+ Assert . assertTrue ("DELETE" , true );
253246 }
254247
255- public static void testUnauthorized () throws Exception {
248+ @ Test
249+ public void testUnauthorized () throws Exception {
256250 WebServiceProxy webServiceProxy = new WebServiceProxy ("GET" , new URL ("http://localhost:8080/httprpc-test/test/unauthorized" ));
257251
258252 int status ;
@@ -264,10 +258,11 @@ public static void testUnauthorized() throws Exception {
264258 status = exception .getStatus ();
265259 }
266260
267- validate ("Unauthorized" , status == HttpURLConnection .HTTP_FORBIDDEN );
261+ Assert . assertTrue ("Unauthorized" , status == HttpURLConnection .HTTP_FORBIDDEN );
268262 }
269263
270- public static void testError () throws Exception {
264+ @ Test
265+ public void testError () throws Exception {
271266 WebServiceProxy webServiceProxy = new WebServiceProxy ("GET" , new URL ("http://localhost:8080/httprpc-test/test/error" ));
272267
273268 boolean error ;
@@ -279,10 +274,11 @@ public static void testError() throws Exception {
279274 error = true ;
280275 }
281276
282- validate ("Error" , error );
277+ Assert . assertTrue ("Error" , error );
283278 }
284279
285- public static void testTimeout () throws Exception {
280+ @ Test
281+ public void testTimeout () throws Exception {
286282 WebServiceProxy webServiceProxy = new WebServiceProxy ("GET" , new URL ("http://localhost:8080/httprpc-test/test" ));
287283
288284 webServiceProxy .setConnectTimeout (3000 );
@@ -302,10 +298,11 @@ public static void testTimeout() throws Exception {
302298 timeout = true ;
303299 }
304300
305- validate ("Timeout" , timeout );
301+ Assert . assertTrue ("Timeout" , timeout );
306302 }
307303
308- public static void testMath () throws Exception {
304+ @ Test
305+ public void testMath () throws Exception {
309306 WebServiceProxy webServiceProxy = new WebServiceProxy ("GET" , new URL ("http://localhost:8080/httprpc-test/math/sum" ));
310307
311308 HashMap <String , Integer > arguments = new HashMap <>();
@@ -317,27 +314,25 @@ public static void testMath() throws Exception {
317314
318315 Number result = webServiceProxy .invoke ();
319316
320- validate ("Math" , result .doubleValue () == 6.0 );
317+ Assert . assertTrue ("Math" , result .doubleValue () == 6.0 );
321318 }
322319
323- public static void testMathService () throws Exception {
320+ @ Test
321+ public void testMathService () throws Exception {
324322 MathService mathService = WebServiceProxy .adapt (new URL ("http://localhost:8080/httprpc-test/math/" ), MathService .class );
325323
326- validate ("Math (service)" , mathService .getSum (4 , 2 ) == 6.0
324+ Assert . assertTrue ("Math (service)" , mathService .getSum (4 , 2 ) == 6.0
327325 && mathService .getSum (listOf (1.0 , 2.0 , 3.0 )) == 6.0 );
328326 }
329327
330- public static void testTree () throws Exception {
328+ @ Test
329+ public void testTree () throws Exception {
331330 WebServiceProxy webServiceProxy = new WebServiceProxy ("GET" , new URL ("http://localhost:8080/httprpc-test/tree" ));
332331
333332 TreeNode root = BeanAdapter .adapt (webServiceProxy .invoke (), TreeNode .class );
334333
335- validate ("Tree" , root .getName ().equals ("Seasons" )
334+ Assert . assertTrue ("Tree" , root .getName ().equals ("Seasons" )
336335 && root .getChildren ().get (0 ).getName ().equals ("Winter" )
337336 && root .getChildren ().get (0 ).getChildren ().get (0 ).getName ().equals ("January" ));
338337 }
339-
340- private static void validate (String test , boolean condition ) {
341- System .out .println (test + ": " + (condition ? "OK" : "FAIL" ));
342- }
343338}
0 commit comments