Commits

Anonymous committed fc0cd61

- XW-523 (eliminate the use of static block to call ConfigurationManager's destroyConfiguration() method)
- XW-522 (Interceptor destroy() method is never called.)

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/xwork_1-2@1533e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

src/java/com/opensymphony/xwork/XWorkTestCase.java

  * Base JUnit TestCase to extend for XWork specific unit tests.
  *
  * @author plightbo
+ * @author tmjee
+ * 
+ * @version $Date$ $Id$
  */
 public abstract class XWorkTestCase extends TestCase {
     
 
         //  clear out configuration
         ConfigurationManager.destroyConfiguration();
+        ConfigurationManager.setConfiguration(null);
 
         // clear out localization
         LocalizedTextUtil.reset();
         
         //  clear out configuration
         ConfigurationManager.destroyConfiguration();
+        ConfigurationManager.setConfiguration(null);
     }
 }

src/java/com/opensymphony/xwork/config/ConfigurationManager.java

 package com.opensymphony.xwork.config;
 
 import com.opensymphony.util.FileManager;
+import com.opensymphony.xwork.config.entities.ActionConfig;
+import com.opensymphony.xwork.config.entities.InterceptorMapping;
 import com.opensymphony.xwork.config.impl.DefaultConfiguration;
 import com.opensymphony.xwork.config.providers.XmlConfigurationProvider;
+import com.opensymphony.xwork.interceptor.Interceptor;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 
 /**
     protected static Configuration configurationInstance;
     private static List configurationProviders = new ArrayList();
 
-    static {
-        destroyConfiguration();
-    }
-
-
+    
     private ConfigurationManager() {
     }
 
         	clearConfigurationProviders(); // let's destroy the ConfigurationProvider first
             configurationProviders = new ArrayList();
             
-            if (configurationInstance != null)
+            if (configurationInstance != null) {
+            	/*  let's destroy the interceptors used. */
+            	
+            	// keeps a copy of the interceptor that we've destroyed before, so we don't destroy them twice
+            	List destroyedInterceptors = new ArrayList();  
+            	Map namespacesMap = configurationInstance.getRuntimeConfiguration().getActionConfigs();
+            	if (namespacesMap != null) {
+            		for (Iterator i = namespacesMap.entrySet().iterator(); i.hasNext(); ) {
+            			Map.Entry namespaceMapEntry = (Map.Entry) i.next();
+            			String namespace = (String) namespaceMapEntry.getKey();
+            			Map actionConfigsMap = (Map) namespaceMapEntry.getValue();
+            		
+            			for (Iterator ii = actionConfigsMap.entrySet().iterator(); ii.hasNext(); ) {
+            				Map.Entry actionConfigMapEntry = (Map.Entry) ii.next();
+            				String actionName = (String) actionConfigMapEntry.getKey();
+            				ActionConfig actionConfig = (ActionConfig) actionConfigMapEntry.getValue();
+            			
+            				List interceptorMappings = actionConfig.getInterceptors();
+            				for (Iterator iii = interceptorMappings.iterator(); iii.hasNext(); ) {
+            					InterceptorMapping interceptorMapping = (InterceptorMapping) iii.next();
+            					if (interceptorMapping != null && interceptorMapping.getInterceptor() != null) {
+            						Interceptor interceptor = interceptorMapping.getInterceptor();
+            						if (! destroyedInterceptors.contains(interceptor)) {
+            							destroyedInterceptors.add(interceptor);  // keep a copy so we know we've destroy this interceptor before.
+            							try {
+            								if (LOG.isDebugEnabled())
+            									LOG.debug("destroying interceptor ["+interceptor+"] registered under name ["+interceptorMapping.getName()+"] with namespace ["+namespace+"] in package ["+actionConfig.getPackageName()+"]");
+            								interceptor.destroy();
+            							}
+            							catch(Exception e) {
+            								// we don't want a bad interceptor to cause the whole destoy process to blow 
+            								// everything up, if this happens, just inform the user by logging the error
+            								LOG.error("an exception occurred while calling destroy() method on interceptor ["+interceptor+"] registered under name ["+interceptorMapping.getName()+"] with namespace ["+namespace+"] in package ["+actionConfig.getPackageName()+"]");
+            							}
+            						}
+            					}
+            				}
+            			}
+            		}
+            	}
+            	
             	configurationInstance.destroy(); // let's destroy it first, before nulling it.
+            }
             configurationInstance = null;
         }
     }

src/test/com/opensymphony/xwork/config/ConfigurationManagerTest.java

  */
 package com.opensymphony.xwork.config;
 
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+
 import org.easymock.MockControl;
 
 import com.mockobjects.dynamic.C;
 import com.mockobjects.dynamic.Mock;
 import com.opensymphony.util.FileManager;
-import junit.framework.TestCase;
-
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.ActionSupport;
+import com.opensymphony.xwork.XWorkTestCase;
+import com.opensymphony.xwork.config.entities.ActionConfig;
+import com.opensymphony.xwork.config.entities.InterceptorMapping;
+import com.opensymphony.xwork.interceptor.Interceptor;
 
 /**
  * ConfigurationManagerTest
  *
  * @author Jason Carreira
- *         Created May 6, 2003 10:59:59 PM
+ * @author tmjee
+ * 
+ * @version $Date$ $Id$
  */
-public class ConfigurationManagerTest extends TestCase {
+public class ConfigurationManagerTest extends XWorkTestCase {
 
     Mock configProviderMock;
 
     	Configuration configuration = (Configuration) control.getMock();
     	ConfigurationManager.setConfiguration(configuration);
     	
+    	control.expectAndDefaultReturn(
+    			configuration.getRuntimeConfiguration(), 
+    			new RuntimeConfiguration() {
+					public ActionConfig getActionConfig(String namespace, String name) {
+						return null;
+					}
+					public Map getActionConfigs() {
+						return Collections.EMPTY_MAP;
+					}
+    				
+    			});
     	configuration.destroy();		// EasyMock
     	configProviderMock.expect("destroy");  // MockObject
     	control.replay();
     	control.verify();
     }
     
+    
+    
+    public void testDestroyInterceptors() throws Exception {
+    	
+    	final MockInterceptor interceptor1 = new MockInterceptor();
+    	final MockInterceptor interceptor2 = new MockInterceptor();
+    	final MockInterceptor interceptor3 = new MockInterceptor();
+    	final MockInterceptor interceptor4 = new MockInterceptor();
+    	final MockInterceptor interceptor5 = new MockInterceptor();
+    	final MockInterceptor interceptor6 = new MockInterceptor();
+    	final MockInterceptor interceptor7 = new MockInterceptor();
+    	final MockInterceptor interceptor8 = new MockInterceptor();
+    	final InterceptorMapping interceptorMapping1 = new InterceptorMapping("interceptor1", interceptor1);
+    	final InterceptorMapping interceptorMapping2 = new InterceptorMapping("interceptor2", interceptor2);
+    	final InterceptorMapping interceptorMapping3 = new InterceptorMapping("interceptor3", interceptor3);
+    	final InterceptorMapping interceptorMapping4 = new InterceptorMapping("interceptor4", interceptor4);
+    	final InterceptorMapping interceptorMapping5 = new InterceptorMapping("interceptor5", interceptor5);
+    	final InterceptorMapping interceptorMapping6 = new InterceptorMapping("interceptor6", interceptor6);
+    	final InterceptorMapping interceptorMapping7 = new InterceptorMapping("interceptor7", interceptor7);
+    	final InterceptorMapping interceptorMapping8 = new InterceptorMapping("interceptor8", interceptor8);
+    	
+    	
+    	MockControl control = MockControl.createControl(Configuration.class);
+    	Configuration configuration = (Configuration) control.getMock();
+    	ConfigurationManager.setConfiguration(configuration);
+    	
+    	control.expectAndDefaultReturn(
+    			configuration.getRuntimeConfiguration(), 
+    			new RuntimeConfiguration() {
+					public ActionConfig getActionConfig(String namespace, String name) {
+						return null;
+					}
+					public Map getActionConfigs() {
+						return new LinkedHashMap() {
+							private static final long serialVersionUID = 3898273938943025926L;
+							{
+								put("/namespace1",  
+										new LinkedHashMap() {
+											private static final long serialVersionUID = -501228436728130730L;
+											{
+												put("action1", new ActionConfig("execute", ActionSupport.class, Collections.EMPTY_MAP, Collections.EMPTY_MAP, 
+														new LinkedList() {
+															private static final long serialVersionUID = -469598749286764352L;
+															{
+																	add(interceptorMapping1);
+																	add(interceptorMapping2);
+																	add(interceptorMapping3);
+															}
+													}));
+											}
+										});
+								put("/namespace2",  
+										new LinkedHashMap() {
+											private static final long serialVersionUID = -3776017770134006105L;
+											{
+												put("action2", new ActionConfig("execute", ActionSupport.class, Collections.EMPTY_MAP, Collections.EMPTY_MAP, 
+														new LinkedList() {
+															private static final long serialVersionUID = 3329054396195275042L;
+															{
+																	add(interceptorMapping2);
+																	add(interceptorMapping3);
+																	add(interceptorMapping4);
+															}
+													}));
+											}
+										});
+								put("/namespace3",  
+										new LinkedHashMap() {
+											private static final long serialVersionUID = 8617571684293955862L;
+											{
+												put("action3", new ActionConfig("execute", ActionSupport.class, Collections.EMPTY_MAP, Collections.EMPTY_MAP, 
+														new LinkedList() {
+															private static final long serialVersionUID = -1373751922832875792L;
+															{
+																	add(interceptorMapping4);
+																	add(interceptorMapping5);
+																	add(interceptorMapping6);
+															}
+													}));
+											}
+										});
+								put("/namespace4",  
+										new LinkedHashMap() {
+											private static final long serialVersionUID = -2184515238440865053L;
+											{
+												put("action4", new ActionConfig("execute", ActionSupport.class, Collections.EMPTY_MAP, Collections.EMPTY_MAP, 
+														new LinkedList() {
+															private static final long serialVersionUID = 5583322314404579945L;
+															{
+																	add(interceptorMapping6);
+																	add(interceptorMapping7);
+																	add(interceptorMapping8);
+															}
+													}));
+											}
+										});
+							}
+						};
+					}
+    			});
+    	configuration.destroy();		// EasyMock
+    	configProviderMock.expect("destroy");  // MockObject
+    	control.replay();
+    	ConfigurationManager.destroyConfiguration();
+    	configProviderMock.verify();
+    	control.verify();
+    	
+    	assertTrue(interceptor1.destroyed);
+    	assertTrue(interceptor2.destroyed);
+    	assertTrue(interceptor3.destroyed);
+    	assertTrue(interceptor4.destroyed);
+    	assertTrue(interceptor5.destroyed);
+    	assertTrue(interceptor6.destroyed);
+    	assertTrue(interceptor7.destroyed);
+    	assertTrue(interceptor8.destroyed);
+    	assertEquals(interceptor1.destroyCallCount, 1);
+    	assertEquals(interceptor2.destroyCallCount, 1);
+    	assertEquals(interceptor3.destroyCallCount, 1);
+    	assertEquals(interceptor4.destroyCallCount, 1);
+    	assertEquals(interceptor5.destroyCallCount, 1);
+    	assertEquals(interceptor6.destroyCallCount, 1);
+    	assertEquals(interceptor7.destroyCallCount, 1);
+    	assertEquals(interceptor8.destroyCallCount, 1);
+    }
+    
+    
+    
     public void testClearConfigurationProviders() throws Exception {
     	configProviderMock.expect("destroy");  
     	ConfigurationManager.clearConfigurationProviders();
         super.tearDown();
         ConfigurationManager.destroyConfiguration();
     }
+    
+    
+    protected static class MockInterceptor implements Interceptor {
+		private static final long serialVersionUID = 7221646431244123611L;
+		public boolean destroyed = false;
+    	public int destroyCallCount = 0;
+		public void destroy() {
+			destroyed = true;
+			destroyCallCount++;
+		}
+		public void init() {
+		}
+		public String intercept(ActionInvocation invocation) throws Exception {
+			return invocation.invoke();
+		}
+    }
 }