View Javadoc

1   // ========================================================================
2   // Copyright (c) Webtide LLC
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   //
8   // The Eclipse Public License is available at 
9   // http://www.eclipse.org/legal/epl-v10.html
10  //
11  // The Apache License v2.0 is available at
12  // http://www.apache.org/licenses/LICENSE-2.0.txt
13  //
14  // You may elect to redistribute this code under either of these licenses. 
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   * Common utility methods for working with JUnit tests cases in a maven friendly way.
28   */
29  public class MavenTestingUtils
30  {
31      private static File basedir;
32      private static File testResourcesDir;
33      private static File targetDir;
34  
35      // private static Boolean surefireRunning;
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       * Get the directory to the /target directory for this project.
56       * 
57       * @return the directory path to the target directory.
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       * Create a {@link File} object for a path in the /target directory.
71       * 
72       * @param path
73       *            the path desired, no validation of existence is performed.
74       * @return the File to the path.
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       * Get a dir in /target/ that uses the JUnit 3.x {@link TestCase#getName()} to make itself unique.
93       * 
94       * @param test
95       *            the junit 3.x testcase to base this new directory on.
96       * @return the File path to the testcase specific testing directory underneath the 
97       *            <code>${basedir}/target</code> sub directory
98       */
99      public static File getTargetTestingDir(TestCase test)
100     {
101         return getTargetTestingDir(test.getName());
102     }
103 
104     /**
105      * Get a dir in /target/ that uses the an arbitrary name.
106      * 
107      * @param testname
108      *            the testname to create directory against.
109      * @return the File path to the testname sepecific testing directory underneath the
110      *            <code>${basedir}/target</code> sub directory
111      */
112     public static File getTargetTestingDir(String testname)
113     {
114         File dir = new File(getTargetDir(),"test-" + testname);
115         return dir;
116     }
117 
118     /**
119      * Get a dir from the src/test/resource directory.
120      * 
121      * @param name
122      *            the name of the path to get (it must exist as a dir)
123      * @return the dir in src/test/resource
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      * Get a file from the src/test/resource directory.
134      * 
135      * @param name
136      *            the name of the path to get (it must exist as a file)
137      * @return the file in src/test/resource
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      * Get a path resource (File or Dir) from the src/test/resource directory.
148      * 
149      * @param name
150      *            the name of the path to get (it must exist)
151      * @return the path in src/test/resource
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      * Get the directory to the src/test/resource directory
162      * 
163      * @return the directory {@link File} to the src/test/resources directory
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      * Read the contents of a file into a String and return it.
177      * 
178      * @param file
179      *            the file to read.
180      * @return the contents of the file.
181      * @throws IOException
182      *             if unable to read the file.
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 }