Organizing fixture preferences for plugin tests - eclipse

To test an eclipse application I have written a test suite that dynamically discovers available test cases at runtime by searching for test projects stored in a subfolder of a given bundle, looking like this:
my.plugin
src/
fixtures/
p1/
p2/
Each fixture is a fully fledged eclipse project. At runtime my suite discovers p1 and p2 and creates a Test for each. Then each test imports its fixture into the workspace on setup, then runs the actual test code and deletes the fixture on teardown. So far this works great, I just dump another fixture to add a new test. No need to write more code for new tests. Yay!
My problem is now that all this runs against a clean workspace with default preferences. What if some of my fixtures need specific preference settings in order to be useful? I somehow would need to provide those custom preferences with the fixture and make sure they are loaded in the test setup for the fixture and restore the default workbench preferences on teardown. What's the best way to do this in eclipse?

I'm not sure if this is the best solution, but I would do something like this:
Let the user choose (in the settings) if the test should be run in a clean workspace or not. If not, just use the current one, so the users can apply settings there. I think this is reasonable, as test running is usually done in the same workspace as the actual project to be tested.
You can access the current workspace by calling ResourcesPlugin.getWorkspace().

If you want to use project settings only, then the solution is simple. Just create a .settings folder in each project and populate the appropriate org.foo.bundle.name.prefs file.
Things get more complicated if you want to have workspace settings. I would recommend looking at the org.osgi.service.prefs.Preferences and org.eclipse.core.runtime.preferences.IScopeContext APIs. Eclipse uses a hierarchical approach to providing preferences. You can set a hierarchy of scopes, eg- Default, workspace, and project scopes. Each scope has one or more nodes and each node has a set of key-value pairs.
So for example, you generally will look up a preference, say the source level compiler property inside a particular project. Here are the steps that this goes through:
Create the scope hierarchy, typically the root is default, then workspace, then the project.
Get the org.eclipse.jdt.core node from the bottom scope.
Ask the node for a value associated with a key.
If this node has a value, then return it, otherwise go to the node from the next scope.
If none of the scopes contain the key, then return some default value.
Your best bet for doing this work simply and not using internal Eclipse APIs is to use a org.eclipse.ui.texteditor.ChainedPreferenceStore instance. You will create the lookup hierarchy of IPreferenceStores that you want to get your preferences from and one of them will be a custom IPreferenceStore created by you that will supply the extra preferences for your project.
Now, of course you need to ensure that all places that require these custom preferences are able to have a custom ChainedPreferenceStore passed in.

Related

Multiple sub-workspaces in Eclipse

I write code in several languages (Python, C, C++, and Java) using Eclipse. Is it possible to designate a directory on my machine (say /home/workspace/) as the "primary" workspace for any Eclipse session, but then to have subfolders, /home/workspace/python, /home/workspace/java, etc., in which I can create new Eclipse projects.
I don't want to have to navigate menus and select different workspaces for each session of Eclipse that I start up. I would rather just always have permission to manipulate any projects from a variety of folders at any time, but I can't find a clear answer about whether this can be done and how to do it.
As I understand your question; You want to have one workspace, but be able to code in several different languages without switching workspace but at the same time keep the projects separated?
First I would suggest you consider several workspaces, I find it convenient to keep settings and projects in separate workspaces. I rarely have to switch language that often.
But. I think what you want to do is to keep several working sets. You create one java working set, one C++ set and associate your different projects with a working set. Then you can minimize the java working set when you are running C++. For working sets you dont need any subfolders on the harddrive.
You might also want to look into Mylyn. Its a great tool for those who often are switching context. It saves the context (eclipse perspective, open files, etc) as associated with a task.
How about setting Eclipse to prompt for the workspace at launch? It wouldn't allow you to work in two languages at once, but should do the trick otherwise.
An Eclipse workspace can contain projects slated for different languages and those projects can live anywhere on your hard drive. There are at least two ways to do what you want. When creating a new project, uncheck the Use default location checkbox and browse to or specify the folder where you want your project to live. If a project already exists import the project into the workspace using the File->Import menu option and then select Existing Projects into workspace. In the next screen make sure the checkbox for Copy projects into workspace is not selected. This will leave the source files in the original folder.
In the Project explorer view, all the projects are going to look like they live at the root level. However you can group related projects into working sets. Then select just the working set you're interested in and all the others will disappear from view.
A warning is in order if you make use of eclipse variables in external tools (and possibly elsewhere). The syntax you use for paths needs to be adjusted. For example with projects outside the workspace this syntax ${workspace_loc:/MyProject/MyFile.txt} is no longer the same as this syntax ${workspace_loc}/MyProject/MyFile.txt

What's the format of an Eclipse preferences export?

An export of the Eclipse preferences looks a lot like a Properties file. Is this correct?
I'd like to share preferences with my team and for that, I need to filter the data (update/remove local paths, etc). Does anyone know any tools for this?
You might want to have a look at Workspace Mechanic for Eclipse:
The Workspace Mechanic automates maintenance of your Eclipse environment by tweaking preferences, adding extension locations, and so on. You can use it to:
Create a consistent environment among groups as large as the entire company, your local team, or even among your own many workspaces
Save time setting up new workspaces
Create tasks that ensure your favorite new preferences are applied to all your current and future workspaces. (This is one of our favorite features!)
If you have project-specific preferences, they will be stored in the .settings directory of you project.
That means you can add them directly in your VCS and share them through version control.
There is no native tool for filter them, but if you are using a Git repo, you can add filter drivers for making sure you ignore any path-specific changes.
Yes.
Preferences in Eclipse are implemented by org.eclipse.core.internal.preferences.EclipsePreferences. The save(IPath location) method will convert the internal structure to a Properties instance using convertToProperties() and then write the result to disk.

How to apply an external formatting script to files in eclipse?

I'd like to run an external beautifier over my code that's open and replace the contents of the document with the output. Is there a plugin or something I can exploit to do this? I thought it would be a no-brainer under the formatter preferences, but it looks like you can only use internal templates.
One solution is the following:
Create an ant builder on your project. See this article about how to do that. The important things you should know after you read the article:
Add a new ant builder by right click on project and properties. See the implicit variables your ant script will have. The variables will contain one or more resource name and path. So be prepared for collections.
Filter the resouces in the "Build
options" tab. This is important,
since launching a JVM (or a new task
inside IDE JVM) for the change of
every file (filetype) is not so
fast. So filter resources to be
processed like this:
The third thing is to ask eclipse to refresh the resource after the script is run. Like this:
Set the target of the ant script to be run as automatic build (third option). You can set all the four anyway it will be ok:
Write your script. You will find the log of the ant script in console, and you may redirect the log into a file as well. You can see the option on the second picture. Start your script with an echoproperties tag to see what your script gets from eclipse.
I used such builders a lot, it works like it should. I like them. Good luck. Consider accepting your answers, 42 % is not too much :D

How do I force Eclipse to rebuild if files in another project change (any change)?

I've got an Eclipse (Galileo) project (called ProguardBuilder) that runs Proguard over a set of class files in other projects and produces a jar file.
I'd like to have the ProguardBuilder project get rebuilt any time any class file in the other projects changes. AutoBuild doesn't do that; presumably it's smart enough to recognize and ignore any changes that don't affect anything externally visible.
My problem is that I don't care whether or not the change is visible, since I need to completely rebuild ProguardBuilder any time the class files it depends on change at all.
How do I tell Eclipse to do this sort of rebuild?
You might have to use an external builder. Check the documentation, because I've never done this. But the place to start is the "Builders" section of the project properties dialogue.

Eclipse: On Save execute a program

I have recently come across the LESS Leaner CSS a template engine for CSS based on ruby. The idea sounded neat, but in practice we need to compile the program to get CSS. This is cumbersome as we make too many changes while working on CSS and for every edit we don't want to compile.
In Eclipse, there are "Save-Actions" but it handles only formatting changes.
Is there a way on saving the file in Eclipse, to call or trigger the compilation?
Its easy to do this in Vi or Emacs.
I think all you need is to define a custom Builder for your project. That way, you can run a program or an ant script whenever certain files change.
Right click on the project -> Properties -> Builders -> New
While the Builders are a good solution, keep in mind they only work when a build is issued - either using auto-build or using a manual build which is invoked, well, manually. If you are looking for something that will operate after a save, regardless of the auto-build state you will need to write a plugin which listens to resource changes in Eclipse.
You do that by creating a workspace change listener and installing it like that:
ResourcesPlugin.getWorkspace().addResourceChangeListener(
..., IResourceChangeEvent.POST_CHANGE);
I'm sure you can take it from here :-)