KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > TestPortletApplicationSpecificationInitializer


1 // Copyright 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.portlet;
16
17 import java.net.URL JavaDoc;
18
19 import javax.portlet.PortletConfig;
20
21 import org.apache.hivemind.Resource;
22 import org.apache.hivemind.test.HiveMindTestCase;
23 import org.apache.tapestry.parse.ISpecificationParser;
24 import org.apache.tapestry.services.ApplicationGlobals;
25 import org.apache.tapestry.services.impl.ApplicationGlobalsImpl;
26 import org.apache.tapestry.spec.IApplicationSpecification;
27 import org.apache.tapestry.web.WebContext;
28 import org.apache.tapestry.web.WebContextResource;
29 import org.easymock.MockControl;
30
31 /**
32  * Tests for {@link PortletApplicationSpecificationInitializer}.
33  *
34  * @author Howard M. Lewis Ship
35  * @since 4.0
36  */

37 public class TestPortletApplicationSpecificationInitializer extends HiveMindTestCase
38 {
39     private PortletConfig newConfig(String JavaDoc name)
40     {
41         MockControl control = newControl(PortletConfig.class);
42         PortletConfig config = (PortletConfig) control.getMock();
43
44         config.getPortletName();
45         control.setReturnValue(name);
46
47         return config;
48     }
49
50     private IApplicationSpecification newSpecification()
51     {
52         return (IApplicationSpecification) newMock(IApplicationSpecification.class);
53     }
54
55     private ISpecificationParser newParser(Resource input, IApplicationSpecification specification)
56     {
57         MockControl control = newControl(ISpecificationParser.class);
58         ISpecificationParser parser = (ISpecificationParser) control.getMock();
59
60         parser.parseApplicationSpecification(input);
61         control.setReturnValue(specification);
62
63         return parser;
64     }
65
66     private ApplicationGlobals newGlobals()
67     {
68         return (ApplicationGlobals) newMock(ApplicationGlobals.class);
69     }
70
71     public void testFoundInSubdir() throws Exception JavaDoc
72     {
73         PortletConfig config = newConfig("myportlet");
74
75         MockControl contextc = newControl(WebContext.class);
76         WebContext context = (WebContext) contextc.getMock();
77
78         IApplicationSpecification specification = newSpecification();
79
80         // Any arbitrary file will work here.
81

82         URL JavaDoc fakeURL = getClass().getResource("hivemodule.xml");
83
84         context.getResource("/WEB-INF/myportlet/myportlet.application");
85         contextc.setReturnValue(fakeURL);
86
87         Resource expectedResource = new WebContextResource(context,
88                 "/WEB-INF/myportlet/myportlet.application");
89
90         ISpecificationParser parser = newParser(expectedResource, specification);
91
92         ApplicationGlobals globals = newGlobals();
93
94         globals.storeSpecification(specification);
95
96         replayControls();
97
98         PortletApplicationSpecificationInitializer init = new PortletApplicationSpecificationInitializer();
99         init.setContext(context);
100         init.setGlobals(globals);
101         init.setParser(parser);
102
103         init.initialize(config);
104
105         verifyControls();
106     }
107
108     public void testFoundInRootDir() throws Exception JavaDoc
109     {
110         PortletConfig config = newConfig("myportlet");
111
112         MockControl contextc = newControl(WebContext.class);
113         WebContext context = (WebContext) contextc.getMock();
114
115         IApplicationSpecification specification = newSpecification();
116
117         // Any arbitrary file will work here.
118

119         URL JavaDoc fakeURL = getClass().getResource("hivemodule.xml");
120
121         context.getResource("/WEB-INF/myportlet/myportlet.application");
122         contextc.setReturnValue(null);
123
124         context.getResource("/WEB-INF/myportlet.application");
125         contextc.setReturnValue(fakeURL);
126
127         Resource expectedResource = new WebContextResource(context,
128                 "/WEB-INF/myportlet.application");
129
130         ISpecificationParser parser = newParser(expectedResource, specification);
131
132         ApplicationGlobals globals = newGlobals();
133
134         globals.storeSpecification(specification);
135
136         replayControls();
137
138         PortletApplicationSpecificationInitializer init = new PortletApplicationSpecificationInitializer();
139         init.setContext(context);
140         init.setGlobals(globals);
141         init.setParser(parser);
142
143         init.initialize(config);
144
145         verifyControls();
146     }
147
148     public void testNotFound() throws Exception JavaDoc
149     {
150         PortletConfig config = newConfig("myportlet");
151
152         MockControl contextc = newControl(WebContext.class);
153         WebContext context = (WebContext) contextc.getMock();
154
155         context.getResource("/WEB-INF/myportlet/myportlet.application");
156         contextc.setReturnValue(null);
157
158         context.getResource("/WEB-INF/myportlet.application");
159         contextc.setReturnValue(null);
160
161         replayControls();
162
163         ApplicationGlobals globals = new ApplicationGlobalsImpl();
164
165         PortletApplicationSpecificationInitializer init = new PortletApplicationSpecificationInitializer();
166         init.setContext(context);
167         init.setGlobals(globals);
168
169         init.initialize(config);
170
171         verifyControls();
172
173         IApplicationSpecification spec = globals.getSpecification();
174
175         assertEquals("myportlet", spec.getName());
176         assertEquals(new WebContextResource(context, "/WEB-INF/myportlet.application"), spec
177                 .getSpecificationLocation());
178     }
179 }
Popular Tags