View Javadoc

1   /*
2    * $Id: JarResourceServletTestCase.java 22927 2011-09-13 15:18:22Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.servlet.jetty;
12  
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  import org.mule.tck.junit4.rule.DynamicPort;
15  import org.mule.transport.servlet.JarResourceServlet;
16  import org.mule.transport.servlet.jetty.util.EmbeddedJettyServer;
17  
18  import java.io.IOException;
19  
20  import org.apache.commons.httpclient.HttpClient;
21  import org.apache.commons.httpclient.HttpStatus;
22  import org.apache.commons.httpclient.methods.GetMethod;
23  import org.junit.After;
24  import org.junit.Before;
25  import org.junit.Rule;
26  import org.junit.Test;
27  
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertTrue;
30  
31  public class JarResourceServletTestCase extends AbstractMuleContextTestCase
32  {
33      @Rule
34      public DynamicPort port1 = new DynamicPort("port1");
35  
36      private EmbeddedJettyServer server;
37  
38      @Before
39      public void startEmbeddedJettyServer() throws Exception
40      {
41          server = new EmbeddedJettyServer(port1.getNumber(), "/", "/mule-resource/*",
42              new JarResourceServlet(), muleContext);
43          server.start();
44      }
45  
46      @After
47      public void shutdownEmbeddedJettyServer() throws Exception
48      {
49          if (server != null)
50          {
51              server.stop();
52              server.destroy();
53          }
54      }
55  
56      @Test
57      public void retriveHtmlFromClasspath() throws Exception
58      {
59          muleContext.start();
60  
61          String result = getContentsOfResource("foo.html");
62          assertTrue(result.contains("${title}"));
63  
64          String replacement = "hello foo";
65          muleContext.getRegistry().registerObject("title", replacement);
66  
67          result = getContentsOfResource("foo.html");
68          assertTrue(result.contains(replacement));
69      }
70  
71      @Test
72      public void retriveXmlFromClasspath() throws Exception
73      {
74          muleContext.start();
75  
76          String result = getContentsOfResource("foo.xml");
77          assertTrue(result.contains("${bar}"));
78  
79          String replacement = "hello bar";
80          muleContext.getRegistry().registerObject("bar", replacement);
81  
82          result = getContentsOfResource("foo.xml");
83          assertTrue(result.contains(replacement));
84      }
85  
86      private String getContentsOfResource(String resource) throws IOException
87      {
88          String url = String.format("http://localhost:%d/mule-resource/files/%s", port1.getNumber(),
89              resource);
90          GetMethod method = new GetMethod(url);
91          int rc = new HttpClient().executeMethod(method);
92          assertEquals(HttpStatus.SC_OK, rc);
93          return method.getResponseBodyAsString();
94      }
95  }