add tests for lazy and early initialization

This commit is contained in:
Yasser Zamani
2018-01-08 14:42:03 +03:30
parent d64365770d
commit df4a18107f
5 changed files with 102 additions and 0 deletions
@@ -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();
@@ -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")
@@ -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;
}
}
@@ -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;
}
}
@@ -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();