Solves problem with rest-showcase app and adds additional test to proof how excludePackages works

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1367763 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2012-07-31 20:38:52 +00:00
parent 5fa0e9a297
commit 77ce77d342
6 changed files with 94 additions and 12 deletions
@@ -1,4 +1,4 @@
package org.apache.struts2.rest.example;
package org.demo.rest.example;
public class Order {
String id;
@@ -1,16 +1,11 @@
package org.apache.struts2.rest.example;
package org.demo.rest.example;
import java.util.Collection;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.InterceptorRef;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Validateable;
@@ -1,4 +1,4 @@
package org.apache.struts2.rest.example;
package org.demo.rest.example;
import java.util.*;
@@ -492,9 +492,17 @@ public class PackageBasedActionConfigBuilder implements ActionConfigBuilder {
* package-based action scan
*/
protected boolean includeClassNameInActionScan(String className) {
String classPackageName = StringUtils.substringBeforeLast(className, ".");
return (checkActionPackages(classPackageName) || checkPackageLocators(classPackageName)) && checkExcludePackages(classPackageName);
}
/**
* Checks if provided class package is on the exclude list
*
* @param classPackageName name of class package
* @return false if class package is on the {@link #excludePackages} list
*/
protected boolean checkExcludePackages(String classPackageName) {
if(excludePackages != null && excludePackages.length > 0) {
WildcardHelper wildcardHelper = new WildcardHelper();
@@ -508,7 +516,16 @@ public class PackageBasedActionConfigBuilder implements ActionConfigBuilder {
}
}
}
return true;
}
/**
* Checks if class package match provided list of action packages
*
* @param classPackageName name of class package
* @return true if class package is on the {@link #actionPackages} list
*/
protected boolean checkActionPackages(String classPackageName) {
if (actionPackages != null) {
for (String packageName : actionPackages) {
String strictPackageName = packageName + ".";
@@ -517,7 +534,16 @@ public class PackageBasedActionConfigBuilder implements ActionConfigBuilder {
return true;
}
}
return false;
}
/**
* Checks if class package match provided list of package locators
*
* @param classPackageName name of class package
* @return true if class package is on the {@link #packageLocators} list
*/
protected boolean checkPackageLocators(String classPackageName) {
if (packageLocators != null && !disablePackageLocatorsScanning) {
for (String packageLocator : packageLocators) {
if (classPackageName.length() > 0
@@ -530,10 +556,9 @@ public class PackageBasedActionConfigBuilder implements ActionConfigBuilder {
}
}
}
return false;
}
/**
* Construct a {@link Test} object that determines if a specified class name
* should be included in the package scan based on the clazz's package name.
@@ -60,6 +60,7 @@ import org.apache.struts2.convention.actions.chain.ChainedAction;
import org.apache.struts2.convention.actions.defaultinterceptor.SingleActionNameAction2;
import org.apache.struts2.convention.actions.exception.ExceptionsActionLevelAction;
import org.apache.struts2.convention.actions.exception.ExceptionsMethodLevelAction;
import org.apache.struts2.convention.actions.exclude.ExcludeAction;
import org.apache.struts2.convention.actions.interceptor.ActionLevelInterceptor2Action;
import org.apache.struts2.convention.actions.interceptor.ActionLevelInterceptor3Action;
import org.apache.struts2.convention.actions.interceptor.ActionLevelInterceptorAction;
@@ -133,6 +134,10 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
run(null, "actions", "dontfind");
}
public void testJustExcludedPackages() throws MalformedURLException {
run("org.apache.struts2.convention.actions", null, "org.apache.struts2.convention.actions.exclude");
}
private void run(String actionPackages, String packageLocators, String excludePackages) throws MalformedURLException {
//setup interceptors
List<InterceptorConfig> defaultInterceptors = new ArrayList<InterceptorConfig>();
@@ -208,6 +213,8 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
"/chain", strutsDefault, null);
PackageConfig transPkg = makePackageConfig("org.apache.struts2.convention.actions.transactions#struts-default#/transactions",
"/transactions", strutsDefault, null);
PackageConfig excludePkg = makePackageConfig("org.apache.struts2.convention.actions.exclude#struts-default#/exclude",
"/exclude", strutsDefault, null);
ResultMapBuilder resultMapBuilder = createStrictMock(ResultMapBuilder.class);
checkOrder(resultMapBuilder, false);
@@ -312,6 +319,12 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
expect(resultMapBuilder.build(TransNameAction.class, getAnnotation(TransNameAction.class, "trans1", Action.class), "trans1", transPkg)).andReturn(results);
expect(resultMapBuilder.build(TransNameAction.class, getAnnotation(TransNameAction.class, "trans2", Action.class), "trans2", transPkg)).andReturn(results);
/* org.apache.struts2.convention.actions.exclude */
// this is only expected when excludePackages was specified with org.apache.struts2.convention.actions.exclude package
if (excludePackages == null || !excludePackages.contains("org.apache.struts2.convention.actions.exclude")) {
expect(resultMapBuilder.build(ExcludeAction.class, getAnnotation(ExcludeAction.class, "run1", Action.class), "exclude1", excludePkg)).andReturn(results);
}
EasyMock.replay(resultMapBuilder);
final DummyContainer mockContainer = new DummyContainer();
@@ -567,7 +580,7 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
/* org.apache.struts2.convention.actions */
pkgConfig = configuration.getPackageConfig("org.apache.struts2.convention.actions#struts-default#");
assertNotNull(pkgConfig);
System.out.println("actions " + pkgConfig.getActionConfigs());
assertEquals(4, pkgConfig.getActionConfigs().size());
verifyActionConfig(pkgConfig, "no-annotation", NoAnnotationAction.class, "execute", pkgConfig.getName());
verifyActionConfig(pkgConfig, "default-result-path", DefaultResultPathAction.class, "execute", pkgConfig.getName());
@@ -580,6 +593,14 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
verifyActionConfig(pkgConfig, "trans1", TransNameAction.class, "trans1", pkgConfig.getName());
verifyActionConfig(pkgConfig, "trans2", TransNameAction.class, "trans2", pkgConfig.getName());
/* org.apache.struts2.convention.actions.exclude */
pkgConfig = configuration.getPackageConfig("org.apache.struts2.convention.actions.exclude#struts-default#/exclude");
if (excludePackages != null && excludePackages.contains("org.apache.struts2.convention.actions.exclude")) {
verifyMissingActionConfig(configuration, "exclude1", ExcludeAction.class, "run1", "org.apache.struts2.convention.actions.exclude#struts-default#/exclude");
} else {
verifyActionConfig(pkgConfig, "exclude1", ExcludeAction.class, "run1", pkgConfig.getName());
}
//test unknown handler automatic chaining
pkgConfig = configuration.getPackageConfig("org.apache.struts2.convention.actions.chain#struts-default#/chain");
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
@@ -606,6 +627,12 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
assertEquals(packageName, ac.getPackageName());
}
private void verifyMissingActionConfig(Configuration configuration, String actionName, Class<?> actionClass,
String methodName, String packageName) {
assertNull(configuration.getPackageConfig(packageName));
assertFalse(configuration.getPackageConfigNames().contains(packageName));
}
private void verifyMissingActionConfig(PackageConfig pkgConfig, String actionName, Class<?> actionClass,
String methodName, String packageName) {
ActionConfig ac = pkgConfig.getAllActionConfigs().get(actionName);
@@ -0,0 +1,35 @@
/*
* $Id$
*
* 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.convention.actions.exclude;
import org.apache.struts2.convention.annotation.Action;
/**
* <p>
* This is a test action.
* </p>
*/
public class ExcludeAction {
@Action("exclude1")
public String run1() {
return null;
}
}