Fixed test refactoring common parts and always destroying the "env" subcontext at end of tests.

This commit is contained in:
Simone Bordet 2012-03-08 12:16:51 +01:00
parent 0ab47e41bc
commit c559383bb2
1 changed files with 36 additions and 35 deletions

View File

@ -1,22 +1,19 @@
package org.eclipse.jetty.annotations.resources; package org.eclipse.jetty.annotations.resources;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import org.eclipse.jetty.annotations.AnnotationIntrospector; import org.eclipse.jetty.annotations.AnnotationIntrospector;
import org.eclipse.jetty.annotations.AnnotationParser;
import org.eclipse.jetty.annotations.ClassNameResolver;
import org.eclipse.jetty.annotations.ResourceAnnotationHandler; import org.eclipse.jetty.annotations.ResourceAnnotationHandler;
import org.eclipse.jetty.annotations.ResourcesAnnotationHandler; import org.eclipse.jetty.annotations.ResourcesAnnotationHandler;
import org.eclipse.jetty.plus.annotation.Injection; import org.eclipse.jetty.plus.annotation.Injection;
import org.eclipse.jetty.plus.annotation.InjectionCollection; import org.eclipse.jetty.plus.annotation.InjectionCollection;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.MetaData;
import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -24,24 +21,39 @@ import static org.junit.Assert.assertNotNull;
public class TestResourceAnnotations public class TestResourceAnnotations
{ {
Object objA=new Integer(1000); private Server server;
Object objB=new Integer(2000); private WebAppContext wac;
private InjectionCollection injections;
private Context comp;
private Context env;
private Object objA = 1000;
private Object objB = 2000;
@Before
public void init() throws Exception
{
server = new Server();
wac = new WebAppContext();
wac.setServer(server);
injections = new InjectionCollection();
wac.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections);
InitialContext ic = new InitialContext();
comp = (Context)ic.lookup("java:comp");
env = comp.createSubcontext("env");
}
@After
public void destroy() throws Exception
{
comp.destroySubcontext("env");
}
@Test @Test
public void testResourceAnnotations () public void testResourceAnnotations ()
throws Exception throws Exception
{ {
Server server = new Server(); new org.eclipse.jetty.plus.jndi.EnvEntry(server, "resA", objA, false);
WebAppContext wac = new WebAppContext(); new org.eclipse.jetty.plus.jndi.EnvEntry(server, "resB", objB, false);
wac.setServer(server);
InjectionCollection injections = new InjectionCollection();
wac.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections);
InitialContext ic = new InitialContext();
Context comp = (Context)ic.lookup("java:comp");
Context env = comp.createSubcontext("env");
org.eclipse.jetty.plus.jndi.EnvEntry resourceA = new org.eclipse.jetty.plus.jndi.EnvEntry(server, "resA", objA, false);
org.eclipse.jetty.plus.jndi.EnvEntry resourceB = new org.eclipse.jetty.plus.jndi.EnvEntry(server, "resB", objB, false);
AnnotationIntrospector parser = new AnnotationIntrospector(); AnnotationIntrospector parser = new AnnotationIntrospector();
ResourceAnnotationHandler handler = new ResourceAnnotationHandler(wac); ResourceAnnotationHandler handler = new ResourceAnnotationHandler(wac);
@ -116,32 +128,21 @@ public class TestResourceAnnotations
f = ResourceA.class.getDeclaredField("n"); f = ResourceA.class.getDeclaredField("n");
f.setAccessible(true); f.setAccessible(true);
assertEquals(objB, f.get(binst)); assertEquals(objB, f.get(binst));
comp.destroySubcontext("env");
} }
@Test @Test
public void testResourcesAnnotation () public void testResourcesAnnotation ()
throws Exception throws Exception
{ {
Server server = new Server(); new org.eclipse.jetty.plus.jndi.EnvEntry(server, "resA", objA, false);
WebAppContext wac = new WebAppContext(); new org.eclipse.jetty.plus.jndi.EnvEntry(server, "resB", objB, false);
wac.setServer(server);
InjectionCollection injections = new InjectionCollection();
wac.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections);
InitialContext ic = new InitialContext();
Context comp = (Context)ic.lookup("java:comp");
Context env = comp.createSubcontext("env");
org.eclipse.jetty.plus.jndi.EnvEntry resourceA = new org.eclipse.jetty.plus.jndi.EnvEntry(server, "resA", objA, false);
org.eclipse.jetty.plus.jndi.EnvEntry resourceB = new org.eclipse.jetty.plus.jndi.EnvEntry(server, "resB", objB, false);
AnnotationIntrospector introspector = new AnnotationIntrospector(); AnnotationIntrospector introspector = new AnnotationIntrospector();
ResourcesAnnotationHandler handler = new ResourcesAnnotationHandler(wac); ResourcesAnnotationHandler handler = new ResourcesAnnotationHandler(wac);
introspector.registerHandler(handler); introspector.registerHandler(handler);
introspector.introspect(ResourceA.class); introspector.introspect(ResourceA.class);
introspector.introspect(ResourceB.class); introspector.introspect(ResourceB.class);
assertEquals(objA, env.lookup("peach")); assertEquals(objA, env.lookup("peach"));
assertEquals(objB, env.lookup("pear")); assertEquals(objB, env.lookup("pear"));
} }