Add tests for java configuration

This commit is contained in:
Aleksandr Mashchenko
2018-03-19 19:52:16 +02:00
parent 32af1370ed
commit 845ab413cd
3 changed files with 246 additions and 0 deletions
@@ -0,0 +1,87 @@
/*
* 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.config;
import java.util.Arrays;
import java.util.List;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.config.entities.BeanConfig;
import org.apache.struts2.config.entities.ConstantConfig;
import org.junit.Assert;
import org.junit.Test;
import com.opensymphony.xwork2.TestBean;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.impl.MockConfiguration;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.ContainerBuilder;
import com.opensymphony.xwork2.inject.Scope;
import com.opensymphony.xwork2.util.location.LocatableProperties;
public class StrutsJavaConfigurationProviderTest {
@Test
public void testRegister() throws Exception {
final ConstantConfig constantConfig = new ConstantConfig();
constantConfig.setDevMode(true);
final String expectedUnknownHandler = "expectedUnknownHandler";
StrutsJavaConfiguration javaConfig = new StrutsJavaConfiguration() {
@Override
public List<String> unknownHandlerStack() {
return Arrays.asList(expectedUnknownHandler);
}
@Override
public List<ConstantConfig> constants() {
return Arrays.asList(constantConfig);
}
@Override
public List<BeanConfig> beans() {
return Arrays.asList(new BeanConfig(TestBean.class),
new BeanConfig(TestBean.class, "testBean", TestBean.class, Scope.PROTOTYPE, true, true));
}
};
StrutsJavaConfigurationProvider provider = new StrutsJavaConfigurationProvider(javaConfig);
Configuration configuration = new MockConfiguration();
provider.init(configuration);
ContainerBuilder builder = new ContainerBuilder();
LocatableProperties props = new LocatableProperties();
provider.register(builder, props);
// constant
Assert.assertEquals(String.valueOf(constantConfig.getDevMode()), props.get(StrutsConstants.STRUTS_DEVMODE));
// unknown-handler-stack
Assert.assertNotNull(configuration.getUnknownHandlerStack());
Assert.assertEquals(1, configuration.getUnknownHandlerStack().size());
Assert.assertEquals(expectedUnknownHandler, configuration.getUnknownHandlerStack().get(0).getName());
// bean
Container container = builder.create(true);
TestBean testBean = container.getInstance(TestBean.class);
Assert.assertNotNull(testBean);
}
}
@@ -0,0 +1,62 @@
/*
* 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.config.entities;
import org.junit.Assert;
import org.junit.Test;
import com.opensymphony.xwork2.TestBean;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Scope;
public class BeanConfigTest {
@Test
public void testConstructor() throws Exception {
Class<TestBean> expectedClass = TestBean.class;
BeanConfig beanConfig = new BeanConfig(expectedClass);
Assert.assertEquals(expectedClass, beanConfig.getClazz());
Assert.assertEquals(Container.DEFAULT_NAME, beanConfig.getName());
Assert.assertEquals(Scope.SINGLETON, beanConfig.getScope());
Assert.assertEquals(expectedClass, beanConfig.getType());
Assert.assertFalse(beanConfig.isOnlyStatic());
Assert.assertFalse(beanConfig.isOptional());
}
@Test
public void testConstructor2() throws Exception {
Class<TestBean> expectedClass = TestBean.class;
String expectedName = "expectedBeanName";
Class<Object> expectedType = Object.class;
Scope expectedScope = Scope.PROTOTYPE;
boolean expectedOnlyStatic = true;
boolean expectedOptional = true;
BeanConfig beanConfig = new BeanConfig(expectedClass, expectedName, expectedType, expectedScope,
expectedOnlyStatic, expectedOptional);
Assert.assertEquals(expectedClass, beanConfig.getClazz());
Assert.assertEquals(expectedName, beanConfig.getName());
Assert.assertEquals(expectedScope, beanConfig.getScope());
Assert.assertEquals(expectedType, beanConfig.getType());
Assert.assertEquals(expectedOnlyStatic, beanConfig.isOnlyStatic());
Assert.assertEquals(expectedOptional, beanConfig.isOptional());
}
}
@@ -0,0 +1,97 @@
/*
* 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.config.entities;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.apache.struts2.StrutsConstants;
import org.junit.Assert;
import org.junit.Test;
import com.opensymphony.xwork2.TestBean;
import com.opensymphony.xwork2.inject.Container;
public class ConstantConfigTest {
@Test
public void testBeanConfToString() throws Exception {
ConstantConfig constantConfig = new ConstantConfig();
String actual = constantConfig.beanConfToString(null);
Assert.assertEquals(null, actual);
actual = constantConfig.beanConfToString(new BeanConfig(TestBean.class));
Assert.assertEquals(Container.DEFAULT_NAME, actual);
String expectedName = "expectedTestBeanName";
actual = constantConfig.beanConfToString(new BeanConfig(TestBean.class, expectedName));
Assert.assertEquals(expectedName, actual);
}
@Test
public void testGetAllAsStringsMap() throws Exception {
ConstantConfig constantConfig = new ConstantConfig();
boolean expectedDevMode = true;
constantConfig.setDevMode(expectedDevMode);
String expectedActionExtensions = ".action,.some,.another";
constantConfig.setActionExtension(Arrays.asList(expectedActionExtensions.split(",")));
String expectedLanguage = "fr";
constantConfig.setLocale(new Locale(expectedLanguage));
Map<String, String> map = constantConfig.getAllAsStringsMap();
Assert.assertEquals(String.valueOf(expectedDevMode), map.get(StrutsConstants.STRUTS_DEVMODE));
Assert.assertEquals(expectedActionExtensions, map.get(StrutsConstants.STRUTS_ACTION_EXTENSION));
Assert.assertEquals(null, map.get(StrutsConstants.STRUTS_I18N_RELOAD));
Assert.assertEquals(expectedLanguage, map.get(StrutsConstants.STRUTS_LOCALE));
}
@Test
public void testEmptyClassesToString() throws Exception {
ConstantConfig constantConfig = new ConstantConfig();
constantConfig.setExcludedClasses(new HashSet<Class<?>>());
Map<String, String> map = constantConfig.getAllAsStringsMap();
Assert.assertEquals(null, map.get(StrutsConstants.STRUTS_EXCLUDED_CLASSES));
}
@Test
public void testClassesToString() throws Exception {
ConstantConfig constantConfig = new ConstantConfig();
Set<Class<?>> excludedClasses = new LinkedHashSet<>();
excludedClasses.add(Object.class);
excludedClasses.add(Runtime.class);
excludedClasses.add(System.class);
constantConfig.setExcludedClasses(excludedClasses);
Map<String, String> map = constantConfig.getAllAsStringsMap();
Assert.assertEquals("java.lang.Object,java.lang.Runtime,java.lang.System",
map.get(StrutsConstants.STRUTS_EXCLUDED_CLASSES));
}
}