001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.test; 016 017 import java.util.List; 018 019 import org.apache.hivemind.Messages; 020 import org.apache.tapestry.AbstractComponent; 021 import org.apache.tapestry.BaseComponentTestCase; 022 import org.apache.tapestry.IComponent; 023 import org.apache.tapestry.spec.IComponentSpecification; 024 import org.testng.annotations.Test; 025 026 /** 027 * Tests for {@link org.apache.tapestry.test.Creator}. 028 * 029 * @author Howard Lewis Ship 030 * @since 4.0 031 */ 032 @Test 033 public class TestCreator extends BaseComponentTestCase 034 { 035 036 public void testInterface() throws Exception 037 { 038 039 try 040 { 041 Creator c = new Creator(); 042 043 c.newInstance(List.class); 044 unreachable(); 045 } 046 catch (IllegalArgumentException ex) 047 { 048 assertEquals( 049 ex.getMessage(), 050 "Can not create instance of java.util.List. Interfaces, arrays and primitive types may not be enhanced."); 051 } 052 053 } 054 055 public void testObjectType() 056 { 057 Creator c = new Creator(); 058 059 StringSubject s = (StringSubject) c.newInstance(StringSubject.class); 060 061 s.setTitle("title"); 062 063 assertEquals("title", s.getTitle()); 064 } 065 066 public void testPrimitiveType() 067 { 068 Creator c = new Creator(); 069 070 IntSubject s = (IntSubject) c.newInstance(IntSubject.class); 071 072 s.setPriority(-1); 073 074 assertEquals(-1, s.getPriority()); 075 } 076 077 public void testArrayType() 078 { 079 Creator c = new Creator(); 080 081 ArraySubject s = (ArraySubject) c.newInstance(ArraySubject.class); 082 083 int[] counts = new int[] 084 { 3, 7, 9 }; 085 086 s.setCounts(counts); 087 088 assertSame(counts, s.getCounts()); 089 } 090 091 public void testInherited() 092 { 093 Creator c = new Creator(); 094 095 InheritedSubject s = (InheritedSubject) c.newInstance(InheritedSubject.class); 096 097 s.setFlag(true); 098 s.setPriority(5); 099 100 assertEquals(true, s.getFlag()); 101 assertEquals(5, s.getPriority()); 102 } 103 104 public void testMethodNameNotOverriden() 105 { 106 Creator c = new Creator(); 107 108 BooleanSubject s = (BooleanSubject) c.newInstance(BooleanSubject.class); 109 110 s.setKnown(true); 111 112 assertEquals(true, s.isKnown()); 113 } 114 115 public void testUniqueInstances() 116 { 117 Creator c = new Creator(); 118 119 StringSubject s1 = (StringSubject) c.newInstance(StringSubject.class); 120 StringSubject s2 = (StringSubject) c.newInstance(StringSubject.class); 121 122 assertNotSame(s1, s2); 123 } 124 125 public void testInitializer() 126 { 127 Creator c = new Creator(); 128 129 StringSubject ss = (StringSubject) c.newInstance(StringSubject.class, new Object[] 130 { "title", "Hitchhiker's Guide" }); 131 132 assertEquals("Hitchhiker's Guide", ss.getTitle()); 133 } 134 135 public void testSpecificationProperty() 136 { 137 IComponentSpecification spec = newMock(IComponentSpecification.class); 138 139 replay(); 140 141 Creator c = new Creator(); 142 143 IComponent component = (IComponent) c.newInstance(AbstractComponent.class, new Object[] 144 { "specification", spec }); 145 146 assertSame(spec, component.getSpecification()); 147 148 verify(); 149 } 150 151 public void testMessagesProperty() 152 { 153 Messages messages = newMock(Messages.class); 154 155 replay(); 156 157 Creator c = new Creator(); 158 159 IComponent component = (IComponent) c.newInstance(AbstractComponent.class, new Object[] 160 { "messages", messages }); 161 162 assertSame(messages, component.getMessages()); 163 164 verify(); 165 } 166 }