Commits

Anonymous committed e881bd3

Adding TestNG support
XW-451

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1246e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (5)

             <version>3.8.1</version>
             <optional>true</optional>
         </dependency>
+        
+        <dependency>
+            <groupId>org.testng</groupId>
+            <artifactId>testng</artifactId>
+            <version>5.1</version>
+            <optional>true</optional>
+            <classifier>jdk15</classifier>
+        </dependency>
+        
         <dependency>
             <groupId>mockobjects</groupId>
             <artifactId>mockobjects-core</artifactId>

src/java/com/opensymphony/xwork2/TestNGXWorkTestCase.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork2;
+
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+
+import com.opensymphony.xwork2.config.Configuration;
+import com.opensymphony.xwork2.config.ConfigurationManager;
+import com.opensymphony.xwork2.config.ConfigurationProvider;
+import com.opensymphony.xwork2.config.impl.MockConfiguration;
+import com.opensymphony.xwork2.inject.Container;
+import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
+
+/**
+ * Base test class for TestNG unit tests.  Provides common XWork variables
+ * and performs XWork setup and teardown processes
+ */
+public class TestNGXWorkTestCase {
+
+    protected ConfigurationManager configurationManager;
+    protected Configuration configuration;
+    protected Container container;
+    protected ActionProxyFactory actionProxyFactory;
+    
+    @BeforeTest
+    protected void setUp() throws Exception {
+        configurationManager = XWorkTestCaseHelper.setUp();
+        configuration = new MockConfiguration();
+        container = configuration.getContainer();
+        actionProxyFactory = container.getInstance(ActionProxyFactory.class);
+    }
+    
+    @AfterTest
+    protected void tearDown() throws Exception {
+        XWorkTestCaseHelper.tearDown(configurationManager);
+        configurationManager = null;
+        configuration = null;
+        container = null;
+        actionProxyFactory = null;
+    }
+    
+    protected void loadConfigurationProviders(ConfigurationProvider... providers) {
+        configurationManager = XWorkTestCaseHelper.loadConfigurationProviders(configurationManager, providers);
+        configuration = configurationManager.getConfiguration();
+        container = configuration.getContainer();
+        actionProxyFactory = container.getInstance(ActionProxyFactory.class);
+    }
+}

src/java/com/opensymphony/xwork2/XWorkTestCase.java

 
 package com.opensymphony.xwork2;
 
+import junit.framework.TestCase;
+
 import com.opensymphony.xwork2.config.Configuration;
 import com.opensymphony.xwork2.config.ConfigurationManager;
 import com.opensymphony.xwork2.config.ConfigurationProvider;
 import com.opensymphony.xwork2.config.impl.MockConfiguration;
 import com.opensymphony.xwork2.inject.Container;
-import com.opensymphony.xwork2.util.LocalizedTextUtil;
-import com.opensymphony.xwork2.util.OgnlValueStack;
-import com.opensymphony.xwork2.util.ValueStack;
-import com.opensymphony.xwork2.util.XWorkConverter;
-import junit.framework.TestCase;
+import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
+
 
 /**
- * Base JUnit TestCase to extend for XWork specific unit tests.
+ * Base JUnit TestCase to extend for XWork specific JUnit tests. Uses 
+ * the generic test setup for logic.
  *
  * @author plightbo
  */
 public abstract class XWorkTestCase extends TestCase {
-
+    
     protected ConfigurationManager configurationManager;
     protected Configuration configuration;
     protected Container container;
     protected ActionProxyFactory actionProxyFactory;
     
+    public XWorkTestCase() {
+        super();
+    }
+    
     protected void setUp() throws Exception {
-        configurationManager = new ConfigurationManager();
-        
-        // Reset the value stack
-        ValueStack stack = new OgnlValueStack();
-        ActionContext.setContext(new ActionContext(stack.getContext()));
-
-        // clear out localization
-        LocalizedTextUtil.reset();
-
-        // type conversion
-        XWorkConverter.resetInstance();
-
-        // reset ognl
-        OgnlValueStack.reset();
-        
+        configurationManager = XWorkTestCaseHelper.setUp();
         configuration = new MockConfiguration();
         container = configuration.getContainer();
         actionProxyFactory = container.getInstance(ActionProxyFactory.class);
-        ObjectFactory.setObjectFactory(new ObjectFactory());
     }
     
-    protected void loadConfigurationProviders(ConfigurationProvider... providers) {
-        if (configurationManager != null) {
-            configurationManager.clearConfigurationProviders();
-        } else {
-            configurationManager = new ConfigurationManager();
-        }
-        configurationManager.clearConfigurationProviders();
-        for (ConfigurationProvider prov : providers) {
-            configurationManager.addConfigurationProvider(prov);
-        }
-        configurationManager.getConfiguration().reload(
-                configurationManager.getConfigurationProviders());
-        container = configurationManager.getConfiguration().getContainer();
-        actionProxyFactory = container.getInstance(ActionProxyFactory.class);
-        ObjectFactory.setObjectFactory(container.getInstance(ObjectFactory.class));
-    }
-
     protected void tearDown() throws Exception {
-        // reset the old object factory
-        ObjectFactory.setObjectFactory(null);
-
-        //  clear out configuration
-        if (configurationManager != null) {
-            configurationManager.destroyConfiguration();
-            configurationManager = null;
-        }
+        XWorkTestCaseHelper.tearDown(configurationManager);
+        configurationManager = null;
         configuration = null;
         container = null;
-        ActionContext.setContext(null);
+        actionProxyFactory = null;
+    }
+    
+    protected void loadConfigurationProviders(ConfigurationProvider... providers) {
+        configurationManager = XWorkTestCaseHelper.loadConfigurationProviders(configurationManager, providers);
+        configuration = configurationManager.getConfiguration();
+        container = configuration.getContainer();
+        actionProxyFactory = container.getInstance(ActionProxyFactory.class);
     }
 }

src/java/com/opensymphony/xwork2/util/XWorkTestCaseHelper.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork2.util;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.ObjectFactory;
+import com.opensymphony.xwork2.config.ConfigurationManager;
+import com.opensymphony.xwork2.config.ConfigurationProvider;
+import com.opensymphony.xwork2.inject.Container;
+
+/**
+ * Generic test setup methods to be used with any unit testing framework. 
+ */
+public class XWorkTestCaseHelper {
+
+    public static ConfigurationManager setUp() throws Exception {
+        ConfigurationManager configurationManager = new ConfigurationManager();
+        
+        // Reset the value stack
+        ValueStack stack = new OgnlValueStack();
+        ActionContext.setContext(new ActionContext(stack.getContext()));
+    
+        // clear out localization
+        LocalizedTextUtil.reset();
+    
+        // type conversion
+        XWorkConverter.resetInstance();
+    
+        // reset ognl
+        OgnlValueStack.reset();
+        
+        ObjectFactory.setObjectFactory(new ObjectFactory());
+        return configurationManager;
+    }
+
+    public static ConfigurationManager loadConfigurationProviders(ConfigurationManager configurationManager,
+            ConfigurationProvider... providers) {
+        if (configurationManager != null) {
+            configurationManager.clearConfigurationProviders();
+        } else {
+            configurationManager = new ConfigurationManager();
+        }
+        configurationManager.clearConfigurationProviders();
+        for (ConfigurationProvider prov : providers) {
+            configurationManager.addConfigurationProvider(prov);
+        }
+        configurationManager.getConfiguration().reload(
+                configurationManager.getConfigurationProviders());
+        Container container = configurationManager.getConfiguration().getContainer();
+        ObjectFactory.setObjectFactory(container.getInstance(ObjectFactory.class));
+        return configurationManager;
+    }
+
+    public static void tearDown(ConfigurationManager configurationManager) throws Exception {
+        // reset the old object factory
+        ObjectFactory.setObjectFactory(null);
+    
+        //  clear out configuration
+        if (configurationManager != null) {
+            configurationManager.destroyConfiguration();
+            configurationManager = null;
+        }
+        ActionContext.setContext(null);
+    }
+}

src/test/com/opensymphony/xwork2/TestNGXWorkTestCaseTest.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork2;
+
+import org.testng.TestListenerAdapter;
+import org.testng.TestNG;
+import org.testng.annotations.Test;
+
+import com.opensymphony.xwork2.TestNGXWorkTestCase;
+import com.opensymphony.xwork2.config.ConfigurationManager;
+
+import junit.framework.TestCase;
+
+public class TestNGXWorkTestCaseTest extends TestCase {
+
+    public void testSimpleTest() throws Exception {
+        TestListenerAdapter tla = new TestListenerAdapter();
+        TestNG testng = new TestNG();
+        testng.setTestClasses(new Class[] { RunTest.class });
+        testng.addListener(tla);
+        try {
+            testng.run();
+            assertEquals(1, tla.getPassedTests().size());
+            assertEquals(0, tla.getFailedTests().size());
+            assertTrue(RunTest.ran);
+            assertNotNull(RunTest.mgr);
+        } finally {
+            RunTest.mgr = null;
+        }
+    }
+    
+    @Test
+    public static class RunTest extends TestNGXWorkTestCase {
+        public static boolean ran = false;
+        public static ConfigurationManager mgr;
+        
+        public void testRun() {
+            ran = true;
+            mgr = this.configurationManager;
+        }
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.