Skip to content

Commit

Permalink
Issue #5020 Add a test and fixes.
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <[email protected]>
  • Loading branch information
janbartel committed Jul 7, 2020
1 parent e007fd6 commit 3a5db40
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@

package org.eclipse.jetty.servlet;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import javax.servlet.DispatcherType;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.eclipse.jetty.http.pathmap.MappedResource;
import org.eclipse.jetty.util.component.Container;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -657,4 +662,72 @@ public void testAddFilterWithMappingAPI() throws Exception
assertTrue(fh3 == mappings[5].getFilterHolder()); //isMatchAfter = true;
assertTrue(pf == mappings[6].getFilterHolder()); //isMatchAfter = true;
}

@Test
public void testFiltersServletsListenersAsBeans() throws Exception
{
ServletContextHandler context = new ServletContextHandler();

ServletHandler handler = context.getServletHandler();

//test that filters, servlets and listeners are added as beans
//and thus reported in a Container.Listener
List<Object> addResults = new ArrayList<>();
List<Object> removeResults = new ArrayList<>();
handler.addEventListener(new Container.Listener()
{
@Override
public void beanAdded(Container parent, Object child)
{
addResults.add(child);
}

@Override
public void beanRemoved(Container parent, Object child)
{
removeResults.add(child);
}

});

handler.addFilter(fh1);
handler.addServlet(sh1);
ListenerHolder lh1 = new ListenerHolder(new Source(Source.Origin.DESCRIPTOR, "foo.xml"));
lh1.setInstance(new HttpSessionListener()
{
@Override
public void sessionDestroyed(HttpSessionEvent se)
{
}

@Override
public void sessionCreated(HttpSessionEvent se)
{
}
});
handler.addListener(lh1);

assertTrue(addResults.contains(fh1));
assertTrue(addResults.contains(sh1));
assertTrue(addResults.contains(lh1));

//test that servlets, filters and listeners are dumped, but
//not as beans
String dump = handler.dump();
dump = dump.substring(0, dump.indexOf("key:"));

assertFalse(dump.contains("+-")); //not dumped as beans
assertFalse(dump.contains("+=")); //not dumped as managed beans
assertFalse(dump.contains("+~")); //not dumped as unmanaged beans
assertFalse(dump.contains("+?")); //not dumped as auto beans

handler.setFilters(null);
handler.setServlets(null);
handler.setListeners(null);

//check they're removed as beans
assertTrue(removeResults.contains(fh1));
assertTrue(removeResults.contains(sh1));
assertTrue(removeResults.contains(lh1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ static void dumpContainer(Appendable out, String indent, Container object, List<
for (Iterator<Object> i = container.getBeans().iterator(); i.hasNext(); )
{
Object bean = i.next();
if (extras.contains(bean))

if (isContainedIn(bean, extras))
continue; //will be explictly formatted by caller

String nextIndent = indent + ((i.hasNext() || !extras.isEmpty()) ? "| " : " ");
Expand Down Expand Up @@ -240,6 +241,26 @@ else if (containerLifeCycle != null && containerLifeCycle.isUnmanaged(bean))
}
}

static boolean isContainedIn(Object object, List<Object> objects)
{
if (object == null)
return false;
if (objects == null)
return false;

if (objects.contains(object))
return true;

for (Object o : objects)
{
if (o instanceof DumpableCollection && ((DumpableCollection)o).contains(object))
{
return true;
}
}
return false;
}

@Deprecated
static void dumpIterable(Appendable out, String indent, Iterable<?> iterable, boolean last) throws IOException
{
Expand All @@ -251,7 +272,7 @@ static void dumpIterable(Appendable out, String indent, Iterable<?> iterable, Li
for (Iterator i = iterable.iterator(); i.hasNext(); )
{
Object item = i.next();
if (extras.contains(item))
if (isContainedIn(item, extras))
continue; //the item will be formatted explicitly by caller

String nextIndent = indent + ((i.hasNext() || !extras.isEmpty()) ? "| " : " ");
Expand All @@ -274,7 +295,7 @@ static void dumpMapEntries(Appendable out, String indent, Map<?, ?> map, List<Ob
for (Iterator<? extends Map.Entry<?, ?>> i = map.entrySet().iterator(); i.hasNext(); )
{
Map.Entry entry = i.next();
if (extras.contains(entry))
if (isContainedIn(entry, extras))
continue; //will be explicitly formatted by caller
String nextIndent = indent + ((i.hasNext() || !extras.isEmpty()) ? "| " : " ");
out.append(indent).append("+@ ").append(String.valueOf(entry.getKey())).append(" = ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public static DumpableCollection from(String name, Object... items)
{
return new DumpableCollection(name, items == null ? Collections.emptyList() : Arrays.asList(items));
}

public boolean contains(Object o)
{
return _collection.contains(o);
}

@Override
public String dump()
Expand Down

0 comments on commit 3a5db40

Please sign in to comment.