geronimo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dain Sundstrom <d...@iq80.com>
Subject Re: svn commit: r432773 - /geronimo/trunk/modules/geronimo-testsupport/src/main/java/org/apache/geronimo/testsupport/TestSupport.java
Date Sat, 19 Aug 2006 15:11:26 GMT
I don't think that trick will work for an IDE, as it will only tell  
you which directory the IDE started in.  In intellij you can use  
something like this:

     public static String baseDir() {
         Class myClass = null; // TestClass.class

         URL classUrl = myClass.getClassLoader().getResource 
(myClass.getName().replace('.', '/') + ".class");
         if (classUrl == null) {
             throw new RuntimeException("Could not find TestClass");
         }

         File targetDir = null;
         try {
             URLConnection urlConnection = classUrl.openConnection();

             if (urlConnection instanceof JarURLConnection) {
                 JarURLConnection jarConnection = (JarURLConnection)  
urlConnection;
                 classUrl = jarConnection.getJarFileURL();

                 targetDir = new File(new URI(classUrl.toString 
())).getParentFile();
             } else {
                 File classesDir = new File(new URI(classUrl.toString 
())).getParentFile();
                 for (char c : myClass.getName().toCharArray()) {
                     if (c == '.') {
                         classesDir = classesDir.getParentFile();
                     }
                 }
             }
         } catch (Exception e) {
             log.debug("Could not determine classes installation  
directory", e);
         }

         if (targetDir != null) {
             File moduleDir = targetDir.getParentFile();
             return moduleDir.getAbsolutePath();
         } else {
             return System.getProperty("basedir", System.getProperty 
("user.dir"));
         }

     }

Make sure to test this code, because I didn't :)

-dain


On Aug 18, 2006, at 4:47 PM, Jason Dillon wrote:

> Ya, I will add that as soon as I can verify that it works in IDEA  
> an Eclipse... probably sometime next week.
>
> --jason
>
>
> On Aug 18, 2006, at 4:31 PM, Gianny Damour wrote:
>
>> On 19/08/2006, at 9:15 AM, jdillon@apache.org wrote:
>>
>>> Author: jdillon
>>> Date: Fri Aug 18 16:15:53 2006
>>> New Revision: 432773
>>>
>>> URL: http://svn.apache.org/viewvc?rev=432773&view=rev
>>> Log:
>>> Comments about BASEDIR and IDE magic
>>>
>>> Modified:
>>>     geronimo/trunk/modules/geronimo-testsupport/src/main/java/org/ 
>>> apache/geronimo/testsupport/TestSupport.java
>>>
>>> Modified: geronimo/trunk/modules/geronimo-testsupport/src/main/ 
>>> java/org/apache/geronimo/testsupport/TestSupport.java
>>> URL: http://svn.apache.org/viewvc/geronimo/trunk/modules/geronimo- 
>>> testsupport/src/main/java/org/apache/geronimo/testsupport/ 
>>> TestSupport.java?rev=432773&r1=432772&r2=432773&view=diff
>>> ==================================================================== 
>>> ==========
>>> --- geronimo/trunk/modules/geronimo-testsupport/src/main/java/org/ 
>>> apache/geronimo/testsupport/TestSupport.java (original)
>>> +++ geronimo/trunk/modules/geronimo-testsupport/src/main/java/org/ 
>>> apache/geronimo/testsupport/TestSupport.java Fri Aug 18 16:15:53  
>>> 2006
>>> @@ -37,6 +37,12 @@
>>>      private static final File BASEDIR;
>>>
>>>      static {
>>> +        //
>>> +        // TODO: Add some special magic here to figure this out  
>>> when we are running from
>>> +        //       and IDE, like IDEA or Eclipse.  user.dir/target  
>>> might work... but need
>>> +        //       to validate what env each IDE has this set to.
>>> +        //
>>> +
>>>          String tmp = System.getProperty("basedir");
>>
>> Hi Jason,
>>
>> This is the trick that Dain has suggested:
>>
>>     private static final tmp = System.getProperty("basedir",  
>> System.getProperty("user.dir"));
>>
>>
>> Thanks,
>> Gianny
>>
>>
>>
>>>          if (tmp == null) {
>>>              throw new Error("Missing 'basedir' property; tests  
>>> need this property set to run correctly");
>>>
>>>
>>


Mime
View raw message