1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.deployment;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.nio.channels.FileChannel;
25
26 import junit.framework.AssertionFailedError;
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.apache.jetspeed.AbstractRequestContextTestCase;
31 import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
32 import org.apache.jetspeed.container.window.PortletWindowAccessor;
33 import org.apache.jetspeed.deployment.impl.StandardDeploymentManager;
34 import org.apache.jetspeed.factory.PortletFactory;
35 import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
36 import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
37 import org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManager;
38 import org.apache.jetspeed.util.DirectoryHelper;
39 import org.apache.jetspeed.util.JarHelper;
40 import org.apache.pluto.om.portlet.PortletDefinition;
41
42 /***
43 * <p>
44 * TestSimpleDeployment
45 * </p>
46 *
47 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
48 * @version $Id: TestSimpleDeployment.java 517719 2007-03-13 15:05:48Z ate $
49 *
50 */
51 public class TestSimpleDeployment extends AbstractRequestContextTestCase
52 {
53 protected static final String TEST_PORTLET_APP_NAME = "HW_App";
54 protected String webAppsDir;
55
56 protected File deploySrc;
57
58 protected File deployRootFile;
59
60 protected String testDb;
61 protected File webAppsDirFile;
62 protected File copyFrom;
63 protected PortletWindowAccessor windowAccess;
64 protected PortletFactory portletFactory;
65 protected ApplicationServerManager manager;
66
67
68 /***
69 * Start the tests.
70 *
71 * @param args
72 * the arguments. Not used
73 */
74 public static void main( String args[] )
75 {
76 junit.awtui.TestRunner.main(new String[]{TestSimpleDeployment.class.getName()});
77 }
78
79 /***
80 * Creates the test suite.
81 *
82 * @return a test suite (<code>TestSuite</code>) that includes all
83 * methods starting with "test"
84 */
85 public static Test suite()
86 {
87
88
89 return new TestSuite(TestSimpleDeployment.class);
90 }
91 public void testFileSystemHelperOnWar() throws Exception
92 {
93 File demoApp = new File(deploySrc, "demo.war");
94
95 JarHelper jarHelper = new JarHelper(demoApp, true);
96 File rootDirectory = jarHelper.getRootDirectory();
97 File webXml = new File(rootDirectory, "WEB-INF/web.xml");
98 assertTrue(webXml.exists());
99 jarHelper.close();
100 assertFalse(webXml.exists());
101
102
103 jarHelper = new JarHelper(demoApp, false);
104 assertTrue(webXml.exists());
105 jarHelper.close();
106 assertTrue(webXml.exists());
107 }
108
109 public void testFileSystemManagerOnDir() throws Exception
110 {
111 File demoApp = new File("./test/testdata/deploy/webapp");
112 assertTrue(demoApp.exists());
113
114 DirectoryHelper dirHelper = new DirectoryHelper(demoApp);
115 File webXml = new File(dirHelper.getRootDirectory(), "WEB-INF/web.xml");
116 assertTrue(webXml.exists());
117
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290 /***
291 * <p>
292 * buildEntityTestData
293 * </p>
294 *
295 * @param autoDeployment
296 * @throws IOException
297 * @throws PortletEntityNotStoredException
298 */
299 protected void buildEntityTestData( StandardDeploymentManager autoDeployment ) throws Exception
300 {
301 copyDeployables();
302
303 File demoApp = new File(webAppsDirFile, TEST_PORTLET_APP_NAME);
304
305 autoDeployment.fireDeploymentEvent();
306
307 verifyDemoAppCreated(TEST_PORTLET_APP_NAME, demoApp);
308
309 MutablePortletApplication app = portletRegistry.getPortletApplication(TEST_PORTLET_APP_NAME);
310
311 PortletDefinition portlet = (PortletDefinition) app.getPortletDefinitionList().iterator().next();
312
313 MutablePortletEntity entity = entityAccess.newPortletEntityInstance(portlet);
314 entity.setId("testEnity");
315
316 entityAccess.storePortletEntity(entity);
317
318
319
320 }
321
322 /***
323 * <p>
324 * verifyDemoAppCreated
325 * </p>
326 *
327 * @param demoApp
328 */
329 private void verifyDemoAppCreated( String appName, File appFile )
330 {
331 assertNotNull(appName + " was not registered into the portlet registery.", portletRegistry
332 .getPortletApplicationByIdentifier(TEST_PORTLET_APP_NAME));
333 assertTrue(appName + " directory was not created, app not deployed.", appFile.exists());
334 }
335
336
337
338
339
340 /***
341 * <p>
342 * verifyDemoAppDeleted
343 * </p>
344 *
345 * @param demoApp
346 *
347 private void verifyDemoAppDeleted( String appName, File appFile )
348 {
349 assertNull(appName + " was not removed from the registry.", portletRegistry
350 .getPortletApplicationByIdentifier(TEST_PORTLET_APP_NAME));
351 assertFalse(appName+" directory was not deleted.", appFile.exists());
352 }
353 */
354
355 /***
356 * @see junit.framework.TestCase#setUp()
357 */
358 public void setUp()
359 {
360
361 try
362 {
363 super.setUp();
364 copyFrom = new File("./test/deployment/deploy");
365 deploySrc = new File("./target/deployment/deploy");
366 deploySrc.mkdirs();
367 deployRootFile = new File("./target/deployment/templates/decorators");
368 deployRootFile.mkdirs();
369 webAppsDirFile = new File("./target/deployment/webapps");
370 webAppsDirFile.mkdirs();
371
372 webAppsDir = webAppsDirFile.getCanonicalPath();
373 testDb = new File("./test/db/hsql/Registry").getCanonicalPath();
374
375 copyDeployables();
376
377 }
378 catch (Exception e)
379 {
380 e.printStackTrace();
381 throw new AssertionFailedError("Unable to set up test environment " + e.toString());
382 }
383
384 }
385
386 /***
387 * <p>
388 * copyDeployables
389 * </p>
390 * @throws IOException
391 */
392 protected void copyDeployables() throws IOException
393 {
394
395
396 copyFiles(copyFrom, deploySrc);
397
398 }
399
400
401
402 /***
403 * <p>
404 * copyFiles
405 * </p>
406 *
407 * @throws IOException
408 * @throws FileNotFoundException
409 */
410 protected void copyFiles(File srcDir, File dstDir) throws IOException, FileNotFoundException
411 {
412 File[] children = srcDir.listFiles();
413 for(int i=0; i<children.length; i++)
414 {
415 File child = children[i];
416 if(child.isFile())
417 {
418 File toFile = new File(dstDir, child.getName());
419 toFile.createNewFile();
420 FileChannel srcChannel = new FileInputStream(child).getChannel();
421 FileChannel dstChannel = new FileOutputStream(toFile).getChannel();
422 dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
423 srcChannel.close();
424 dstChannel.close();
425 }
426 else
427 {
428 File newSubDir = new File(dstDir, child.getName());
429 newSubDir.mkdir();
430 copyFiles(child, newSubDir);
431 }
432 }
433 }
434
435 /***
436 * @see junit.framework.TestCase#tearDown()
437 */
438 public void tearDown() throws Exception
439 {
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473 super.tearDown();
474
475 }
476
477
478 }