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 javax.ws.rs.core.MediaType;
20  
21  import org.apache.commons.httpclient.HttpClient;
22  import org.apache.commons.httpclient.methods.PostMethod;
23  import org.apache.commons.httpclient.methods.PutMethod;
24  import org.apache.commons.httpclient.methods.StringRequestEntity;
25  import org.jitr.Jitr;
26  import org.jitr.annotation.JitrConfiguration;
27  import org.jitr.core.OperationalMode;
28  import org.junit.Assert;
29  import org.junit.Before;
30  import org.junit.runner.RunWith;
31  import org.springframework.test.context.ContextConfiguration;
32  import org.springframework.test.context.support.GenericXmlContextLoader;
33  
34  @RunWith(Jitr.class)
35  @JitrConfiguration(mode = OperationalMode.MIXED)
36  @ContextConfiguration(locations = { "/META-INF/spring/test-beans.xml" }, loader = GenericXmlContextLoader.class)
37  public class JitrTestWithExternalSpring extends JitrTestBase {
38  
39      private static final String EXTERNAL_TEST_STRING = "EXTERNAL_TEST_STRING";
40  
41      /**
42       * In this test, we need to setup the remote Jetty service since the service injected is a local
43       * and different Spring application context.
44       */
45      @Before
46      public void before() throws Exception {
47  
48          final HttpClient httpClient = new HttpClient();
49  
50          final PostMethod postMethod = new PostMethod(baseUri + "string/echo");
51          postMethod.setRequestEntity(new StringRequestEntity(EXTERNAL_TEST_STRING,
52                  MediaType.TEXT_PLAIN, "UTF-8"));
53  
54          int statusCode = httpClient.executeMethod(postMethod);
55          Assert.assertEquals(200, statusCode);
56  
57          String responseString = getInputStreamAsString(postMethod.getResponseBodyAsStream());
58          Assert.assertEquals(EXTERNAL_TEST_STRING, responseString);
59  
60          final PutMethod putMethod = new PutMethod(baseUri + "string");
61          putMethod.setRequestEntity(new StringRequestEntity(EXTERNAL_TEST_STRING,
62                  MediaType.TEXT_PLAIN, "UTF-8"));
63  
64          statusCode = httpClient.executeMethod(putMethod);
65          Assert.assertEquals(200, statusCode);
66  
67          putMethod.releaseConnection();
68      }
69  
70      @Override
71      protected String getRemoteTestString() {
72          return EXTERNAL_TEST_STRING;
73      }
74  }