Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2acfffd2c6a825c2cb3614318e15d0657c36c37a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*******************************************************************************
 * Copyright (c) 2009, 2010 VMware Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution. 
 * The Eclipse Public License is available at
 *   http://www.eclipse.org/legal/epl-v10.html
 * and the Apache License v2.0 is available at 
 *   http://www.opensource.org/licenses/apache2.0.php.
 * You may elect to redistribute this code under either of these licenses.  
 *
 * Contributors:
 *   VMware Inc. - initial contribution
 *******************************************************************************/

package org.eclipse.gemini.web.test.tomcat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Set;

import javax.servlet.ServletContext;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;

import org.eclipse.gemini.web.core.spi.ServletContainer;
import org.eclipse.gemini.web.core.spi.WebApplicationHandle;
import org.eclipse.virgo.test.framework.OsgiTestRunner;
import org.eclipse.virgo.test.framework.TestFrameworkUtils;
import org.eclipse.virgo.util.io.IOUtils;
import org.eclipse.virgo.util.io.PathReference;
import org.eclipse.virgo.util.io.ZipUtils;

@RunWith(OsgiTestRunner.class)
public class TomcatServletContainerTests {

    private static final String PATH_WAR_WITH_TLD_WAR = "../org.eclipse.gemini.web.test/src/test/resources/war-with-tld.war?Web-ContextPath=/war-with-tld";

    private static final String PATH_WAR_WITH_SERVLET = "../org.eclipse.gemini.web.test/src/test/resources/war-with-servlet.war?Web-ContextPath=/war-with-servlet";

    private static final String LOCATION_PREFIX = "webbundle:file:";

    private static final String LOCATION_SIMPLE_WAR = LOCATION_PREFIX
        + "../org.eclipse.gemini.web.core/src/test/resources/simple-war.war?Web-ContextPath=/simple-war";

    private static final String LOCATION_WAR_WITH_SERVLET = "webbundle:file:" + PATH_WAR_WITH_SERVLET;

    private static final String LOCATION_WAR_WITH_JSP = LOCATION_PREFIX
        + "../org.eclipse.gemini.web.test/src/test/resources/war-with-jsp.war?Web-ContextPath=/war-with-jsp";

    private static final String LOCATION_WAR_WITH_TLD = LOCATION_PREFIX + PATH_WAR_WITH_TLD_WAR;

    private static final String LOCATION_WAR_WITH_TLD_FROM_DEPENDENCY = LOCATION_PREFIX
        + "../org.eclipse.gemini.web.test/src/test/resources/war-with-tld-from-dependency.war?Web-ContextPath=/war-with-tld-from-dependency";

    private static final String LOCATION_WAR_WITH_TLD_IMPORT_SYSTEM_PACKAGES = LOCATION_PREFIX
        + "../org.eclipse.gemini.web.test/src/test/resources/war-with-tld-import-system-packages.war?Web-ContextPath=/war-with-tld-import-system-packages";

    private static final String LOCATION_WAR_WITH_CONTEXT_XML_RESOURCES = LOCATION_PREFIX
        + "../org.eclipse.gemini.web.test/src/test/resources/war-with-context-xml-resources.war?Web-ContextPath=/war-with-context-xml-resources";

    private static final String LOCATION_WAR_WITH_CONTEXT_XML_CROSS_CONTEXT = LOCATION_PREFIX
        + "../org.eclipse.gemini.web.test/src/test/resources/war-with-context-xml-cross-context.war?Web-ContextPath=/war-with-context-xml-cross-context";
    
    private BundleContext bundleContext;

    private ServletContainer container;

    @Before
    public void before() throws Exception {
        this.bundleContext = TestFrameworkUtils.getBundleContextForTestClass(getClass());
        ServiceReference ref = bundleContext.getServiceReference(ServletContainer.class.getName());
        this.container = (ServletContainer) bundleContext.getService(ref);
    }

    @Test
    public void testServletContainerAvailable() {
        assertNotNull(this.container);
        try {
            new Socket("localhost", 8080);
        } catch (UnknownHostException e) {
            fail("Unable to connect");
        } catch (IOException e) {
            fail("Unable to connect");
        }
    }

    @Test
    public void testInstallSimpleWar() throws Exception {
        String location = LOCATION_SIMPLE_WAR;
        Bundle bundle = this.bundleContext.installBundle(location);
        bundle.start();

        validateNotFound("http://localhost:8080/test/index.html");

        WebApplicationHandle handle = this.container.createWebApplication("/test", bundle);
        this.container.startWebApplication(handle);
        assertNotNull(handle);

        validateURL("http://localhost:8080/test/index.html");

        this.container.stopWebApplication(handle);

        validateNotFound("http://localhost:8080/test/index.html");

    }

    @Test
    public void testWarWithServlet() throws Exception {
        String location = LOCATION_WAR_WITH_SERVLET;
        Bundle bundle = this.bundleContext.installBundle(location);
        bundle.start();

        WebApplicationHandle handle = this.container.createWebApplication("/war-with-servlet", bundle);
        this.container.startWebApplication(handle);
        try {
            validateURL("http://localhost:8080/war-with-servlet/test");
        } finally {
            this.container.stopWebApplication(handle);
        }
    }

    @Test
    public void testWarWithBasicJSP() throws Exception {
        String location = LOCATION_WAR_WITH_JSP;
        Bundle bundle = this.bundleContext.installBundle(location);
        bundle.start();

        WebApplicationHandle handle = this.container.createWebApplication("/war-with-jsp", bundle);
        this.container.startWebApplication(handle);
        try {
            validateURL("http://localhost:8080/war-with-jsp/index.jsp");
        } finally {
            this.container.stopWebApplication(handle);
        }
    }

    @Test
    public void testWarWithJSTL() throws Exception {
        testWarWithJSTL("");
    }

    private void testWarWithJSTL(String addtionalUrlSuffix) throws MalformedURLException, IOException, BundleException {
        String location = LOCATION_WAR_WITH_TLD + addtionalUrlSuffix;
        Bundle bundle = this.bundleContext.installBundle(location);
        bundle.start();

        WebApplicationHandle handle = this.container.createWebApplication("/war-with-tld", bundle);
        this.container.startWebApplication(handle);
        try {
            String realPath = handle.getServletContext().getRealPath("/");
            System.out.println(realPath);
            validateURL("http://localhost:8080/war-with-tld/test.jsp");
        } finally {
            this.container.stopWebApplication(handle);
            bundle.uninstall();
        }
    }

    @Test
    public void testWarWithJSTLFromDependency() throws MalformedURLException, IOException, BundleException {
        String jstlLocation = "file:../ivy-cache/repository/javax.servlet/com.springsource.javax.servlet.jsp.jstl/1.2.0/com.springsource.javax.servlet.jsp.jstl-1.2.0.jar";
        Bundle jstlBundle = this.bundleContext.installBundle(jstlLocation);

        Bundle bundle = this.bundleContext.installBundle(LOCATION_WAR_WITH_TLD_FROM_DEPENDENCY);
        bundle.start();

        WebApplicationHandle handle = this.container.createWebApplication("/war-with-tld-from-dependency", bundle);
        this.container.startWebApplication(handle);
        try {
            String realPath = handle.getServletContext().getRealPath("/");
            System.out.println(realPath);
            validateURL("http://localhost:8080/war-with-tld-from-dependency/test.jsp");
        } finally {
            this.container.stopWebApplication(handle);
            bundle.uninstall();
            jstlBundle.uninstall();
        }
    }

    @Test
    public void testWarWithJSTLFromExplodedDependency() throws MalformedURLException, IOException, BundleException {
        String jstlPath = "../ivy-cache/repository/javax.servlet/com.springsource.javax.servlet.jsp.jstl/1.2.0/com.springsource.javax.servlet.jsp.jstl-1.2.0.jar";
        PathReference jstl = new PathReference(jstlPath);
        PathReference unzippedJstl = explode(jstl);

        String jstlLocation = "file:" + unzippedJstl.getAbsolutePath();
        Bundle jstlBundle = this.bundleContext.installBundle(jstlLocation);

        Bundle bundle = this.bundleContext.installBundle(LOCATION_WAR_WITH_TLD_FROM_DEPENDENCY);
        bundle.start();

        WebApplicationHandle handle = this.container.createWebApplication("/war-with-tld-from-dependency", bundle);
        this.container.startWebApplication(handle);
        try {
            validateURL("http://localhost:8080/war-with-tld-from-dependency/test.jsp");
        } finally {
            this.container.stopWebApplication(handle);
            bundle.uninstall();
            jstlBundle.uninstall();
            unzippedJstl.delete(true);
        }
    }

    @Test
    public void testWarWithJSTLThatImportsSystemPackages() throws MalformedURLException, IOException, BundleException {
        String location = LOCATION_WAR_WITH_TLD_IMPORT_SYSTEM_PACKAGES;
        Bundle bundle = this.bundleContext.installBundle(location);
        bundle.start();

        WebApplicationHandle handle = this.container.createWebApplication("/war-with-tld", bundle);
        this.container.startWebApplication(handle);
        try {
            String realPath = handle.getServletContext().getRealPath("/");
            System.out.println(realPath);
            validateURL("http://localhost:8080/war-with-tld/test.jsp");
        } finally {
            this.container.stopWebApplication(handle);
            bundle.uninstall();
        }
    }

    @Test
    public void testGetRealPathWithJarBundle() throws Exception {
        String location = LOCATION_WAR_WITH_SERVLET;
        Bundle bundle = this.bundleContext.installBundle(location);
        bundle.start();

        WebApplicationHandle handle = this.container.createWebApplication("/war-with-servlet", bundle);
        this.container.startWebApplication(handle);
        try {
            ServletContext context = handle.getServletContext();
            assertNotNull(context);

            String path = context.getRealPath("/WEB-INF/web.xml");
            assertNull(path);
        } finally {
            this.container.stopWebApplication(handle);
        }
    }

    @Test
    public void testServletContextResourceLookup() throws Exception {
        String location = LOCATION_WAR_WITH_SERVLET;
        Bundle bundle = this.bundleContext.installBundle(location);
        bundle.start();

        WebApplicationHandle handle = this.container.createWebApplication("/war-with-servlet", bundle);
        this.container.startWebApplication(handle);
        try {
            ServletContext context = handle.getServletContext();
            assertNotNull(context);

            URL resource = context.getResource("/WEB-INF/web.xml");
            assertNotNull(resource);

            URLConnection connection = resource.openConnection();
            assertNotNull(connection);

            Set<?> paths = context.getResourcePaths("/WEB-INF");
            assertNotNull(paths);
            assertEquals(3, paths.size());

        } finally {
            this.container.stopWebApplication(handle);
        }
    }

    @Test
    public void rootContextPath() throws Exception {
        String location = LOCATION_WAR_WITH_SERVLET;
        Bundle bundle = this.bundleContext.installBundle(location);
        bundle.start();

        WebApplicationHandle handle = this.container.createWebApplication("", bundle);
        this.container.startWebApplication(handle);
        try {
            ServletContext context = handle.getServletContext();
            assertEquals("", context.getContextPath());
        } finally {
            this.container.stopWebApplication(handle);
        }
    }

    private void validateURL(String path) throws MalformedURLException, IOException {
        URL url = new URL(path);
        InputStream stream = url.openConnection().getInputStream();
        assertNotNull(stream);
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    }

    private void validateNotFound(String path) throws Exception {
        URL url = new URL(path);
        try {
            url.openConnection().getInputStream();
        } catch (IOException e) {
            assertTrue("success case", true);
            return;
        }
        fail("URL '" + path + "' is still deployed");
    }

    private PathReference explode(PathReference packed) throws IOException {
        PathReference target = new PathReference("target");
        return ZipUtils.unzipTo(packed, target);
    }

    @Test
    public void testLastModified() throws Exception {
        String location = LOCATION_WAR_WITH_SERVLET;
        Bundle bundle = this.bundleContext.installBundle(location);
        bundle.start();

        WebApplicationHandle handle = this.container.createWebApplication("", bundle);
        this.container.startWebApplication(handle);
        try {
            ServletContext context = handle.getServletContext();
            long lm = context.getResource("/META-INF/").openConnection().getLastModified();
            assertTrue(lm != 0);
        } finally {
            this.container.stopWebApplication(handle);
        }
    }

    @Test
    public void testWarWithContextXml() throws Exception {
        // Copy default context.xml
        File defaultContextXml = new File("config/context.xml");
        createFileWithContent(defaultContextXml, "<Context crossContext=\"true\"/>");

        // Copy default context.xml.default
        File defaultHostContextXml = new File("config/Catalina/localhost/context.xml.default");
        String content = "<Context>"
                + "<Resource name=\"mail/Session1\" auth=\"Container\" type=\"javax.mail.Session\" mail.smtp.host=\"localhost\"/>"
                + "</Context>";
        createFileWithContent(defaultHostContextXml, content);

        File tomcatServerXml = new File("config/tomcat-server.xml");
        createFileWithContent(tomcatServerXml, "");

        String location1 = LOCATION_WAR_WITH_CONTEXT_XML_RESOURCES;
        Bundle bundle1 = this.bundleContext.installBundle(location1);
        bundle1.start();

        String location2 = LOCATION_WAR_WITH_CONTEXT_XML_CROSS_CONTEXT;
        Bundle bundle2 = this.bundleContext.installBundle(location2);
        bundle2.start();

        WebApplicationHandle handle1 = this.container.createWebApplication("/war-with-context-xml-resources", bundle1);
        this.container.startWebApplication(handle1);

        WebApplicationHandle handle2 = this.container.createWebApplication("/war-with-context-xml-cross-context",
                bundle2);
        this.container.startWebApplication(handle2);
        try {
            // tests JNDI resources
            validateURL("http://localhost:8080/war-with-context-xml-resources/index.jsp");

            // tests cross context functionality
            validateURL("http://localhost:8080/war-with-context-xml-cross-context/index.jsp");
        } finally {
            this.container.stopWebApplication(handle1);
            bundle1.uninstall();

            this.container.stopWebApplication(handle2);
            bundle2.uninstall();
            
            defaultContextXml.delete();
            defaultHostContextXml.delete();
            tomcatServerXml.delete();
        }
    }

    private void createFileWithContent(File file, String content) throws Exception {
        file.getParentFile().mkdirs();
        FileWriter fWriter = null;
        try {
            fWriter = new FileWriter(file);
            fWriter.write(content);
            fWriter.flush();
        } finally {
            IOUtils.closeQuietly(fWriter);
        }
    }
}

Back to the top