tags:

views:

64

answers:

2

I have created an eclipse plugin project and a corresponding fragment project which I use for junit tests.

In the fragment I specify the plugin project as the "Host plugin". Further I specify the following on the build.properties pane:

source.. = src/ output.. = bin/ bin.includes = META-INF/,\ .,\ my.properties

where my.properties is a file located at the root of the fragment project. I have then written a test where I try to load the my.properties file like this:

Properties properties = new Properties();
InputStream istream = this.getClass().getClassLoader()
    .getResourceAsStream("my.properties");

try {
  properties.load(istream);
} catch (IOException e) {
  e.printStackTrace();
}

but the "istream" is null and the test fails with a NullPointerException when calling load in the try block.

I have tried to do the same thing in the host plugin and there it works fine. Any ideas about why I can't read resouces in my PDE fragment when using Junit?

A: 

One problem you MIGHT be having is that

InputStream istream = this.getClass().getClassLoader().
getResourceAsStream("my.properties");

behaves differently in two situations where "this" is located in a different package. Since you did not append "/" to the beginning, java will automatically start looking at the package root instead of the classpath root for the resource. If the code in your plug-in project and your fragment project exist in different packages, you have a problem.

insipid
A: 

Try using Bundle#getEntry. If your plug-in has an Activator, you get a BundleContext object when your plugin is started (use Bundle-ActivationPolicy: lazy in your manifest). You can get the Bundle object from the BundleContext:

public class Activator implements BundleActivator {
   private static Bundle bundle;

   public static Bundle getBundle() {
      return myBundle;
   }
   public void start(BundleContext context) throws Exception {
      bundle = context.getBundle();
   }
}

...
URL url = Activator.getBundle().getEntry("my.properties");
InputStream stream = url.openStream();
properties.load(stream);
Andrew Niefer
Problem solved just needed to use the junit test launcher for pde projects instead of the regular junit launcher.
tul