1   /*
2    * Copyright 2009, Josh Devins, Jitr.org.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.jitr;
18  
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  
24  import org.apache.commons.httpclient.HttpClient;
25  import org.apache.commons.httpclient.HttpMethod;
26  import org.apache.commons.httpclient.methods.GetMethod;
27  import org.jitr.annotation.BaseUri;
28  import org.jitr.annotation.Port;
29  import org.jitr.core.Defaults;
30  import org.jitr.test.service.TestService;
31  import org.junit.Assert;
32  import org.junit.Test;
33  import org.springframework.beans.factory.annotation.Autowired;
34  
35  public abstract class JitrTestBase {
36  
37      private static final String TEST_STRING = "TEST_STRING";
38  
39      @Autowired
40      private TestService testService;
41  
42      @Port
43      protected int port;
44  
45      @BaseUri
46      protected String baseUri;
47  
48      @Test
49      public void testHttpCall() throws Exception {
50  
51          // setup test and make sure still valid
52          testJitrFieldWiring();
53  
54          HttpMethod method = new GetMethod(baseUri + "string");
55  
56          HttpClient httpClient = new HttpClient();
57          httpClient.executeMethod(method);
58  
59          InputStream is = method.getResponseBodyAsStream();
60          String body = getInputStreamAsString(is);
61          Assert.assertEquals(getRemoteTestString(), body);
62  
63          method.releaseConnection();
64      }
65  
66      @Test
67      public void testJitrFieldWiring() throws Exception {
68  
69          Assert.assertNotSame(Defaults.DEFAULT_PORT, port);
70          Assert.assertNotSame(0, port);
71          Assert.assertEquals("http://localhost:" + port + Defaults.DEFAULT_CONTEXT_PATH, baseUri);
72          Assert.assertNotNull(testService);
73  
74          testService.setString(TEST_STRING);
75          Assert.assertEquals(TEST_STRING, testService.getString());
76      }
77  
78      protected String getRemoteTestString() {
79          return TEST_STRING;
80      }
81  
82      protected static String getInputStreamAsString(final InputStream is) throws IOException {
83  
84          BufferedReader br = new BufferedReader(new InputStreamReader(is));
85          StringBuilder sb = new StringBuilder();
86          String line = br.readLine();
87  
88          while (line != null) {
89              sb.append(line);
90              line = br.readLine();
91  
92              if (line != null) {
93                  sb.append("\n");
94              }
95          }
96  
97          br.close();
98  
99          return sb.toString();
100     }
101 }