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  // START SNIPPET: example
20  import org.apache.commons.httpclient.HttpClient;
21  import org.apache.commons.httpclient.HttpMethod;
22  import org.apache.commons.httpclient.methods.GetMethod;
23  import org.jitr.annotation.BaseUri;
24  import org.jitr.annotation.JitrConfiguration;
25  import org.jitr.annotation.Port;
26  import org.jitr.test.service.TestService;
27  import org.junit.After;
28  import org.junit.Assert;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import org.springframework.beans.factory.annotation.Autowired;
33  
34  @RunWith(Jitr.class)
35  @JitrConfiguration(contextPath = "/example")
36  public class ExampleTest {
37  
38      private static final String TEST_STRING = "TEST_STRING";
39  
40      @Port
41      private int port;
42  
43      @BaseUri
44      private String baseUri;
45  
46      @Autowired
47      private TestService testService;
48  
49      private HttpClient httpClient;
50  
51      private HttpMethod method;
52  
53      @After
54      public void after() {
55          if (method != null) {
56              method.releaseConnection();
57          }
58      }
59  
60      @Before
61      public void before() {
62  
63          httpClient = new HttpClient();
64  
65          // make sure the Spring autowiring was successful
66          Assert.assertNotNull(testService);
67  
68          // make sure the Jitr fields have been set properly
69          Assert.assertNotSame(-1, port);
70          Assert.assertNotSame(8080, port);
71  
72          Assert.assertNotNull(baseUri);
73          Assert.assertTrue(baseUri.length() > 0);
74          Assert.assertTrue(baseUri.contains("http://localhost:" + port + "/example"));
75      }
76  
77      @Test
78      public void testServerGet() throws Exception {
79  
80          // set value on bean
81          testService.setString(TEST_STRING);
82  
83          // make sure value was set on bean properly
84          Assert.assertEquals(TEST_STRING, testService.getString());
85  
86          // fetch the same value from server through HTTP GET
87          method = new GetMethod(baseUri + "/string");
88          httpClient.executeMethod(method);
89  
90          // make sure the value from the server is the same
91          Assert.assertEquals(TEST_STRING, method.getResponseBodyAsString());
92      }
93  }
94  // END SNIPPET: example