From df4a18107f0fbf21f80cdbe8fbadc42bd2c78555 Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Mon, 8 Jan 2018 14:42:03 +0330 Subject: [PATCH] add tests for lazy and early initialization --- .../config/ConfigurationManagerTest.java | 7 ++++ .../xwork2/inject/ContainerImplTest.java | 21 ++++++++++++ .../inject/EarlyInitializableCheck.java | 34 +++++++++++++++++++ .../xwork2/inject/InitializableCheck.java | 34 +++++++++++++++++++ .../validator/SimpleActionValidationTest.java | 6 ++++ 5 files changed, 102 insertions(+) create mode 100644 core/src/test/java/com/opensymphony/xwork2/inject/EarlyInitializableCheck.java create mode 100644 core/src/test/java/com/opensymphony/xwork2/inject/InitializableCheck.java diff --git a/core/src/test/java/com/opensymphony/xwork2/config/ConfigurationManagerTest.java b/core/src/test/java/com/opensymphony/xwork2/config/ConfigurationManagerTest.java index 62449ed36..864f790d5 100644 --- a/core/src/test/java/com/opensymphony/xwork2/config/ConfigurationManagerTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/config/ConfigurationManagerTest.java @@ -23,6 +23,7 @@ import com.mockobjects.dynamic.Mock; import com.opensymphony.xwork2.FileManagerFactory; import com.opensymphony.xwork2.XWorkTestCase; import com.opensymphony.xwork2.config.providers.XWorkConfigurationProvider; +import com.opensymphony.xwork2.conversion.TypeConverterHolder; import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.inject.ContainerBuilder; import com.opensymphony.xwork2.util.location.LocatableProperties; @@ -152,6 +153,12 @@ public class ConfigurationManagerTest extends XWorkTestCase { configProviderMock.verify(); } + public void testEarlyInitializable() throws Exception { + TypeConverterHolder converterHolder = container.getInstance(TypeConverterHolder.class); + assertTrue("java.io.File mapping should being putted by DefaultConversionPropertiesProcessor.init()", + converterHolder.containsDefaultMapping("java.io.File")); + } + @Override protected void setUp() throws Exception { super.setUp(); diff --git a/core/src/test/java/com/opensymphony/xwork2/inject/ContainerImplTest.java b/core/src/test/java/com/opensymphony/xwork2/inject/ContainerImplTest.java index 17f1b8037..238bef986 100644 --- a/core/src/test/java/com/opensymphony/xwork2/inject/ContainerImplTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/inject/ContainerImplTest.java @@ -37,6 +37,8 @@ public class ContainerImplTest extends TestCase { ContainerBuilder cb = new ContainerBuilder(); cb.constant("methodCheck.name", "Lukasz"); cb.constant("fieldCheck.name", "Lukasz"); + cb.factory(EarlyInitializableCheck.class, EarlyInitializableCheck.class, Scope.SINGLETON); + cb.factory(InitializableCheck.class, InitializableCheck.class, Scope.SINGLETON); c = cb.create(false); } @@ -109,6 +111,25 @@ public class ContainerImplTest extends TestCase { } } + public void testEarlyInitializable() throws Exception { + assertTrue("should being initialized already", EarlyInitializableCheck.initializedEarly); + + EarlyInitializableCheck earlyInitializableCheck = c.getInstance(EarlyInitializableCheck.class); + assertEquals("initialized early", earlyInitializableCheck.getMessage()); + earlyInitializableCheck = c.getInstance(EarlyInitializableCheck.class); + assertEquals("initialized early", earlyInitializableCheck.getMessage()); + } + + public void testInitializable() throws Exception { + assertFalse("should not being initialized already", InitializableCheck.initialized); + + InitializableCheck initializableCheck = c.getInstance(InitializableCheck.class); + assertTrue("should being initialized here", InitializableCheck.initialized); + assertEquals("initialized", initializableCheck.getMessage()); + initializableCheck = c.getInstance(InitializableCheck.class); + assertEquals("initialized", initializableCheck.getMessage()); + } + class FieldCheck { @Inject("fieldCheck.name") diff --git a/core/src/test/java/com/opensymphony/xwork2/inject/EarlyInitializableCheck.java b/core/src/test/java/com/opensymphony/xwork2/inject/EarlyInitializableCheck.java new file mode 100644 index 000000000..b428d588e --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/inject/EarlyInitializableCheck.java @@ -0,0 +1,34 @@ +/* + * 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 com.opensymphony.xwork2.inject; + +class EarlyInitializableCheck implements EarlyInitializable { + private String message = ""; + static boolean initializedEarly; + + public String getMessage() { + return message; + } + + @Override + public void init() { + message += "initialized early"; + initializedEarly = true; + } +} diff --git a/core/src/test/java/com/opensymphony/xwork2/inject/InitializableCheck.java b/core/src/test/java/com/opensymphony/xwork2/inject/InitializableCheck.java new file mode 100644 index 000000000..24294db22 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/inject/InitializableCheck.java @@ -0,0 +1,34 @@ +/* + * 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 com.opensymphony.xwork2.inject; + +public class InitializableCheck implements Initializable { + private String message = ""; + static boolean initialized; + + public String getMessage() { + return message; + } + + @Override + public void init() { + message += "initialized"; + initialized = true; + } +} diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/SimpleActionValidationTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/SimpleActionValidationTest.java index d6cae3869..42075db5f 100644 --- a/core/src/test/java/com/opensymphony/xwork2/validator/SimpleActionValidationTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/validator/SimpleActionValidationTest.java @@ -226,6 +226,12 @@ public class SimpleActionValidationTest extends XWorkTestCase { } } + public void testInitializable() throws Exception { + ValidatorFactory validatorFactory = container.getInstance(ValidatorFactory.class); + assertEquals("com.opensymphony.xwork2.validator.validators.RequiredFieldValidator", + validatorFactory.lookupRegisteredValidatorType("requiredAnother")); + } + @Override protected void setUp() throws Exception { super.setUp();