You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by am...@apache.org on 2018/03/19 17:52:27 UTC
[struts] branch master updated: Add tests for java configuration
This is an automated email from the ASF dual-hosted git repository.
amashchenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git
The following commit(s) were added to refs/heads/master by this push:
new 845ab41 Add tests for java configuration
845ab41 is described below
commit 845ab413cd077d06de8dcc33c68a1e236067b346
Author: Aleksandr Mashchenko <am...@apache.org>
AuthorDate: Mon Mar 19 19:52:16 2018 +0200
Add tests for java configuration
---
.../StrutsJavaConfigurationProviderTest.java | 87 +++++++++++++++++++
.../struts2/config/entities/BeanConfigTest.java | 62 ++++++++++++++
.../config/entities/ConstantConfigTest.java | 97 ++++++++++++++++++++++
3 files changed, 246 insertions(+)
diff --git a/core/src/test/java/org/apache/struts2/config/StrutsJavaConfigurationProviderTest.java b/core/src/test/java/org/apache/struts2/config/StrutsJavaConfigurationProviderTest.java
new file mode 100644
index 0000000..933de81
--- /dev/null
+++ b/core/src/test/java/org/apache/struts2/config/StrutsJavaConfigurationProviderTest.java
@@ -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);
+ }
+}
diff --git a/core/src/test/java/org/apache/struts2/config/entities/BeanConfigTest.java b/core/src/test/java/org/apache/struts2/config/entities/BeanConfigTest.java
new file mode 100644
index 0000000..72709e6
--- /dev/null
+++ b/core/src/test/java/org/apache/struts2/config/entities/BeanConfigTest.java
@@ -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());
+ }
+}
diff --git a/core/src/test/java/org/apache/struts2/config/entities/ConstantConfigTest.java b/core/src/test/java/org/apache/struts2/config/entities/ConstantConfigTest.java
new file mode 100644
index 0000000..00421d4
--- /dev/null
+++ b/core/src/test/java/org/apache/struts2/config/entities/ConstantConfigTest.java
@@ -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));
+ }
+}
--
To stop receiving notification emails like this one, please contact
amashchenko@apache.org.