You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by bl...@apache.org on 2010/06/07 02:20:06 UTC
svn commit: r952028 [1/2] - in /incubator/wink/trunk:
wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/
wink-common/src/main/java/org/apache/wink/common/internal/registry/
wink-component-test-support/src/main/java/org/apache/wink/ser...
Author: bluk
Date: Mon Jun 7 00:20:05 2010
New Revision: 952028
URL: http://svn.apache.org/viewvc?rev=952028&view=rev
Log:
Add ObjectFactory.release() and releaseAll()
Methods will be used to complete the lifecycle
for when instances (or supporting objects)
will need to be called back.
releaseInstance(T instance, RuntimeContext) is
intended to release single objects, mainly
during request scoped objects for instance.
releaseAll(RuntimeContext) is intended to
release all objects currently known to the
ObjectFactory during the cycle.
It is up to each ObjectFactory to determine
the implementation. A null passed in through
RuntimeContext can indicate that it is
outside any single context. For example,
a releaseAll(null) could be the callback
for a singleton ObjectFactory which means
this is the last time the ObjectFactory
references are intended to be used.
Added:
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle1Test.java (with props)
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle2Test.java (with props)
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle.java (with props)
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle1Test.java (with props)
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle2Test.java (with props)
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle3Test.java (with props)
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle4Test.java (with props)
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle5Test.java (with props)
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle6Test.java (with props)
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle7Test.java (with props)
Modified:
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/ObjectFactory.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/PrototypeObjectFactory.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/SingletonObjectFactory.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java
incubator/wink/trunk/wink-component-test-support/src/main/java/org/apache/wink/server/internal/servlet/MockServletInvocationTest.java
incubator/wink/trunk/wink-guice-server/src/main/java/org/apache/wink/guice/server/internal/lifecycle/GuiceInjectorLifeCycleManager.java
incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/RequestProcessor.java
incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindRootResourceHandler.java
incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceInstance.java
incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRecordFactory.java
incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRegistry.java
incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestServlet.java
incubator/wink/trunk/wink-spring-support/src/main/java/org/apache/wink/spring/internal/SpringObjectFactory.java
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/ObjectFactory.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/ObjectFactory.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/ObjectFactory.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/ObjectFactory.java Mon Jun 7 00:20:05 2010
@@ -27,4 +27,7 @@ public interface ObjectFactory<T> {
Class<T> getInstanceClass();
+ void releaseInstance(T instance, RuntimeContext context);
+
+ void releaseAll(RuntimeContext context);
}
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/PrototypeObjectFactory.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/PrototypeObjectFactory.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/PrototypeObjectFactory.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/PrototypeObjectFactory.java Mon Jun 7 00:20:05 2010
@@ -54,4 +54,12 @@ class PrototypeObjectFactory<T> implemen
public String toString() {
return String.format("ClassMetadataPrototypeOF %s", metadata); //$NON-NLS-1$
}
+
+ public void releaseInstance(T instance, RuntimeContext context) {
+ /* do nothing */
+ }
+
+ public void releaseAll(RuntimeContext context) {
+ /* do nothing */
+ }
}
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/SingletonObjectFactory.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/SingletonObjectFactory.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/SingletonObjectFactory.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/SingletonObjectFactory.java Mon Jun 7 00:20:05 2010
@@ -49,4 +49,12 @@ class SingletonObjectFactory<T> implemen
public String toString() {
return String.format("SingletonOF: %s", objectClass); //$NON-NLS-1$
}
+
+ public void releaseInstance(T instance, RuntimeContext context) {
+ /* do nothing */
+ }
+
+ public void releaseAll(RuntimeContext context) {
+ /* do nothing */
+ }
}
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java Mon Jun 7 00:20:05 2010
@@ -173,6 +173,19 @@ public class ProvidersRegistry {
return addProvider(provider, WinkApplication.DEFAULT_PRIORITY);
}
+ /**
+ * Removes all providers in the registry.
+ */
+ public void removeAllProviders() {
+ contextResolvers.removeAll();
+ messageBodyReaders.removeAll();
+ messageBodyWriters.removeAll();
+
+ for (ObjectFactory<?> of : exceptionMappers) {
+ of.releaseAll(null);
+ }
+ }
+
@SuppressWarnings("unchecked")
public <T> ContextResolver<T> getContextResolver(final Class<T> contextType,
MediaType mediaType,
@@ -576,6 +589,22 @@ public class ProvidersRegistry {
return data.isEmpty();
}
+ @SuppressWarnings("unchecked")
+ synchronized void removeAll() {
+ // order of operations for the next 4 lines matter
+ Entry<MediaType, HashSet<PriorityObjectFactory<T>>>[] oldEntrySet = entrySet;
+ entrySet = data.entrySet().toArray(new Entry[0]);
+ data = new HashMap<MediaType, HashSet<PriorityObjectFactory<T>>>();
+ providersCache.clear();
+
+ for (Entry<MediaType, HashSet<PriorityObjectFactory<T>>> entry : oldEntrySet) {
+ HashSet<PriorityObjectFactory<T>> set = entry.getValue();
+ for (PriorityObjectFactory<T> of : set) {
+ of.releaseAll(null);
+ }
+ }
+ }
+
/**
* returns providers by mediaType and by type
*
@@ -817,6 +846,14 @@ public class ProvidersRegistry {
return of.getInstanceClass();
}
+ public void releaseInstance(T instance, RuntimeContext context) {
+ of.releaseInstance(instance, context);
+ }
+
+ public void releaseAll(RuntimeContext context) {
+ of.releaseAll(context);
+ }
+
public int compareTo(OFHolder<T> o) {
// check if this is a system provider
// system providers are less than
@@ -880,6 +917,14 @@ public class ProvidersRegistry {
return of.getInstanceClass();
}
+ public void releaseInstance(T instance, RuntimeContext context) {
+ of.releaseInstance(instance, context);
+ }
+
+ public void releaseAll(RuntimeContext context) {
+ of.releaseAll(context);
+ }
+
// this compare is used by exception mappers
public int compareTo(PriorityObjectFactory<T> o) {
return Double.compare(priority, o.priority);
Modified: incubator/wink/trunk/wink-component-test-support/src/main/java/org/apache/wink/server/internal/servlet/MockServletInvocationTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-component-test-support/src/main/java/org/apache/wink/server/internal/servlet/MockServletInvocationTest.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-component-test-support/src/main/java/org/apache/wink/server/internal/servlet/MockServletInvocationTest.java (original)
+++ incubator/wink/trunk/wink-component-test-support/src/main/java/org/apache/wink/server/internal/servlet/MockServletInvocationTest.java Mon Jun 7 00:20:05 2010
@@ -93,11 +93,25 @@ public abstract class MockServletInvocat
servletConfig.addInitParameter("propertiesLocation", propertiesFile);
}
+ String deploymentConfigClass = getDeploymentConfigurationClassName();
+ if (deploymentConfigClass != null) {
+ servletConfig.addInitParameter("deploymentConfiguration", deploymentConfigClass);
+ }
+
tls.set(this);
servlet.init(servletConfig);
}
/**
+ * @return the value of the deploymentConfiguration parameter. The class
+ * name must be a name of a DeploymentConfiguration class on the
+ * classpath.
+ */
+ protected String getDeploymentConfigurationClassName() {
+ return null;
+ }
+
+ /**
* @return the name of the properties file. The file must be located on the
* classpath. By default returns null.
*/
@@ -137,10 +151,11 @@ public abstract class MockServletInvocat
return response;
}
-
+
/**
- * Return the servlet object. Users of MockServletInvocationTest do not need to manipulate the servlet object directly.
- * This method is intended to allow users to inspect the servlet if their test class needs to do so.
+ * Return the servlet object. Users of MockServletInvocationTest do not need
+ * to manipulate the servlet object directly. This method is intended to
+ * allow users to inspect the servlet if their test class needs to do so.
*
* @return the HttpServlet object
*/
Modified: incubator/wink/trunk/wink-guice-server/src/main/java/org/apache/wink/guice/server/internal/lifecycle/GuiceInjectorLifeCycleManager.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-guice-server/src/main/java/org/apache/wink/guice/server/internal/lifecycle/GuiceInjectorLifeCycleManager.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-guice-server/src/main/java/org/apache/wink/guice/server/internal/lifecycle/GuiceInjectorLifeCycleManager.java (original)
+++ incubator/wink/trunk/wink-guice-server/src/main/java/org/apache/wink/guice/server/internal/lifecycle/GuiceInjectorLifeCycleManager.java Mon Jun 7 00:20:05 2010
@@ -60,6 +60,14 @@ public class GuiceInjectorLifeCycleManag
public Class<T> getInstanceClass() {
return (Class<T>)object.getClass();
}
+
+ public void releaseInstance(T instance, RuntimeContext context) {
+ /* do nothing */
+ }
+
+ public void releaseAll(RuntimeContext context) {
+ /* do nothing */
+ }
};
}
@@ -137,6 +145,14 @@ public class GuiceInjectorLifeCycleManag
public Class<T> getInstanceClass() {
return clazz;
}
+
+ public void releaseInstance(T instance, RuntimeContext context) {
+ /* do nothing */
+ }
+
+ public void releaseAll(RuntimeContext context) {
+ /* do nothing */
+ }
}
private static class GuicePrototypeObjectFactory<T> implements ObjectFactory<T> {
@@ -181,6 +197,14 @@ public class GuiceInjectorLifeCycleManag
public Class<T> getInstanceClass() {
return clazz;
}
+
+ public void releaseInstance(T instance, RuntimeContext context) {
+ /* do nothing */
+ }
+
+ public void releaseAll(RuntimeContext context) {
+ /* do nothing */
+ }
}
private static <T> ClassMetadata collectClassMetadata(final Class<T> cls) {
Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/RequestProcessor.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/RequestProcessor.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/RequestProcessor.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/RequestProcessor.java Mon Jun 7 00:20:05 2010
@@ -22,6 +22,7 @@ package org.apache.wink.server.internal;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.util.List;
import java.util.Properties;
import java.util.Set;
@@ -33,11 +34,14 @@ import javax.ws.rs.WebApplicationExcepti
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
+import org.apache.wink.common.RuntimeContext;
import org.apache.wink.common.WinkApplication;
import org.apache.wink.common.internal.i18n.Messages;
import org.apache.wink.common.internal.runtime.RuntimeContextTLS;
import org.apache.wink.server.internal.application.ServletApplicationFileLoader;
+import org.apache.wink.server.internal.handlers.SearchResult;
import org.apache.wink.server.internal.handlers.ServerMessageContext;
+import org.apache.wink.server.internal.registry.ResourceInstance;
import org.apache.wink.server.internal.resources.HtmlServiceDocumentResource;
import org.apache.wink.server.internal.resources.RootResource;
import org.apache.wink.server.utils.RegistrationUtils;
@@ -150,7 +154,7 @@ public class RequestProcessor {
private void handleRequestWithoutFaultBarrier(HttpServletRequest request,
HttpServletResponse response) throws Throwable {
-
+ boolean isReleaseResourcesCalled = false;
try {
ServerMessageContext msgContext = createMessageContext(request, response);
RuntimeContextTLS.setRuntimeContext(msgContext);
@@ -162,23 +166,69 @@ public class RequestProcessor {
msgContext);
// run the response handler chain
configuration.getResponseHandlersChain().run(msgContext);
+
+ logger.debug("Attempting to release resource instance");
+ isReleaseResourcesCalled = true;
+ try {
+ releaseResources(msgContext);
+ } catch (Exception e) {
+ logger.debug("Caught exception when releasing resource object", e);
+ throw e;
+ }
} catch (Throwable t) {
- logException(t);
- ServerMessageContext msgContext = createMessageContext(request, response);
- RuntimeContextTLS.setRuntimeContext(msgContext);
- msgContext.setResponseEntity(t);
- // run the error handler chain
- logger.debug("Exception occured, starting error handlers chain: {}", msgContext); //$NON-NLS-1$
- configuration.getErrorHandlersChain().run(msgContext);
+ RuntimeContext originalContext = RuntimeContextTLS.getRuntimeContext();
+ ServerMessageContext msgContext = null;
+ try {
+ logException(t);
+ msgContext = createMessageContext(request, response);
+ RuntimeContextTLS.setRuntimeContext(msgContext);
+ msgContext.setResponseEntity(t);
+ // run the error handler chain
+ logger.debug("Exception occured, starting error handlers chain: {}", msgContext); //$NON-NLS-1$
+ configuration.getErrorHandlersChain().run(msgContext);
+
+ RuntimeContextTLS.setRuntimeContext(originalContext);
+ if (!isReleaseResourcesCalled) {
+ isReleaseResourcesCalled = true;
+ try {
+ releaseResources(originalContext);
+ } catch (Exception e2) {
+ logger.debug("Caught exception when releasing resource object", e2);
+ }
+ }
+ } catch (Exception e) {
+ RuntimeContextTLS.setRuntimeContext(originalContext);
+ if (!isReleaseResourcesCalled) {
+ isReleaseResourcesCalled = true;
+ try {
+ releaseResources(originalContext);
+ } catch (Exception e2) {
+ logger.debug("Caught exception when releasing resource object", e2);
+ }
+ }
+ throw e;
+ }
} finally {
logger.debug("Finished response handlers chain"); //$NON-NLS-1$
RuntimeContextTLS.setRuntimeContext(null);
}
}
+ private void releaseResources(RuntimeContext msgContext) throws Exception {
+ SearchResult searchResult = msgContext.getAttribute(SearchResult.class);
+ if (searchResult != null) {
+ List<ResourceInstance> resourceInstances = searchResult.getData().getMatchedResources();
+ for (ResourceInstance res : resourceInstances) {
+ logger.debug("Releasing resource instance");
+ res.releaseInstance(msgContext);
+ }
+ }
+ }
+
private void logException(Throwable t) {
String exceptionName = t.getClass().getSimpleName();
- String messageFormat = Messages.getMessage("exceptionOccurredDuringInvocation", exceptionName);
+ String messageFormat =
+ Messages.getMessage("exceptionOccurredDuringInvocation", exceptionName);
if (t instanceof WebApplicationException) {
WebApplicationException wae = (WebApplicationException)t;
int statusCode = wae.getResponse().getStatus();
Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindRootResourceHandler.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindRootResourceHandler.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindRootResourceHandler.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindRootResourceHandler.java Mon Jun 7 00:20:05 2010
@@ -38,7 +38,7 @@ import org.slf4j.LoggerFactory;
public class FindRootResourceHandler implements RequestHandler {
public static final String SEARCH_POLICY_CONTINUED_SEARCH_KEY =
- "wink.searchPolicyContinuedSearch"; //$NON-NLS-1$
+ "wink.searchPolicyContinuedSearch"; //$NON-NLS-1$
private static final Logger logger =
LoggerFactory
.getLogger(FindRootResourceHandler.class);
@@ -96,6 +96,15 @@ public class FindRootResourceHandler imp
if (result.isFound()) {
break;
}
+
+ // if the search result was unsuccessful, should automatically do
+ // the release on any root resources created (and any subresource
+ // instances used; the subresource is dead)
+ List<ResourceInstance> resourceInstances = result.getData().getMatchedResources();
+ for (ResourceInstance res : resourceInstances) {
+ logger.debug("Releasing resource instance");
+ res.releaseInstance(context);
+ }
}
}
Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceInstance.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceInstance.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceInstance.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceInstance.java Mon Jun 7 00:20:05 2010
@@ -81,6 +81,14 @@ public class ResourceInstance {
return instance;
}
+ @SuppressWarnings("unchecked")
+ public void releaseInstance(RuntimeContext context) {
+ if (instance != null) {
+ ObjectFactory of = record.getObjectFactory();
+ of.releaseInstance(instance, context);
+ }
+ }
+
/**
* Returns whether the matcher used to match this resource was an exact
* match
Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRecordFactory.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRecordFactory.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRecordFactory.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRecordFactory.java Mon Jun 7 00:20:05 2010
@@ -286,6 +286,14 @@ public class ResourceRecordFactory {
public String toString() {
return String.format("InstanceObjectFactory: %s", getInstanceClass()); //$NON-NLS-1$
}
+
+ public void releaseInstance(T instance, RuntimeContext context) {
+ /* do nothing */
+ }
+
+ public void releaseAll(RuntimeContext context) {
+ /* do nothing */
+ }
}
}
Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRegistry.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRegistry.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRegistry.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRegistry.java Mon Jun 7 00:20:05 2010
@@ -164,6 +164,22 @@ public class ResourceRegistry {
}
/**
+ * Removes all the root resource records.
+ */
+ public void removeAllResources() {
+ writersLock.lock();
+ try {
+ for (ResourceRecord record : rootResources) {
+ record.getObjectFactory().releaseAll(null);
+ }
+ rootResources.clear();
+ assertSorted();
+ } finally {
+ writersLock.unlock();
+ }
+ }
+
+ /**
* Get the {@link ResourceRecord} of the specified root resource instance.
* This is a shortcut for {@code getRecord(instance, true)}
*
Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestServlet.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestServlet.java?rev=952028&r1=952027&r2=952028&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestServlet.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestServlet.java Mon Jun 7 00:20:05 2010
@@ -251,4 +251,14 @@ public class RestServlet extends Abstrac
return properties;
}
+
+ @Override
+ public void destroy() {
+ getRequestProcessor().getConfiguration().getProvidersRegistry().removeAllProviders();
+ getRequestProcessor().getConfiguration().getResourceRegistry().removeAllResources();
+ /*
+ * Be sure to call super.destroy()
+ */
+ super.destroy();
+ }
}
Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle1Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle1Test.java?rev=952028&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle1Test.java (added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle1Test.java Mon Jun 7 00:20:05 2010
@@ -0,0 +1,208 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.wink.server.internal.lifecycle;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.wink.common.RuntimeContext;
+import org.apache.wink.common.internal.lifecycle.LifecycleManager;
+import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
+import org.apache.wink.common.internal.lifecycle.ObjectFactory;
+import org.apache.wink.server.internal.DeploymentConfiguration;
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@SuppressWarnings("unchecked")
+public class ProvidersLifecycle1Test extends MockServletInvocationTest {
+
+ public static class CustomDeploymentConfiguration extends DeploymentConfiguration {
+ public CustomDeploymentConfiguration() {
+ LifecycleManagersRegistry lifecycleManagersRegistry = new LifecycleManagersRegistry();
+ setOfFactoryRegistry(lifecycleManagersRegistry);
+ lifecycleManagersRegistry.addFactoryFactory(lifecycleManagerMock);
+ }
+ }
+
+ static Mockery ofMockery = new Mockery();
+
+ static LifecycleManager lifecycleManagerMock = ofMockery.mock(LifecycleManager.class);
+
+ static ObjectFactory messageBodyProviderOFMock =
+ ofMockery
+ .mock(ObjectFactory.class,
+ "messageBodyProviderOfFactory");
+
+ static ObjectFactory exceptionMapperOFMock =
+ ofMockery.mock(ObjectFactory.class,
+ "exceptionMapperOFMock");
+ static ObjectFactory contextResolverOFMock =
+ ofMockery.mock(ObjectFactory.class,
+ "contextResolverOFMock");
+
+ @Override
+ public String getDeploymentConfigurationClassName() {
+ return CustomDeploymentConfiguration.class.getName();
+ }
+
+ @Provider
+ public static class MyExceptionMapper implements ExceptionMapper<NullPointerException> {
+
+ public Response toResponse(NullPointerException arg0) {
+ return null;
+ }
+
+ }
+
+ @Provider
+ public static class MyContextResolver implements ContextResolver<Object> {
+
+ public Object getContext(Class<?> arg0) {
+ return null;
+ }
+
+ }
+ @Provider
+ public static class MessageBodyEntityProvider implements MessageBodyReader<String>,
+ MessageBodyWriter<String> {
+
+ public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
+ return true;
+ }
+
+ public String readFrom(Class<String> arg0,
+ Type arg1,
+ Annotation[] arg2,
+ MediaType arg3,
+ MultivaluedMap<String, String> arg4,
+ InputStream arg5) throws IOException, WebApplicationException {
+ return "Read overwrite whatever written";
+ }
+
+ public long getSize(String arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
+ return -1;
+ }
+
+ public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
+ return true;
+ }
+
+ public void writeTo(String arg0,
+ Class<?> arg1,
+ Type arg2,
+ Annotation[] arg3,
+ MediaType arg4,
+ MultivaluedMap<String, Object> arg5,
+ OutputStream arg6) throws IOException, WebApplicationException {
+ arg6.write((arg0 + "::write overwrite whatever written").getBytes());
+ }
+
+ }
+
+ static {
+ ofMockery.checking(new Expectations() {
+ {
+ exactly(2).of(messageBodyProviderOFMock)
+ .getInstance(with(any(RuntimeContext.class)));
+ will(returnValue(new MessageBodyEntityProvider()));
+
+ allowing(messageBodyProviderOFMock).getInstanceClass();
+ will(returnValue(MessageBodyEntityProvider.class));
+
+ allowing(exceptionMapperOFMock).getInstanceClass();
+ will(returnValue(MyExceptionMapper.class));
+
+ allowing(contextResolverOFMock).getInstanceClass();
+ will(returnValue(MyContextResolver.class));
+
+ oneOf(lifecycleManagerMock).createObjectFactory(MessageBodyEntityProvider.class);
+ will(returnValue(messageBodyProviderOFMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(MyExceptionMapper.class);
+ will(returnValue(exceptionMapperOFMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(MyContextResolver.class);
+ will(returnValue(contextResolverOFMock));
+
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Class.class)));
+ will(returnValue(null));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Object.class)));
+ will(returnValue(null));
+ }
+ });
+ }
+
+ @Path("myresource")
+ public static class MyResource {
+ @POST
+ public String getHello(String entity) {
+ return entity;
+ }
+ }
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class[] {MyResource.class, MessageBodyEntityProvider.class,
+ MyExceptionMapper.class, MyContextResolver.class};
+ }
+
+ /**
+ * Tests that the resource ObjectFactory's removeInstance() call is made
+ * when the ObjectFactory represents a regular request scoped resource.
+ *
+ * @throws Throwable
+ */
+ public void testResourceRemoveInstanceCalled() throws Throwable {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/myresource",
+ "text/plain",
+ "text/plain",
+ "hello".getBytes());
+ try {
+ MockHttpServletResponse response = invoke(request);
+ assertTrue(response.getStatus() == Status.OK.getStatusCode());
+ assertEquals("Read overwrite whatever written::write overwrite whatever written",
+ response.getContentAsString());
+ ofMockery.assertIsSatisfied();
+ } catch (ServletException e) {
+ throw e.getRootCause();
+ }
+ }
+}
Propchange: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle1Test.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle2Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle2Test.java?rev=952028&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle2Test.java (added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle2Test.java Mon Jun 7 00:20:05 2010
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.wink.server.internal.lifecycle;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.wink.common.RuntimeContext;
+import org.apache.wink.common.internal.lifecycle.LifecycleManager;
+import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
+import org.apache.wink.common.internal.lifecycle.ObjectFactory;
+import org.apache.wink.server.internal.DeploymentConfiguration;
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@SuppressWarnings("unchecked")
+public class ProvidersLifecycle2Test extends MockServletInvocationTest {
+
+ public static class CustomDeploymentConfiguration extends DeploymentConfiguration {
+ public CustomDeploymentConfiguration() {
+ LifecycleManagersRegistry lifecycleManagersRegistry = new LifecycleManagersRegistry();
+ setOfFactoryRegistry(lifecycleManagersRegistry);
+ lifecycleManagersRegistry.addFactoryFactory(lifecycleManagerMock);
+ }
+ }
+
+ static Mockery ofMockery = new Mockery();
+
+ static LifecycleManager lifecycleManagerMock = ofMockery.mock(LifecycleManager.class);
+
+ static ObjectFactory messageBodyProviderOFMock =
+ ofMockery
+ .mock(ObjectFactory.class,
+ "messageBodyProviderOfFactory");
+
+ static ObjectFactory exceptionMapperOFMock =
+ ofMockery.mock(ObjectFactory.class,
+ "exceptionMapperOFMock");
+ static ObjectFactory contextResolverOFMock =
+ ofMockery.mock(ObjectFactory.class,
+ "contextResolverOFMock");
+
+ @Override
+ public String getDeploymentConfigurationClassName() {
+ return CustomDeploymentConfiguration.class.getName();
+ }
+
+ @Provider
+ public static class MyExceptionMapper implements ExceptionMapper<NullPointerException> {
+
+ public Response toResponse(NullPointerException arg0) {
+ return null;
+ }
+
+ }
+
+ @Provider
+ public static class MyContextResolver implements ContextResolver<Object> {
+
+ public Object getContext(Class<?> arg0) {
+ return null;
+ }
+
+ }
+
+ @Provider
+ public static class MessageBodyEntityProvider implements MessageBodyReader<String>,
+ MessageBodyWriter<String> {
+
+ public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
+ return true;
+ }
+
+ public String readFrom(Class<String> arg0,
+ Type arg1,
+ Annotation[] arg2,
+ MediaType arg3,
+ MultivaluedMap<String, String> arg4,
+ InputStream arg5) throws IOException, WebApplicationException {
+ return "Read overwrite whatever written";
+ }
+
+ public long getSize(String arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
+ return -1;
+ }
+
+ public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
+ return true;
+ }
+
+ public void writeTo(String arg0,
+ Class<?> arg1,
+ Type arg2,
+ Annotation[] arg3,
+ MediaType arg4,
+ MultivaluedMap<String, Object> arg5,
+ OutputStream arg6) throws IOException, WebApplicationException {
+ arg6.write((arg0 + "::write overwrite whatever written").getBytes());
+ }
+
+ }
+
+ static {
+ ofMockery.checking(new Expectations() {
+ {
+ exactly(2).of(messageBodyProviderOFMock)
+ .getInstance(with(any(RuntimeContext.class)));
+ will(returnValue(new MessageBodyEntityProvider()));
+
+ allowing(messageBodyProviderOFMock).getInstanceClass();
+ will(returnValue(MessageBodyEntityProvider.class));
+
+ exactly(2).of(messageBodyProviderOFMock).releaseAll(null);
+
+ allowing(exceptionMapperOFMock).getInstanceClass();
+ will(returnValue(MyExceptionMapper.class));
+
+ oneOf(exceptionMapperOFMock).releaseAll(null);
+
+ allowing(contextResolverOFMock).getInstanceClass();
+ will(returnValue(MyContextResolver.class));
+
+ oneOf(contextResolverOFMock).releaseAll(null);
+
+ oneOf(lifecycleManagerMock).createObjectFactory(MessageBodyEntityProvider.class);
+ will(returnValue(messageBodyProviderOFMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(MyExceptionMapper.class);
+ will(returnValue(exceptionMapperOFMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(MyContextResolver.class);
+ will(returnValue(contextResolverOFMock));
+
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Class.class)));
+ will(returnValue(null));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Object.class)));
+ will(returnValue(null));
+ }
+ });
+ }
+
+ @Path("myresource")
+ public static class MyResource {
+ @POST
+ public String getHello(String entity) {
+ return entity;
+ }
+ }
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class[] {MyResource.class, MessageBodyEntityProvider.class,
+ MyExceptionMapper.class, MyContextResolver.class};
+ }
+
+ /**
+ * Tests that the resource ObjectFactory's removeInstance() call is made
+ * when the ObjectFactory represents a regular request scoped resource.
+ *
+ * @throws Throwable
+ */
+ public void testResourceRemoveInstanceCalled() throws Throwable {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/myresource",
+ "text/plain",
+ "text/plain",
+ "hello".getBytes());
+ try {
+ MockHttpServletResponse response = invoke(request);
+ assertTrue(response.getStatus() == Status.OK.getStatusCode());
+ assertEquals("Read overwrite whatever written::write overwrite whatever written",
+ response.getContentAsString());
+ getServlet().destroy();
+ ofMockery.assertIsSatisfied();
+ } catch (ServletException e) {
+ throw e.getRootCause();
+ }
+ }
+}
Propchange: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ProvidersLifecycle2Test.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle.java?rev=952028&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle.java (added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle.java Mon Jun 7 00:20:05 2010
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.wink.server.internal.lifecycle;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+
+public class ResourceLifecycle extends MockServletInvocationTest {
+
+ @Path("/resource1")
+ public static class Resource1 {
+ @GET
+ public String hello() {
+ return "resource1";
+ }
+ }
+
+ @Path("/resource2")
+ public static class Resource2 {
+ @GET
+ public String hello2() {
+ return "resource2";
+ }
+ }
+
+ @Path("/resource3")
+ public static class Resource3 {
+ @GET
+ public String hello3() {
+ return "resource3";
+ }
+ }
+}
Propchange: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle1Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle1Test.java?rev=952028&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle1Test.java (added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle1Test.java Mon Jun 7 00:20:05 2010
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.wink.server.internal.lifecycle;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.core.Response.Status;
+
+import org.apache.wink.common.RuntimeContext;
+import org.apache.wink.common.internal.lifecycle.LifecycleManager;
+import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
+import org.apache.wink.common.internal.lifecycle.ObjectFactory;
+import org.apache.wink.server.internal.DeploymentConfiguration;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@SuppressWarnings("unchecked")
+public class ResourceLifecycle1Test extends ResourceLifecycle {
+
+ public static class CustomDeploymentConfiguration extends DeploymentConfiguration {
+ public CustomDeploymentConfiguration() {
+ LifecycleManagersRegistry lifecycleManagersRegistry = new LifecycleManagersRegistry();
+ setOfFactoryRegistry(lifecycleManagersRegistry);
+ lifecycleManagersRegistry.addFactoryFactory(lifecycleManagerMock);
+ }
+ }
+
+ static Mockery ofMockery = new Mockery();
+
+ static LifecycleManager lifecycleManagerMock = ofMockery.mock(LifecycleManager.class);
+
+ static ObjectFactory res1OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res1OfMock");
+
+ static ObjectFactory res2OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res2OfMock");
+ static ObjectFactory res3OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res3OfMock");
+
+ @Override
+ public String getDeploymentConfigurationClassName() {
+ return CustomDeploymentConfiguration.class.getName();
+ }
+
+ static {
+ ofMockery.checking(new Expectations() {
+ {
+ Resource2 res2 = new Resource2();
+
+ oneOf(res2OfMock).getInstance(with(any(RuntimeContext.class)));
+ will(returnValue(res2));
+
+ // expectation is 1 release call during invocation
+ oneOf(res2OfMock).releaseInstance(with(res2), with(any(RuntimeContext.class)));
+
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource1.class);
+ will(returnValue(res1OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource2.class);
+ will(returnValue(res2OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource3.class);
+ will(returnValue(res3OfMock));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Class.class)));
+ will(returnValue(null));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Object.class)));
+ will(returnValue(null));
+ }
+ });
+ }
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class[] {Resource1.class, Resource2.class, Resource3.class};
+ }
+
+ /**
+ * Tests that the resource ObjectFactory's removeInstance() call is made
+ * when the ObjectFactory represents a regular request scoped resource.
+ *
+ * @throws Throwable
+ */
+ public void testResourceRemoveInstanceCalled() throws Throwable {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET", "/resource2", "text/plain");
+ try {
+ MockHttpServletResponse response = invoke(request);
+ assertTrue(response.getStatus() == Status.OK.getStatusCode());
+ assertEquals("resource2", response.getContentAsString());
+ ofMockery.assertIsSatisfied();
+ } catch (ServletException e) {
+ throw e.getRootCause();
+ }
+ }
+
+}
Propchange: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle1Test.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle2Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle2Test.java?rev=952028&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle2Test.java (added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle2Test.java Mon Jun 7 00:20:05 2010
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.wink.server.internal.lifecycle;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.core.Response.Status;
+
+import org.apache.wink.common.RuntimeContext;
+import org.apache.wink.common.internal.lifecycle.LifecycleManager;
+import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
+import org.apache.wink.common.internal.lifecycle.ObjectFactory;
+import org.apache.wink.server.internal.DeploymentConfiguration;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@SuppressWarnings("unchecked")
+public class ResourceLifecycle2Test extends ResourceLifecycle {
+
+ public static class CustomDeploymentConfiguration extends DeploymentConfiguration {
+ public CustomDeploymentConfiguration() {
+ LifecycleManagersRegistry lifecycleManagersRegistry = new LifecycleManagersRegistry();
+ setOfFactoryRegistry(lifecycleManagersRegistry);
+ lifecycleManagersRegistry.addFactoryFactory(lifecycleManagerMock);
+ }
+ }
+
+ static Mockery ofMockery = new Mockery();
+
+ static LifecycleManager lifecycleManagerMock = ofMockery.mock(LifecycleManager.class);
+
+ static ObjectFactory res1OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res1OfMock");
+
+ static ObjectFactory res2OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res2OfMock");
+ static ObjectFactory res3OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res3OfMock");
+
+ @Override
+ public String getDeploymentConfigurationClassName() {
+ return CustomDeploymentConfiguration.class.getName();
+ }
+
+ static {
+ ofMockery.checking(new Expectations() {
+ {
+ Resource2 res2 = new Resource2();
+
+ oneOf(res2OfMock).getInstance(with(any(RuntimeContext.class)));
+ will(returnValue(res2));
+
+ // expectation is 1 release call during invocation
+ oneOf(res2OfMock).releaseInstance(with(res2), with(any(RuntimeContext.class)));
+
+ // expectation is every resource object factory will eventually
+ // have removeAll called
+ oneOf(res1OfMock).releaseAll(null);
+ oneOf(res2OfMock).releaseAll(null);
+ oneOf(res3OfMock).releaseAll(null);
+
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource1.class);
+ will(returnValue(res1OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource2.class);
+ will(returnValue(res2OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource3.class);
+ will(returnValue(res3OfMock));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Class.class)));
+ will(returnValue(null));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Object.class)));
+ will(returnValue(null));
+ }
+ });
+ }
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class[] {Resource1.class, Resource2.class, Resource3.class};
+ }
+
+ /**
+ * Tests that the resource ObjectFactory's removeAll call is made when the
+ * servlet is destroyed and when the ObjectFactory represents a regular request
+ * scoped resource.
+ *
+ * @throws Throwable
+ */
+ public void testResourceRemoveAllCalled() throws Throwable {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET", "/resource2", "text/plain");
+ try {
+ MockHttpServletResponse response = invoke(request);
+ assertTrue(response.getStatus() == Status.OK.getStatusCode());
+ assertEquals("resource2", response.getContentAsString());
+ getServlet().destroy();
+ ofMockery.assertIsSatisfied();
+ } catch (ServletException e) {
+ throw e.getRootCause();
+ }
+ }
+
+}
Propchange: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle2Test.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle3Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle3Test.java?rev=952028&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle3Test.java (added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle3Test.java Mon Jun 7 00:20:05 2010
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.wink.server.internal.lifecycle;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.core.Response.Status;
+
+import org.apache.wink.common.RuntimeContext;
+import org.apache.wink.common.internal.lifecycle.LifecycleManager;
+import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
+import org.apache.wink.common.internal.lifecycle.ObjectFactory;
+import org.apache.wink.server.internal.DeploymentConfiguration;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@SuppressWarnings("unchecked")
+public class ResourceLifecycle3Test extends ResourceLifecycle {
+
+ public static class CustomDeploymentConfiguration extends DeploymentConfiguration {
+ public CustomDeploymentConfiguration() {
+ LifecycleManagersRegistry lifecycleManagersRegistry = new LifecycleManagersRegistry();
+ setOfFactoryRegistry(lifecycleManagersRegistry);
+ lifecycleManagersRegistry.addFactoryFactory(lifecycleManagerMock);
+ }
+ }
+
+ static Mockery ofMockery = new Mockery();
+
+ static LifecycleManager lifecycleManagerMock = ofMockery.mock(LifecycleManager.class);
+
+ static ObjectFactory res1OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res1OfMock");
+
+ static ObjectFactory res2OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res2OfMock");
+ static ObjectFactory res3OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res3OfMock");
+
+ @Override
+ public String getDeploymentConfigurationClassName() {
+ return CustomDeploymentConfiguration.class.getName();
+ }
+
+ static Resource1 res1 = new Resource1();
+ static Resource2 res2 = new Resource2();
+ static Resource3 res3 = new Resource3();
+
+ static {
+ ofMockery.checking(new Expectations() {
+ {
+
+ oneOf(res2OfMock).getInstance(with(any(RuntimeContext.class)));
+ will(returnValue(res2));
+
+ // expectation is 1 release call during invocation
+ oneOf(res2OfMock).releaseInstance(with(res2), with(any(RuntimeContext.class)));
+
+ oneOf(lifecycleManagerMock).createObjectFactory(res1);
+ will(returnValue(res1OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(res2);
+ will(returnValue(res2OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(res3);
+ will(returnValue(res3OfMock));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Class.class)));
+ will(returnValue(null));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Object.class)));
+ will(returnValue(null));
+ }
+ });
+ }
+
+ @Override
+ protected Object[] getSingletons() {
+ return new Object[] {res1, res2, res3};
+ }
+
+ /**
+ * Tests that the resource ObjectFactory's removeInstance call is made when
+ * the ObjectFactory represents a singleton scoped resource.
+ *
+ * @throws Throwable
+ */
+ public void testResourceRemoveAllCalled() throws Throwable {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET", "/resource2", "text/plain");
+ try {
+ MockHttpServletResponse response = invoke(request);
+ assertTrue(response.getStatus() == Status.OK.getStatusCode());
+ assertEquals("resource2", response.getContentAsString());
+ ofMockery.assertIsSatisfied();
+ } catch (ServletException e) {
+ throw e.getRootCause();
+ }
+ }
+}
Propchange: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle3Test.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle4Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle4Test.java?rev=952028&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle4Test.java (added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle4Test.java Mon Jun 7 00:20:05 2010
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.wink.server.internal.lifecycle;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.core.Response.Status;
+
+import org.apache.wink.common.RuntimeContext;
+import org.apache.wink.common.internal.lifecycle.LifecycleManager;
+import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
+import org.apache.wink.common.internal.lifecycle.ObjectFactory;
+import org.apache.wink.server.internal.DeploymentConfiguration;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@SuppressWarnings("unchecked")
+public class ResourceLifecycle4Test extends ResourceLifecycle {
+
+ public static class CustomDeploymentConfiguration extends DeploymentConfiguration {
+ public CustomDeploymentConfiguration() {
+ LifecycleManagersRegistry lifecycleManagersRegistry = new LifecycleManagersRegistry();
+ setOfFactoryRegistry(lifecycleManagersRegistry);
+ lifecycleManagersRegistry.addFactoryFactory(lifecycleManagerMock);
+ }
+ }
+
+ static Mockery ofMockery = new Mockery();
+
+ static LifecycleManager lifecycleManagerMock = ofMockery.mock(LifecycleManager.class);
+
+ static ObjectFactory res1OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res1OfMock");
+
+ static ObjectFactory res2OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res2OfMock");
+ static ObjectFactory res3OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res3OfMock");
+
+ @Override
+ public String getDeploymentConfigurationClassName() {
+ return CustomDeploymentConfiguration.class.getName();
+ }
+
+ static Resource1 res1 = new Resource1();
+ static Resource2 res2 = new Resource2();
+ static Resource3 res3 = new Resource3();
+
+ static {
+ ofMockery.checking(new Expectations() {
+ {
+
+ oneOf(res2OfMock).getInstance(with(any(RuntimeContext.class)));
+ will(returnValue(res2));
+
+ // expectation is 1 release call during invocation
+ oneOf(res2OfMock).releaseInstance(with(res2), with(any(RuntimeContext.class)));
+
+ // expectation is every resource object factory will eventually
+ // have removeAll called
+ oneOf(res1OfMock).releaseAll(null);
+ oneOf(res2OfMock).releaseAll(null);
+ oneOf(res3OfMock).releaseAll(null);
+
+ oneOf(lifecycleManagerMock).createObjectFactory(res1);
+ will(returnValue(res1OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(res2);
+ will(returnValue(res2OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(res3);
+ will(returnValue(res3OfMock));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Class.class)));
+ will(returnValue(null));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Object.class)));
+ will(returnValue(null));
+ }
+ });
+ }
+
+ @Override
+ protected Object[] getSingletons() {
+ return new Object[] {res1, res2, res3};
+ }
+
+ /**
+ * Tests that the resource ObjectFactory's removeAll call is made when the
+ * servlet is destroyed and when the ObjectFactory represents a singleton scoped
+ * resource.
+ *
+ * @throws Throwable
+ */
+ public void testResourceRemoveAllCalled() throws Throwable {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET", "/resource2", "text/plain");
+ try {
+ MockHttpServletResponse response = invoke(request);
+ assertTrue(response.getStatus() == Status.OK.getStatusCode());
+ assertEquals("resource2", response.getContentAsString());
+ getServlet().destroy();
+ ofMockery.assertIsSatisfied();
+ } catch (ServletException e) {
+ throw e.getRootCause();
+ }
+ }
+}
Propchange: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle4Test.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle5Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle5Test.java?rev=952028&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle5Test.java (added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle5Test.java Mon Jun 7 00:20:05 2010
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.wink.server.internal.lifecycle;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.wink.common.RuntimeContext;
+import org.apache.wink.common.internal.lifecycle.LifecycleManager;
+import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
+import org.apache.wink.common.internal.lifecycle.ObjectFactory;
+import org.apache.wink.server.internal.DeploymentConfiguration;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.Sequence;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@SuppressWarnings("unchecked")
+public class ResourceLifecycle5Test extends ResourceLifecycle {
+
+ public static class CustomDeploymentConfiguration extends DeploymentConfiguration {
+ public CustomDeploymentConfiguration() {
+ LifecycleManagersRegistry lifecycleManagersRegistry = new LifecycleManagersRegistry();
+ setOfFactoryRegistry(lifecycleManagersRegistry);
+ lifecycleManagersRegistry.addFactoryFactory(lifecycleManagerMock);
+ }
+ }
+
+ static Mockery ofMockery = new Mockery();
+
+ static LifecycleManager lifecycleManagerMock = ofMockery.mock(LifecycleManager.class);
+
+ static ObjectFactory res1OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res1OfMock");
+
+ static ObjectFactory res2OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res2OfMock");
+ static ObjectFactory res3OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res3OfMock");
+
+ static ObjectFactory exceptionResOfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "exceptionResOfMock");
+
+ static ExceptionMapper exceptionMapperMock = ofMockery.mock(ExceptionMapper.class);
+
+ @Provider
+ public static class MyExceptionMapper implements ExceptionMapper<WebApplicationException> {
+
+ public Response toResponse(WebApplicationException arg0) {
+ return exceptionMapperMock.toResponse(arg0);
+ }
+ }
+
+ @Path("exception1")
+ public static class MyExceptionResource {
+ @GET
+ public String helloException() {
+ throw new WebApplicationException();
+ }
+ }
+
+ @Override
+ public String getDeploymentConfigurationClassName() {
+ return CustomDeploymentConfiguration.class.getName();
+ }
+
+ static {
+ ofMockery.checking(new Expectations() {
+ {
+ MyExceptionResource exceptionRes = new MyExceptionResource();
+
+ final Sequence releaseSequence = ofMockery.sequence("sequence-name");
+
+ oneOf(exceptionResOfMock).getInstance(with(any(RuntimeContext.class)));
+ will(returnValue(exceptionRes));
+ inSequence(releaseSequence);
+
+ oneOf(exceptionMapperMock).toResponse(with(any(WebApplicationException.class)));
+ will(returnValue(Response.status(Status.BAD_REQUEST)
+ .entity("exception mapper mock").type(MediaType.TEXT_PLAIN_TYPE).build()));
+ inSequence(releaseSequence);
+
+ // expectation is 1 release call during invocation after
+ // exception mapper is already called
+ oneOf(exceptionResOfMock).releaseInstance(with(exceptionRes),
+ with(any(RuntimeContext.class)));
+ inSequence(releaseSequence);
+
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource1.class);
+ will(returnValue(res1OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource2.class);
+ will(returnValue(res2OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource3.class);
+ will(returnValue(res3OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(MyExceptionResource.class);
+ will(returnValue(exceptionResOfMock));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Class.class)));
+ will(returnValue(null));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Object.class)));
+ will(returnValue(null));
+ }
+ });
+ }
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class[] {Resource1.class, Resource2.class, Resource3.class,
+ MyExceptionResource.class, MyExceptionMapper.class};
+ }
+
+ /**
+ * Tests that the resource ObjectFactory's removeInstance call is made when
+ * an unhandled exception occurs during the request processing. Also, the
+ * removeInstance call should be made after the response processing is done.
+ *
+ * @throws Throwable
+ */
+ public void testResourceRemoveInstanceCalled() throws Throwable {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET", "/exception1", "text/plain");
+ try {
+ MockHttpServletResponse response = invoke(request);
+ assertTrue(response.getStatus() == Status.BAD_REQUEST.getStatusCode());
+ assertEquals("exception mapper mock", response.getContentAsString());
+ ofMockery.assertIsSatisfied();
+ } catch (ServletException e) {
+ throw e.getRootCause();
+ }
+ }
+
+}
Propchange: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle5Test.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle6Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle6Test.java?rev=952028&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle6Test.java (added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle6Test.java Mon Jun 7 00:20:05 2010
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.wink.server.internal.lifecycle;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.wink.common.RuntimeContext;
+import org.apache.wink.common.internal.lifecycle.LifecycleManager;
+import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
+import org.apache.wink.common.internal.lifecycle.ObjectFactory;
+import org.apache.wink.server.internal.DeploymentConfiguration;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.Sequence;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@SuppressWarnings("unchecked")
+public class ResourceLifecycle6Test extends ResourceLifecycle {
+
+ public static class CustomDeploymentConfiguration extends DeploymentConfiguration {
+ public CustomDeploymentConfiguration() {
+ LifecycleManagersRegistry lifecycleManagersRegistry = new LifecycleManagersRegistry();
+ setOfFactoryRegistry(lifecycleManagersRegistry);
+ lifecycleManagersRegistry.addFactoryFactory(lifecycleManagerMock);
+ }
+ }
+
+ static Mockery ofMockery = new Mockery();
+
+ static LifecycleManager lifecycleManagerMock = ofMockery.mock(LifecycleManager.class);
+
+ static ObjectFactory res1OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res1OfMock");
+
+ static ObjectFactory res2OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res2OfMock");
+ static ObjectFactory res3OfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "res3OfMock");
+
+ static ObjectFactory exceptionResOfMock =
+ ofMockery.mock(ObjectFactory.class,
+ "exceptionResOfMock");
+
+ static ExceptionMapper exceptionMapperMock = ofMockery.mock(ExceptionMapper.class);
+
+ @Provider
+ public static class MyExceptionMapper implements ExceptionMapper<NullPointerException> {
+
+ public Response toResponse(NullPointerException arg0) {
+ return exceptionMapperMock.toResponse(arg0);
+ }
+ }
+
+ @Path("exception1")
+ public static class MyExceptionResource {
+ @GET
+ public String helloException() {
+ throw new NullPointerException();
+ }
+ }
+
+ @Override
+ public String getDeploymentConfigurationClassName() {
+ return CustomDeploymentConfiguration.class.getName();
+ }
+
+ static {
+ ofMockery.checking(new Expectations() {
+ {
+ MyExceptionResource exceptionRes = new MyExceptionResource();
+
+ final Sequence releaseSequence = ofMockery.sequence("sequence-name");
+
+ oneOf(exceptionResOfMock).getInstance(with(any(RuntimeContext.class)));
+ will(returnValue(exceptionRes));
+ inSequence(releaseSequence);
+
+ oneOf(exceptionMapperMock).toResponse(with(any(NullPointerException.class)));
+ will(throwException(new NullPointerException()));
+ inSequence(releaseSequence);
+
+ // expectation is 1 release call during invocation after
+ // exception mapper is already called
+ oneOf(exceptionResOfMock).releaseInstance(with(exceptionRes),
+ with(any(RuntimeContext.class)));
+ inSequence(releaseSequence);
+
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource1.class);
+ will(returnValue(res1OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource2.class);
+ will(returnValue(res2OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(Resource3.class);
+ will(returnValue(res3OfMock));
+ oneOf(lifecycleManagerMock).createObjectFactory(MyExceptionResource.class);
+ will(returnValue(exceptionResOfMock));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Class.class)));
+ will(returnValue(null));
+ allowing(lifecycleManagerMock).createObjectFactory(with(any(Object.class)));
+ will(returnValue(null));
+ }
+ });
+ }
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class[] {Resource1.class, Resource2.class, Resource3.class,
+ MyExceptionResource.class, MyExceptionMapper.class};
+ }
+
+ /**
+ * Tests that the resource ObjectFactory's remove call is still made when an
+ * ExceptionMapper throws an exception. Also, the removeInstance call should
+ * be made after the response processing is done.
+ *
+ * @throws Throwable
+ */
+ public void testResourceRemoveInstanceCalled() throws Throwable {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET", "/exception1", "text/plain");
+ MockHttpServletResponse response = null;
+ try {
+ response = invoke(request);
+
+ assertTrue(response.getStatus() == Status.INTERNAL_SERVER_ERROR.getStatusCode());
+ assertEquals("", response.getContentAsString());
+ ofMockery.assertIsSatisfied();
+ } catch (ServletException e) {
+ throw e.getRootCause();
+ }
+ }
+
+}
Propchange: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/lifecycle/ResourceLifecycle6Test.java
------------------------------------------------------------------------------
svn:eol-style = native