1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.eclipse.jetty.server.session.test;
17
18 import java.io.File;
19 import java.io.FileReader;
20 import java.io.IOException;
21
22 import junit.framework.TestCase;
23
24 import org.eclipse.jetty.util.IO;
25
26
27
28
29 public class MavenTestingUtils
30 {
31 private static File basedir;
32 private static File testResourcesDir;
33 private static File targetDir;
34
35
36
37 public static File getBasedir()
38 {
39 if (basedir == null)
40 {
41 String cwd = System.getProperty("basedir");
42
43 if (cwd == null)
44 {
45 cwd = System.getProperty("user.dir");
46 }
47
48 basedir = new File(cwd);
49 }
50
51 return basedir;
52 }
53
54
55
56
57
58
59 public static File getTargetDir()
60 {
61 if (targetDir == null)
62 {
63 targetDir = new File(getBasedir(),"target");
64 PathAssert.assertDirExists("Target Dir",targetDir);
65 }
66 return targetDir;
67 }
68
69
70
71
72
73
74
75
76 public static File getTargetFile(String path)
77 {
78 return new File(getTargetDir(),path.replace("/",File.separator));
79 }
80
81 public static File getTargetTestingDir()
82 {
83 File dir = new File(getTargetDir(),"testing");
84 if (!dir.exists())
85 {
86 dir.mkdirs();
87 }
88 return dir;
89 }
90
91
92
93
94
95
96
97
98
99 public static File getTargetTestingDir(TestCase test)
100 {
101 return getTargetTestingDir(test.getName());
102 }
103
104
105
106
107
108
109
110
111
112 public static File getTargetTestingDir(String testname)
113 {
114 File dir = new File(getTargetDir(),"test-" + testname);
115 return dir;
116 }
117
118
119
120
121
122
123
124
125 public static File getTestResourceDir(String name)
126 {
127 File dir = new File(getTestResourcesDir(),name);
128 PathAssert.assertDirExists("Test Resource Dir",dir);
129 return dir;
130 }
131
132
133
134
135
136
137
138
139 public static File getTestResourceFile(String name)
140 {
141 File file = new File(getTestResourcesDir(),name);
142 PathAssert.assertFileExists("Test Resource File",file);
143 return file;
144 }
145
146
147
148
149
150
151
152
153 public static File getTestResourcePath(String name)
154 {
155 File path = new File(getTestResourcesDir(),name);
156 PathAssert.assertExists("Test Resource Path",path);
157 return path;
158 }
159
160
161
162
163
164
165 public static File getTestResourcesDir()
166 {
167 if (testResourcesDir == null)
168 {
169 testResourcesDir = new File(basedir,"src/test/resources".replace("/",File.separator));
170 PathAssert.assertDirExists("Test Resources Dir",testResourcesDir);
171 }
172 return testResourcesDir;
173 }
174
175
176
177
178
179
180
181
182
183
184 public static String readToString(File file) throws IOException
185 {
186 FileReader reader = null;
187 try
188 {
189 reader = new FileReader(file);
190 return IO.toString(reader);
191 }
192 finally
193 {
194 IO.close(reader);
195 }
196 }
197
198 public static String getTestID()
199 {
200 StackTraceElement stacked[] = new Throwable().getStackTrace();
201
202 String name = null;
203
204 for(StackTraceElement stack: stacked) {
205 if(stack.getClassName().endsWith("Test"))
206 {
207 name = stack.getClassName();
208 if (stack.getMethodName().startsWith("test"))
209 {
210 return stack.getClassName() + "#" + stack.getMethodName();
211 }
212 }
213 }
214
215 if(name == null)
216 {
217 return "Unidentified_Test";
218 }
219 return name;
220 }
221 }