Author: mrdon Date: Thu Nov 30 17:35:38 2006 New Revision: 481125 URL: http://svn.apache.org/viewvc?view=rev&rev=481125 Log: Adding TestNG support WW-1531
Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/TestNGStrutsTestCase.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/TestNGStrutsTestCaseTest.java Modified: struts/struts2/trunk/core/pom.xml struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsTestCase.java Modified: struts/struts2/trunk/core/pom.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/pom.xml?view=diff&rev=481125&r1=481124&r2=481125 ============================================================================== --- struts/struts2/trunk/core/pom.xml (original) +++ struts/struts2/trunk/core/pom.xml Thu Nov 30 17:35:38 2006 @@ -139,7 +139,7 @@ <dependency> <groupId>opensymphony</groupId> <artifactId>xwork</artifactId> - <version>2.0-beta-2</version> + <version>2.0-SNAPSHOT</version> </dependency> <dependency> @@ -271,6 +271,14 @@ <version>3.8.1</version> <!-- has to be compile for StrutsTestCase, which is part of the base package so others can write unit tests --> <optional>true</optional> + </dependency> + + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>5.1</version> + <optional>true</optional> + <classifier>jdk15</classifier> </dependency> <dependency> Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsTestCase.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsTestCase.java?view=diff&rev=481125&r1=481124&r2=481125 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsTestCase.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsTestCase.java Thu Nov 30 17:35:38 2006 @@ -20,41 +20,29 @@ */ package org.apache.struts2; -import java.util.HashMap; import java.util.Map; -import org.apache.struts2.config.StrutsXmlConfigurationProvider; import org.apache.struts2.dispatcher.Dispatcher; -import org.springframework.mock.web.MockServletContext; +import org.apache.struts2.util.StrutsTestCaseHelper; import com.opensymphony.xwork2.XWorkTestCase; -import com.opensymphony.xwork2.config.ConfigurationManager; -import com.opensymphony.xwork2.util.LocalizedTextUtil; /** - * Base test case for unit testing Struts. + * Base test case for JUnit testing Struts. */ public abstract class StrutsTestCase extends XWorkTestCase { - /** * Sets up the configuration settings, XWork configuration, and * message resources */ protected void setUp() throws Exception { super.setUp(); - LocalizedTextUtil.clearDefaultResourceBundles(); initDispatcher(null); - } protected Dispatcher initDispatcher(Map<String,String> params) { - if (params == null) { - params = new HashMap<String,String>(); - } - Dispatcher du = new Dispatcher(new MockServletContext(), params); - du.init(); - Dispatcher.setInstance(du); + Dispatcher du = StrutsTestCaseHelper.initDispatcher(params); configurationManager = du.getConfigurationManager(); configuration = configurationManager.getConfiguration(); container = configuration.getContainer(); @@ -63,7 +51,7 @@ protected void tearDown() throws Exception { super.tearDown(); - Dispatcher.setInstance(null); + StrutsTestCaseHelper.tearDown(); } } Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/TestNGStrutsTestCase.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/TestNGStrutsTestCase.java?view=auto&rev=481125 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/TestNGStrutsTestCase.java (added) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/TestNGStrutsTestCase.java Thu Nov 30 17:35:38 2006 @@ -0,0 +1,58 @@ +/* + * $Id: StrutsTestCase.java 476696 2006-11-19 03:56:18Z tmjee $ + * + * 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.struts2; + +import java.util.Map; + +import org.apache.struts2.dispatcher.Dispatcher; +import org.apache.struts2.util.StrutsTestCaseHelper; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; + +import com.opensymphony.xwork2.TestNGXWorkTestCase; + +/** + * Base test class for TestNG unit tests. Provides common Struts variables + * and performs Struts setup and teardown processes + */ +public class TestNGStrutsTestCase extends TestNGXWorkTestCase { + + @BeforeTest + protected void setUp() throws Exception { + super.setUp(); + initDispatcher(null); + } + + protected Dispatcher initDispatcher(Map<String,String> params) { + Dispatcher du = StrutsTestCaseHelper.initDispatcher(params); + configurationManager = du.getConfigurationManager(); + configuration = configurationManager.getConfiguration(); + container = configuration.getContainer(); + return du; + } + + @AfterTest + protected void tearDown() throws Exception { + super.tearDown(); + StrutsTestCaseHelper.tearDown(); + } +} + Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java?view=auto&rev=481125 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java (added) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java Thu Nov 30 17:35:38 2006 @@ -0,0 +1,57 @@ +/* + * $Id: StrutsTestCase.java 476696 2006-11-19 03:56:18Z tmjee $ + * + * 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.struts2.util; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.struts2.dispatcher.Dispatcher; +import org.springframework.mock.web.MockServletContext; + +import com.opensymphony.xwork2.util.LocalizedTextUtil; + +/** + * Generic test setup methods to be used with any unit testing framework. + */ +public class StrutsTestCaseHelper { + + /** + * Sets up the configuration settings, XWork configuration, and + * message resources + */ + public static void setUp() throws Exception { + LocalizedTextUtil.clearDefaultResourceBundles(); + } + + public static Dispatcher initDispatcher(Map<String,String> params) { + if (params == null) { + params = new HashMap<String,String>(); + } + Dispatcher du = new Dispatcher(new MockServletContext(), params); + du.init(); + Dispatcher.setInstance(du); + return du; + } + + public static void tearDown() throws Exception { + Dispatcher.setInstance(null); + } +} Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/TestNGStrutsTestCaseTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/TestNGStrutsTestCaseTest.java?view=auto&rev=481125 ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/TestNGStrutsTestCaseTest.java (added) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/TestNGStrutsTestCaseTest.java Thu Nov 30 17:35:38 2006 @@ -0,0 +1,65 @@ +/* + * $Id: StrutsTestCase.java 476696 2006-11-19 03:56:18Z tmjee $ + * + * 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.struts2; + +import junit.framework.TestCase; + +import org.apache.struts2.dispatcher.Dispatcher; +import org.testng.TestListenerAdapter; +import org.testng.TestNG; +import org.testng.annotations.Test; + +import com.opensymphony.xwork2.config.ConfigurationManager; + +public class TestNGStrutsTestCaseTest 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); + assertNotNull(RunTest.du); + assertNull(Dispatcher.getInstance()); + } finally { + RunTest.mgr = null; + } + } + + @Test + public static class RunTest extends TestNGStrutsTestCase { + public static boolean ran = false; + public static ConfigurationManager mgr; + public static Dispatcher du; + + public void testRun() { + ran = true; + mgr = this.configurationManager; + du = Dispatcher.getInstance(); + } + } +} +