moving unit tests from .testsuite -> .core, .beans, .web, .web.portlet, .web.servlet
This commit is contained in:
parent
285be534df
commit
93e30a4fc5
|
@ -14,50 +14,24 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util.comparator;
|
||||
package org.springframework.beans.support;
|
||||
|
||||
import java.util.Comparator;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.support.PropertyComparator;
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.comparator.CompoundComparator;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PropertyComparator}
|
||||
*
|
||||
* @see org.springframework.util.comparator.ComparatorTests
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class ComparatorTests extends TestCase {
|
||||
|
||||
public void testComparableComparator() {
|
||||
Comparator c = new ComparableComparator();
|
||||
String s1 = "abc";
|
||||
String s2 = "cde";
|
||||
assertTrue(c.compare(s1, s2) < 0);
|
||||
}
|
||||
|
||||
public void testComparableComparatorIllegalArgs() {
|
||||
Comparator c = new ComparableComparator();
|
||||
Object o1 = new Object();
|
||||
Object o2 = new Object();
|
||||
try {
|
||||
c.compare(o1, o2);
|
||||
}
|
||||
catch (ClassCastException e) {
|
||||
return;
|
||||
}
|
||||
fail("Comparator should have thrown a cce");
|
||||
}
|
||||
|
||||
public void testBooleanComparatorTrueLow() {
|
||||
Comparator c = BooleanComparator.TRUE_LOW;
|
||||
assertTrue(c.compare(new Boolean(true), new Boolean(false)) < 0);
|
||||
}
|
||||
|
||||
public void testBooleanComparatorTrueHigh() {
|
||||
Comparator c = BooleanComparator.TRUE_HIGH;
|
||||
assertTrue(c.compare(new Boolean(true), new Boolean(false)) > 0);
|
||||
assertTrue(c.compare(Boolean.TRUE, Boolean.TRUE) == 0);
|
||||
}
|
||||
public class PropertyComparatorTests {
|
||||
|
||||
@Test
|
||||
public void testPropertyComparator() {
|
||||
Dog dog = new Dog();
|
||||
dog.setNickName("mace");
|
||||
|
@ -71,6 +45,7 @@ public class ComparatorTests extends TestCase {
|
|||
assertTrue(c.compare(dog2, dog) < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyComparatorNulls() {
|
||||
Dog dog = new Dog();
|
||||
Dog dog2 = new Dog();
|
||||
|
@ -78,30 +53,10 @@ public class ComparatorTests extends TestCase {
|
|||
assertTrue(c.compare(dog, dog2) == 0);
|
||||
}
|
||||
|
||||
public void testNullSafeComparatorNullsLow() {
|
||||
Comparator c = NullSafeComparator.NULLS_LOW;
|
||||
assertTrue(c.compare(null, "boo") < 0);
|
||||
}
|
||||
|
||||
public void testNullSafeComparatorNullsHigh() {
|
||||
Comparator c = NullSafeComparator.NULLS_HIGH;
|
||||
assertTrue(c.compare(null, "boo") > 0);
|
||||
assertTrue(c.compare(null, null) == 0);
|
||||
}
|
||||
|
||||
public void testCompoundComparatorEmpty() {
|
||||
Comparator c = new CompoundComparator();
|
||||
try {
|
||||
c.compare("foo", "bar");
|
||||
}
|
||||
catch (IllegalStateException e) {
|
||||
return;
|
||||
}
|
||||
fail("illegal state should've been thrown on empty list");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testCompoundComparator() {
|
||||
CompoundComparator c = new CompoundComparator();
|
||||
CompoundComparator<Dog> c = new CompoundComparator<Dog>();
|
||||
c.addComparator(new PropertyComparator("lastName", false, true));
|
||||
|
||||
Dog dog1 = new Dog();
|
||||
|
@ -121,8 +76,10 @@ public class ComparatorTests extends TestCase {
|
|||
assertTrue(c.compare(dog2, dog1) > 0);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testCompoundComparatorInvert() {
|
||||
CompoundComparator c = new CompoundComparator();
|
||||
CompoundComparator<Dog> c = new CompoundComparator<Dog>();
|
||||
c.addComparator(new PropertyComparator("lastName", false, true));
|
||||
c.addComparator(new PropertyComparator("firstName", false, true));
|
||||
Dog dog1 = new Dog();
|
||||
|
@ -139,7 +96,7 @@ public class ComparatorTests extends TestCase {
|
|||
}
|
||||
|
||||
|
||||
private static class Dog implements Comparable {
|
||||
private static class Dog implements Comparable<Object> {
|
||||
|
||||
private String nickName;
|
||||
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package org.springframework.ui;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
@ -28,20 +30,20 @@ import java.util.HashSet;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.test.AssertThrows;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Rick Evans
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class ModelMapTests extends TestCase {
|
||||
public final class ModelMapTests {
|
||||
|
||||
@Test
|
||||
public void testNoArgCtorYieldsEmptyModel() throws Exception {
|
||||
assertEquals(0, new ModelMap().size());
|
||||
}
|
||||
|
@ -49,6 +51,7 @@ public final class ModelMapTests extends TestCase {
|
|||
/*
|
||||
* SPR-2185 - Null model assertion causes backwards compatibility issue
|
||||
*/
|
||||
@Test
|
||||
public void testAddNullObjectWithExplicitKey() throws Exception {
|
||||
ModelMap model = new ModelMap();
|
||||
model.addAttribute("foo", null);
|
||||
|
@ -59,12 +62,14 @@ public final class ModelMapTests extends TestCase {
|
|||
/*
|
||||
* SPR-2185 - Null model assertion causes backwards compatibility issue
|
||||
*/
|
||||
@Test
|
||||
public void testAddNullObjectViaCtorWithExplicitKey() throws Exception {
|
||||
ModelMap model = new ModelMap("foo", null);
|
||||
assertTrue(model.containsKey("foo"));
|
||||
assertNull(model.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamedObjectCtor() throws Exception {
|
||||
ModelMap model = new ModelMap("foo", "bing");
|
||||
assertEquals(1, model.size());
|
||||
|
@ -73,6 +78,7 @@ public final class ModelMapTests extends TestCase {
|
|||
assertEquals("bing", bing);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnnamedCtorScalar() throws Exception {
|
||||
ModelMap model = new ModelMap("foo", "bing");
|
||||
assertEquals(1, model.size());
|
||||
|
@ -81,6 +87,7 @@ public final class ModelMapTests extends TestCase {
|
|||
assertEquals("bing", bing);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneArgCtorWithScalar() throws Exception {
|
||||
ModelMap model = new ModelMap("bing");
|
||||
assertEquals(1, model.size());
|
||||
|
@ -89,14 +96,13 @@ public final class ModelMapTests extends TestCase {
|
|||
assertEquals("bing", string);
|
||||
}
|
||||
|
||||
public void testOneArgCtorWithNull() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class, "Null model arguments added without a name being explicitly supplied are not allowed.") {
|
||||
public void test() throws Exception {
|
||||
new ModelMap(null);
|
||||
}
|
||||
}.runTest();
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testOneArgCtorWithNull() {
|
||||
//Null model arguments added without a name being explicitly supplied are not allowed
|
||||
new ModelMap(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneArgCtorWithCollection() throws Exception {
|
||||
ModelMap model = new ModelMap(new String[]{"foo", "boing"});
|
||||
assertEquals(1, model.size());
|
||||
|
@ -107,21 +113,21 @@ public final class ModelMapTests extends TestCase {
|
|||
assertEquals("boing", strings[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneArgCtorWithEmptyCollection() throws Exception {
|
||||
ModelMap model = new ModelMap(new HashSet());
|
||||
ModelMap model = new ModelMap(new HashSet<Object>());
|
||||
// must not add if collection is empty...
|
||||
assertEquals(0, model.size());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testAddObjectWithNull() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class, "Null model arguments added without a name being explicitly supplied are not allowed.") {
|
||||
public void test() throws Exception {
|
||||
ModelMap model = new ModelMap();
|
||||
model.addAttribute(null);
|
||||
}
|
||||
}.runTest();
|
||||
// Null model arguments added without a name being explicitly supplied are not allowed
|
||||
ModelMap model = new ModelMap();
|
||||
model.addAttribute(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddObjectWithEmptyArray() throws Exception {
|
||||
ModelMap model = new ModelMap(new int[]{});
|
||||
assertEquals(1, model.size());
|
||||
|
@ -130,32 +136,33 @@ public final class ModelMapTests extends TestCase {
|
|||
assertEquals(0, ints.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAllObjectsWithNullMap() throws Exception {
|
||||
ModelMap model = new ModelMap();
|
||||
model.addAllAttributes((Map) null);
|
||||
model.addAllAttributes((Map<String, ?>) null);
|
||||
assertEquals(0, model.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAllObjectsWithNullCollection() throws Exception {
|
||||
ModelMap model = new ModelMap();
|
||||
model.addAllAttributes((Collection) null);
|
||||
model.addAllAttributes((Collection<Object>) null);
|
||||
assertEquals(0, model.size());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testAddAllObjectsWithSparseArrayList() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class, "Null model arguments added without a name being explicitly supplied are not allowed.") {
|
||||
public void test() throws Exception {
|
||||
ModelMap model = new ModelMap();
|
||||
ArrayList list = new ArrayList();
|
||||
list.add("bing");
|
||||
list.add(null);
|
||||
model.addAllAttributes(list);
|
||||
}
|
||||
}.runTest();
|
||||
// Null model arguments added without a name being explicitly supplied are not allowed
|
||||
ModelMap model = new ModelMap();
|
||||
ArrayList<String> list = new ArrayList<String>();
|
||||
list.add("bing");
|
||||
list.add(null);
|
||||
model.addAllAttributes(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddMap() throws Exception {
|
||||
Map map = new HashMap();
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("one", "one-value");
|
||||
map.put("two", "two-value");
|
||||
ModelMap model = new ModelMap();
|
||||
|
@ -165,6 +172,7 @@ public final class ModelMapTests extends TestCase {
|
|||
assertTrue(model.containsKey(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddObjectNoKeyOfSameTypeOverrides() throws Exception {
|
||||
ModelMap model = new ModelMap();
|
||||
model.addAttribute("foo");
|
||||
|
@ -174,8 +182,9 @@ public final class ModelMapTests extends TestCase {
|
|||
assertEquals("bar", bar);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddListOfTheSameObjects() throws Exception {
|
||||
List beans = new ArrayList();
|
||||
List<TestBean> beans = new ArrayList<TestBean>();
|
||||
beans.add(new TestBean("one"));
|
||||
beans.add(new TestBean("two"));
|
||||
beans.add(new TestBean("three"));
|
||||
|
@ -184,8 +193,9 @@ public final class ModelMapTests extends TestCase {
|
|||
assertEquals(1, model.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeMapWithOverriding() throws Exception {
|
||||
Map beans = new HashMap();
|
||||
Map<String, TestBean> beans = new HashMap<String, TestBean>();
|
||||
beans.put("one", new TestBean("one"));
|
||||
beans.put("two", new TestBean("two"));
|
||||
beans.put("three", new TestBean("three"));
|
||||
|
@ -196,6 +206,7 @@ public final class ModelMapTests extends TestCase {
|
|||
assertEquals("oneOld", ((TestBean) model.get("one")).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInnerClass() throws Exception {
|
||||
ModelMap map = new ModelMap();
|
||||
SomeInnerClass inner = new SomeInnerClass();
|
||||
|
@ -203,6 +214,7 @@ public final class ModelMapTests extends TestCase {
|
|||
assertSame(inner, map.get("someInnerClass"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInnerClassWithTwoUpperCaseLetters() throws Exception {
|
||||
ModelMap map = new ModelMap();
|
||||
UKInnerClass inner = new UKInnerClass();
|
||||
|
@ -210,6 +222,7 @@ public final class ModelMapTests extends TestCase {
|
|||
assertSame(inner, map.get("UKInnerClass"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAopCglibProxy() throws Exception {
|
||||
ModelMap map = new ModelMap();
|
||||
ProxyFactory factory = new ProxyFactory();
|
||||
|
@ -221,10 +234,11 @@ public final class ModelMapTests extends TestCase {
|
|||
assertEquals(date, map.get("date"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAopJdkProxy() throws Exception {
|
||||
ModelMap map = new ModelMap();
|
||||
ProxyFactory factory = new ProxyFactory();
|
||||
Map target = new HashMap();
|
||||
Map<?, ?> target = new HashMap<Object, Object>();
|
||||
factory.setTarget(target);
|
||||
factory.addInterface(Map.class);
|
||||
Object proxy = factory.getProxy();
|
||||
|
@ -232,9 +246,10 @@ public final class ModelMapTests extends TestCase {
|
|||
assertSame(proxy, map.get("map"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAopJdkProxyWithMultipleInterfaces() throws Exception {
|
||||
ModelMap map = new ModelMap();
|
||||
Map target = new HashMap();
|
||||
Map<?, ?> target = new HashMap<Object, Object>();
|
||||
ProxyFactory factory = new ProxyFactory();
|
||||
factory.setTarget(target);
|
||||
factory.addInterface(Serializable.class);
|
||||
|
@ -246,15 +261,17 @@ public final class ModelMapTests extends TestCase {
|
|||
assertSame(proxy, map.get("map"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAopJdkProxyWithDetectedInterfaces() throws Exception {
|
||||
ModelMap map = new ModelMap();
|
||||
Map target = new HashMap();
|
||||
Map<?, ?> target = new HashMap<Object, Object>();
|
||||
ProxyFactory factory = new ProxyFactory(target);
|
||||
Object proxy = factory.getProxy();
|
||||
map.addAttribute(proxy);
|
||||
assertSame(proxy, map.get("map"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRawJdkProxy() throws Exception {
|
||||
ModelMap map = new ModelMap();
|
||||
Object proxy = Proxy.newProxyInstance(
|
|
@ -16,37 +16,35 @@
|
|||
|
||||
package org.springframework.validation;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.test.AssertThrows;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ValidationUtils}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Rick Evans
|
||||
* @author Chris Beams
|
||||
* @since 08.10.2004
|
||||
*/
|
||||
public class ValidationUtilsTests extends TestCase {
|
||||
public class ValidationUtilsTests {
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testInvokeValidatorWithNullValidator() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
TestBean tb = new TestBean();
|
||||
Errors errors = new BeanPropertyBindingResult(tb, "tb");
|
||||
ValidationUtils.invokeValidator(null, tb, errors);
|
||||
}
|
||||
}.runTest();
|
||||
TestBean tb = new TestBean();
|
||||
Errors errors = new BeanPropertyBindingResult(tb, "tb");
|
||||
ValidationUtils.invokeValidator(null, tb, errors);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testInvokeValidatorWithNullErrors() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
TestBean tb = new TestBean();
|
||||
ValidationUtils.invokeValidator(new EmptyValidator(), tb, null);
|
||||
}
|
||||
}.runTest();
|
||||
TestBean tb = new TestBean();
|
||||
ValidationUtils.invokeValidator(new EmptyValidator(), tb, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeValidatorSunnyDay() throws Exception {
|
||||
TestBean tb = new TestBean();
|
||||
Errors errors = new BeanPropertyBindingResult(tb, "tb");
|
||||
|
@ -55,6 +53,7 @@ public class ValidationUtilsTests extends TestCase {
|
|||
assertEquals("EMPTY", errors.getFieldError("name").getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationUtilsSunnyDay() throws Exception {
|
||||
TestBean tb = new TestBean("");
|
||||
|
||||
|
@ -70,6 +69,7 @@ public class ValidationUtilsTests extends TestCase {
|
|||
assertFalse(errors.hasFieldErrors("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationUtilsNull() throws Exception {
|
||||
TestBean tb = new TestBean();
|
||||
Errors errors = new BeanPropertyBindingResult(tb, "tb");
|
||||
|
@ -79,6 +79,7 @@ public class ValidationUtilsTests extends TestCase {
|
|||
assertEquals("EMPTY", errors.getFieldError("name").getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationUtilsEmpty() throws Exception {
|
||||
TestBean tb = new TestBean("");
|
||||
Errors errors = new BeanPropertyBindingResult(tb, "tb");
|
||||
|
@ -88,6 +89,7 @@ public class ValidationUtilsTests extends TestCase {
|
|||
assertEquals("EMPTY", errors.getFieldError("name").getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationUtilsEmptyVariants() {
|
||||
TestBean tb = new TestBean();
|
||||
|
||||
|
@ -105,6 +107,7 @@ public class ValidationUtilsTests extends TestCase {
|
|||
assertEquals("msg", errors.getFieldError("name").getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationUtilsEmptyOrWhitespace() throws Exception {
|
||||
TestBean tb = new TestBean();
|
||||
Validator testValidator = new EmptyOrWhitespaceValidator();
|
||||
|
@ -136,6 +139,7 @@ public class ValidationUtilsTests extends TestCase {
|
|||
assertFalse(errors.hasFieldErrors("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationUtilsEmptyOrWhitespaceVariants() {
|
||||
TestBean tb = new TestBean();
|
||||
tb.setName(" ");
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 21.08.2003
|
||||
*/
|
||||
public class DerivedTestBean extends TestBean implements Serializable {
|
||||
|
||||
private String beanName;
|
||||
|
||||
private boolean initialized;
|
||||
|
||||
private boolean destroyed;
|
||||
|
||||
|
||||
public DerivedTestBean() {
|
||||
}
|
||||
|
||||
public DerivedTestBean(String[] names) {
|
||||
if (names == null || names.length < 2) {
|
||||
throw new IllegalArgumentException("Invalid names array");
|
||||
}
|
||||
setName(names[0]);
|
||||
setBeanName(names[1]);
|
||||
}
|
||||
|
||||
public static DerivedTestBean create(String[] names) {
|
||||
return new DerivedTestBean(names);
|
||||
}
|
||||
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
if (this.beanName == null || beanName == null) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setSpouseRef(String name) {
|
||||
setSpouse(new TestBean(name));
|
||||
}
|
||||
|
||||
|
||||
public void initialize() {
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
public boolean wasInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
|
||||
public void destroy() {
|
||||
this.destroyed = true;
|
||||
}
|
||||
|
||||
public boolean wasDestroyed() {
|
||||
return destroyed;
|
||||
}
|
||||
|
||||
}
|
|
@ -28,7 +28,7 @@ import java.util.List;
|
|||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.junit.Ignore;
|
||||
import org.springframework.beans.DerivedTestBean;
|
||||
import org.springframework.beans.IOther;
|
||||
import org.springframework.beans.ITestBean;
|
||||
|
@ -129,14 +129,15 @@ public class ClassUtilsTests extends TestCase {
|
|||
assertEquals("Class name did not match", "ClassUtilsTests.InnerClass", className);
|
||||
}
|
||||
|
||||
public void testGetShortNameForCglibClass() {
|
||||
TestBean tb = new TestBean();
|
||||
ProxyFactory pf = new ProxyFactory();
|
||||
pf.setTarget(tb);
|
||||
pf.setProxyTargetClass(true);
|
||||
TestBean proxy = (TestBean) pf.getProxy();
|
||||
String className = ClassUtils.getShortName(proxy.getClass());
|
||||
assertEquals("Class name did not match", "TestBean", className);
|
||||
@Ignore
|
||||
public void ignoreTestGetShortNameForCglibClass() {
|
||||
// TestBean tb = new TestBean();
|
||||
// ProxyFactory pf = new ProxyFactory();
|
||||
// pf.setTarget(tb);
|
||||
// pf.setProxyTargetClass(true);
|
||||
// TestBean proxy = (TestBean) pf.getProxy();
|
||||
// String className = ClassUtils.getShortName(proxy.getClass());
|
||||
// assertEquals("Class name did not match", "TestBean", className);
|
||||
}
|
||||
|
||||
public void testGetShortNameAsProperty() {
|
||||
|
@ -253,7 +254,7 @@ public class ClassUtilsTests extends TestCase {
|
|||
public void testGetAllInterfaces() {
|
||||
DerivedTestBean testBean = new DerivedTestBean();
|
||||
List ifcs = Arrays.asList(ClassUtils.getAllInterfaces(testBean));
|
||||
assertEquals("Correct number of interfaces", 7, ifcs.size());
|
||||
assertEquals("Correct number of interfaces", 4, ifcs.size());
|
||||
assertTrue("Contains Serializable", ifcs.contains(Serializable.class));
|
||||
assertTrue("Contains ITestBean", ifcs.contains(ITestBean.class));
|
||||
assertTrue("Contains IOther", ifcs.contains(IOther.class));
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.util.comparator;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PropertyComparator}
|
||||
*
|
||||
* @see org.springframework.beans.support.PropertyComparatorTests
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class ComparatorTests {
|
||||
|
||||
@Test
|
||||
public void testComparableComparator() {
|
||||
Comparator<String> c = new ComparableComparator<String>();
|
||||
String s1 = "abc";
|
||||
String s2 = "cde";
|
||||
assertTrue(c.compare(s1, s2) < 0);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testComparableComparatorIllegalArgs() {
|
||||
Comparator c = new ComparableComparator();
|
||||
Object o1 = new Object();
|
||||
Object o2 = new Object();
|
||||
try {
|
||||
c.compare(o1, o2);
|
||||
}
|
||||
catch (ClassCastException e) {
|
||||
return;
|
||||
}
|
||||
fail("Comparator should have thrown a cce");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBooleanComparatorTrueLow() {
|
||||
Comparator<Boolean> c = BooleanComparator.TRUE_LOW;
|
||||
assertTrue(c.compare(new Boolean(true), new Boolean(false)) < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBooleanComparatorTrueHigh() {
|
||||
Comparator<Boolean> c = BooleanComparator.TRUE_HIGH;
|
||||
assertTrue(c.compare(new Boolean(true), new Boolean(false)) > 0);
|
||||
assertTrue(c.compare(Boolean.TRUE, Boolean.TRUE) == 0);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testNullSafeComparatorNullsLow() {
|
||||
Comparator<String> c = NullSafeComparator.NULLS_LOW;
|
||||
assertTrue(c.compare(null, "boo") < 0);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testNullSafeComparatorNullsHigh() {
|
||||
Comparator<String> c = NullSafeComparator.NULLS_HIGH;
|
||||
assertTrue(c.compare(null, "boo") > 0);
|
||||
assertTrue(c.compare(null, null) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompoundComparatorEmpty() {
|
||||
Comparator<String> c = new CompoundComparator<String>();
|
||||
try {
|
||||
c.compare("foo", "bar");
|
||||
}
|
||||
catch (IllegalStateException e) {
|
||||
return;
|
||||
}
|
||||
fail("illegal state should've been thrown on empty list");
|
||||
}
|
||||
|
||||
private static class Dog implements Comparable<Object> {
|
||||
|
||||
private String nickName;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
public int compareTo(Object o) {
|
||||
return nickName.compareTo(((Dog)o).nickName);
|
||||
}
|
||||
|
||||
public String getNickName() {
|
||||
return nickName;
|
||||
}
|
||||
|
||||
public void setNickName(String nickName) {
|
||||
this.nickName = nickName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
typeMismatch=Field {0} did not have correct type
|
|
@ -1,2 +0,0 @@
|
|||
typeMismatch=Field {0} did not have correct type
|
||||
age=Age
|
|
@ -1,2 +0,0 @@
|
|||
typeMismatch=Field {0} did not have correct type
|
||||
person.age=Person Age
|
|
@ -1,461 +0,0 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.web.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.context.support.ApplicationObjectSupport;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.context.request.WebRequestInterceptor;
|
||||
import org.springframework.web.context.support.RequestHandledEvent;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest;
|
||||
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
|
||||
import org.springframework.web.servlet.handler.SimpleServletHandlerAdapter;
|
||||
import org.springframework.web.servlet.handler.SimpleServletPostProcessor;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
import org.springframework.web.servlet.mvc.Controller;
|
||||
import org.springframework.web.servlet.mvc.ParameterizableViewController;
|
||||
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.SimpleFormController;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
import org.springframework.web.servlet.theme.SessionThemeResolver;
|
||||
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 21.05.2003
|
||||
*/
|
||||
public class ComplexWebApplicationContext extends StaticWebApplicationContext {
|
||||
|
||||
public void refresh() throws BeansException {
|
||||
registerSingleton(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, SessionLocaleResolver.class);
|
||||
registerSingleton(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, SessionThemeResolver.class);
|
||||
|
||||
LocaleChangeInterceptor interceptor1 = new LocaleChangeInterceptor();
|
||||
LocaleChangeInterceptor interceptor2 = new LocaleChangeInterceptor();
|
||||
interceptor2.setParamName("locale2");
|
||||
ThemeChangeInterceptor interceptor3 = new ThemeChangeInterceptor();
|
||||
ThemeChangeInterceptor interceptor4 = new ThemeChangeInterceptor();
|
||||
interceptor4.setParamName("theme2");
|
||||
UserRoleAuthorizationInterceptor interceptor5 = new UserRoleAuthorizationInterceptor();
|
||||
interceptor5.setAuthorizedRoles(new String[] {"role1", "role2"});
|
||||
|
||||
List interceptors = new ArrayList();
|
||||
interceptors.add(interceptor5);
|
||||
interceptors.add(interceptor1);
|
||||
interceptors.add(interceptor2);
|
||||
interceptors.add(interceptor3);
|
||||
interceptors.add(interceptor4);
|
||||
interceptors.add(new MyHandlerInterceptor1());
|
||||
interceptors.add(new MyHandlerInterceptor2());
|
||||
interceptors.add(new MyWebRequestInterceptor());
|
||||
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue(
|
||||
"mappings", "/view.do=viewHandler\n/locale.do=localeHandler\nloc.do=anotherLocaleHandler");
|
||||
pvs.addPropertyValue("interceptors", interceptors);
|
||||
registerSingleton("myUrlMapping1", SimpleUrlHandlerMapping.class, pvs);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue(
|
||||
"mappings", "/form.do=localeHandler\n/unknown.do=unknownHandler\nservlet.do=myServlet");
|
||||
pvs.addPropertyValue("order", "2");
|
||||
registerSingleton("myUrlMapping2", SimpleUrlHandlerMapping.class, pvs);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue(
|
||||
"mappings", "/form.do=formHandler\n/head.do=headController\n" +
|
||||
"body.do=bodyController\n/noview*=noviewController\n/noview/simple*=noviewController");
|
||||
pvs.addPropertyValue("order", "1");
|
||||
registerSingleton("handlerMapping", SimpleUrlHandlerMapping.class, pvs);
|
||||
|
||||
registerSingleton("myDummyAdapter", MyDummyAdapter.class);
|
||||
registerSingleton("myHandlerAdapter", MyHandlerAdapter.class);
|
||||
registerSingleton("standardHandlerAdapter", SimpleControllerHandlerAdapter.class);
|
||||
registerSingleton("noviewController", NoViewController.class);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("order", new Integer(0));
|
||||
pvs.addPropertyValue("basename", "org.springframework.web.servlet.complexviews");
|
||||
registerSingleton("viewResolver", ResourceBundleViewResolver.class, pvs);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("suffix", ".jsp");
|
||||
registerSingleton("viewResolver2", InternalResourceViewResolver.class, pvs);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("commandClass", "org.springframework.beans.TestBean");
|
||||
pvs.addPropertyValue("formView", "form");
|
||||
registerSingleton("formHandler", SimpleFormController.class, pvs);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("viewName", "form");
|
||||
registerSingleton("viewHandler", ParameterizableViewController.class, pvs);
|
||||
|
||||
registerSingleton("localeHandler", ComplexLocaleChecker.class);
|
||||
registerSingleton("anotherLocaleHandler", ComplexLocaleChecker.class);
|
||||
registerSingleton("unknownHandler", Object.class);
|
||||
|
||||
registerSingleton("headController", HeadController.class);
|
||||
registerSingleton("bodyController", BodyController.class);
|
||||
|
||||
registerSingleton("servletPostProcessor", SimpleServletPostProcessor.class);
|
||||
registerSingleton("handlerAdapter", SimpleServletHandlerAdapter.class);
|
||||
registerSingleton("myServlet", MyServlet.class);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("order", "1");
|
||||
pvs.addPropertyValue("exceptionMappings",
|
||||
"java.lang.IllegalAccessException=failed2\n" +
|
||||
"ServletRequestBindingException=failed3");
|
||||
pvs.addPropertyValue("defaultErrorView", "failed0");
|
||||
registerSingleton("exceptionResolver1", SimpleMappingExceptionResolver.class, pvs);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("order", "0");
|
||||
pvs.addPropertyValue("exceptionMappings", "java.lang.Exception=failed1");
|
||||
List mappedHandlers = new ManagedList();
|
||||
mappedHandlers.add(new RuntimeBeanReference("anotherLocaleHandler"));
|
||||
pvs.addPropertyValue("mappedHandlers", mappedHandlers);
|
||||
pvs.addPropertyValue("defaultStatusCode", "500");
|
||||
pvs.addPropertyValue("defaultErrorView", "failed2");
|
||||
registerSingleton("handlerExceptionResolver", SimpleMappingExceptionResolver.class, pvs);
|
||||
|
||||
registerSingleton("multipartResolver", MockMultipartResolver.class);
|
||||
registerSingleton("testListener", TestApplicationListener.class);
|
||||
|
||||
addMessage("test", Locale.ENGLISH, "test message");
|
||||
addMessage("test", Locale.CANADA, "Canadian & test message");
|
||||
|
||||
super.refresh();
|
||||
}
|
||||
|
||||
|
||||
public static class HeadController implements Controller {
|
||||
|
||||
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
if ("HEAD".equals(request.getMethod())) {
|
||||
response.setContentLength(5);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class BodyController implements Controller {
|
||||
|
||||
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
response.getOutputStream().write("body".getBytes());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class NoViewController implements Controller {
|
||||
|
||||
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
return new ModelAndView();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyServlet implements Servlet {
|
||||
|
||||
private ServletConfig servletConfig;
|
||||
|
||||
public void init(ServletConfig servletConfig) throws ServletException {
|
||||
this.servletConfig = servletConfig;
|
||||
}
|
||||
|
||||
public ServletConfig getServletConfig() {
|
||||
return servletConfig;
|
||||
}
|
||||
|
||||
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException {
|
||||
servletResponse.getOutputStream().write("body".getBytes());
|
||||
}
|
||||
|
||||
public String getServletInfo() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
this.servletConfig = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static interface MyHandler {
|
||||
|
||||
public void doSomething(HttpServletRequest request) throws ServletException, IllegalAccessException;
|
||||
|
||||
public long lastModified();
|
||||
}
|
||||
|
||||
|
||||
public static class MyHandlerAdapter extends ApplicationObjectSupport implements HandlerAdapter, Ordered {
|
||||
|
||||
public int getOrder() {
|
||||
return 99;
|
||||
}
|
||||
|
||||
public boolean supports(Object handler) {
|
||||
return handler != null && MyHandler.class.isAssignableFrom(handler.getClass());
|
||||
}
|
||||
|
||||
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object delegate)
|
||||
throws ServletException, IllegalAccessException {
|
||||
((MyHandler) delegate).doSomething(request);
|
||||
return null;
|
||||
}
|
||||
|
||||
public long getLastModified(HttpServletRequest request, Object delegate) {
|
||||
return ((MyHandler) delegate).lastModified();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyDummyAdapter extends ApplicationObjectSupport implements HandlerAdapter {
|
||||
|
||||
public boolean supports(Object handler) {
|
||||
return handler != null && MyHandler.class.isAssignableFrom(handler.getClass());
|
||||
}
|
||||
|
||||
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object delegate)
|
||||
throws IOException, ServletException {
|
||||
throw new ServletException("dummy");
|
||||
}
|
||||
|
||||
public long getLastModified(HttpServletRequest request, Object delegate) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyHandlerInterceptor1 implements HandlerInterceptor {
|
||||
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws ServletException {
|
||||
if (request.getAttribute("test2") != null) {
|
||||
throw new ServletException("Wrong interceptor order");
|
||||
}
|
||||
request.setAttribute("test1", "test1");
|
||||
request.setAttribute("test1x", "test1x");
|
||||
request.setAttribute("test1y", "test1y");
|
||||
return true;
|
||||
}
|
||||
|
||||
public void postHandle(
|
||||
HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
|
||||
throws ServletException {
|
||||
if (request.getAttribute("test2x") != null) {
|
||||
throw new ServletException("Wrong interceptor order");
|
||||
}
|
||||
if (!"test1x".equals(request.getAttribute("test1x"))) {
|
||||
throw new ServletException("Incorrect request attribute");
|
||||
}
|
||||
request.removeAttribute("test1x");
|
||||
}
|
||||
|
||||
public void afterCompletion(
|
||||
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
||||
throws ServletException {
|
||||
if (request.getAttribute("test2y") != null) {
|
||||
throw new ServletException("Wrong interceptor order");
|
||||
}
|
||||
request.removeAttribute("test1y");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyHandlerInterceptor2 implements HandlerInterceptor {
|
||||
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws ServletException {
|
||||
if (request.getAttribute("test1x") == null) {
|
||||
throw new ServletException("Wrong interceptor order");
|
||||
}
|
||||
if (request.getParameter("abort") != null) {
|
||||
return false;
|
||||
}
|
||||
request.setAttribute("test2", "test2");
|
||||
request.setAttribute("test2x", "test2x");
|
||||
request.setAttribute("test2y", "test2y");
|
||||
return true;
|
||||
}
|
||||
|
||||
public void postHandle(
|
||||
HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
|
||||
throws ServletException {
|
||||
if (request.getParameter("noView") != null) {
|
||||
modelAndView.clear();
|
||||
}
|
||||
if (request.getAttribute("test1x") == null) {
|
||||
throw new ServletException("Wrong interceptor order");
|
||||
}
|
||||
if (!"test2x".equals(request.getAttribute("test2x"))) {
|
||||
throw new ServletException("Incorrect request attribute");
|
||||
}
|
||||
request.removeAttribute("test2x");
|
||||
}
|
||||
|
||||
public void afterCompletion(
|
||||
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
||||
throws Exception {
|
||||
if (request.getAttribute("test1y") == null) {
|
||||
throw new ServletException("Wrong interceptor order");
|
||||
}
|
||||
request.removeAttribute("test2y");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyWebRequestInterceptor implements WebRequestInterceptor {
|
||||
|
||||
public void preHandle(WebRequest request) throws Exception {
|
||||
request.setAttribute("test3", "test3", WebRequest.SCOPE_REQUEST);
|
||||
}
|
||||
|
||||
public void postHandle(WebRequest request, ModelMap model) throws Exception {
|
||||
request.setAttribute("test3x", "test3x", WebRequest.SCOPE_REQUEST);
|
||||
}
|
||||
|
||||
public void afterCompletion(WebRequest request, Exception ex) throws Exception {
|
||||
request.setAttribute("test3y", "test3y", WebRequest.SCOPE_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ComplexLocaleChecker implements MyHandler {
|
||||
|
||||
public void doSomething(HttpServletRequest request) throws ServletException, IllegalAccessException {
|
||||
WebApplicationContext wac = RequestContextUtils.getWebApplicationContext(request);
|
||||
if (!(wac instanceof ComplexWebApplicationContext)) {
|
||||
throw new ServletException("Incorrect WebApplicationContext");
|
||||
}
|
||||
if (!(request instanceof MultipartHttpServletRequest)) {
|
||||
throw new ServletException("Not in a MultipartHttpServletRequest");
|
||||
}
|
||||
if (!(RequestContextUtils.getLocaleResolver(request) instanceof SessionLocaleResolver)) {
|
||||
throw new ServletException("Incorrect LocaleResolver");
|
||||
}
|
||||
if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
|
||||
throw new ServletException("Incorrect Locale");
|
||||
}
|
||||
if (!Locale.CANADA.equals(LocaleContextHolder.getLocale())) {
|
||||
throw new ServletException("Incorrect Locale");
|
||||
}
|
||||
if (!(RequestContextUtils.getThemeResolver(request) instanceof SessionThemeResolver)) {
|
||||
throw new ServletException("Incorrect ThemeResolver");
|
||||
}
|
||||
if (!"theme".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) {
|
||||
throw new ServletException("Incorrect theme name");
|
||||
}
|
||||
if (request.getParameter("fail") != null) {
|
||||
throw new ModelAndViewDefiningException(new ModelAndView("failed1"));
|
||||
}
|
||||
if (request.getParameter("access") != null) {
|
||||
throw new IllegalAccessException("illegal access");
|
||||
}
|
||||
if (request.getParameter("servlet") != null) {
|
||||
throw new ServletRequestBindingException("servlet");
|
||||
}
|
||||
if (request.getParameter("exception") != null) {
|
||||
throw new RuntimeException("servlet");
|
||||
}
|
||||
}
|
||||
|
||||
public long lastModified() {
|
||||
return 99;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MockMultipartResolver implements MultipartResolver {
|
||||
|
||||
public boolean isMultipart(HttpServletRequest request) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
|
||||
if (request.getAttribute("fail") != null) {
|
||||
throw new MaxUploadSizeExceededException(1000);
|
||||
}
|
||||
if (request instanceof MultipartHttpServletRequest) {
|
||||
throw new IllegalStateException("Already a multipart request");
|
||||
}
|
||||
if (request.getAttribute("resolved") != null) {
|
||||
throw new IllegalStateException("Already resolved");
|
||||
}
|
||||
request.setAttribute("resolved", Boolean.TRUE);
|
||||
return new AbstractMultipartHttpServletRequest(request) {
|
||||
};
|
||||
}
|
||||
|
||||
public void cleanupMultipart(MultipartHttpServletRequest request) {
|
||||
if (request.getAttribute("cleanedUp") != null) {
|
||||
throw new IllegalStateException("Already cleaned up");
|
||||
}
|
||||
request.setAttribute("cleanedUp", Boolean.TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class TestApplicationListener implements ApplicationListener {
|
||||
|
||||
public int counter = 0;
|
||||
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
if (event instanceof RequestHandledEvent) {
|
||||
this.counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,118 +0,0 @@
|
|||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.web.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.context.support.StaticMessageSource;
|
||||
import org.springframework.ui.context.Theme;
|
||||
import org.springframework.ui.context.ThemeSource;
|
||||
import org.springframework.ui.context.support.SimpleTheme;
|
||||
import org.springframework.ui.context.support.UiApplicationContextUtils;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
||||
import org.springframework.web.servlet.mvc.Controller;
|
||||
import org.springframework.web.servlet.mvc.LastModified;
|
||||
import org.springframework.web.servlet.mvc.SimpleFormController;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
import org.springframework.web.servlet.theme.AbstractThemeResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.XmlViewResolver;
|
||||
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 21.05.2003
|
||||
*/
|
||||
public class SimpleWebApplicationContext extends StaticWebApplicationContext {
|
||||
|
||||
public void refresh() throws BeansException {
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("commandClass", "org.springframework.beans.TestBean");
|
||||
pvs.addPropertyValue("formView", "form");
|
||||
registerSingleton("/form.do", SimpleFormController.class, pvs);
|
||||
|
||||
registerSingleton("/locale.do", LocaleChecker.class);
|
||||
|
||||
addMessage("test", Locale.ENGLISH, "test message");
|
||||
addMessage("test", Locale.CANADA, "Canadian & test message");
|
||||
addMessage("testArgs", Locale.ENGLISH, "test {0} message {1}");
|
||||
addMessage("testArgsFormat", Locale.ENGLISH, "test {0} message {1,number,#.##} X");
|
||||
|
||||
registerSingleton(UiApplicationContextUtils.THEME_SOURCE_BEAN_NAME, DummyThemeSource.class);
|
||||
|
||||
registerSingleton("handlerMapping", BeanNameUrlHandlerMapping.class);
|
||||
registerSingleton("viewResolver", InternalResourceViewResolver.class);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("location", "org/springframework/web/context/WEB-INF/sessionContext.xml");
|
||||
registerSingleton("viewResolver2", XmlViewResolver.class, pvs);
|
||||
|
||||
super.refresh();
|
||||
}
|
||||
|
||||
|
||||
public static class LocaleChecker implements Controller, LastModified {
|
||||
|
||||
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
if (!(RequestContextUtils.getWebApplicationContext(request) instanceof SimpleWebApplicationContext)) {
|
||||
throw new ServletException("Incorrect WebApplicationContext");
|
||||
}
|
||||
if (!(RequestContextUtils.getLocaleResolver(request) instanceof AcceptHeaderLocaleResolver)) {
|
||||
throw new ServletException("Incorrect LocaleResolver");
|
||||
}
|
||||
if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
|
||||
throw new ServletException("Incorrect Locale");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public long getLastModified(HttpServletRequest request) {
|
||||
return 98;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class DummyThemeSource implements ThemeSource {
|
||||
|
||||
private StaticMessageSource messageSource;
|
||||
|
||||
public DummyThemeSource() {
|
||||
this.messageSource = new StaticMessageSource();
|
||||
this.messageSource.addMessage("themetest", Locale.ENGLISH, "theme test message");
|
||||
this.messageSource.addMessage("themetestArgs", Locale.ENGLISH, "theme test message {0}");
|
||||
}
|
||||
|
||||
public Theme getTheme(String themeName) {
|
||||
if (AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME.equals(themeName)) {
|
||||
return new SimpleTheme(AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME, this.messageSource);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -14,5 +14,9 @@
|
|||
<classpathentry kind="var" path="IVY_CACHE/javax.portlet/com.springsource.javax.portlet/1.0.0/com.springsource.javax.portlet-1.0.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.fileupload/1.2.0/com.springsource.org.apache.commons.fileupload-1.2.0.jar" sourcepath="/IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.fileupload/1.2.0/com.springsource.org.apache.commons.fileupload-sources-1.2.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-1.1.1.jar" sourcepath="/IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-sources-1.1.1.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.junit/com.springsource.org.junit/4.5.0/com.springsource.org.junit-4.5.0.jar" sourcepath="/IVY_CACHE/org.junit/com.springsource.org.junit/4.5.0/com.springsource.org.junit-sources-4.5.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.easymock/com.springsource.org.easymock/2.3.0/com.springsource.org.easymock-2.3.0.jar" sourcepath="/IVY_CACHE/org.easymock/com.springsource.org.easymock/2.3.0/com.springsource.org.easymock-sources-2.3.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.objectweb.asm/com.springsource.org.objectweb.asm/2.2.3/com.springsource.org.objectweb.asm-2.2.3.jar" sourcepath="/IVY_CACHE/org.objectweb.asm/com.springsource.org.objectweb.asm/2.2.3/com.springsource.org.objectweb.asm-sources-2.2.3.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.objectweb.asm/com.springsource.org.objectweb.asm.commons/2.2.3/com.springsource.org.objectweb.asm.commons-2.2.3.jar" sourcepath="/IVY_CACHE/org.objectweb.asm/com.springsource.org.objectweb.asm.commons/2.2.3/com.springsource.org.objectweb.asm.commons-sources-2.2.3.jar"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
|
|
|
@ -29,6 +29,10 @@
|
|||
<dependency org="org.springframework" name="org.springframework.core" rev="latest.integration" conf="compile->compile"/>
|
||||
<dependency org="org.springframework" name="org.springframework.web" rev="latest.integration" conf="compile->compile"/>
|
||||
<dependency org="org.springframework" name="org.springframework.web.servlet" rev="latest.integration" conf="compile->compile"/>
|
||||
<dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm" rev="2.2.3" conf="test->runtime" />
|
||||
<dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm.commons" rev="2.2.3" conf="test->runtime" />
|
||||
<dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" conf="test->compile"/>
|
||||
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.5.0" conf="test->runtime"/>
|
||||
</dependencies>
|
||||
|
||||
</ivy-module>
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.mock.web;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.servlet.ServletInputStream;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Delegating implementation of {@link javax.servlet.ServletInputStream}.
|
||||
*
|
||||
* <p>Used by {@link org.springframework.mock.web.MockHttpServletRequest}; typically not directly
|
||||
* used for testing application controllers.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.0.2
|
||||
* @see org.springframework.mock.web.MockHttpServletRequest
|
||||
*/
|
||||
public class DelegatingServletInputStream extends ServletInputStream {
|
||||
|
||||
private final InputStream sourceStream;
|
||||
|
||||
|
||||
/**
|
||||
* Create a DelegatingServletInputStream for the given source stream.
|
||||
* @param sourceStream the source stream (never <code>null</code>)
|
||||
*/
|
||||
public DelegatingServletInputStream(InputStream sourceStream) {
|
||||
Assert.notNull(sourceStream, "Source InputStream must not be null");
|
||||
this.sourceStream = sourceStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the underlying source stream (never <code>null</code>).
|
||||
*/
|
||||
public final InputStream getSourceStream() {
|
||||
return this.sourceStream;
|
||||
}
|
||||
|
||||
|
||||
public int read() throws IOException {
|
||||
return this.sourceStream.read();
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
super.close();
|
||||
this.sourceStream.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans;
|
||||
|
||||
import org.springframework.core.enums.ShortCodedLabeledEnum;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
*/
|
||||
public class Colour extends ShortCodedLabeledEnum {
|
||||
|
||||
public static final Colour RED = new Colour(0, "RED");
|
||||
public static final Colour BLUE = new Colour(1, "BLUE");
|
||||
public static final Colour GREEN = new Colour(2, "GREEN");
|
||||
public static final Colour PURPLE = new Colour(3, "PURPLE");
|
||||
|
||||
private Colour(int code, String label) {
|
||||
super(code, label);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 21.08.2003
|
||||
*/
|
||||
public class DerivedTestBean extends TestBean implements Serializable, BeanNameAware, DisposableBean {
|
||||
|
||||
private String beanName;
|
||||
|
||||
private boolean initialized;
|
||||
|
||||
private boolean destroyed;
|
||||
|
||||
|
||||
public DerivedTestBean() {
|
||||
}
|
||||
|
||||
public DerivedTestBean(String[] names) {
|
||||
if (names == null || names.length < 2) {
|
||||
throw new IllegalArgumentException("Invalid names array");
|
||||
}
|
||||
setName(names[0]);
|
||||
setBeanName(names[1]);
|
||||
}
|
||||
|
||||
public static DerivedTestBean create(String[] names) {
|
||||
return new DerivedTestBean(names);
|
||||
}
|
||||
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
if (this.beanName == null || beanName == null) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setSpouseRef(String name) {
|
||||
setSpouse(new TestBean(name));
|
||||
}
|
||||
|
||||
|
||||
public void initialize() {
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
public boolean wasInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
|
||||
public void destroy() {
|
||||
this.destroyed = true;
|
||||
}
|
||||
|
||||
public boolean wasDestroyed() {
|
||||
return destroyed;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans;
|
||||
|
||||
public interface INestedTestBean {
|
||||
|
||||
public String getCompany();
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans;
|
||||
|
||||
public interface IOther {
|
||||
|
||||
void absquatulate();
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Interface used for {@link org.springframework.beans.TestBean}.
|
||||
*
|
||||
* <p>Two methods are the same as on Person, but if this
|
||||
* extends person it breaks quite a few tests..
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public interface ITestBean {
|
||||
|
||||
int getAge();
|
||||
|
||||
void setAge(int age);
|
||||
|
||||
String getName();
|
||||
|
||||
void setName(String name);
|
||||
|
||||
ITestBean getSpouse();
|
||||
|
||||
void setSpouse(ITestBean spouse);
|
||||
|
||||
ITestBean[] getSpouses();
|
||||
|
||||
String[] getStringArray();
|
||||
|
||||
void setStringArray(String[] stringArray);
|
||||
|
||||
/**
|
||||
* Throws a given (non-null) exception.
|
||||
*/
|
||||
void exceptional(Throwable t) throws Throwable;
|
||||
|
||||
Object returnsThis();
|
||||
|
||||
INestedTestBean getDoctor();
|
||||
|
||||
INestedTestBean getLawyer();
|
||||
|
||||
IndexedTestBean getNestedIndexedBean();
|
||||
|
||||
/**
|
||||
* Increment the age by one.
|
||||
* @return the previous age
|
||||
*/
|
||||
int haveBirthday();
|
||||
|
||||
void unreliableFileOperation() throws IOException;
|
||||
|
||||
}
|
|
@ -0,0 +1,145 @@
|
|||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 11.11.2003
|
||||
*/
|
||||
public class IndexedTestBean {
|
||||
|
||||
private TestBean[] array;
|
||||
|
||||
private Collection collection;
|
||||
|
||||
private List list;
|
||||
|
||||
private Set set;
|
||||
|
||||
private SortedSet sortedSet;
|
||||
|
||||
private Map map;
|
||||
|
||||
private SortedMap sortedMap;
|
||||
|
||||
|
||||
public IndexedTestBean() {
|
||||
this(true);
|
||||
}
|
||||
|
||||
public IndexedTestBean(boolean populate) {
|
||||
if (populate) {
|
||||
populate();
|
||||
}
|
||||
}
|
||||
|
||||
public void populate() {
|
||||
TestBean tb0 = new TestBean("name0", 0);
|
||||
TestBean tb1 = new TestBean("name1", 0);
|
||||
TestBean tb2 = new TestBean("name2", 0);
|
||||
TestBean tb3 = new TestBean("name3", 0);
|
||||
TestBean tb4 = new TestBean("name4", 0);
|
||||
TestBean tb5 = new TestBean("name5", 0);
|
||||
TestBean tb6 = new TestBean("name6", 0);
|
||||
TestBean tb7 = new TestBean("name7", 0);
|
||||
TestBean tbX = new TestBean("nameX", 0);
|
||||
TestBean tbY = new TestBean("nameY", 0);
|
||||
this.array = new TestBean[] {tb0, tb1};
|
||||
this.list = new ArrayList();
|
||||
this.list.add(tb2);
|
||||
this.list.add(tb3);
|
||||
this.set = new TreeSet();
|
||||
this.set.add(tb6);
|
||||
this.set.add(tb7);
|
||||
this.map = new HashMap();
|
||||
this.map.put("key1", tb4);
|
||||
this.map.put("key2", tb5);
|
||||
this.map.put("key.3", tb5);
|
||||
List list = new ArrayList();
|
||||
list.add(tbX);
|
||||
list.add(tbY);
|
||||
this.map.put("key4", list);
|
||||
}
|
||||
|
||||
|
||||
public TestBean[] getArray() {
|
||||
return array;
|
||||
}
|
||||
|
||||
public void setArray(TestBean[] array) {
|
||||
this.array = array;
|
||||
}
|
||||
|
||||
public Collection getCollection() {
|
||||
return collection;
|
||||
}
|
||||
|
||||
public void setCollection(Collection collection) {
|
||||
this.collection = collection;
|
||||
}
|
||||
|
||||
public List getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public Set getSet() {
|
||||
return set;
|
||||
}
|
||||
|
||||
public void setSet(Set set) {
|
||||
this.set = set;
|
||||
}
|
||||
|
||||
public SortedSet getSortedSet() {
|
||||
return sortedSet;
|
||||
}
|
||||
|
||||
public void setSortedSet(SortedSet sortedSet) {
|
||||
this.sortedSet = sortedSet;
|
||||
}
|
||||
|
||||
public Map getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setMap(Map map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public SortedMap getSortedMap() {
|
||||
return sortedMap;
|
||||
}
|
||||
|
||||
public void setSortedMap(SortedMap sortedMap) {
|
||||
this.sortedMap = sortedMap;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans;
|
||||
|
||||
/**
|
||||
* Simple nested test bean used for testing bean factories, AOP framework etc.
|
||||
*
|
||||
* @author Trevor D. Cook
|
||||
* @since 30.09.2003
|
||||
*/
|
||||
public class NestedTestBean implements INestedTestBean {
|
||||
|
||||
private String company = "";
|
||||
|
||||
public NestedTestBean() {
|
||||
}
|
||||
|
||||
public NestedTestBean(String company) {
|
||||
setCompany(company);
|
||||
}
|
||||
|
||||
public void setCompany(String company) {
|
||||
this.company = (company != null ? company : "");
|
||||
}
|
||||
|
||||
public String getCompany() {
|
||||
return company;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof NestedTestBean)) {
|
||||
return false;
|
||||
}
|
||||
NestedTestBean ntb = (NestedTestBean) obj;
|
||||
return this.company.equals(ntb.company);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.company.hashCode();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "NestedTestBean: " + this.company;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,437 @@
|
|||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Simple test bean used for testing bean factories, the AOP framework etc.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @since 15 April 2001
|
||||
*/
|
||||
public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOther, Comparable {
|
||||
|
||||
private String beanName;
|
||||
|
||||
private String country;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
private boolean postProcessed;
|
||||
|
||||
private String name;
|
||||
|
||||
private String sex;
|
||||
|
||||
private int age;
|
||||
|
||||
private boolean jedi;
|
||||
|
||||
private ITestBean[] spouses;
|
||||
|
||||
private String touchy;
|
||||
|
||||
private String[] stringArray;
|
||||
|
||||
private Integer[] someIntegerArray;
|
||||
|
||||
private Date date = new Date();
|
||||
|
||||
private Float myFloat = new Float(0.0);
|
||||
|
||||
private Collection friends = new LinkedList();
|
||||
|
||||
private Set someSet = new HashSet();
|
||||
|
||||
private Map someMap = new HashMap();
|
||||
|
||||
private List someList = new ArrayList();
|
||||
|
||||
private Properties someProperties = new Properties();
|
||||
|
||||
private INestedTestBean doctor = new NestedTestBean();
|
||||
|
||||
private INestedTestBean lawyer = new NestedTestBean();
|
||||
|
||||
private IndexedTestBean nestedIndexedBean;
|
||||
|
||||
private boolean destroyed;
|
||||
|
||||
private Number someNumber;
|
||||
|
||||
private Colour favouriteColour;
|
||||
|
||||
private Boolean someBoolean;
|
||||
|
||||
private List otherColours;
|
||||
|
||||
private List pets;
|
||||
|
||||
|
||||
public TestBean() {
|
||||
}
|
||||
|
||||
public TestBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public TestBean(ITestBean spouse) {
|
||||
this.spouses = new ITestBean[] {spouse};
|
||||
}
|
||||
|
||||
public TestBean(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public TestBean(ITestBean spouse, Properties someProperties) {
|
||||
this.spouses = new ITestBean[] {spouse};
|
||||
this.someProperties = someProperties;
|
||||
}
|
||||
|
||||
public TestBean(List someList) {
|
||||
this.someList = someList;
|
||||
}
|
||||
|
||||
public TestBean(Set someSet) {
|
||||
this.someSet = someSet;
|
||||
}
|
||||
|
||||
public TestBean(Map someMap) {
|
||||
this.someMap = someMap;
|
||||
}
|
||||
|
||||
public TestBean(Properties someProperties) {
|
||||
this.someProperties = someProperties;
|
||||
}
|
||||
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
public BeanFactory getBeanFactory() {
|
||||
return beanFactory;
|
||||
}
|
||||
|
||||
public void setPostProcessed(boolean postProcessed) {
|
||||
this.postProcessed = postProcessed;
|
||||
}
|
||||
|
||||
public boolean isPostProcessed() {
|
||||
return postProcessed;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSex() {
|
||||
return sex;
|
||||
}
|
||||
|
||||
public void setSex(String sex) {
|
||||
this.sex = sex;
|
||||
if (this.name == null) {
|
||||
this.name = sex;
|
||||
}
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public boolean isJedi() {
|
||||
return jedi;
|
||||
}
|
||||
|
||||
public void setJedi(boolean jedi) {
|
||||
this.jedi = jedi;
|
||||
}
|
||||
|
||||
public ITestBean getSpouse() {
|
||||
return (spouses != null ? spouses[0] : null);
|
||||
}
|
||||
|
||||
public void setSpouse(ITestBean spouse) {
|
||||
this.spouses = new ITestBean[] {spouse};
|
||||
}
|
||||
|
||||
public ITestBean[] getSpouses() {
|
||||
return spouses;
|
||||
}
|
||||
|
||||
public String getTouchy() {
|
||||
return touchy;
|
||||
}
|
||||
|
||||
public void setTouchy(String touchy) throws Exception {
|
||||
if (touchy.indexOf('.') != -1) {
|
||||
throw new Exception("Can't contain a .");
|
||||
}
|
||||
if (touchy.indexOf(',') != -1) {
|
||||
throw new NumberFormatException("Number format exception: contains a ,");
|
||||
}
|
||||
this.touchy = touchy;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String[] getStringArray() {
|
||||
return stringArray;
|
||||
}
|
||||
|
||||
public void setStringArray(String[] stringArray) {
|
||||
this.stringArray = stringArray;
|
||||
}
|
||||
|
||||
public Integer[] getSomeIntegerArray() {
|
||||
return someIntegerArray;
|
||||
}
|
||||
|
||||
public void setSomeIntegerArray(Integer[] someIntegerArray) {
|
||||
this.someIntegerArray = someIntegerArray;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Float getMyFloat() {
|
||||
return myFloat;
|
||||
}
|
||||
|
||||
public void setMyFloat(Float myFloat) {
|
||||
this.myFloat = myFloat;
|
||||
}
|
||||
|
||||
public Collection getFriends() {
|
||||
return friends;
|
||||
}
|
||||
|
||||
public void setFriends(Collection friends) {
|
||||
this.friends = friends;
|
||||
}
|
||||
|
||||
public Set getSomeSet() {
|
||||
return someSet;
|
||||
}
|
||||
|
||||
public void setSomeSet(Set someSet) {
|
||||
this.someSet = someSet;
|
||||
}
|
||||
|
||||
public Map getSomeMap() {
|
||||
return someMap;
|
||||
}
|
||||
|
||||
public void setSomeMap(Map someMap) {
|
||||
this.someMap = someMap;
|
||||
}
|
||||
|
||||
public List getSomeList() {
|
||||
return someList;
|
||||
}
|
||||
|
||||
public void setSomeList(List someList) {
|
||||
this.someList = someList;
|
||||
}
|
||||
|
||||
public Properties getSomeProperties() {
|
||||
return someProperties;
|
||||
}
|
||||
|
||||
public void setSomeProperties(Properties someProperties) {
|
||||
this.someProperties = someProperties;
|
||||
}
|
||||
|
||||
public INestedTestBean getDoctor() {
|
||||
return doctor;
|
||||
}
|
||||
|
||||
public void setDoctor(INestedTestBean doctor) {
|
||||
this.doctor = doctor;
|
||||
}
|
||||
|
||||
public INestedTestBean getLawyer() {
|
||||
return lawyer;
|
||||
}
|
||||
|
||||
public void setLawyer(INestedTestBean lawyer) {
|
||||
this.lawyer = lawyer;
|
||||
}
|
||||
|
||||
public Number getSomeNumber() {
|
||||
return someNumber;
|
||||
}
|
||||
|
||||
public void setSomeNumber(Number someNumber) {
|
||||
this.someNumber = someNumber;
|
||||
}
|
||||
|
||||
public Colour getFavouriteColour() {
|
||||
return favouriteColour;
|
||||
}
|
||||
|
||||
public void setFavouriteColour(Colour favouriteColour) {
|
||||
this.favouriteColour = favouriteColour;
|
||||
}
|
||||
|
||||
public Boolean getSomeBoolean() {
|
||||
return someBoolean;
|
||||
}
|
||||
|
||||
public void setSomeBoolean(Boolean someBoolean) {
|
||||
this.someBoolean = someBoolean;
|
||||
}
|
||||
|
||||
public IndexedTestBean getNestedIndexedBean() {
|
||||
return nestedIndexedBean;
|
||||
}
|
||||
|
||||
public void setNestedIndexedBean(IndexedTestBean nestedIndexedBean) {
|
||||
this.nestedIndexedBean = nestedIndexedBean;
|
||||
}
|
||||
|
||||
public List getOtherColours() {
|
||||
return otherColours;
|
||||
}
|
||||
|
||||
public void setOtherColours(List otherColours) {
|
||||
this.otherColours = otherColours;
|
||||
}
|
||||
|
||||
public List getPets() {
|
||||
return pets;
|
||||
}
|
||||
|
||||
public void setPets(List pets) {
|
||||
this.pets = pets;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.ITestBean#exceptional(Throwable)
|
||||
*/
|
||||
public void exceptional(Throwable t) throws Throwable {
|
||||
if (t != null) {
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
public void unreliableFileOperation() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
/**
|
||||
* @see org.springframework.beans.ITestBean#returnsThis()
|
||||
*/
|
||||
public Object returnsThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.IOther#absquatulate()
|
||||
*/
|
||||
public void absquatulate() {
|
||||
}
|
||||
|
||||
public int haveBirthday() {
|
||||
return age++;
|
||||
}
|
||||
|
||||
|
||||
public void destroy() {
|
||||
this.destroyed = true;
|
||||
}
|
||||
|
||||
public boolean wasDestroyed() {
|
||||
return destroyed;
|
||||
}
|
||||
|
||||
|
||||
public boolean equals(Object other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
if (other == null || !(other instanceof TestBean)) {
|
||||
return false;
|
||||
}
|
||||
TestBean tb2 = (TestBean) other;
|
||||
return (ObjectUtils.nullSafeEquals(this.name, tb2.name) && this.age == tb2.age);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.age;
|
||||
}
|
||||
|
||||
public int compareTo(Object other) {
|
||||
if (this.name != null && other instanceof TestBean) {
|
||||
return this.name.compareTo(((TestBean) other).getName());
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans.factory;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
|
||||
/**
|
||||
* Simple factory to allow testing of FactoryBean support in AbstractBeanFactory.
|
||||
* Depending on whether its singleton property is set, it will return a singleton
|
||||
* or a prototype instance.
|
||||
*
|
||||
* <p>Implements InitializingBean interface, so we can check that
|
||||
* factories get this lifecycle callback if they want.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @since 10.03.2003
|
||||
*/
|
||||
public class DummyFactory
|
||||
implements FactoryBean, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
|
||||
|
||||
public static final String SINGLETON_NAME = "Factory singleton";
|
||||
|
||||
private static boolean prototypeCreated;
|
||||
|
||||
/**
|
||||
* Clear static state.
|
||||
*/
|
||||
public static void reset() {
|
||||
prototypeCreated = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Default is for factories to return a singleton instance.
|
||||
*/
|
||||
private boolean singleton = true;
|
||||
|
||||
private String beanName;
|
||||
|
||||
private AutowireCapableBeanFactory beanFactory;
|
||||
|
||||
private boolean postProcessed;
|
||||
|
||||
private boolean initialized;
|
||||
|
||||
private TestBean testBean;
|
||||
|
||||
private TestBean otherTestBean;
|
||||
|
||||
|
||||
public DummyFactory() {
|
||||
this.testBean = new TestBean();
|
||||
this.testBean.setName(SINGLETON_NAME);
|
||||
this.testBean.setAge(25);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if the bean managed by this factory is a singleton.
|
||||
* @see FactoryBean#isSingleton()
|
||||
*/
|
||||
public boolean isSingleton() {
|
||||
return this.singleton;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if the bean managed by this factory is a singleton.
|
||||
*/
|
||||
public void setSingleton(boolean singleton) {
|
||||
this.singleton = singleton;
|
||||
}
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = (AutowireCapableBeanFactory) beanFactory;
|
||||
this.beanFactory.applyBeanPostProcessorsBeforeInitialization(this.testBean, this.beanName);
|
||||
}
|
||||
|
||||
public BeanFactory getBeanFactory() {
|
||||
return beanFactory;
|
||||
}
|
||||
|
||||
public void setPostProcessed(boolean postProcessed) {
|
||||
this.postProcessed = postProcessed;
|
||||
}
|
||||
|
||||
public boolean isPostProcessed() {
|
||||
return postProcessed;
|
||||
}
|
||||
|
||||
public void setOtherTestBean(TestBean otherTestBean) {
|
||||
this.otherTestBean = otherTestBean;
|
||||
this.testBean.setSpouse(otherTestBean);
|
||||
}
|
||||
|
||||
public TestBean getOtherTestBean() {
|
||||
return otherTestBean;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
if (initialized) {
|
||||
throw new RuntimeException("Cannot call afterPropertiesSet twice on the one bean");
|
||||
}
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Was this initialized by invocation of the
|
||||
* afterPropertiesSet() method from the InitializingBean interface?
|
||||
*/
|
||||
public boolean wasInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
public static boolean wasPrototypeCreated() {
|
||||
return prototypeCreated;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the managed object, supporting both singleton
|
||||
* and prototype mode.
|
||||
* @see FactoryBean#getObject()
|
||||
*/
|
||||
public Object getObject() throws BeansException {
|
||||
if (isSingleton()) {
|
||||
return this.testBean;
|
||||
}
|
||||
else {
|
||||
TestBean prototype = new TestBean("prototype created at " + System.currentTimeMillis(), 11);
|
||||
if (this.beanFactory != null) {
|
||||
this.beanFactory.applyBeanPostProcessorsBeforeInitialization(prototype, this.beanName);
|
||||
}
|
||||
prototypeCreated = true;
|
||||
return prototype;
|
||||
}
|
||||
}
|
||||
|
||||
public Class getObjectType() {
|
||||
return TestBean.class;
|
||||
}
|
||||
|
||||
|
||||
public void destroy() {
|
||||
if (this.testBean != null) {
|
||||
this.testBean.setName(null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,158 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans.factory;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
|
||||
/**
|
||||
* Simple test of BeanFactory initialization and lifecycle callbacks.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Colin Sampaleanu
|
||||
* @since 12.03.2003
|
||||
*/
|
||||
public class LifecycleBean implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
|
||||
|
||||
protected boolean initMethodDeclared = false;
|
||||
|
||||
protected String beanName;
|
||||
|
||||
protected BeanFactory owningFactory;
|
||||
|
||||
protected boolean postProcessedBeforeInit;
|
||||
|
||||
protected boolean inited;
|
||||
|
||||
protected boolean initedViaDeclaredInitMethod;
|
||||
|
||||
protected boolean postProcessedAfterInit;
|
||||
|
||||
protected boolean destroyed;
|
||||
|
||||
|
||||
public void setInitMethodDeclared(boolean initMethodDeclared) {
|
||||
this.initMethodDeclared = initMethodDeclared;
|
||||
}
|
||||
|
||||
public boolean isInitMethodDeclared() {
|
||||
return initMethodDeclared;
|
||||
}
|
||||
|
||||
public void setBeanName(String name) {
|
||||
this.beanName = name;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.owningFactory = beanFactory;
|
||||
}
|
||||
|
||||
public void postProcessBeforeInit() {
|
||||
if (this.inited || this.initedViaDeclaredInitMethod) {
|
||||
throw new RuntimeException("Factory called postProcessBeforeInit after afterPropertiesSet");
|
||||
}
|
||||
if (this.postProcessedBeforeInit) {
|
||||
throw new RuntimeException("Factory called postProcessBeforeInit twice");
|
||||
}
|
||||
this.postProcessedBeforeInit = true;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
if (this.owningFactory == null) {
|
||||
throw new RuntimeException("Factory didn't call setBeanFactory before afterPropertiesSet on lifecycle bean");
|
||||
}
|
||||
if (!this.postProcessedBeforeInit) {
|
||||
throw new RuntimeException("Factory didn't call postProcessBeforeInit before afterPropertiesSet on lifecycle bean");
|
||||
}
|
||||
if (this.initedViaDeclaredInitMethod) {
|
||||
throw new RuntimeException("Factory initialized via declared init method before initializing via afterPropertiesSet");
|
||||
}
|
||||
if (this.inited) {
|
||||
throw new RuntimeException("Factory called afterPropertiesSet twice");
|
||||
}
|
||||
this.inited = true;
|
||||
}
|
||||
|
||||
public void declaredInitMethod() {
|
||||
if (!this.inited) {
|
||||
throw new RuntimeException("Factory didn't call afterPropertiesSet before declared init method");
|
||||
}
|
||||
|
||||
if (this.initedViaDeclaredInitMethod) {
|
||||
throw new RuntimeException("Factory called declared init method twice");
|
||||
}
|
||||
this.initedViaDeclaredInitMethod = true;
|
||||
}
|
||||
|
||||
public void postProcessAfterInit() {
|
||||
if (!this.inited) {
|
||||
throw new RuntimeException("Factory called postProcessAfterInit before afterPropertiesSet");
|
||||
}
|
||||
if (this.initMethodDeclared && !this.initedViaDeclaredInitMethod) {
|
||||
throw new RuntimeException("Factory called postProcessAfterInit before calling declared init method");
|
||||
}
|
||||
if (this.postProcessedAfterInit) {
|
||||
throw new RuntimeException("Factory called postProcessAfterInit twice");
|
||||
}
|
||||
this.postProcessedAfterInit = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dummy business method that will fail unless the factory
|
||||
* managed the bean's lifecycle correctly
|
||||
*/
|
||||
public void businessMethod() {
|
||||
if (!this.inited || (this.initMethodDeclared && !this.initedViaDeclaredInitMethod) ||
|
||||
!this.postProcessedAfterInit) {
|
||||
throw new RuntimeException("Factory didn't initialize lifecycle object correctly");
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
if (this.destroyed) {
|
||||
throw new IllegalStateException("Already destroyed");
|
||||
}
|
||||
this.destroyed = true;
|
||||
}
|
||||
|
||||
public boolean isDestroyed() {
|
||||
return destroyed;
|
||||
}
|
||||
|
||||
|
||||
public static class PostProcessor implements BeanPostProcessor {
|
||||
|
||||
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
|
||||
if (bean instanceof LifecycleBean) {
|
||||
((LifecycleBean) bean).postProcessBeforeInit();
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
|
||||
if (bean instanceof LifecycleBean) {
|
||||
((LifecycleBean) bean).postProcessAfterInit();
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.beans.factory;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
/**
|
||||
* Simple test of BeanFactory initialization
|
||||
* @author Rod Johnson
|
||||
* @since 12.03.2003
|
||||
*/
|
||||
public class MustBeInitialized implements InitializingBean {
|
||||
|
||||
private boolean inited;
|
||||
|
||||
/**
|
||||
* @see InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.inited = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dummy business method that will fail unless the factory
|
||||
* managed the bean's lifecycle correctly
|
||||
*/
|
||||
public void businessMethod() {
|
||||
if (!this.inited)
|
||||
throw new RuntimeException("Factory didn't call afterPropertiesSet() on MustBeInitialized object");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.context;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.context.NoSuchMessageException;
|
||||
|
||||
public class ACATester implements ApplicationContextAware {
|
||||
|
||||
private ApplicationContext ac;
|
||||
|
||||
public void setApplicationContext(ApplicationContext ctx) throws ApplicationContextException {
|
||||
// check reinitialization
|
||||
if (this.ac != null) {
|
||||
throw new IllegalStateException("Already initialized");
|
||||
}
|
||||
|
||||
// check message source availability
|
||||
if (ctx != null) {
|
||||
try {
|
||||
ctx.getMessage("code1", null, Locale.getDefault());
|
||||
}
|
||||
catch (NoSuchMessageException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
this.ac = ctx;
|
||||
}
|
||||
|
||||
public ApplicationContext getApplicationContext() {
|
||||
return ac;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.context;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class BeanThatBroadcasts implements ApplicationContextAware {
|
||||
|
||||
public ApplicationContext applicationContext;
|
||||
|
||||
public int receivedCount;
|
||||
|
||||
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
if (applicationContext.getDisplayName().indexOf("listener") != -1) {
|
||||
applicationContext.getBean("listener");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.context;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A stub {@link ApplicationListener}.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class BeanThatListens implements ApplicationListener {
|
||||
|
||||
private BeanThatBroadcasts beanThatBroadcasts;
|
||||
|
||||
private int eventCount;
|
||||
|
||||
|
||||
public BeanThatListens() {
|
||||
}
|
||||
|
||||
public BeanThatListens(BeanThatBroadcasts beanThatBroadcasts) {
|
||||
this.beanThatBroadcasts = beanThatBroadcasts;
|
||||
Map beans = beanThatBroadcasts.applicationContext.getBeansOfType(BeanThatListens.class);
|
||||
if (!beans.isEmpty()) {
|
||||
throw new IllegalStateException("Shouldn't have found any BeanThatListens instances");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
eventCount++;
|
||||
if (beanThatBroadcasts != null) {
|
||||
beanThatBroadcasts.receivedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
public int getEventCount() {
|
||||
return eventCount;
|
||||
}
|
||||
|
||||
public void zero() {
|
||||
eventCount = 0;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package org.springframework.context;
|
||||
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.LifecycleBean;
|
||||
|
||||
/**
|
||||
* Simple bean to test ApplicationContext lifecycle methods for beans
|
||||
*
|
||||
* @author Colin Sampaleanu
|
||||
* @since 03.07.2004
|
||||
*/
|
||||
public class LifecycleContextBean extends LifecycleBean implements ApplicationContextAware {
|
||||
|
||||
protected ApplicationContext owningContext;
|
||||
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
super.setBeanFactory(beanFactory);
|
||||
if (this.owningContext != null)
|
||||
throw new RuntimeException("Factory called setBeanFactory after setApplicationContext");
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
super.afterPropertiesSet();
|
||||
if (this.owningContext == null)
|
||||
throw new RuntimeException("Factory didn't call setAppliationContext before afterPropertiesSet on lifecycle bean");
|
||||
}
|
||||
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if (this.owningFactory == null)
|
||||
throw new RuntimeException("Factory called setApplicationContext before setBeanFactory");
|
||||
|
||||
this.owningContext = applicationContext;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package org.springframework.context;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
/**
|
||||
* Listener that maintains a global count of events.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @since January 21, 2001
|
||||
*/
|
||||
public class TestListener implements ApplicationListener {
|
||||
|
||||
private int eventCount;
|
||||
|
||||
public int getEventCount() {
|
||||
return eventCount;
|
||||
}
|
||||
|
||||
public void zeroCounter() {
|
||||
eventCount = 0;
|
||||
}
|
||||
|
||||
public void onApplicationEvent(ApplicationEvent e) {
|
||||
++eventCount;
|
||||
}
|
||||
|
||||
}
|
|
@ -16,39 +16,38 @@
|
|||
|
||||
package org.springframework.web.portlet.context;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.portlet.PortletRequest;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.portlet.MockPortletRequest;
|
||||
import org.springframework.mock.web.portlet.MockPortletSession;
|
||||
import org.springframework.test.AssertThrows;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
|
||||
/**
|
||||
* @author Rick Evans
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class PortletRequestAttributesTests extends TestCase {
|
||||
public class PortletRequestAttributesTests {
|
||||
|
||||
private static final String KEY = "ThatThingThatThing";
|
||||
|
||||
|
||||
private static final Serializable VALUE = new Serializable() {
|
||||
};
|
||||
@SuppressWarnings("serial")
|
||||
private static final Serializable VALUE = new Serializable() { };
|
||||
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCtorRejectsNullArg() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
new PortletRequestAttributes(null);
|
||||
}
|
||||
}.runTest();
|
||||
new PortletRequestAttributes(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateAccessedAttributes() throws Exception {
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
|
@ -60,6 +59,7 @@ public class PortletRequestAttributesTests extends TestCase {
|
|||
attrs.requestCompleted();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetRequestScopedAttribute() throws Exception {
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
PortletRequestAttributes attrs = new PortletRequestAttributes(request);
|
||||
|
@ -68,6 +68,7 @@ public class PortletRequestAttributesTests extends TestCase {
|
|||
assertSame(VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetRequestScopedAttributeAfterCompletion() throws Exception {
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
PortletRequestAttributes attrs = new PortletRequestAttributes(request);
|
||||
|
@ -81,6 +82,7 @@ public class PortletRequestAttributesTests extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSessionScopedAttribute() throws Exception {
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
|
@ -92,6 +94,7 @@ public class PortletRequestAttributesTests extends TestCase {
|
|||
assertSame(VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSessionScopedAttributeAfterCompletion() throws Exception {
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
|
@ -105,6 +108,7 @@ public class PortletRequestAttributesTests extends TestCase {
|
|||
assertSame(VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetGlobalSessionScopedAttribute() throws Exception {
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
|
@ -116,6 +120,7 @@ public class PortletRequestAttributesTests extends TestCase {
|
|||
assertSame(VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetGlobalSessionScopedAttributeAfterCompletion() throws Exception {
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
|
@ -129,20 +134,20 @@ public class PortletRequestAttributesTests extends TestCase {
|
|||
assertSame(VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception {
|
||||
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
|
||||
PortletRequest request = (PortletRequest) mockRequest.getMock();
|
||||
request.getPortletSession(false);
|
||||
mockRequest.setReturnValue(null, 1);
|
||||
mockRequest.replay();
|
||||
PortletRequest request = createMock(PortletRequest.class);
|
||||
expect(request.getPortletSession(false)).andReturn(null);
|
||||
replay(request);
|
||||
|
||||
PortletRequestAttributes attrs = new PortletRequestAttributes(request);
|
||||
Object value = attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION);
|
||||
assertNull(value);
|
||||
|
||||
mockRequest.verify();
|
||||
verify(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveSessionScopedAttribute() throws Exception {
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
|
@ -154,17 +159,16 @@ public class PortletRequestAttributesTests extends TestCase {
|
|||
assertNull(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception {
|
||||
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
|
||||
PortletRequest request = (PortletRequest) mockRequest.getMock();
|
||||
request.getPortletSession(false);
|
||||
mockRequest.setReturnValue(null, 1);
|
||||
mockRequest.replay();
|
||||
PortletRequest request = createMock(PortletRequest.class);
|
||||
expect(request.getPortletSession(false)).andReturn(null);
|
||||
replay(request);
|
||||
|
||||
PortletRequestAttributes attrs = new PortletRequestAttributes(request);
|
||||
attrs.removeAttribute(KEY, RequestAttributes.SCOPE_SESSION);
|
||||
|
||||
mockRequest.verify();
|
||||
verify(request);
|
||||
}
|
||||
|
||||
}
|
|
@ -16,24 +16,39 @@
|
|||
|
||||
package org.springframework.web.portlet.mvc;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.portlet.ActionRequest;
|
||||
import javax.portlet.ActionResponse;
|
||||
import javax.portlet.Portlet;
|
||||
import javax.portlet.PortletConfig;
|
||||
import javax.portlet.PortletException;
|
||||
import javax.portlet.RenderRequest;
|
||||
import javax.portlet.RenderResponse;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.mock.web.portlet.*;
|
||||
import org.springframework.test.AssertThrows;
|
||||
import org.springframework.mock.web.portlet.MockActionRequest;
|
||||
import org.springframework.mock.web.portlet.MockActionResponse;
|
||||
import org.springframework.mock.web.portlet.MockPortletConfig;
|
||||
import org.springframework.mock.web.portlet.MockPortletContext;
|
||||
import org.springframework.mock.web.portlet.MockRenderRequest;
|
||||
import org.springframework.mock.web.portlet.MockRenderResponse;
|
||||
import org.springframework.web.portlet.context.ConfigurablePortletApplicationContext;
|
||||
import org.springframework.web.portlet.context.StaticPortletApplicationContext;
|
||||
|
||||
import javax.portlet.*;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link PortletWrappingController} class.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Rick Evans
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class PortletWrappingControllerTests extends TestCase {
|
||||
public final class PortletWrappingControllerTests {
|
||||
|
||||
private static final String RESULT_RENDER_PARAMETER_NAME = "result";
|
||||
private static final String PORTLET_WRAPPING_CONTROLLER_BEAN_NAME = "controller";
|
||||
|
@ -44,6 +59,7 @@ public final class PortletWrappingControllerTests extends TestCase {
|
|||
private PortletWrappingController controller;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
ConfigurablePortletApplicationContext applicationContext = new MyApplicationContext();
|
||||
MockPortletConfig config = new MockPortletConfig(new MockPortletContext(), "wrappedPortlet");
|
||||
|
@ -53,6 +69,7 @@ public final class PortletWrappingControllerTests extends TestCase {
|
|||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testActionRequest() throws Exception {
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
|
@ -62,6 +79,7 @@ public final class PortletWrappingControllerTests extends TestCase {
|
|||
assertEquals("myPortlet-action", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRenderRequest() throws Exception {
|
||||
MockRenderRequest request = new MockRenderRequest();
|
||||
MockRenderResponse response = new MockRenderResponse();
|
||||
|
@ -70,48 +88,37 @@ public final class PortletWrappingControllerTests extends TestCase {
|
|||
assertEquals(RENDERED_RESPONSE_CONTENT, result);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testActionRequestWithNoParameters() throws Exception {
|
||||
final MockActionRequest request = new MockActionRequest();
|
||||
final MockActionResponse response = new MockActionResponse();
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
controller.handleActionRequest(request, response);
|
||||
}
|
||||
}.runTest();
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
controller.handleActionRequest(request, response);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testRejectsPortletClassThatDoesNotImplementPortletInterface() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletWrappingController controller = new PortletWrappingController();
|
||||
controller.setPortletClass(String.class);
|
||||
controller.afterPropertiesSet();
|
||||
}
|
||||
}.runTest();
|
||||
PortletWrappingController controller = new PortletWrappingController();
|
||||
controller.setPortletClass(String.class);
|
||||
controller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testRejectsIfPortletClassIsNotSupplied() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletWrappingController controller = new PortletWrappingController();
|
||||
controller.setPortletClass(null);
|
||||
controller.afterPropertiesSet();
|
||||
}
|
||||
}.runTest();
|
||||
PortletWrappingController controller = new PortletWrappingController();
|
||||
controller.setPortletClass(null);
|
||||
controller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
public void testDestroyingTheControllerPropagatesDestroyToWrappedPortlet() throws Exception {
|
||||
final PortletWrappingController controller = new PortletWrappingController();
|
||||
controller.setPortletClass(MyPortlet.class);
|
||||
controller.afterPropertiesSet();
|
||||
// test for destroy() call being propagated via exception being thrown :(
|
||||
new AssertThrows(IllegalStateException.class) {
|
||||
public void test() throws Exception {
|
||||
controller.destroy();
|
||||
}
|
||||
}.runTest();
|
||||
controller.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPortletName() throws Exception {
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
|
@ -121,6 +128,7 @@ public final class PortletWrappingControllerTests extends TestCase {
|
|||
assertEquals("wrappedPortlet", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelegationToMockPortletConfigIfSoConfigured() throws Exception {
|
||||
|
||||
final String BEAN_NAME = "Sixpence None The Richer";
|
|
@ -16,6 +16,9 @@
|
|||
|
||||
package org.springframework.web.portlet.util;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Collections;
|
||||
|
@ -27,31 +30,27 @@ import javax.portlet.PortletContext;
|
|||
import javax.portlet.PortletRequest;
|
||||
import javax.portlet.PortletSession;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.mock.easymock.AbstractScalarMockTemplate;
|
||||
import org.springframework.mock.web.portlet.MockActionRequest;
|
||||
import org.springframework.mock.web.portlet.MockActionResponse;
|
||||
import org.springframework.mock.web.portlet.MockPortletContext;
|
||||
import org.springframework.mock.web.portlet.MockPortletRequest;
|
||||
import org.springframework.mock.web.portlet.MockPortletSession;
|
||||
import org.springframework.test.AssertThrows;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PortletUtils}.
|
||||
*
|
||||
* @author Rick Evans
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class PortletUtilsTests extends TestCase {
|
||||
public final class PortletUtilsTests {
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testGetTempDirWithNullPortletContext() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class, "null PortletContext passed as argument") {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getTempDir(null);
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.getTempDir(null);
|
||||
}
|
||||
|
||||
public void testGetTempDirSunnyDay() throws Exception {
|
||||
|
@ -64,48 +63,36 @@ public final class PortletUtilsTests extends TestCase {
|
|||
public void testGetRealPathInterpretsLocationAsRelativeToWebAppRootIfPathDoesNotBeginWithALeadingSlash() throws Exception {
|
||||
final String originalPath = "web/foo";
|
||||
final String expectedRealPath = "/" + originalPath;
|
||||
new AbstractScalarMockTemplate(PortletContext.class) {
|
||||
public void setupExpectations(MockControl mockControl, Object mockObject) throws Exception {
|
||||
PortletContext ctx = (PortletContext) mockObject;
|
||||
ctx.getRealPath(expectedRealPath);
|
||||
mockControl.setReturnValue(expectedRealPath);
|
||||
}
|
||||
public void doTest(Object mockObject) throws Exception {
|
||||
PortletContext ctx = (PortletContext) mockObject;
|
||||
String actualRealPath = PortletUtils.getRealPath(ctx, originalPath);
|
||||
assertEquals(expectedRealPath, actualRealPath);
|
||||
}
|
||||
}.test();
|
||||
PortletContext ctx = createMock(PortletContext.class);
|
||||
expect(ctx.getRealPath(expectedRealPath)).andReturn(expectedRealPath);
|
||||
replay(ctx);
|
||||
|
||||
String actualRealPath = PortletUtils.getRealPath(ctx, originalPath);
|
||||
assertEquals(expectedRealPath, actualRealPath);
|
||||
|
||||
verify(ctx);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testGetRealPathWithNullPortletContext() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getRealPath(null, "/foo");
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.getRealPath(null, "/foo");
|
||||
}
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void testGetRealPathWithNullPath() throws Exception {
|
||||
new AssertThrows(NullPointerException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getRealPath(new MockPortletContext(), null);
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.getRealPath(new MockPortletContext(), null);
|
||||
}
|
||||
|
||||
@Test(expected=FileNotFoundException.class)
|
||||
public void testGetRealPathWithPathThatCannotBeResolvedToFile() throws Exception {
|
||||
new AssertThrows(FileNotFoundException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getRealPath(new MockPortletContext() {
|
||||
public String getRealPath(String path) {
|
||||
return null;
|
||||
}
|
||||
}, "/rubbish");
|
||||
PortletUtils.getRealPath(new MockPortletContext() {
|
||||
public String getRealPath(String path) {
|
||||
return null;
|
||||
}
|
||||
}.runTest();
|
||||
}, "/rubbish");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassAllParametersToRenderPhase() throws Exception {
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
request.setParameter("William", "Baskerville");
|
||||
|
@ -116,6 +103,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
request.getParameterMap().size(), response.getRenderParameterMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetParametersStartingWith() throws Exception {
|
||||
final String targetPrefix = "francisan_";
|
||||
final String badKey = "dominican_Bernard";
|
||||
|
@ -123,7 +111,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
request.setParameter(targetPrefix + "William", "Baskerville");
|
||||
request.setParameter(targetPrefix + "Adso", "Melk");
|
||||
request.setParameter(badKey, "Gui");
|
||||
Map actualParameters = PortletUtils.getParametersStartingWith(request, targetPrefix);
|
||||
Map<?, ?> actualParameters = PortletUtils.getParametersStartingWith(request, targetPrefix);
|
||||
assertNotNull("PortletUtils.getParametersStartingWith(..) must never return a null Map", actualParameters);
|
||||
assertEquals("Obviously not finding all of the correct parameters", 2, actualParameters.size());
|
||||
assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("William"));
|
||||
|
@ -132,6 +120,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
actualParameters.containsKey(badKey));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetParametersStartingWithUnpicksScalarParameterValues() throws Exception {
|
||||
final String targetPrefix = "francisan_";
|
||||
final String badKey = "dominican_Bernard";
|
||||
|
@ -139,7 +128,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
request.setParameter(targetPrefix + "William", "Baskerville");
|
||||
request.setParameter(targetPrefix + "Adso", new String[]{"Melk", "Of Melk"});
|
||||
request.setParameter(badKey, "Gui");
|
||||
Map actualParameters = PortletUtils.getParametersStartingWith(request, targetPrefix);
|
||||
Map<?, ?> actualParameters = PortletUtils.getParametersStartingWith(request, targetPrefix);
|
||||
assertNotNull("PortletUtils.getParametersStartingWith(..) must never return a null Map", actualParameters);
|
||||
assertEquals("Obviously not finding all of the correct parameters", 2, actualParameters.size());
|
||||
assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("William"));
|
||||
|
@ -150,12 +139,13 @@ public final class PortletUtilsTests extends TestCase {
|
|||
actualParameters.containsKey(badKey));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetParametersStartingWithYieldsEverythingIfTargetPrefixIsNull() throws Exception {
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
request.setParameter("William", "Baskerville");
|
||||
request.setParameter("Adso", "Melk");
|
||||
request.setParameter("dominican_Bernard", "Gui");
|
||||
Map actualParameters = PortletUtils.getParametersStartingWith(request, null);
|
||||
Map<?, ?> actualParameters = PortletUtils.getParametersStartingWith(request, null);
|
||||
assertNotNull("PortletUtils.getParametersStartingWith(..) must never return a null Map", actualParameters);
|
||||
assertEquals("Obviously not finding all of the correct parameters", request.getParameterMap().size(), actualParameters.size());
|
||||
assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("William"));
|
||||
|
@ -163,12 +153,13 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("dominican_Bernard"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetParametersStartingWithYieldsEverythingIfTargetPrefixIsTheEmptyString() throws Exception {
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
request.setParameter("William", "Baskerville");
|
||||
request.setParameter("Adso", "Melk");
|
||||
request.setParameter("dominican_Bernard", "Gui");
|
||||
Map actualParameters = PortletUtils.getParametersStartingWith(request, "");
|
||||
Map<?, ?> actualParameters = PortletUtils.getParametersStartingWith(request, "");
|
||||
assertNotNull("PortletUtils.getParametersStartingWith(..) must never return a null Map", actualParameters);
|
||||
assertEquals("Obviously not finding all of the correct parameters", request.getParameterMap().size(), actualParameters.size());
|
||||
assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("William"));
|
||||
|
@ -176,14 +167,16 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertTrue("Obviously not finding all of the correct parameters", actualParameters.containsKey("dominican_Bernard"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetParametersStartingWithYieldsEmptyNonNullMapWhenNoParamaterExistInRequest() throws Exception {
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
Map actualParameters = PortletUtils.getParametersStartingWith(request, null);
|
||||
Map<?, ?> actualParameters = PortletUtils.getParametersStartingWith(request, null);
|
||||
assertNotNull("PortletUtils.getParametersStartingWith(..) must never return a null Map", actualParameters);
|
||||
assertEquals("Obviously finding some parameters from somewhere (incorrectly)",
|
||||
request.getParameterMap().size(), actualParameters.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSubmitParameterWithStraightNameMatch() throws Exception {
|
||||
final String targetSubmitParameter = "William";
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
|
@ -195,6 +188,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertEquals(targetSubmitParameter, submitParameter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSubmitParameterWithPrefixedParameterMatch() throws Exception {
|
||||
final String bareParameterName = "William";
|
||||
final String targetParameterName = bareParameterName + WebUtils.SUBMIT_IMAGE_SUFFIXES[0];
|
||||
|
@ -206,6 +200,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertEquals(targetParameterName, submitParameter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSubmitParameterWithNoParameterMatchJustReturnsNull() throws Exception {
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
request.setParameter("Bill", "Baskerville");
|
||||
|
@ -214,18 +209,16 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertNull(submitParameter);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testGetSubmitParameterWithNullRequest() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
final String targetSubmitParameter = "William";
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
request.setParameter(targetSubmitParameter, "Baskerville");
|
||||
request.setParameter("Adso", "Melk");
|
||||
PortletUtils.getSubmitParameter(null, targetSubmitParameter);
|
||||
}
|
||||
}.runTest();
|
||||
final String targetSubmitParameter = "William";
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
request.setParameter(targetSubmitParameter, "Baskerville");
|
||||
request.setParameter("Adso", "Melk");
|
||||
PortletUtils.getSubmitParameter(null, targetSubmitParameter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassAllParametersToRenderPhaseDoesNotPropagateExceptionIfRedirectAlreadySentAtTimeOfCall() throws Exception {
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
request.setParameter("William", "Baskerville");
|
||||
|
@ -240,6 +233,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
0, response.getRenderParameterMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClearAllRenderParameters() throws Exception {
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
response.setRenderParameter("William", "Baskerville");
|
||||
|
@ -249,8 +243,10 @@ public final class PortletUtilsTests extends TestCase {
|
|||
0, response.getRenderParameterMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClearAllRenderParametersDoesNotPropagateExceptionIfRedirectAlreadySentAtTimeOfCall() throws Exception {
|
||||
MockActionResponse response = new MockActionResponse() {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setRenderParameters(Map parameters) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
@ -262,6 +258,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
2, response.getRenderParameterMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasSubmitParameterWithStraightNameMatch() throws Exception {
|
||||
final String targetSubmitParameter = "William";
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
|
@ -271,6 +268,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertTrue(PortletUtils.hasSubmitParameter(request, targetSubmitParameter));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasSubmitParameterWithPrefixedParameterMatch() throws Exception {
|
||||
final String bareParameterName = "William";
|
||||
final String targetParameterName = bareParameterName + WebUtils.SUBMIT_IMAGE_SUFFIXES[0];
|
||||
|
@ -280,6 +278,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertTrue(PortletUtils.hasSubmitParameter(request, bareParameterName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasSubmitParameterWithNoParameterMatch() throws Exception {
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
request.setParameter("Bill", "Baskerville");
|
||||
|
@ -287,44 +286,39 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertFalse(PortletUtils.hasSubmitParameter(request, "William"));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testHasSubmitParameterWithNullRequest() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.hasSubmitParameter(null, "bingo");
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.hasSubmitParameter(null, "bingo");
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testExposeRequestAttributesWithNullRequest() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.exposeRequestAttributes(null, Collections.EMPTY_MAP);
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.exposeRequestAttributes(null, Collections.EMPTY_MAP);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testExposeRequestAttributesWithNullAttributesMap() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.exposeRequestAttributes(new MockPortletRequest(), null);
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.exposeRequestAttributes(new MockPortletRequest(), null);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test(expected=ClassCastException.class)
|
||||
public void testExposeRequestAttributesWithAttributesMapContainingBadKeyType() throws Exception {
|
||||
new AssertThrows(ClassCastException.class) {
|
||||
public void test() throws Exception {
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
Map attributes = new HashMap();
|
||||
attributes.put(new Object(), "bad key type");
|
||||
PortletUtils.exposeRequestAttributes(request, attributes);
|
||||
}
|
||||
}.runTest();
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
Map attributes = new HashMap<Object, Object>();
|
||||
attributes.put(new Object(), "bad key type");
|
||||
PortletUtils.exposeRequestAttributes(request, attributes);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testExposeRequestAttributesSunnyDay() throws Exception {
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
Map attributes = new HashMap();
|
||||
Map attributes = new HashMap<Object, Object>();
|
||||
attributes.put("ace", "Rick Hunter");
|
||||
attributes.put("mentor", "Roy Fokker");
|
||||
PortletUtils.exposeRequestAttributes(request, attributes);
|
||||
|
@ -334,22 +328,22 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertEquals("Roy Fokker", request.getAttribute("mentor"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testExposeRequestAttributesWithEmptyAttributesMapIsAnIdempotentOperation() throws Exception {
|
||||
MockPortletRequest request = new MockPortletRequest();
|
||||
Map attributes = new HashMap();
|
||||
Map attributes = new HashMap<Object, Object>();
|
||||
PortletUtils.exposeRequestAttributes(request, attributes);
|
||||
assertEquals("Obviously all of the entries in the supplied attributes Map are not being copied over (exposed)",
|
||||
attributes.size(), countElementsIn(request.getAttributeNames()));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testGetOrCreateSessionAttributeWithNullSession() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getOrCreateSessionAttribute(null, "bean", TestBean.class);
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.getOrCreateSessionAttribute(null, "bean", TestBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOrCreateSessionAttributeJustReturnsAttributeIfItAlreadyExists() throws Exception {
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
final TestBean expectedAttribute = new TestBean("Donna Tartt");
|
||||
|
@ -358,6 +352,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertSame(expectedAttribute, actualAttribute);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOrCreateSessionAttributeCreatesAttributeIfItDoesNotAlreadyExist() throws Exception {
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
Object actualAttribute = PortletUtils.getOrCreateSessionAttribute(session, "bean", TestBean.class);
|
||||
|
@ -365,38 +360,27 @@ public final class PortletUtilsTests extends TestCase {
|
|||
assertEquals("Wrong type of object being instantiated", TestBean.class, actualAttribute.getClass());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testGetOrCreateSessionAttributeWithNoExistingAttributeAndNullClass() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getOrCreateSessionAttribute(new MockPortletSession(), "bean", null);
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.getOrCreateSessionAttribute(new MockPortletSession(), "bean", null);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testGetOrCreateSessionAttributeWithNoExistingAttributeAndClassThatIsAnInterfaceType() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getOrCreateSessionAttribute(new MockPortletSession(), "bean", ITestBean.class);
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.getOrCreateSessionAttribute(new MockPortletSession(), "bean", ITestBean.class);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testGetOrCreateSessionAttributeWithNoExistingAttributeAndClassWithNoPublicCtor() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getOrCreateSessionAttribute(new MockPortletSession(), "bean", NoPublicCtor.class);
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.getOrCreateSessionAttribute(new MockPortletSession(), "bean", NoPublicCtor.class);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testGetSessionMutexWithNullSession() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getSessionMutex(null);
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.getSessionMutex(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSessionMutexWithNoExistingSessionMutexDefinedJustReturnsTheSessionArgument() throws Exception {
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
Object sessionMutex = PortletUtils.getSessionMutex(session);
|
||||
|
@ -405,6 +389,7 @@ public final class PortletUtilsTests extends TestCase {
|
|||
session, sessionMutex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSessionMutexWithExistingSessionMutexReturnsTheExistingSessionMutex() throws Exception {
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
Object expectSessionMutex = new Object();
|
||||
|
@ -415,177 +400,153 @@ public final class PortletUtilsTests extends TestCase {
|
|||
expectSessionMutex, actualSessionMutex);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testGetSessionAttributeWithNullPortletRequest() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getSessionAttribute(null, "foo");
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.getSessionAttribute(null, "foo");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testGetRequiredSessionAttributeWithNullPortletRequest() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getRequiredSessionAttribute(null, "foo");
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.getRequiredSessionAttribute(null, "foo");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testSetSessionAttributeWithNullPortletRequest() throws Exception {
|
||||
new AssertThrows(IllegalArgumentException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.setSessionAttribute(null, "foo", "bar");
|
||||
}
|
||||
}.runTest();
|
||||
PortletUtils.setSessionAttribute(null, "foo", "bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSessionAttributeDoes_Not_CreateANewSession() throws Exception {
|
||||
MockControl mock = MockControl.createControl(PortletRequest.class);
|
||||
PortletRequest request = (PortletRequest) mock.getMock();
|
||||
request.getPortletSession(false);
|
||||
mock.setReturnValue(null);
|
||||
mock.replay();
|
||||
PortletRequest request = createMock(PortletRequest.class);
|
||||
expect(request.getPortletSession(false)).andReturn(null);
|
||||
replay(request);
|
||||
|
||||
Object sessionAttribute = PortletUtils.getSessionAttribute(request, "foo");
|
||||
assertNull("Must return null if session attribute does not exist (or if Session does not exist)", sessionAttribute);
|
||||
mock.verify();
|
||||
verify(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSessionAttributeWithExistingSession() throws Exception {
|
||||
MockControl mock = MockControl.createControl(PortletRequest.class);
|
||||
PortletRequest request = (PortletRequest) mock.getMock();
|
||||
request.getPortletSession(false);
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
session.setAttribute("foo", "foo");
|
||||
mock.setReturnValue(session);
|
||||
mock.replay();
|
||||
|
||||
PortletRequest request = createMock(PortletRequest.class);
|
||||
expect(request.getPortletSession(false)).andReturn(session);
|
||||
replay(request);
|
||||
|
||||
Object sessionAttribute = PortletUtils.getSessionAttribute(request, "foo");
|
||||
assertNotNull("Must not return null if session attribute exists (and Session exists)", sessionAttribute);
|
||||
assertEquals("foo", sessionAttribute);
|
||||
mock.verify();
|
||||
|
||||
verify(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRequiredSessionAttributeWithExistingSession() throws Exception {
|
||||
MockControl mock = MockControl.createControl(PortletRequest.class);
|
||||
PortletRequest request = (PortletRequest) mock.getMock();
|
||||
request.getPortletSession(false);
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
session.setAttribute("foo", "foo");
|
||||
mock.setReturnValue(session);
|
||||
mock.replay();
|
||||
|
||||
PortletRequest request = createMock(PortletRequest.class);
|
||||
expect(request.getPortletSession(false)).andReturn(session);
|
||||
replay(request);
|
||||
|
||||
Object sessionAttribute = PortletUtils.getRequiredSessionAttribute(request, "foo");
|
||||
assertNotNull("Must not return null if session attribute exists (and Session exists)", sessionAttribute);
|
||||
assertEquals("foo", sessionAttribute);
|
||||
mock.verify();
|
||||
|
||||
verify(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRequiredSessionAttributeWithExistingSessionAndNoAttribute() throws Exception {
|
||||
MockControl mock = MockControl.createControl(PortletRequest.class);
|
||||
final PortletRequest request = (PortletRequest) mock.getMock();
|
||||
request.getPortletSession(false);
|
||||
MockPortletSession session = new MockPortletSession();
|
||||
mock.setReturnValue(session);
|
||||
mock.replay();
|
||||
new AssertThrows(IllegalStateException.class) {
|
||||
public void test() throws Exception {
|
||||
PortletUtils.getRequiredSessionAttribute(request, "foo");
|
||||
}
|
||||
}.runTest();
|
||||
mock.verify();
|
||||
|
||||
final PortletRequest request = createMock(PortletRequest.class);
|
||||
expect(request.getPortletSession(false)).andReturn(session);
|
||||
replay(request);
|
||||
try {
|
||||
PortletUtils.getRequiredSessionAttribute(request, "foo");
|
||||
fail("expected IllegalStateException");
|
||||
} catch (IllegalStateException ex) { /* expected */ }
|
||||
verify(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSessionAttributeWithExistingSessionAndNullValue() throws Exception {
|
||||
MockControl mockSession = MockControl.createControl(PortletSession.class);
|
||||
PortletSession session = (PortletSession) mockSession.getMock();
|
||||
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
|
||||
PortletRequest request = (PortletRequest) mockRequest.getMock();
|
||||
PortletSession session = createMock(PortletSession.class);
|
||||
PortletRequest request = createMock(PortletRequest.class);
|
||||
|
||||
request.getPortletSession(false); // must not create Session for null value...
|
||||
mockRequest.setReturnValue(session);
|
||||
expect(request.getPortletSession(false)).andReturn(session); // must not create Session for null value...
|
||||
session.removeAttribute("foo", PortletSession.APPLICATION_SCOPE);
|
||||
mockSession.setVoidCallable();
|
||||
mockRequest.replay();
|
||||
mockSession.replay();
|
||||
replay(request, session);
|
||||
|
||||
PortletUtils.setSessionAttribute(request, "foo", null, PortletSession.APPLICATION_SCOPE);
|
||||
mockRequest.verify();
|
||||
mockSession.verify();
|
||||
|
||||
verify(request, session);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSessionAttributeWithNoExistingSessionAndNullValue() throws Exception {
|
||||
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
|
||||
PortletRequest request = (PortletRequest) mockRequest.getMock();
|
||||
PortletRequest request = createMock(PortletRequest.class);
|
||||
|
||||
request.getPortletSession(false); // must not create Session for null value...
|
||||
mockRequest.setReturnValue(null);
|
||||
mockRequest.replay();
|
||||
expect(request.getPortletSession(false)).andReturn(null); // must not create Session for null value...
|
||||
replay(request);
|
||||
|
||||
PortletUtils.setSessionAttribute(request, "foo", null, PortletSession.APPLICATION_SCOPE);
|
||||
mockRequest.verify();
|
||||
|
||||
verify(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSessionAttributeWithExistingSessionAndSpecificScope() throws Exception {
|
||||
MockControl mockSession = MockControl.createControl(PortletSession.class);
|
||||
PortletSession session = (PortletSession) mockSession.getMock();
|
||||
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
|
||||
PortletRequest request = (PortletRequest) mockRequest.getMock();
|
||||
PortletSession session = createMock(PortletSession.class);
|
||||
PortletRequest request = createMock(PortletRequest.class);
|
||||
|
||||
request.getPortletSession(); // must create Session...
|
||||
mockRequest.setReturnValue(session);
|
||||
expect(request.getPortletSession()).andReturn(session); // must not create Session ...
|
||||
session.setAttribute("foo", "foo", PortletSession.APPLICATION_SCOPE);
|
||||
mockSession.setVoidCallable();
|
||||
mockRequest.replay();
|
||||
mockSession.replay();
|
||||
replay(request, session);
|
||||
|
||||
PortletUtils.setSessionAttribute(request, "foo", "foo", PortletSession.APPLICATION_SCOPE);
|
||||
mockRequest.verify();
|
||||
mockSession.verify();
|
||||
|
||||
verify(request, session);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSessionAttributeWithExistingSessionAndSpecificScope() throws Exception {
|
||||
MockControl mockSession = MockControl.createControl(PortletSession.class);
|
||||
PortletSession session = (PortletSession) mockSession.getMock();
|
||||
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
|
||||
PortletRequest request = (PortletRequest) mockRequest.getMock();
|
||||
PortletSession session = createMock(PortletSession.class);
|
||||
PortletRequest request = createMock(PortletRequest.class);
|
||||
|
||||
request.getPortletSession(false);
|
||||
mockRequest.setReturnValue(session);
|
||||
session.getAttribute("foo", PortletSession.APPLICATION_SCOPE);
|
||||
mockSession.setReturnValue("foo");
|
||||
mockRequest.replay();
|
||||
mockSession.replay();
|
||||
expect(request.getPortletSession(false)).andReturn(session);
|
||||
expect(session.getAttribute("foo", PortletSession.APPLICATION_SCOPE)).andReturn("foo");
|
||||
replay(request, session);
|
||||
|
||||
Object sessionAttribute = PortletUtils.getSessionAttribute(request, "foo", PortletSession.APPLICATION_SCOPE);
|
||||
assertNotNull("Must not return null if session attribute exists (and Session exists)", sessionAttribute);
|
||||
assertEquals("foo", sessionAttribute);
|
||||
mockRequest.verify();
|
||||
mockSession.verify();
|
||||
|
||||
verify(request, session);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSessionAttributeWithExistingSessionDefaultsToPortletScope() throws Exception {
|
||||
MockControl mockSession = MockControl.createControl(PortletSession.class);
|
||||
PortletSession session = (PortletSession) mockSession.getMock();
|
||||
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
|
||||
PortletRequest request = (PortletRequest) mockRequest.getMock();
|
||||
PortletSession session = createMock(PortletSession.class);
|
||||
PortletRequest request = createMock(PortletRequest.class);
|
||||
|
||||
request.getPortletSession(false);
|
||||
mockRequest.setReturnValue(session);
|
||||
session.getAttribute("foo", PortletSession.PORTLET_SCOPE);
|
||||
mockSession.setReturnValue("foo");
|
||||
mockRequest.replay();
|
||||
mockSession.replay();
|
||||
expect(request.getPortletSession(false)).andReturn(session);
|
||||
expect(session.getAttribute("foo", PortletSession.PORTLET_SCOPE)).andReturn("foo");
|
||||
|
||||
replay(request, session);
|
||||
|
||||
Object sessionAttribute = PortletUtils.getSessionAttribute(request, "foo");
|
||||
assertNotNull("Must not return null if session attribute exists (and Session exists)", sessionAttribute);
|
||||
assertEquals("foo", sessionAttribute);
|
||||
mockRequest.verify();
|
||||
mockSession.verify();
|
||||
|
||||
verify(request, session);
|
||||
}
|
||||
|
||||
|
||||
private static int countElementsIn(Enumeration enumeration) {
|
||||
private static int countElementsIn(Enumeration<?> enumeration) {
|
||||
int count = 0;
|
||||
while (enumeration.hasMoreElements()) {
|
||||
enumeration.nextElement();
|
|
@ -38,5 +38,6 @@
|
|||
<classpathentry kind="var" path="IVY_CACHE"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/javax.activation/com.springsource.javax.activation/1.1.0/com.springsource.javax.activation-1.1.0.jar" sourcepath="/IVY_CACHE/javax.activation/com.springsource.javax.activation/1.1.0/com.springsource.javax.activation-sources-1.1.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/javax.el/com.springsource.javax.el/1.0.0/com.springsource.javax.el-1.0.0.jar" sourcepath="/IVY_CACHE/javax.el/com.springsource.javax.el/1.0.0/com.springsource.javax.el-sources-1.0.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.lang/2.1.0/com.springsource.org.apache.commons.lang-2.1.0.jar" sourcepath="/IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.lang/2.1.0/com.springsource.org.apache.commons.lang-sources-2.1.0.jar"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
|
|
|
@ -458,4 +458,4 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext {
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue