users@jersey.java.net

[Jersey] Singletons not disposed with in memory Jersey test container

From: Michael Hixson <michael.hixson_at_gmail.com>
Date: Fri, 29 Jan 2016 21:09:23 -0800

Hello,

I notice that when I write a JerseyTest with
jersey-test-framework-provider-inmemory, the dispose(instance) methods
of my factories are never called. This leads to some of my components
leaving debris after unit tests.

Is this intentional? I'm wondering if this is a bug.

https://github.com/jersey/jersey/blob/1b9b0772e15fe2378c1b68705dd6146f75e5d6b3/test-framework/providers/inmemory/src/main/java/org/glassfish/jersey/test/inmemory/InMemoryTestContainerFactory.java#L102

@Override
public void stop() {
    if (started.compareAndSet(true, false)) {
        LOGGER.log(Level.FINE, "Stopping InMemoryContainer...");
    } else {
        LOGGER.log(Level.WARNING, "Ignoring stop request -
InMemoryTestContainer is already stopped.");
    }
}

That code doesn't shut down the ApplicationHandler. Why not? Would
something like this work instead?

@Override
public void stop() {
    if (started.compareAndSet(true, false)) {
        LOGGER.log(Level.FINE, "Stopping InMemoryContainer...");
        appHandler.onShutdown(new Container() {
            @Override
            public ResourceConfig getConfiguration() {
                return appHandler.getConfiguration();
            }

            @Override
            public void reload() {
                throw new UnsupportedOperationException();
            }

            @Override
            public void reload(ResourceConfig configuration) {
                throw new UnsupportedOperationException();
            }

            @Override
            public ApplicationHandler getApplicationHandler() {
                return appHandler;
            }
        });
    } else {
        LOGGER.log(Level.WARNING, "Ignoring stop request -
InMemoryTestContainer is already stopped.");
    }
}

-Michael