Refine search
/** * Creates the {@link HTTPContext} instance, with the required preconditions in place. * * @throws Exception * When an exception is encountered during creation of the context. */ private void buildHTTPContext() throws Exception { // If there are no running servers, we'll abort as the test cannot be executed. if (wlServerRuntimes.length < 1) { throw new DeploymentException("None of the targets are in the RUNNING state."); } else { // For now, we'll use the first server to populate the context. // This may change in a future Arquillian release, // to allow different strategies for testing a clustered deployment. ObjectName wlServerRuntime = wlServerRuntimes[0]; String httpUrlAsString = (String) connection.invoke(wlServerRuntime, "getURL", new Object[] { "http" }, new String[] { "java.lang.String" } ); URL serverHttpUrl = new URL(httpUrlAsString); httpContext = new HTTPContext(serverHttpUrl.getHost(), serverHttpUrl.getPort()); List<ObjectName> servletRuntimes = findServletRuntimes(wlServerRuntime, deploymentName); for (ObjectName servletRuntime : servletRuntimes) { String servletName = (String) connection.getAttribute(servletRuntime, "ServletName"); String servletContextRoot = (String) connection.getAttribute(servletRuntime, "ContextPath"); httpContext.add(new Servlet(servletName, servletContextRoot)); } } }
private URL locateCommandEventBusURI(HTTPContext context) { List<Servlet> contextServlets = context.getServlets(); if (contextServlets == null) { throw new IllegalArgumentException("Could not determine URI for WarpFilter in context " + context + ". There are no Servlets in context."); } Set<String> contextRoots = new HashSet<String>(); for (Servlet servlet : contextServlets) { contextRoots.add(servlet.getContextRoot()); } if (contextRoots.size() == 1) { try { URI baseURI = context.getServlets().get(0).getBaseURI(); return new URL("http", baseURI.getHost(), baseURI.getPort(), baseURI.getPath()); } catch (MalformedURLException e) { throw new RuntimeException("Could not convert Servlet to URI, " + context.getServlets().get(0), e); } } else { try { return new URL("http", context.getHost(), context.getPort(), "/"); } catch (MalformedURLException e) { throw new RuntimeException("Could not convert HTTPContext to URI, " + context, e); } } } }
public Servlet getServletByName(String name) { for (Servlet servlet : getServlets()) { if (servlet.getName().equals(name)) { return servlet; } } return null; }
@BeforeClass public static void before() { servlet = new Servlet(TEST_SERVLET_NAME, TEST_CONTEXT_ROOT); servletWithParent = new Servlet(TEST_SERVLET_NAME, TEST_CONTEXT_ROOT); servletWithParent.setParent(TEST_HTTP_CONTEXT); rootContextServletWithParent = new Servlet(TEST_SERVLET_NAME, Servlet.ROOT_CONTEXT); rootContextServletWithParent.setParent(TEST_HTTP_CONTEXT); }
@Test // equal ports, different ip public void shouldUpdateWithIPFromDocker() throws Exception { Binding binding = new Binding(GATEWAY_IP); binding.addPortBinding(EXPOSED_PORT, EXPOSED_PORT); Mockito.when(cube.getMetadata(HasPortBindings.class)).thenReturn(new TestPortBindings(binding)); bind(ContainerScoped.class, Container.class, new ContainerImpl(CUBE_CONTAINER_NAME, deployableContainer, new ContainerDefImpl("arquillian.xml"))); ProtocolMetaData metadata = new ProtocolMetaData(); metadata.addContext(new HTTPContext(LOCALHOST, EXPOSED_PORT).add(new Servlet("A", "B"))); bind(DeploymentScoped.class, ProtocolMetaData.class, metadata); fire(metadata); ProtocolMetaData updated = getManager().getContext(DeploymentContext.class) .getObjectStore().get(ProtocolMetaData.class); Assert.assertEquals(EXPOSED_PORT.intValue(), updated.getContexts(HTTPContext.class).iterator().next().getPort()); Assert.assertEquals(GATEWAY_IP, updated.getContexts(HTTPContext.class).iterator().next().getHost()); assertEventFired(ProtocolMetaData.class, 1); // twice, but original fire is intercepted and never hit the Counter }
@Override protected HTTPContext locateHTTPContext(Method method) { HTTPContext previous = null; if (httpContexts != null && httpContexts.size() > 0) { previous = super.locateHTTPContext(method); } final ModuleContext mc = locateModuleContext(method); HTTPContext context; if (previous == null) { context = new HTTPContext(mc.getHost(), mc.getPort()); addArquillianServlet(context); } else { context = new HTTPContext(previous.getName(), mc.getHost(), mc.getPort()); boolean foundArqServlet = false; for (Servlet servlet : previous.getServlets()) { if (foundArqServlet == false && ServletMethodExecutor.ARQUILLIAN_SERVLET_NAME.equals(servlet.getName())) { foundArqServlet = true; } context.add(servlet); } if (foundArqServlet == false) { addArquillianServlet(context); } } return context; }
@Test(expected = IllegalArgumentException.class) public void testServletForNullContextRoot() { new Servlet(TEST_SERVLET_NAME, null); }
public static URI determineBaseURI(ServletProtocolConfiguration config, HTTPContext context, String servletName) { String scheme = config.getScheme(); String host = config.getHost(); Integer port = config.getPort(); // TODO: can not set contextRoot in config, change to prefixContextRoot String contextRoot = null; //protocolConfiguration.getContextRoot(); Servlet servlet = context.getServletByName(servletName); if (servlet != null) { // use the context where the Arquillian servlet is found if (scheme == null) { scheme = "http"; } if (host == null) { host = context.getHost(); } if (port == null) { port = context.getPort(); } contextRoot = servlet.getContextRoot(); } else { throw new IllegalArgumentException( servletName + " not found. " + "Could not determine ContextRoot from ProtocolMetadata, please contact DeployableContainer developer."); } return URI.create(scheme + "://" + host + ":" + port + contextRoot); }
protected URI getBaseURL() { HTTPContext context = metaDataInst.get().getContext(HTTPContext.class); if (allInSameContext(context.getServlets())) { return context.getServlets().get(0).getBaseURI(); } throw new IllegalStateException("No baseURL found in HTTPContext"); }
private URL toURL(Servlet servlet) { try { return servlet.getBaseURI().toURL(); } catch (Exception e) { throw new RuntimeException("Could not convert Servlet to URL, " + servlet, e); } }
private boolean allInSameContext(List<Servlet> servlets) { Set<String> context = new HashSet<String>(); for (Servlet servlet : servlets) { context.add(servlet.getContextRoot()); } return context.size() == 1; }
@Test // equal ip, different ports public void shouldUpdateWithPortFromDocker() throws Exception { Binding binding = new Binding(LOCALHOST); binding.addPortBinding(EXPOSED_PORT, BOUND_PORT); Mockito.when(cube.getMetadata(HasPortBindings.class)).thenReturn(new TestPortBindings(binding)); bind(ContainerScoped.class, Container.class, new ContainerImpl(CUBE_CONTAINER_NAME, deployableContainer, new ContainerDefImpl("arquillian.xml"))); ProtocolMetaData metadata = new ProtocolMetaData(); metadata.addContext(new HTTPContext(LOCALHOST, EXPOSED_PORT).add(new Servlet("A", "B"))); bind(DeploymentScoped.class, ProtocolMetaData.class, metadata); fire(metadata); ProtocolMetaData updated = getManager().getContext(DeploymentContext.class) .getObjectStore().get(ProtocolMetaData.class); Assert.assertEquals(BOUND_PORT.intValue(), updated.getContexts(HTTPContext.class).iterator().next().getPort()); Assert.assertEquals(LOCALHOST, updated.getContexts(HTTPContext.class).iterator().next().getHost()); assertEventFired(ProtocolMetaData.class, 1); // twice, but original fire is intercepted and never hit the Counter }
@Test(expected = IllegalArgumentException.class) public void testServletForNullName() { new Servlet(null, TEST_CONTEXT_ROOT); }
@BeforeClass public static void before() { servlet = new Servlet(TEST_SERVLET_NAME, TEST_CONTEXT_ROOT); servletWithParent = new Servlet(TEST_SERVLET_NAME, TEST_CONTEXT_ROOT); servletWithParent.setParent(TEST_HTTP_CONTEXT); rootContextServletWithParent = new Servlet(TEST_SERVLET_NAME, Servlet.ROOT_CONTEXT); rootContextServletWithParent.setParent(TEST_HTTP_CONTEXT); }
protected URI getBaseURI(Method method) throws Exception { ProtocolDefinition definition = registry.get().getProtocol(ProtocolDescription.DEFAULT); ModulesProtocolConfiguration configuration = (ModulesProtocolConfiguration) definition.createProtocolConfiguration(); HTTPContext context = ModulesApi.findHTTPContext(configuration, protocolMetaData, method); return context.getServlets().get(0).getBaseURI(); }
private URL toURL(Servlet servlet) { try { return servlet.getBaseURI().toURL(); } catch (Exception e) { throw new RuntimeException("Could not convert Servlet to URL, " + servlet, e); } }
private boolean allInSameContext(List<Servlet> servlets) { Set<String> context = new HashSet<String>(); for (Servlet servlet : servlets) { context.add(servlet.getContextRoot()); } return context.size() == 1; }