KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > test > TestCreator


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.test;
16
17 import java.util.List JavaDoc;
18
19 import org.apache.hivemind.Messages;
20 import org.apache.hivemind.test.HiveMindTestCase;
21 import org.apache.tapestry.AbstractComponent;
22 import org.apache.tapestry.IComponent;
23 import org.apache.tapestry.spec.IComponentSpecification;
24
25 /**
26  * Tests for {@link org.apache.tapestry.test.Creator}.
27  *
28  * @author Howard Lewis Ship
29  * @since 4.0
30  */

31 public class TestCreator extends HiveMindTestCase
32 {
33
34     public void testInterface() throws Exception JavaDoc
35     {
36
37         try
38         {
39             Creator c = new Creator();
40
41             c.newInstance(List JavaDoc.class);
42             unreachable();
43         }
44         catch (IllegalArgumentException JavaDoc ex)
45         {
46             assertEquals(
47                     ex.getMessage(),
48                     "Can not create instance of java.util.List. Interfaces, arrays and primitive types may not be enhanced.");
49         }
50
51     }
52
53     public void testObjectType()
54     {
55         Creator c = new Creator();
56
57         StringSubject s = (StringSubject) c.newInstance(StringSubject.class);
58
59         s.setTitle("title");
60
61         assertEquals("title", s.getTitle());
62     }
63
64     public void testPrimitiveType()
65     {
66         Creator c = new Creator();
67
68         IntSubject s = (IntSubject) c.newInstance(IntSubject.class);
69
70         s.setPriority(-1);
71
72         assertEquals(-1, s.getPriority());
73     }
74
75     public void testArrayType()
76     {
77         Creator c = new Creator();
78
79         ArraySubject s = (ArraySubject) c.newInstance(ArraySubject.class);
80
81         int[] counts = new int[]
82         { 3, 7, 9 };
83
84         s.setCounts(counts);
85
86         assertSame(counts, s.getCounts());
87     }
88
89     public void testInherited()
90     {
91         Creator c = new Creator();
92
93         InheritedSubject s = (InheritedSubject) c.newInstance(InheritedSubject.class);
94
95         s.setFlag(true);
96         s.setPriority(5);
97
98         assertEquals(true, s.getFlag());
99         assertEquals(5, s.getPriority());
100     }
101
102     public void testMethodNameNotOverriden()
103     {
104         Creator c = new Creator();
105
106         BooleanSubject s = (BooleanSubject) c.newInstance(BooleanSubject.class);
107
108         s.setKnown(true);
109
110         assertEquals(true, s.isKnown());
111     }
112
113     public void testUniqueInstances()
114     {
115         Creator c = new Creator();
116
117         StringSubject s1 = (StringSubject) c.newInstance(StringSubject.class);
118         StringSubject s2 = (StringSubject) c.newInstance(StringSubject.class);
119
120         assertNotSame(s1, s2);
121     }
122
123     public void testInitializer()
124     {
125         Creator c = new Creator();
126
127         StringSubject ss = (StringSubject) c.newInstance(StringSubject.class, new Object JavaDoc[]
128         { "title", "Hitchhiker's Guide" });
129
130         assertEquals("Hitchhiker's Guide", ss.getTitle());
131     }
132
133     public void testSpecificationProperty()
134     {
135         IComponentSpecification spec = (IComponentSpecification) newMock(IComponentSpecification.class);
136
137         replayControls();
138
139         Creator c = new Creator();
140
141         IComponent component = (IComponent) c.newInstance(AbstractComponent.class, new Object JavaDoc[]
142         { "specification", spec });
143
144         assertSame(spec, component.getSpecification());
145
146         verifyControls();
147     }
148
149     public void testMessagesProperty()
150     {
151         Messages messages = (Messages) newMock(Messages.class);
152
153         replayControls();
154
155         Creator c = new Creator();
156
157         IComponent component = (IComponent) c.newInstance(AbstractComponent.class, new Object JavaDoc[]
158         { "messages", messages });
159
160         assertSame(messages, component.getMessages());
161
162         verifyControls();
163     }
164 }
Popular Tags