Skip to content

Commit

Permalink
Updating Annotation tests that need Descriptor to be more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Sep 22, 2022
1 parent f482ffd commit 64f8c8d
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 204 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,25 @@
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.eclipse.jetty.ee10.webapp.WebDescriptor;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.xml.XmlParser;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(WorkDirExtension.class)
public class TestAnnotationDecorator
{
public WorkDir workDir;

public class TestWebDescriptor extends WebDescriptor
{
public TestWebDescriptor(Resource resource, MetaData.Complete metadata)
public TestWebDescriptor(Path xml, MetaData.Complete metadata)
{
super(resource);
super(xml);
_metaDataComplete = metadata;
}

Expand Down Expand Up @@ -86,9 +81,9 @@ public int getMinorVersion()
@Test
public void testAnnotationDecorator() throws Exception
{
Path dummyXml = workDir.getEmptyPathDir().resolve("dummy.xml");
Files.createFile(dummyXml);
Resource dummyXmlResource = ResourceFactory.root().newResource(dummyXml);
Path docroot = workDir.getEmptyPathDir();
Path dummyDescriptor = docroot.resolve("dummy.xml");
Files.createFile(dummyDescriptor);

assertThrows(NullPointerException.class, () ->
{
Expand All @@ -108,7 +103,7 @@ public void testAnnotationDecorator() throws Exception
context.removeAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);

//test with BaseHolder metadata, should not introspect with metdata-complete==true
context.getMetaData().setWebDescriptor(new TestWebDescriptor(dummyXmlResource, MetaData.Complete.True));
context.getMetaData().setWebDescriptor(new TestWebDescriptor(dummyDescriptor, MetaData.Complete.True));
assertTrue(context.getMetaData().isMetaDataComplete());
ServletHolder holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, ""));
holder.setHeldClass(ServletE.class);
Expand All @@ -124,7 +119,7 @@ public void testAnnotationDecorator() throws Exception
context.removeAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);

//test with BaseHolder metadata, should introspect with metadata-complete==false
context.getMetaData().setWebDescriptor(new TestWebDescriptor(dummyXmlResource, MetaData.Complete.False));
context.getMetaData().setWebDescriptor(new TestWebDescriptor(dummyDescriptor, MetaData.Complete.False));
DecoratedObjectFactory.associateInfo(holder);
decorator = new AnnotationDecorator(context);
decorator.decorate(servlet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package org.eclipse.jetty.ee10.annotations;

import java.io.File;
import java.nio.file.Path;

import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.servlet.Source;
Expand All @@ -23,7 +23,6 @@
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -63,31 +62,29 @@ public void testIsIntrospectable() throws Exception
assertTrue(introspector.isIntrospectable(new ServletE(), holder));

//a DESCRIPTOR sourced servlet can be introspected if web.xml metdata-complete==false
File file = MavenTestingUtils.getTestResourceFile("web31false.xml");
Resource resource = ResourceFactory.root().newResource(file.toPath());
wac.getMetaData().setWebDescriptor(new WebDescriptor(resource));
holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString()));
Path xml = MavenTestingUtils.getTestResourcePathFile("web31false.xml");
wac.getMetaData().setWebDescriptor(new WebDescriptor(xml));
holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString()));
assertTrue(introspector.isIntrospectable(new ServletE(), holder));

//a DESCRIPTOR sourced servlet can be introspected if web-fragment.xml medata-complete==false && web.xml metadata-complete==false
file = MavenTestingUtils.getTestResourceFile("web-fragment4false.xml");
resource = ResourceFactory.root().newResource(file.toPath());
wac.getMetaData().addFragmentDescriptor(ResourceFactory.root().newResource(file.getParentFile().toPath()), new FragmentDescriptor(resource));
holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString()));
xml = MavenTestingUtils.getTestResourcePathFile("web-fragment4false.xml");
Resource parent = wac.getResourceFactory().newResource(xml.getParent());
wac.getMetaData().addFragmentDescriptor(parent, new FragmentDescriptor(xml));
holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString()));
assertTrue(introspector.isIntrospectable(new ServletE(), holder));

//a DESCRIPTOR sourced servlet cannot be introspected if web-fragment.xml medata-complete==true (&& web.xml metadata-complete==false)
file = MavenTestingUtils.getTestResourceFile("web-fragment4true.xml");
resource = ResourceFactory.root().newResource(file.toPath());
wac.getMetaData().addFragmentDescriptor(ResourceFactory.root().newResource(file.getParentFile().toPath()), new FragmentDescriptor(resource));
holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString()));
xml = MavenTestingUtils.getTestResourcePathFile("web-fragment4true.xml");
parent = wac.getResourceFactory().newResource(xml.getParent());
wac.getMetaData().addFragmentDescriptor(parent, new FragmentDescriptor(xml));
holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString()));
assertFalse(introspector.isIntrospectable(new ServletE(), holder));

//a DESCRIPTOR sourced servlet cannot be introspected if web.xml medata-complete==true
file = MavenTestingUtils.getTestResourceFile("web31true.xml");
resource = ResourceFactory.root().newResource(file.toPath());
wac.getMetaData().setWebDescriptor(new WebDescriptor(resource));
holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString()));
xml = MavenTestingUtils.getTestResourcePathFile("web31true.xml");
wac.getMetaData().setWebDescriptor(new WebDescriptor(xml));
holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString()));
assertFalse(introspector.isIntrospectable(new ServletE(), holder));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,18 @@
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.eclipse.jetty.ee10.webapp.WebDescriptor;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

@ExtendWith(WorkDirExtension.class)
public class TestRunAsAnnotation
{
public WorkDir workDir;

@Test
public void testRunAsAnnotation() throws Exception
{
Path dummyXml = workDir.getEmptyPathDir().resolve("dummy.xml");
Files.createFile(dummyXml);
Resource dummyXmlResource = ResourceFactory.root().newResource(dummyXml);

WebAppContext wac = new WebAppContext();

//pre-add a servlet but not by descriptor
Expand All @@ -55,14 +47,16 @@ public void testRunAsAnnotation() throws Exception
holder2.setHeldClass(ServletC.class);
holder2.setInitOrder(1);
wac.getServletHandler().addServletWithMapping(holder2, "/foo2/*");
wac.getMetaData().setOrigin(holder2.getName() + ".servlet.run-as", new WebDescriptor(dummyXmlResource));
Path fakeXml = workDir.getEmptyPathDir().resolve("fake.xml");
Files.createFile(fakeXml);
wac.getMetaData().setOrigin(holder2.getName() + ".servlet.run-as", new WebDescriptor(fakeXml));

AnnotationIntrospector parser = new AnnotationIntrospector(wac);
RunAsAnnotationHandler handler = new RunAsAnnotationHandler(wac);
parser.registerHandler(handler);
parser.introspect(new ServletC(), null);

assertEquals("admin", holder.getRunAsRole());
assertEquals(null, holder2.getRunAsRole());
assertNull(holder2.getRunAsRole());
}
}
Loading

0 comments on commit 64f8c8d

Please sign in to comment.