KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > junit > mock > TestMocks


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.junit.mock;
16
17 import java.io.BufferedOutputStream JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.PrintStream JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.hivemind.util.PropertyUtils;
26 import org.apache.tapestry.junit.TapestryTestCase;
27
28 /**
29  * Test case for Mock Servlet API tests using the Simple application.
30  *
31  * @author Howard Lewis Ship
32  * @since 2.2
33  */

34
35 public class TestMocks extends TapestryTestCase
36 {
37     public static final String JavaDoc LOGS_DIR = "/target/logs";
38
39     public static final String JavaDoc DEFAULT_BASE_DIR = ".";
40
41     public static final String JavaDoc SCRIPTS_DIR = "/src/scripts";
42
43     private PrintStream JavaDoc _savedOut;
44
45     private PrintStream JavaDoc _savedErr;
46
47     private static String JavaDoc _baseDir;
48
49     public static String JavaDoc getBaseDirectory()
50     {
51         if (_baseDir == null)
52             _baseDir = System.getProperty("BASEDIR", DEFAULT_BASE_DIR);
53
54         return _baseDir;
55     }
56
57     protected void runTest() throws Throwable JavaDoc
58     {
59         String JavaDoc path = getBaseDirectory() + SCRIPTS_DIR + "/" + getName();
60
61         MockTester tester = new MockTester(getBaseDirectory() + "/src/test-data/", path);
62
63         tester.execute();
64
65         PropertyUtils.clearCache();
66     }
67
68     public static Test suite()
69     {
70         TestSuite suite = new TestSuite("Mock Unit Test Suite");
71
72         if (Boolean.getBoolean("skip-mock-tests"))
73         {
74             System.out.println("*** Skipping Mock Unit Test Suite");
75         }
76         else
77         {
78             addScripts(suite);
79
80             // Handy place to perform one-time
81
deleteDir(getBaseDirectory() + "/target/.private");
82         }
83
84         return suite;
85     }
86
87     private static void addScripts(TestSuite suite)
88     {
89         File JavaDoc scriptsDir = new File JavaDoc(getBaseDirectory() + SCRIPTS_DIR);
90
91         String JavaDoc[] names = scriptsDir.list();
92
93         for (int i = 0; i < names.length; i++)
94         {
95             String JavaDoc name = names[i];
96
97             if (name.endsWith(".xml"))
98             {
99                 TestMocks test = new TestMocks();
100
101                 test.setName(name);
102
103                 suite.addTest(test);
104             }
105         }
106     }
107
108     private static void deleteDir(String JavaDoc path)
109     {
110         File JavaDoc file = new File JavaDoc(path);
111
112         if (!file.exists())
113             return;
114
115         deleteRecursive(file);
116     }
117
118     private static void deleteRecursive(File JavaDoc file)
119     {
120         if (file.isFile())
121         {
122             file.delete();
123             return;
124         }
125
126         String JavaDoc[] names = file.list();
127
128         for (int i = 0; i < names.length; i++)
129         {
130             File JavaDoc f = new File JavaDoc(file, names[i]);
131             deleteRecursive(f);
132         }
133
134         file.delete();
135     }
136
137     /**
138      * Ensures that the log directory exists, then redirects System.out and System.err to files
139      * within the log.
140      */

141     protected void setUp() throws Exception JavaDoc
142     {
143         File JavaDoc outDir = new File JavaDoc(getBaseDirectory() + LOGS_DIR);
144
145         if (!outDir.isDirectory())
146             outDir.mkdirs();
147
148         _savedOut = System.out;
149         _savedErr = System.err;
150
151         System.setOut(createPrintStream(outDir, "out"));
152         System.setErr(createPrintStream(outDir, "err"));
153     }
154
155     protected PrintStream JavaDoc createPrintStream(File JavaDoc directory, String JavaDoc extension) throws Exception JavaDoc
156     {
157         String JavaDoc name = getName() + "." + extension;
158
159         File JavaDoc file = new File JavaDoc(directory, name);
160
161         // Open and truncate file.
162

163         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
164
165         BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(fos);
166
167         return new PrintStream JavaDoc(bos, true);
168     }
169
170     /**
171      * Closes System.out and System.err, then restores them to their original values.
172      */

173     protected void tearDown() throws Exception JavaDoc
174     {
175         System.err.close();
176         System.setErr(_savedErr);
177
178         System.out.close();
179         System.setOut(_savedOut);
180     }
181 }
Popular Tags