Skip to content

Commit

Permalink
WW-5382 Rework existing Dispatcher tests and base test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Jan 1, 2024
1 parent 2024d83 commit b9e3c5e
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 385 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ public void setUp() throws Exception {
@After
public void tearDown() throws Exception {
XWorkTestCaseHelper.tearDown(configurationManager);
configurationManager = null;
configuration = null;
container = null;
actionProxyFactory = null;
}

protected void loadConfigurationProviders(ConfigurationProvider... providers) {
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ protected void setUp() throws Exception {
@Override
protected void tearDown() throws Exception {
XWorkTestCaseHelper.tearDown(configurationManager);
configurationManager = null;
configuration = null;
container = null;
actionProxyFactory = null;
}

protected void loadConfigurationProviders(ConfigurationProvider... providers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@
/**
* Simple class to hold Container instance per thread to minimise number of attempts
* to read configuration and build each time a new configuration.
*
* <p>
* As ContainerHolder operates just per thread (which means per request) there is no need
* to check if configuration changed during the same request. If changed between requests,
* first call to store Container in ContainerHolder will be with the new configuration.
*/
class ContainerHolder {

private static ThreadLocal<Container> instance = new ThreadLocal<>();
private static final ThreadLocal<Container> instance = new ThreadLocal<>();

public static void store(Container instance) {
ContainerHolder.instance.set(instance);
public static void store(Container newInstance) {
instance.set(newInstance);
}

public static Container get() {
return ContainerHolder.instance.get();
return instance.get();
}

public static void clear() {
ContainerHolder.instance.remove();
instance.remove();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public void cleanup() {
}

// clean up Dispatcher itself for this thread
instance.set(null);
instance.remove();
servletContext.setAttribute(StrutsStatics.SERVLET_DISPATCHER, null);

// clean up DispatcherListeners
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.emptyMap;

/**
* Generic test setup methods to be used with any unit testing framework.
* Generic test setup methods to be used with any unit testing framework.
*/
public class StrutsTestCaseHelper {

public static Dispatcher initDispatcher(ServletContext ctx, Map<String,String> params) {
if (params == null) {
params = new HashMap<>();
}
Dispatcher du = new DispatcherWrapper(ctx, params);

public static Dispatcher initDispatcher(ServletContext ctx, Map<String, String> params) {
Dispatcher du = new DispatcherWrapper(ctx, params != null ? params : emptyMap());
du.init();
Dispatcher.setInstance(du);

Expand All @@ -52,7 +50,15 @@ public static Dispatcher initDispatcher(ServletContext ctx, Map<String,String> p
return du;
}

public static void tearDown() throws Exception {
public static void tearDown(Dispatcher dispatcher) {
if (dispatcher != null && dispatcher.getConfigurationManager() != null) {
dispatcher.cleanup();
}
tearDown();
}

public static void tearDown() {
(new Dispatcher(null, null)).cleanUpAfterInit(); // Clear ContainerHolder
Dispatcher.setInstance(null);
ActionContext.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ protected Dispatcher initDispatcherWithConfigs(String configs) {

@Override
protected void tearDown() throws Exception {
// maybe someone else already destroyed Dispatcher
if (dispatcher != null && dispatcher.getConfigurationManager() != null) {
dispatcher.cleanup();
dispatcher = null;
}
StrutsTestCaseHelper.tearDown();
StrutsTestCaseHelper.tearDown(dispatcher);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 com.opensymphony.xwork2.ActionProxyFactory;
import com.opensymphony.xwork2.XWorkJUnit4TestCase;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.util.StrutsTestCaseHelper;
import org.apache.struts2.views.jsp.StrutsMockServletContext;
import org.junit.After;
import org.junit.Before;

import java.util.Map;

public class StrutsJUnit4InternalTestCase extends XWorkJUnit4TestCase {

protected StrutsMockServletContext servletContext;
protected Dispatcher dispatcher;

@Override
@Before
public void setUp() throws Exception {
initDispatcher();
}

@Override
@After
public void tearDown() throws Exception {
StrutsTestCaseHelper.tearDown(dispatcher);
}

protected void initDispatcher() {
initDispatcher(null);
}

protected void initDispatcher(Map<String, String> params) {
StrutsTestCaseHelper.tearDown();
servletContext = new StrutsMockServletContext();
dispatcher = StrutsTestCaseHelper.initDispatcher(servletContext, params);
configurationManager = dispatcher.getConfigurationManager();
configuration = configurationManager.getConfiguration();
container = configuration.getContainer();
actionProxyFactory = container.getInstance(ActionProxyFactory.class);
}
}
Loading

0 comments on commit b9e3c5e

Please sign in to comment.