Skip to content

Commit

Permalink
Fix static test problems
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Apr 24, 2013
1 parent 6730893 commit 2a122c3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.jboss.forge.container;

import java.util.List;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.container.spi.ContainerLifecycleListener;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -24,9 +29,15 @@ public static ForgeArchive getDeployment()
return archive;
}

@Inject
private Forge forge;

@Test
public void testContainerStartup()
{
Assert.assertEquals(1, TestLifecycleListener.beforeStartTimesCalled);
ForgeImpl impl = (ForgeImpl) forge;
List<ContainerLifecycleListener> listeners = impl.getRegisteredListeners();
Assert.assertEquals(1, listeners.size());
Assert.assertEquals(1, ((TestLifecycleListener) listeners.get(0)).beforeStartTimesCalled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

public class TestLifecycleListener implements ContainerLifecycleListener
{
public static int beforeStartTimesCalled;
public static int beforeStopTimesCalled;
public static int afterStopTimesCalled;
public int beforeStartTimesCalled;
public int beforeStopTimesCalled;
public int afterStopTimesCalled;

@Override
public void beforeStart(Forge forge) throws ContainerException
Expand Down
31 changes: 17 additions & 14 deletions container/src/main/java/org/jboss/forge/container/ForgeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ForgeImpl implements Forge
private boolean serverMode = true;
private AddonRegistryImpl registry;
private List<ContainerLifecycleListener> registeredListeners = new ArrayList<ContainerLifecycleListener>();
private List<ListenerRegistration<ContainerLifecycleListener>> loadedListenerRegistrations = new ArrayList<ListenerRegistration<ContainerLifecycleListener>>();

private ClassLoader loader;

Expand Down Expand Up @@ -105,6 +106,13 @@ public Forge start(ClassLoader loader)
{
assertNotAlive();
this.loader = loader;

for (ContainerLifecycleListener listener : ServiceLoader.load(ContainerLifecycleListener.class, loader))
{
ListenerRegistration<ContainerLifecycleListener> registration = addContainerLifecycleListener(listener);
loadedListenerRegistrations.add(registration);
}

fireBeforeContainerStartedEvent(loader);
if (!alive)
{
Expand Down Expand Up @@ -178,6 +186,10 @@ public Forge start(ClassLoader loader)
}
}
fireAfterContainerStoppedEvent(loader);
for (ListenerRegistration<ContainerLifecycleListener> registation : loadedListenerRegistrations)
{
registation.removeListener();
}
return this;
}

Expand All @@ -192,10 +204,6 @@ private void fireBeforeContainerStartedEvent(ClassLoader loader)
{
listener.beforeStart(this);
}
for (ContainerLifecycleListener listener : ServiceLoader.load(ContainerLifecycleListener.class, loader))
{
listener.beforeStart(this);
}
status = ContainerStatus.STARTED;
}

Expand All @@ -205,10 +213,6 @@ private void fireBeforeContainerStoppedEvent(ClassLoader loader)
{
listener.beforeStop(this);
}
for (ContainerLifecycleListener listener : ServiceLoader.load(ContainerLifecycleListener.class, loader))
{
listener.beforeStop(this);
}
status = ContainerStatus.STOPPED;
}

Expand All @@ -218,12 +222,6 @@ private void fireAfterContainerStoppedEvent(ClassLoader loader)
{
listener.afterStop(this);
}

for (ContainerLifecycleListener listener : ServiceLoader.load(ContainerLifecycleListener.class, loader))
{
listener.afterStop(this);
}

}

@Override
Expand Down Expand Up @@ -304,4 +302,9 @@ public ContainerStatus getStatus()
{
return isStartingAddons() ? ContainerStatus.STARTING : status;
}

public List<ContainerLifecycleListener> getRegisteredListeners()
{
return Collections.unmodifiableList(registeredListeners);
}
}

0 comments on commit 2a122c3

Please sign in to comment.