Java Code Examples for org.apache.catalina.startup.Tomcat#enableNaming()

The following examples show how to use org.apache.catalina.startup.Tomcat#enableNaming() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestNamingContextListener.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug49132() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Enable JNDI - it is disabled by default
    tomcat.enableNaming();

    ContextEnvironment environment = new ContextEnvironment();
    environment.setType(BUG49132_VALUE.getClass().getName());
    environment.setName(BUG49132_NAME);
    environment.setValue(BUG49132_VALUE);
    ctx.getNamingResources().addEnvironment(environment);

    ctx.addApplicationListener(Bug49132Listener.class.getName());

    tomcat.start();

    Assert.assertEquals(LifecycleState.STARTED, ctx.getState());
}
 
Example 2
Source File: TestNamingContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void doTestBug51744(boolean exceptionOnFailedWrite)
        throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);
    
    ctx.setJndiExceptionOnFailedWrite(exceptionOnFailedWrite);

    // Map the test Servlet
    Bug51744Servlet bug51744Servlet = new Bug51744Servlet();
    Tomcat.addServlet(ctx, "bug51744Servlet", bug51744Servlet);
    ctx.addServletMapping("/", "bug51744Servlet");

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null);
    assertEquals(200, rc);
    assertTrue(bc.toString().contains(Bug51744Servlet.EXPECTED));
    if (exceptionOnFailedWrite) {
        assertTrue(bc.toString().contains(Bug51744Servlet.ERROR_MESSAGE));
    }
}
 
Example 3
Source File: TestNamingContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doTestBug51744(boolean exceptionOnFailedWrite)
        throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);

    ctx.setJndiExceptionOnFailedWrite(exceptionOnFailedWrite);

    // Map the test Servlet
    Bug51744Servlet bug51744Servlet = new Bug51744Servlet();
    Tomcat.addServlet(ctx, "bug51744Servlet", bug51744Servlet);
    ctx.addServletMappingDecoded("/", "bug51744Servlet");

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null);
    Assert.assertEquals(200, rc);
    Assert.assertTrue(bc.toString().contains(Bug51744Servlet.EXPECTED));
    if (exceptionOnFailedWrite) {
        Assert.assertTrue(bc.toString().contains(Bug51744Servlet.ERROR_MESSAGE));
    }
}
 
Example 4
Source File: TestNamingContext.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBeanFactory() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);
    
    // Create the resource
    ContextResource cr = new ContextResource();
    cr.setName("bug50351");
    cr.setType("org.apache.naming.resources.TesterObject");
    cr.setProperty("factory", "org.apache.naming.factory.BeanFactory");
    cr.setProperty("foo", "value");
    ctx.getNamingResources().addResource(cr);
    
    // Map the test Servlet
    Bug50351Servlet bug50351Servlet = new Bug50351Servlet();
    Tomcat.addServlet(ctx, "bug50351Servlet", bug50351Servlet);
    ctx.addServletMapping("/", "bug50351Servlet");

    tomcat.start();

    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
    assertEquals("value", bc.toString());
}
 
Example 5
Source File: TestNamingContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testListBindings() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    org.apache.catalina.Context ctx = tomcat.addContext("", null);

    // Create the resource
    ContextResource cr = new ContextResource();
    cr.setName("list/foo");
    cr.setType("org.apache.naming.resources.TesterObject");
    cr.setProperty("factory", "org.apache.naming.resources.TesterFactory");
    ctx.getNamingResources().addResource(cr);

    // Map the test Servlet
    Bug23950Servlet bug23950Servlet = new Bug23950Servlet();
    Tomcat.addServlet(ctx, "bug23950Servlet", bug23950Servlet);
    ctx.addServletMappingDecoded("/", "bug23950Servlet");

    tomcat.start();

    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
    Assert.assertEquals("org.apache.naming.resources.TesterObject", bc.toString());
}
 
Example 6
Source File: TestEnvEntry.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doTestJndiInjection(String injectionName, String expected) throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-fragments");
    Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());

    Tomcat.addServlet(context, "InjectionServlet", "org.apache.naming.TesterInjectionServlet");
    context.addServletMappingDecoded("/injection", "InjectionServlet");

    tomcat.enableNaming();
    tomcat.start();

    ByteChunk out = new ByteChunk();

    int rc = getUrl("http://localhost:" + getPort() + "/test/injection?injectionName=" +
            injectionName, out, null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);

    // JSP has leading and trailing white-space
    String result = out.toString().trim();
    Assert.assertEquals(expected, result);
}
 
Example 7
Source File: TestEnvEntry.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doTestJndiLookup(String jndiName, String expected) throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-fragments");
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());

    tomcat.enableNaming();
    tomcat.start();

    ByteChunk out = new ByteChunk();

    int rc = getUrl("http://localhost:" + getPort() + "/test/jndi.jsp?jndiName=" +
            jndiName, out, null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);

    // JSP has leading and trailing white-space
    String result = out.toString().trim();
    Assert.assertEquals(expected, result);
}
 
Example 8
Source File: TestNamingContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void doTestLookup(boolean useSingletonResource) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    org.apache.catalina.Context ctx = tomcat.addContext("", null);

    // Create the resource
    ContextResource cr = new ContextResource();
    cr.setName("list/foo");
    cr.setType("org.apache.naming.resources.TesterObject");
    cr.setProperty("factory", "org.apache.naming.resources.TesterFactory");
    cr.setSingleton(useSingletonResource);
    ctx.getNamingResources().addResource(cr);

    // Map the test Servlet
    Bug49994Servlet bug49994Servlet = new Bug49994Servlet();
    Tomcat.addServlet(ctx, "bug49994Servlet", bug49994Servlet);
    ctx.addServletMappingDecoded("/", "bug49994Servlet");

    tomcat.start();

    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");

    String expected;
    if (useSingletonResource) {
        expected = "EQUAL";
    } else {
        expected = "NOTEQUAL";
    }
    Assert.assertEquals(expected, bc.toString());

}
 
Example 9
Source File: TestNamingContext.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug52830() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);

    // Create the resource
    ContextEnvironment env = new ContextEnvironment();
    env.setName("boolean");
    env.setType(Boolean.class.getName());
    env.setValue("true");
    ctx.getNamingResources().addEnvironment(env);

    // Map the test Servlet
    Bug52830Servlet bug52830Servlet = new Bug52830Servlet();
    Tomcat.addServlet(ctx, "bug52830Servlet", bug52830Servlet);
    ctx.addServletMapping("/", "bug52830Servlet");

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null);
    assertEquals(200, rc);
    assertTrue(bc.toString().contains("truetrue"));
}
 
Example 10
Source File: TestNamingContextListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug54096() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    org.apache.catalina.Context ctx = tomcat.addContext("", null);

    // Enable JNDI - it is disabled by default
    tomcat.enableNaming();

    ContextEnvironment environmentA = new ContextEnvironment();
    environmentA.setType(Bug54096EnvA.class.getName());
    environmentA.setName(BUG54096_NameA);
    environmentA.setValue(BUG54096_ValueA);
    ctx.getNamingResources().addEnvironment(environmentA);

    ContextEnvironment environmentB = new ContextEnvironment();
    environmentB.setType(Bug54096EnvB.class.getName());
    environmentB.setName(BUG54096_NameB);
    environmentB.setValue(BUG54096_ValueB);
    ctx.getNamingResources().addEnvironment(environmentB);

    ctx.addApplicationListener(Bug54096Listener.class.getName());

    tomcat.start();

    assertEquals(LifecycleState.STARTED, ctx.getState());
}
 
Example 11
Source File: TestNamingContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug52830() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);

    // Create the resource
    ContextEnvironment env = new ContextEnvironment();
    env.setName("boolean");
    env.setType(Boolean.class.getName());
    env.setValue("true");
    ctx.getNamingResources().addEnvironment(env);

    // Map the test Servlet
    Bug52830Servlet bug52830Servlet = new Bug52830Servlet();
    Tomcat.addServlet(ctx, "bug52830Servlet", bug52830Servlet);
    ctx.addServletMapping("/", "bug52830Servlet");

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null);
    assertEquals(200, rc);
    assertTrue(bc.toString().contains("truetrue"));
}
 
Example 12
Source File: TestNamingContext.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public void doTestLookup(boolean useSingletonResource) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);
    
    // Create the resource
    ContextResource cr = new ContextResource();
    cr.setName("list/foo");
    cr.setType("org.apache.naming.resources.TesterObject");
    cr.setProperty("factory", "org.apache.naming.resources.TesterFactory");
    cr.setSingleton(useSingletonResource);
    ctx.getNamingResources().addResource(cr);
    
    // Map the test Servlet
    Bug49994Servlet bug49994Servlet = new Bug49994Servlet();
    Tomcat.addServlet(ctx, "bug49994Servlet", bug49994Servlet);
    ctx.addServletMapping("/", "bug49994Servlet");

    tomcat.start();

    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
    
    String expected;
    if (useSingletonResource) {
        expected = "EQUAL";
    } else {
        expected = "NOTEQUAL";
    }
    assertEquals(expected, bc.toString());

}
 
Example 13
Source File: TestNamingContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void doTestLookup(boolean useSingletonResource) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);
    
    // Create the resource
    ContextResource cr = new ContextResource();
    cr.setName("list/foo");
    cr.setType("org.apache.naming.resources.TesterObject");
    cr.setProperty("factory", "org.apache.naming.resources.TesterFactory");
    cr.setSingleton(useSingletonResource);
    ctx.getNamingResources().addResource(cr);
    
    // Map the test Servlet
    Bug49994Servlet bug49994Servlet = new Bug49994Servlet();
    Tomcat.addServlet(ctx, "bug49994Servlet", bug49994Servlet);
    ctx.addServletMapping("/", "bug49994Servlet");

    tomcat.start();

    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
    
    String expected;
    if (useSingletonResource) {
        expected = "EQUAL";
    } else {
        expected = "NOTEQUAL";
    }
    assertEquals(expected, bc.toString());

}
 
Example 14
Source File: AbstractTomcatMetricsTest.java    From tomcat_exporter with Apache License 2.0 5 votes vote down vote up
public static void setUpTomcat(String dataSourceFactory) throws LifecycleException, ServletException {
    // create a tomcat instance
    tomcat = new Tomcat();
    tomcat.setBaseDir(".");
    tomcat.setPort(0);
    tomcat.enableNaming();

    // create a context with our test servlet
    Context ctx = tomcat.addContext(CONTEXT_PATH, new File(".").getAbsolutePath());
    Tomcat.addServlet(ctx, SERVLET_NAME, new TestServlet());
    ctx.addServletMappingDecoded("/*", SERVLET_NAME);

    // add our metrics filter
    FilterDef def = new FilterDef();
    def.setFilterClass(TomcatServletMetricsFilter.class.getName());
    def.setFilterName("metricsFilter");
    def.addInitParameter("buckets",".01, .05, .1, .25, .5, 1, 2.5, 5, 10, 30");
    ctx.addFilterDef(def);
    FilterMap map = new FilterMap();
    map.setFilterName("metricsFilter");
    map.addURLPattern("/*");
    ctx.addFilterMap(map);

    // create a datasource
    ContextResource resource = new ContextResource();
    resource.setName("jdbc/db");
    resource.setAuth("Container");
    resource.setType("javax.sql.DataSource");
    resource.setScope("Sharable");
    resource.setProperty("name", "foo");
    resource.setProperty("factory", dataSourceFactory);
    resource.setProperty("driverClassName", "org.h2.Driver");
    resource.setProperty("url", "jdbc:h2:mem:dummy");
    resource.setProperty("jdbcInterceptors", "nl.nlighten.prometheus.tomcat.TomcatJdbcInterceptor(logFailed=true,logSlow=true,threshold=0,buckets=.01|.05|.1|1|10,slowQueryBuckets=1|10|30)");
    ctx.getNamingResources().addResource(resource);

    // start instance
    tomcat.init();
    tomcat.start();
}
 
Example 15
Source File: TestNamingContext.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testGlobalNaming() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    org.apache.catalina.Context ctx = tomcat.addContext("", null);

    tomcat.start();

    Context webappInitial = ContextBindings.getContext(ctx);

    // Nothing added at the moment so should be null
    Object obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);

    ContextEnvironment ce = new ContextEnvironment();
    ce.setName(GLOBAL_NAME);
    ce.setValue(DATA);
    ce.setType(DATA.getClass().getName());

    tomcat.getServer().getGlobalNamingResources().addEnvironment(ce);

    // No link so still should be null
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);

    // Now add a resource link to the context
    ContextResourceLink crl = new ContextResourceLink();
    crl.setGlobal(GLOBAL_NAME);
    crl.setName(LOCAL_NAME);
    crl.setType(DATA.getClass().getName());
    ctx.getNamingResources().addResourceLink(crl);

    // Link exists so should be OK now
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertEquals(DATA, obj);

    // Try shortcut
    ResourceLinkFactory factory = new ResourceLinkFactory();
    ResourceLinkRef rlr = new ResourceLinkRef(DATA.getClass().getName(), GLOBAL_NAME, null, null);
    obj = factory.getObjectInstance(rlr, null, null, null);
    Assert.assertEquals(DATA, obj);

    // Remove the link
    ctx.getNamingResources().removeResourceLink(LOCAL_NAME);

    // No link so should be null
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);

    // Shortcut should fail too
    obj = factory.getObjectInstance(rlr, null, null, null);
    Assert.assertNull(obj);
}
 
Example 16
Source File: AbstractUiTest.java    From TeaStore with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws ServletException, LifecycleException, InterruptedException, JsonProcessingException {
	webUITomcat = new Tomcat();
	webUITomcat.setPort(3000);
	webUITomcat.setBaseDir(testWorkingDir);
	webUITomcat.enableNaming();
	Context context = webUITomcat.addWebapp(CONTEXT, System.getProperty("user.dir") + File.separator + "src"
			+ File.separator + "main" + File.separator + "webapp");
	ContextEnvironment registryURL = new ContextEnvironment();
	registryURL.setDescription("");
	registryURL.setOverride(false);
	registryURL.setType("java.lang.String");
	registryURL.setName("registryURL");
	registryURL.setValue("http://localhost:9001/tools.descartes.teastore.registry/rest/services/");
	context.getNamingResources().addEnvironment(registryURL);
	webUITomcat.addServlet(CONTEXT, "servlet", getServlet());
	webUITomcat.addServlet(CONTEXT, "index", new IndexServlet());
	webUITomcat.addServlet(CONTEXT, "login", new LoginServlet());
	webUITomcat.addServlet(CONTEXT, "order", new OrderServlet());
	context.addServletMappingDecoded("/test", "servlet");
	context.addServletMappingDecoded("/index", "index");
	context.addServletMappingDecoded("/login", "login");
	context.addServletMappingDecoded("/order", "order");
	context.addWelcomeFile("/index");
	webUITomcat.start();

	// Mock registry
	List<String> strings = new LinkedList<String>();
	strings.add("localhost:9001");
	String json = new ObjectMapper().writeValueAsString(strings);
	wireMockRule.stubFor(get(urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.IMAGE.getServiceName() + "/"))
					.willReturn(okJson(json)));
	wireMockRule.stubFor(get(urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.AUTH.getServiceName() + "/"))
					.willReturn(okJson(json)));
	wireMockRule.stubFor(get(urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.PERSISTENCE.getServiceName() + "/"))
					.willReturn(okJson(json)));
	wireMockRule.stubFor(get(urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.RECOMMENDER.getServiceName() + "/"))
					.willReturn(okJson(json)));

	// Mock images
	HashMap<String, String> img = new HashMap<>();
	img.put("andreBauer", "andreBauer");
	img.put("johannesGrohmann", "johannesGrohmann");
	img.put("joakimKistowski", "joakimKistowski");
	img.put("simonEismann", "simonEismann");
	img.put("norbertSchmitt", "norbertSchmitt");
	img.put("descartesLogo", "descartesLogo");
	img.put("icon", "icon");
	mockValidPostRestCall(img, "/tools.descartes.teastore.image/rest/image/getWebImages");
}
 
Example 17
Source File: AbstractRecommenderRestTest.java    From TeaStore with Apache License 2.0 4 votes vote down vote up
/**
 * Sets up a registry, a persistance unit and a store.
 * 
 * @throws Throwable
 *             Throws uncaught throwables for test to fail.
 */
@Before
public void setup() throws Throwable {
	persistence = new MockPersistenceProvider(persistenceWireMockRule);

	otherrecommender = new MockOtherRecommenderProvider(otherRecommenderWireMockRule);

	setRegistry(new MockRegistry(registryWireMockRule, Arrays.asList(getPersistence().getPort()),
			Arrays.asList(RECOMMENDER_TEST_PORT, otherrecommender.getPort())));

	// debuggging response
	// Response response1 = ClientBuilder
	// .newBuilder().build().target("http://localhost:" + otherrecommender.getPort()
	// + "/"
	// + Service.RECOMMENDER.getServiceName() + "/rest/train/timestamp")
	// .request(MediaType.APPLICATION_JSON).get();
	// System.out.println(response1.getStatus() + ":" +
	// response1.readEntity(String.class));

	// debuggging response
	// Response response0 = ClientBuilder.newBuilder().build()
	// .target("http://localhost:" + MockRegistry.DEFAULT_MOCK_REGISTRY_PORT
	// + "/tools.descartes.teastore.registry/rest/services/"
	// + Service.PERSISTENCE.getServiceName() + "/")
	// .request(MediaType.APPLICATION_JSON).get();
	// System.out.println(response0.getStatus() + ":" +
	// response0.readEntity(String.class));
	//
	// Response response1 = ClientBuilder.newBuilder().build()
	// .target("http://localhost:" + persistence.getPort()
	// + "/tools.descartes.teastore.persistence/rest/orderitems")
	// .request(MediaType.APPLICATION_JSON).get();
	// System.out.println(response1.getStatus() + ":" +
	// response1.readEntity(String.class));

	// Setup recommend tomcat
	testTomcat = new Tomcat();
	testTomcat.setPort(RECOMMENDER_TEST_PORT);
	testTomcat.setBaseDir(testWorkingDir);
	testTomcat.enableNaming();
	Context context = testTomcat.addWebapp("/" + Service.RECOMMENDER.getServiceName(), testWorkingDir);
	ContextEnvironment registryURL3 = new ContextEnvironment();
	registryURL3.setDescription("");
	registryURL3.setOverride(false);
	registryURL3.setType("java.lang.String");
	registryURL3.setName("registryURL");
	registryURL3.setValue(
			"http://localhost:" + registry.getPort() + "/tools.descartes.teastore.registry/rest/services/");
	context.getNamingResources().addEnvironment(registryURL3);
	ContextEnvironment servicePort3 = new ContextEnvironment();
	servicePort3.setDescription("");
	servicePort3.setOverride(false);
	servicePort3.setType("java.lang.String");
	servicePort3.setName("servicePort");
	servicePort3.setValue("" + RECOMMENDER_TEST_PORT);
	context.getNamingResources().addEnvironment(servicePort3);
	ResourceConfig restServletConfig3 = new ResourceConfig();
	restServletConfig3.register(TrainEndpoint.class);
	restServletConfig3.register(RecommendEndpoint.class);
	restServletConfig3.register(RecommendSingleEndpoint.class);
	ServletContainer restServlet3 = new ServletContainer(restServletConfig3);
	testTomcat.addServlet("/" + Service.RECOMMENDER.getServiceName(), "restServlet", restServlet3);
	context.addServletMappingDecoded("/rest/*", "restServlet");
	context.addApplicationListener(RecommenderStartup.class.getName());

	ContextEnvironment recommender = new ContextEnvironment();
	recommender.setDescription("");
	recommender.setOverride(false);
	recommender.setType("java.lang.String");
	recommender.setName("recommenderAlgorithm");
	recommender.setValue("PreprocessedSlopeOne");
	context.getNamingResources().addEnvironment(recommender);

	ContextEnvironment retrainlooptime = new ContextEnvironment();
	retrainlooptime.setDescription("");
	retrainlooptime.setOverride(false);
	retrainlooptime.setType("java.lang.Long");
	retrainlooptime.setName("recommenderLoopTime");
	retrainlooptime.setValue("100");
	context.getNamingResources().addEnvironment(retrainlooptime);

	testTomcat.start();

	try {
		Thread.sleep(5000);
	} catch (InterruptedException e) {
	}
}
 
Example 18
Source File: AbstractStoreRestTest.java    From TeaStore with Apache License 2.0 4 votes vote down vote up
/**
 * Sets up a store.
 * 
 * @throws Throwable
 *           Throws uncaught throwables for test to fail.
 */
@Before
public void setup() throws Throwable {
  BasicConfigurator.configure();

  storeTomcat = new Tomcat();
  storeTomcat.setPort(3000);
  storeTomcat.setBaseDir(testWorkingDir);
  storeTomcat.enableNaming();
  ContextEnvironment registryUrl3 = new ContextEnvironment();
  registryUrl3.setDescription("");
  registryUrl3.setOverride(false);
  registryUrl3.setType("java.lang.String");
  registryUrl3.setName("registryURL");
  registryUrl3
      .setValue("http://localhost:18080/tools.descartes.teastore.registry/rest/services/");
  Context context3 = storeTomcat.addWebapp("/tools.descartes.teastore.auth", testWorkingDir);
  context3.getNamingResources().addEnvironment(registryUrl3);
  ContextEnvironment servicePort3 = new ContextEnvironment();
  servicePort3.setDescription("");
  servicePort3.setOverride(false);
  servicePort3.setType("java.lang.String");
  servicePort3.setName("servicePort");
  servicePort3.setValue("3000");
  context3.getNamingResources().addEnvironment(servicePort3);
  ResourceConfig restServletConfig3 = new ResourceConfig();
  restServletConfig3.register(AuthCartRest.class);
  restServletConfig3.register(AuthUserActionsRest.class);
  ServletContainer restServlet3 = new ServletContainer(restServletConfig3);
  storeTomcat.addServlet("/tools.descartes.teastore.auth", "restServlet", restServlet3);
  context3.addServletMappingDecoded("/rest/*", "restServlet");
  context3.addApplicationListener(EmptyAuthStartup.class.getName());

  // Mock registry
  List<String> strings = new LinkedList<String>();
  strings.add("localhost:18080");
  String json = new ObjectMapper().writeValueAsString(strings);
  List<String> strings2 = new LinkedList<String>();
  strings2.add("localhost:3000");
  String json2 = new ObjectMapper().writeValueAsString(strings2);
  wireMockRule.stubFor(get(urlEqualTo(
      "/tools.descartes.teastore.registry/rest/services/" + Service.IMAGE.getServiceName() + "/"))
          .willReturn(okJson(json)));
  wireMockRule.stubFor(get(urlEqualTo(
      "/tools.descartes.teastore.registry/rest/services/" + Service.AUTH.getServiceName() + "/"))
          .willReturn(okJson(json2)));
  wireMockRule.stubFor(
      WireMock.put(WireMock.urlMatching("/tools.descartes.teastore.registry/rest/services/"
          + Service.AUTH.getServiceName() + "/.*")).willReturn(okJson(json2)));
  wireMockRule.stubFor(
      WireMock.delete(WireMock.urlMatching("/tools.descartes.teastore.registry/rest/services/"
          + Service.AUTH.getServiceName() + "/.*")).willReturn(okJson(json2)));
  wireMockRule.stubFor(get(urlEqualTo("/tools.descartes.teastore.registry/rest/services/"
      + Service.PERSISTENCE.getServiceName() + "/")).willReturn(okJson(json)));
  wireMockRule.stubFor(get(urlEqualTo("/tools.descartes.teastore.registry/rest/services/"
      + Service.RECOMMENDER.getServiceName() + "/")).willReturn(okJson(json)));

  // Mock images
  HashMap<String, String> img = new HashMap<>();
  img.put("andreBauer", "andreBauer");
  img.put("johannesGrohmann", "johannesGrohmann");
  img.put("joakimKistowski", "joakimKistowski");
  img.put("simonEismann", "simonEismann");
  img.put("norbertSchmitt", "norbertSchmitt");
  img.put("descartesLogo", "descartesLogo");
  img.put("icon", "icon");
  mockValidPostRestCall(img, "/tools.descartes.teastore.image/rest/image/getWebImages");

  storeTomcat.start();
}
 
Example 19
Source File: TestNamingContext.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testGlobalNaming() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    org.apache.catalina.Context ctx = tomcat.addContext("", null);

    tomcat.start();

    Context webappInitial = ContextBindings.getContext(ctx);

    // Nothing added at the moment so should be null
    Object obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);

    ContextEnvironment ce = new ContextEnvironment();
    ce.setName(GLOBAL_NAME);
    ce.setValue(DATA);
    ce.setType(DATA.getClass().getName());

    tomcat.getServer().getGlobalNamingResources().addEnvironment(ce);

    // No link so still should be null
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);

    // Now add a resource link to the context
    ContextResourceLink crl = new ContextResourceLink();
    crl.setGlobal(GLOBAL_NAME);
    crl.setName(LOCAL_NAME);
    crl.setType(DATA.getClass().getName());
    ctx.getNamingResources().addResourceLink(crl);

    // Link exists so should be OK now
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertEquals(DATA, obj);

    // Try shortcut
    ResourceLinkFactory factory = new ResourceLinkFactory();
    ResourceLinkRef rlr = new ResourceLinkRef(DATA.getClass().getName(), GLOBAL_NAME, null, null);
    obj = factory.getObjectInstance(rlr, null, null, null);
    Assert.assertEquals(DATA, obj);

    // Remove the link
    ctx.getNamingResources().removeResourceLink(LOCAL_NAME);

    // No link so should be null
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);

    // Shortcut should fail too
    obj = factory.getObjectInstance(rlr, null, null, null);
    Assert.assertNull(obj);
}
 
Example 20
Source File: TestWebSocket.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testBug53339() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "Bug53339", new Bug53339Servlet());
    ctx.addServletMapping("/*", "Bug53339");

    // Create the resource
    ContextEnvironment env = new ContextEnvironment();
    env.setName(Bug53339WsInbound.JNDI_NAME);
    env.setType(String.class.getName());
    env.setValue(Bug53339WsInbound.TEST_MESSAGE);
    ctx.getNamingResources().addEnvironment(env);

    tomcat.start();

    WebSocketClient client= new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET / HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an upgrade response
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 101"));

    // Swallow the headers
    String responseHeaderLine = client.reader.readLine();
    while (!responseHeaderLine.equals("")) {
        responseHeaderLine = client.reader.readLine();
    }

    // Now we can do WebSocket
    String msg = client.readMessage();
    assertEquals(Bug53339WsInbound.TEST_MESSAGE, msg);

    // Finished with the socket
    client.close();
}