View Javadoc
1   /*
2    * Copyright (C) 2007-2014 JMPEAX.
3    *
4    *     This program is free software: you can redistribute it and/or modify
5    *     it under the terms of the GNU General Public License as published by
6    *     the Free Software Foundation, either version 3 of the License, or
7    *     (at your option) any later version.
8    *
9    *     This program is distributed in the hope that it will be useful,
10   *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *     GNU General Public License for more details.
13   *
14   *     You should have received a copy of the GNU General Public License
15   *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  
18  package com.jmpeax.osgi.undertow.http;
19  
20  import io.undertow.Undertow;
21  import io.undertow.server.HttpHandler;
22  import io.undertow.server.HttpServerExchange;
23  import io.undertow.server.handlers.PathHandler;
24  import io.undertow.servlet.Servlets;
25  import io.undertow.servlet.api.DeploymentInfo;
26  import io.undertow.servlet.api.DeploymentManager;
27  import io.undertow.util.Headers;
28  import org.mockito.Mockito;
29  import org.osgi.framework.BundleContext;
30  import org.testng.annotations.BeforeMethod;
31  import org.testng.annotations.Test;
32  
33  import javax.servlet.ServletException;
34  import java.io.IOException;
35  
36  public class UndertowHttpServiceActivatorTest {
37  
38      private BundleContext dummyBundleContext;
39  
40      public static void main(String[] args) throws IOException, ServletException {
41          PathHandler pathHandler = new PathHandler();
42          pathHandler.addExactPath("/hello", new HttpHandler() {
43              @Override
44              public void handleRequest(final HttpServerExchange exchange) throws Exception {
45                  exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
46                  exchange.getResponseSender().send("Hello World");
47              }
48          });
49          Undertow server = Undertow.builder().addHttpListener(9090, "localhost")
50                  .setHandler(pathHandler).build();
51          server.start();
52          pathHandler.addExactPath("/bye", new HttpHandler() {
53              @Override
54              public void handleRequest(final HttpServerExchange exchange) throws Exception {
55                  exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
56                  exchange.getResponseSender().send("bye World");
57              }
58          });
59          DeploymentInfo servletBuilder = Servlets.deployment().addServlet(Servlets.servlet("sample", SampleServlet
60                  .class).addMapping("/*")).setDeploymentName("Test").setContextPath("/servlets").setClassLoader
61                  (UndertowHttpServiceActivatorTest.class.getClassLoader());
62          final DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
63          manager.deploy();
64          pathHandler.addExactPath("/servlets", manager.start());
65  
66      }
67  
68      @BeforeMethod
69      public void setUp() throws Exception {
70          dummyBundleContext= Mockito.mock(BundleContext.class);
71      }
72  
73      @Test
74      public void testStart() throws Exception {
75          dummyBundleContext.notify();
76      }
77  
78      @Test
79      public void testStop() throws Exception {
80          // new UndertowHttpServiceActivator().stop(dummyBundleContext);
81      }
82  
83  }