Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/transform/worker/RetainWorkerTest.java URL: http://svn.apache.org/viewcvs/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/transform/worker/RetainWorkerTest.java?rev=395912&view=auto ============================================================================== --- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/transform/worker/RetainWorkerTest.java (added) +++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/transform/worker/RetainWorkerTest.java Fri Apr 21 08:19:11 2006 @@ -0,0 +1,77 @@ +// Copyright 2006 The Howard M. Lewis Ship +// +// 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.apache.tapestry.internal.transform.worker; + +import static org.apache.tapestry.util.CollectionFactory.newList; + +import java.util.Collections; +import java.util.List; + +import org.apache.tapestry.annotations.Retain; +import org.apache.tapestry.model.MutableComponentModel; +import org.apache.tapestry.test.BaseTestCase; +import org.apache.tapestry.transform.ClassTransformation; +import org.testng.annotations.Test; + +/** + * @author Howard M. Lewis Ship + */ +public class RetainWorkerTest extends BaseTestCase +{ + @Test + public void noFields() + { + ClassTransformation ct = newClassTransformation(); + MutableComponentModel model = newMutableComponentModel(); + + // There are limits to the compiler's ability to infer types and generics. + // Assigning to an intermediate variable gets rid of warnings. + List<String> names = Collections.emptyList(); + trainFindFieldsWithAnnotation(ct, Retain.class, names); + + replay(); + + RetainWorker worker = new RetainWorker(); + + worker.transform(ct, model); + + verify(); + } + + @Test + public void normal() + { + ClassTransformation ct = newClassTransformation(); + MutableComponentModel model = newMutableComponentModel(); + Retain annotation = newMock(Retain.class); + + List<String> fieldNames = newList(); + fieldNames.add("fred"); + + trainFindFieldsWithAnnotation(ct, Retain.class, fieldNames); + + trainGetFieldAnnotation(ct, "fred", Retain.class, annotation); + + ct.claimField("fred", annotation); + + replay(); + + RetainWorker worker = new RetainWorker(); + + worker.transform(ct, model); + + verify(); + } +}
Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/transform/worker/UnclaimedFieldWorkerTest.java URL: http://svn.apache.org/viewcvs/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/transform/worker/UnclaimedFieldWorkerTest.java?rev=395912&view=auto ============================================================================== --- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/transform/worker/UnclaimedFieldWorkerTest.java (added) +++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/transform/worker/UnclaimedFieldWorkerTest.java Fri Apr 21 08:19:11 2006 @@ -0,0 +1,94 @@ +// Copyright 2006 The Howard M. Lewis Ship +// +// 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.apache.tapestry.internal.transform.worker; + +import java.lang.reflect.Modifier; + +import org.apache.hivemind.service.BodyBuilder; +import org.apache.tapestry.events.ComponentLifecycle; +import org.apache.tapestry.internal.transform.TransformConstants; +import org.apache.tapestry.model.MutableComponentModel; +import org.apache.tapestry.test.BaseTestCase; +import org.apache.tapestry.transform.ClassTransformation; +import org.testng.annotations.Test; + +/** + * @author Howard M. Lewis Ship + */ +public class UnclaimedFieldWorkerTest extends BaseTestCase +{ + @Test + public void noFields() + { + ClassTransformation ct = newClassTransformation(); + MutableComponentModel model = newMutableComponentModel(); + + trainFindUnclaimedFields(ct, new String[0]); + + replay(); + + new UnclaimedFieldWorker().transform(ct, model); + + verify(); + } + + protected final void trainFindUnclaimedFields(ClassTransformation transformation, + String[] fieldNames) + { + transformation.findUnclaimedFields(); + setReturnValue(fieldNames); + } + + @Test + public void normal() + { + ClassTransformation ct = newClassTransformation(); + MutableComponentModel model = newMutableComponentModel(); + + trainFindUnclaimedFields(ct, new String[] + { "_fred" }); + + ct.getFieldType("_fred"); + setReturnValue("foo.Bar"); + + ct.newField(Modifier.PRIVATE, "foo.Bar", "_fredDefault"); + setReturnValue("_$fredDefault"); + + ct.addImplementedInterface(ComponentLifecycle.class); + + BodyBuilder builder = new BodyBuilder(); + builder.begin(); + builder.addln("$proceed($$);"); + builder.addln("_$fredDefault = _fred;"); + builder.end(); + + ct.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, builder.toString()); + + builder.clear(); + builder.begin(); + builder.addln("$proceed($$);"); + builder.addln("_fred = _$fredDefault;"); + builder.end(); + + ct.extendMethod(TransformConstants.CONTAINING_PAGE_DID_DETACH, builder.toString()); + + replay(); + + new UnclaimedFieldWorker().transform(ct, model); + + verify(); + } + +} Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/transform/MethodSignatureTest.java URL: http://svn.apache.org/viewcvs/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/transform/MethodSignatureTest.java?rev=395912&view=auto ============================================================================== --- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/transform/MethodSignatureTest.java (added) +++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/transform/MethodSignatureTest.java Fri Apr 21 08:19:11 2006 @@ -0,0 +1,141 @@ +// Copyright 2006 The Howard M. Lewis Ship +// +// 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.apache.tapestry.transform; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; + +import java.lang.reflect.Modifier; + +import org.apache.tapestry.transform.MethodSignature; +import org.testng.annotations.Test; + +/** + * @author Howard M. Lewis Ship + */ +public class MethodSignatureTest +{ + + @Test + public void toStringTest() + { + MethodSignature sig = new MethodSignature(Modifier.PUBLIC, "int", "doSomething", + new String[] + { "java.lang.String", "int" }, new String[] + { "java.lang.RuntimeException", "org.foo.FredException" }); + + assertEquals( + sig.toString(), + "public int doSomething(java.lang.String, int) throws java.lang.RuntimeException, org.foo.FredException"); + + sig = new MethodSignature(Modifier.ABSTRACT + Modifier.PROTECTED, "boolean", "misoHapi", + new String[0], new String[0]); + + assertEquals(sig.toString(), "protected abstract boolean misoHapi()"); + } + + @Test + public void gettersTest() + { + MethodSignature sig = new MethodSignature(Modifier.PUBLIC, "int", "doSomething", + new String[] + { "java.lang.String", "int" }, new String[] + { "java.lang.RuntimeException", "org.foo.FredException" }); + + assertEquals(sig.getModifiers(), Modifier.PUBLIC); + assertEquals(sig.getReturnType(), "int"); + assertEquals(sig.getMethodName(), "doSomething"); + assertEquals(sig.getParameterTypes(), new String[] + { "java.lang.String", "int" }); + assertEquals(sig.getExceptionTypes(), new String[] + { "java.lang.RuntimeException", "org.foo.FredException" }); + } + + @Test + public void hashCodeAndEqualsTest() + { + MethodSignature sig1 = new MethodSignature(Modifier.PUBLIC, "int", "doSomething", + new String[] + { "int" }, new String[] + { "org.foo.BarException" }); + int hashCode1 = sig1.hashCode(); + + // Check that same value returned each time. + + assertEquals(sig1.hashCode(), hashCode1); + + MethodSignature sig2 = new MethodSignature(Modifier.PUBLIC, "int", "doSomething", + new String[] + { "int" }, new String[] + { "org.foo.BarException" }); + + assertEquals(sig2.hashCode(), hashCode1); + assertEquals(sig2, sig1); + + // Now work through the different properties, changing each one. + + sig2 = new MethodSignature(Modifier.PRIVATE, "int", "doSomething", new String[] + { "int" }, new String[] + { "org.foo.BarException" }); + + assertFalse(sig2.hashCode() == hashCode1); + assertFalse(sig2.equals(sig1)); + + sig2 = new MethodSignature(Modifier.PUBLIC, "long", "doSomething", new String[] + { "int" }, new String[] + { "org.foo.BarException" }); + + assertFalse(sig2.hashCode() == hashCode1); + assertFalse(sig2.equals(sig1)); + + sig2 = new MethodSignature(Modifier.PUBLIC, "int", "doSomethingElse", new String[] + { "int" }, new String[] + { "org.foo.BarException" }); + + assertFalse(sig2.hashCode() == hashCode1); + assertFalse(sig2.equals(sig1)); + + sig2 = new MethodSignature(Modifier.PUBLIC, "int", "doSomething", new String[] + { "long" }, new String[] + { "org.foo.BarException" }); + + assertFalse(sig2.hashCode() == hashCode1); + assertFalse(sig2.equals(sig1)); + + sig2 = new MethodSignature(Modifier.PUBLIC, "int", "doSomething", new String[] + { "int" }, new String[0]); + + assertFalse(sig2.hashCode() == hashCode1); + assertFalse(sig2.equals(sig1)); + + // Other equality checks + + assertFalse(sig1.equals(null)); + assertFalse(sig1.equals("")); + } + + /** Tests the simple, no arguments constructor. */ + @Test + public void shortConstructorTest() + { + MethodSignature sig = new MethodSignature("pageLoad"); + + assertEquals(sig.getModifiers(), Modifier.PUBLIC); + assertEquals(sig.getReturnType(), "void"); + assertEquals(sig.getMethodName(), "pageLoad"); + assertEquals(sig.getParameterTypes(), new String[0]); + assertEquals(sig.getExceptionTypes(), new String[0]); + } +} Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/CollectionFactoryTest.java URL: http://svn.apache.org/viewcvs/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/CollectionFactoryTest.java?rev=395912&view=auto ============================================================================== --- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/CollectionFactoryTest.java (added) +++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/CollectionFactoryTest.java Fri Apr 21 08:19:11 2006 @@ -0,0 +1,88 @@ +// Copyright 2006 The Howard M. Lewis Ship +// +// 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.apache.tapestry.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.testng.annotations.Test; + +import static org.apache.tapestry.util.CollectionFactory.newList; +import static org.apache.tapestry.util.CollectionFactory.newMap; +import static org.apache.tapestry.util.CollectionFactory.newSet; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +/** + * @author Howard M. Lewis Ship + */ +public class CollectionFactoryTest +{ + + @Test + public void newMapTest() + { + Map<String, Class> map = newMap(); + + assertTrue(map instanceof HashMap); + } + + @Test + public void copyMapTest() + { + Map<String, Class> map = newMap(); + + map.put("this", CollectionFactoryTest.class); + + Map<String, Class> copy = CollectionFactory.copyMap(map); + + assertEquals(copy, map); + + map.put("other", Map.class); + + assertFalse(copy.equals(map)); + } + + @Test + public void newSetTest() + { + Set<String> set = newSet(); + + assertTrue(set instanceof HashSet); + } + + @Test + public void newListTest() + { + List<String> list = newList(); + + assertTrue(list instanceof ArrayList); + } + + @Test + public void newListVarargs() + { + List<String> actual = newList("Fred", "Barney", "Wilma"); + + assertEquals(actual, Arrays.asList(new String[] + { "Fred", "Barney", "Wilma" })); + } +} Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/DefenseTest.java URL: http://svn.apache.org/viewcvs/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/DefenseTest.java?rev=395912&view=auto ============================================================================== --- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/DefenseTest.java (added) +++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/DefenseTest.java Fri Apr 21 08:19:11 2006 @@ -0,0 +1,92 @@ +// Copyright 2006 The Howard M. Lewis Ship +// +// 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.apache.tapestry.util; + +import static org.apache.tapestry.util.Defense.notBlank; +import static org.apache.tapestry.util.Defense.notNull; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertSame; + +import org.apache.tapestry.test.TestBase; +import org.testng.annotations.Test; + +/** + * @author Howard M. Lewis Ship + */ +public class DefenseTest extends TestBase +{ + + /** + * Check that [EMAIL PROTECTED] Defense#notNull(T, String)} returns a non-null value. + */ + @Test + public void parameterNotNull() + { + assertSame(this, notNull(this, "foo")); + } + + /** + * Check that [EMAIL PROTECTED] Defense#notNull(T, String)} throws NPE when null, and + * check the message. + */ + + @Test + public void parameterIsNull() + { + try + { + notNull(null, "foo"); + unreachable(); + } + catch (NullPointerException ex) + { + assertEquals(ex.getMessage(), "Parameter foo was null."); + } + } + + @Test + public void nonBlankParameterIsNull() + { + try + { + notBlank(null, "bar"); + unreachable(); + } + catch (IllegalArgumentException ex) + { + assertEquals(ex.getMessage(), "Parameter bar was null or contained only whitespace."); + } + } + + @Test + public void nonBlankParameterIsOnlyWhitespace() + { + try + { + notBlank(" \t\n", "baz"); + unreachable(); + } + catch (IllegalArgumentException ex) + { + assertEquals(ex.getMessage(), "Parameter baz was null or contained only whitespace."); + } + } + + @Test + public void nonBlankParameterIsValid() + { + assertEquals("fred", notBlank(" fred\n", "biff")); + } +} Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/IdAllocatorTest.java URL: http://svn.apache.org/viewcvs/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/IdAllocatorTest.java?rev=395912&view=auto ============================================================================== --- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/IdAllocatorTest.java (added) +++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/IdAllocatorTest.java Fri Apr 21 08:19:11 2006 @@ -0,0 +1,127 @@ +// Copyright 2004, 2005, 2006 The Howard M. Lewis Ship +// +// 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.apache.tapestry.util; + +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; + +/** + * Tests the [EMAIL PROTECTED] org.apache.tapestry.util.IdAllocator}class. + * + * @author Howard Lewis Ship + */ +public class IdAllocatorTest +{ + + @Test + public void simple() + { + IdAllocator a = new IdAllocator(); + + assertEquals(a.allocateId("name"), "name"); + + for (int i = 0; i < 10; i++) + assertEquals(a.allocateId("name"), "name_" + i); + } + + @Test + public void simpleNamespace() + { + IdAllocator a = new IdAllocator("_NS"); + + assertEquals(a.allocateId("name"), "name_NS"); + + for (int i = 0; i < 10; i++) + assertEquals(a.allocateId("name"), "name_NS_" + i); + + // This is current behavior, but is probably something + // that could be improved. + + assertEquals(a.allocateId("foo_NS"), "foo_NS_NS"); + assertEquals(a.allocateId("foo_NS"), "foo_NS_NS_0"); + } + + @Test + public void degenerate() + { + IdAllocator a = new IdAllocator(); + + assertEquals(a.allocateId("d_1"), "d_1"); + + assertEquals(a.allocateId("d"), "d"); + assertEquals(a.allocateId("d"), "d_0"); + assertEquals(a.allocateId("d"), "d_2"); + + assertEquals(a.allocateId("d"), "d_3"); + assertEquals(a.allocateId("d_1"), "d_1_0"); + } + + @Test + public void degenerateNamespace() + { + IdAllocator a = new IdAllocator("_NS"); + + assertEquals(a.allocateId("d_1"), "d_1_NS"); + + assertEquals(a.allocateId("d"), "d_NS"); + assertEquals(a.allocateId("d"), "d_NS_0"); + assertEquals(a.allocateId("d"), "d_NS_1"); + assertEquals(a.allocateId("d"), "d_NS_2"); + assertEquals(a.allocateId("d"), "d_NS_3"); + + assertEquals(a.allocateId("d_1"), "d_1_NS_0"); + + // This is very degenerate, and maybe something that needs fixing. + + assertEquals(a.allocateId("d_1_NS"), "d_1_NS_NS"); + } + + @Test + public void clear() + { + IdAllocator a = new IdAllocator(); + + assertEquals(a.allocateId("foo"), "foo"); + assertEquals(a.allocateId("foo_0"), "foo_0"); + + a.clear(); + + assertEquals(a.allocateId("foo"), "foo"); + assertEquals(a.allocateId("foo_0"), "foo_0"); + } + + @Test + public void cloneTest() + { + IdAllocator a = new IdAllocator(); + + assertEquals(a.allocateId("foo"), "foo"); + assertEquals(a.allocateId("foo_0"), "foo_0"); + assertEquals(a.allocateId("foo"), "foo_1"); + + IdAllocator b = a.clone(); + + // After making a clone, parallel operations should return the same results. + // If anything under the covers was shared, then parallel operations would + // interfere with each other. + + assertEquals(b.allocateId("bar"), a.allocateId("bar")); + assertEquals(b.allocateId("foo"), a.allocateId("foo")); + assertEquals(b.allocateId("foo_0"), a.allocateId("foo_0")); + + } + +} Added: tapestry/tapestry5/tapestry-core/trunk/src/test/resources/log4j.properties URL: http://svn.apache.org/viewcvs/tapestry/tapestry5/tapestry-core/trunk/src/test/resources/log4j.properties?rev=395912&view=auto ============================================================================== --- tapestry/tapestry5/tapestry-core/trunk/src/test/resources/log4j.properties (added) +++ tapestry/tapestry5/tapestry-core/trunk/src/test/resources/log4j.properties Fri Apr 21 08:19:11 2006 @@ -0,0 +1,24 @@ +# Copyright 2005, 2006 The Howard M. Lewis Ship +# +# 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. + +log4j.rootCategory=WARN, A1 + +# A1 is set to be a ConsoleAppender. +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +# A1 uses PatternLayout. +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%c{1} [%p] %m%n + +log4j.category.org.apache.tapestry5=debug --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]