1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.services.scripting;
16
17 import junit.framework.TestCase;
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20
21 import java.io.File;
22 import java.io.InputStream;
23 import java.io.FileOutputStream;
24
25 import org.figure8.join.services.scripting.ant.AntScriptLauncherTest;
26 /**
27 * This is a base class for JUnit tests for ScriptLauncher. It realizes
28 * the setUp() and the tearDown() of a working directory for script tests.
29 * It also provides convenient methods for copying and writing script files.
30 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31 * @version $Revision: 1.2 $
32 */
33 public class ScriptLauncherTest extends TestCase{
34
35
36
37 /** The working directory for Ant script. */
38 protected File workingDir = null;
39
40
41
42
43 /** Default constructor. Build a test case using a name. */
44 public ScriptLauncherTest(String testName){
45 super(testName);
46 }
47
48
49
50
51 /**
52 * Make this class a TestSuite.
53 * @return A TestSuite containing this TestCase.
54 */
55 public static Test suite(){
56 TestSuite suite = new TestSuite(ScriptLauncherTest.class);
57 return suite;
58 }
59
60
61
62
63 /** Setup the test fixtures */
64 public void setUp() throws Exception{
65 super.setUp();
66
67 String userDir = System.getProperty("user.dir");
68 workingDir = new File(userDir, "join-script-test");
69 workingDir.mkdir();
70 }
71
72 /** Tear down resources (ie: delete created directory) */
73 public void tearDown() throws Exception{
74 if (workingDir != null && workingDir.isDirectory()){
75 deleteDir(workingDir);
76 workingDir.delete();
77 }
78 }
79
80
81
82
83 /**
84 * Write stream for testing. This allow subclasses to copy script file
85 * from in-memory stream to real filesystem file (with a path access)
86 * @param targetFile The file to write
87 * @param is The input stream to use as source
88 * @throws Exception IOException may occur
89 */
90 protected void writeToFile(File targetFile, InputStream is) throws Exception{
91 FileOutputStream fos = new FileOutputStream(targetFile);
92
93 int bytes = 0;
94 byte[] buffer = new byte[8192];
95 while ((bytes = is.read(buffer, 0, 8192)) != -1)
96 fos.write(buffer, 0, bytes);
97
98 fos.close();
99 }
100
101
102
103
104 /** Delete a directory content. */
105 private void deleteDir(File directory){
106 if (directory != null && directory.isDirectory()){
107 File[] files = directory.listFiles();
108 for (int i=0; i < files.length; i++){
109 File file = files[i];
110 if (file.isDirectory())
111 deleteDir(file);
112 file.delete();
113 }
114 }
115 }
116 }