How do i add a test project to the Eclipse environment created by JUnit for Plugin-testing? - junit

I currently am working on an Eclipse plugin which needs to access the selected project in the Project Explorer. I have to provide JUnit tests, but i'm very unsure how to write proper tests for an Eclipse plugin.
I think JUnit is atleast properly creating a test-eclipse, since i can use calls like "PlatformUI.getWorkbench()" inside the test. But how do i setup a test-project inside this test-eclipse that my JUnit tests can work with? (I also need to set some of the project more internal stuff, since i'm checking natureIds and builderNames)
Thanks in advance for your answers! I would also be glad for links to a walkthrough of writing tests for an eclipse-plugin ;)

You write your tests in a plug-in as well, so that they're part of the executing Eciipse runtime. Then you have access to the APIs from org.eclipse.core.resources to create projects, folders, and files.
For creating a project specifically:
IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(name);
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
// set nature IDs on the description here
try {
project.create(description, new NullProgressMonitor());
project.open(new NullProgressMonitor());
}
catch (CoreException e) {
e.printStackTrace();
}
return project;

Related

How to check if we are on Design Automation cloud or not?

I'm running a plugin in Design Automation platform on forge however I do run it locally as well for testing. I'd like a way to check if the code is running on forge or not.
Searching I came across this example:
https://forge.autodesk.com/blog/how-generate-dynamic-number-output-design-automation-revit-v3
which use if (RuntimeValue.RunOnCloud) however I didn't manage to get it work (nor to find any references for it in forge documentation).
How can I check if I run on forge?
Design automation service sets a special environmental variable DAS_WORKITEM_ID for your appbundle code to make use of it should you need. Given that, you should be able to check if this variable is set to determine if your code is running in DA.
public static string GetWorkitemId()
{
return Environment.GetEnvironmentVariable("DAS_WORKITEM_ID");
}
public static bool IsRunningInDA()
{
return !String.IsNullOrEmpty(GetWorkitemId());
}
Please note that we recommend using same code for your DA appbundle and Desktop Revit DB addin. Use such tactics with caution and try to minimize the differences between your DB addin and DA appbundle.
The startup method of your application differs: OnApplicationInitialized versus OnDesignAutomationReadyEvent. You can set a flag in these and check it from you plugin code, cf. e.g., Preparing a Revit Add-in for Design Automation.

Want to use JUnit in Domino Designer / Java Beans - but keep getting a "Class not found" error?

I do the following:
From the Package Explorer I select "New, Other, JUnit Test Case"
I write this code:
package dk.sample;
import org.junit.*;
import static org.junit.Assert.*;
public class TestCase {
#Test
public void alwaysTrue(){
assertTrue( true );
}
}
I then select "Run As, JUnit test"
Get this error: "Class not found dk.sample.TestCase
java.lang.ClassNotFoundException: ...."
What do I miss? Have tried with different Run Configurations - but it seems like I miss a classpath somewhere? But to what and where?
To make JUnit work within Domino Designer you need to perform few additional steps:
set up source control for your application
adjust the on-disk project to be recognized as Java application
run JUnit tests within your on-disk project
Please note that java agents have to be tested in a different way..
You can find more detailed explanation about enabling JUnit for both XPages and Agents in the following blog post: Unit Tests for Lotus Domino Applications
Here's also a great how-to on this topic.
Coundn't get JUnit to work inside the Domino Designer. Instead of running the tests from DDE, I now run the tests from a XPages. This works like a dream. Made my own 'JUnit runner' class - that is, I just call the JUnit runners but handles the result my self in order to display it as html on the XPage.
Code can be found here: http://xpages.dk/wp-content/uploads/2013/10/junitrunner.txt
Danish blog post here: http://xpages.dk/?p=1162

Globally setting a JUnit runner instead of #RunWith

Without looking into JUnit source itself (my next step) is there an easy way to set the default Runner to be used with every test without having to set #RunWith on every test? We've got a huge pile of unit tests, and I want to be able to add some support across the board without having to change every file.
Ideally I'm hope for something like: -Djunit.runner="com.example.foo".
I don't think this is possible to define globally, but if writing you own main function is an option, you can do something similar through code. You can create a custom RunnerBuilder and pass it to a Suite together with your test classes.
Class<?>[] testClasses = { TestFoo.class, TestBar.class, ... };
RunnerBuilder runnerBuilder = new RunnerBuilder() {
#Override
public Runner runnerForClass(Class<?> testClass) throws Throwable {
return new MyCustomRunner(testClass);
}
};
new JUnitCore().run(new Suite(runnerBuilder, testClasses));
This won't integrate with UI test runners like the one in Eclipse, but for some automated testing scenarios it could be an option.
JUnit doesn’t supporting setting the runner globally. You can hide away the #RunWith in a base class, but this probably won't help in your situation.
Depending on what you want to achieve, you might be able to influence the test behavior globally by using a custom RunListener. Here is how to configure it with the Maven Surefire plugin: http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html#Using_custom_listeners_and_reporters

Android JUnit: Define a different Application subclass

So for my normal Android project, I have the following in AndroidManifest.xml:
<application android:name=".utilities.App" ...>
....
</application>
And then I have my App class:
public class App extends Application {
....
}
And then I have an Android JUnit Test project associated with the Android project. Everything is all fine and dandy and I can write JUnit tests. However, I'm trying to run code coverage with my JUnit tests and I'm getting bloated results. The reason is because my App class gets called and initialized as if my application were actually started. I do not want my custom App class to execute when I run the JUnit tests or code coverage. Any setup I would need for the JUnit tests will go in the appropriate JUnit setup() method. Is there any way I can prevent it from executing my custom App class or a way that any classes/methods/lines that are executed due to the creation of my App class aren't counted towards the code coverage?
A temporary solution that I've found will work unless someone has any better ideas.
Go into the main Android project's AndroidManifest.xml.
Change the android:name attribute from ".utilities.App" to "android.app.Application"
Run the code coverage utility/JUnit tests
Change the android:name attribute back from "android.app.Application" to ".utilities.App"
Re-deploy the app onto the device (so that it uses the right Application class when it runs external to the code coverage/JUnit tests)
I'm sure the real solution is to automate this process, but I'm too lazy to do so, and it just feels hackish and wrong. But at least it's a workaround unless someone has any ideas.

how does Hudson generate a job config.xml?

I need your help !
I want to know how do Hudson generate a job's config.xml?
I explain: I want to add in my application a hudson-like build tool, to do this, a user will have, like in Hudson's GUI, define some parameters like path to jdk, where the pom.xml is stored, etc... and then the config.xml for this job is generated.
Once i will have the config.xml for this job, i will create and build it.
I tried to search for Hudson's API, but it's all about creating a job, building, deleting.. but no way to give it parameters (personalize it). This is a "create" code sample:
private void put(HttpClient client, String hudsonBaseURL,String jobName ,File configFile) throws IOException, HttpException {
PostMethod postMethod = new PostMethod(hudsonBaseURL+ "/createItem?name=" + jobName);
postMethod.setRequestHeader("Content-type","application/xml; charset=ISO-8859-1");
postMethod.setRequestBody(new FileInputStream(configFile));
postMethod.setDoAuthentication(true);
try {
int status = client.executeMethod(postMethod);
System.out.println("Projet existe déjà\n"+status + "\n"+ postMethod.getResponseBodyAsString());
} finally {
postMethod.releaseConnection();
}
}
This method requires the config.xml to create a job.
I'm now trying to see the content of the hudson.war, inside its classes, but i have to say that this is not easy.
I wish i was clear.
Any idea would be welcome.
Nacef.
I recommend using Hudson's remote API for automating creation of a job.
Have a look at http://your.hudson.server/api. Hudson will return HTML documentation for the remote API. Under Create Job you'll see that you can POST a config.xml to a Hudson URL in order to create a job. You should be able to create a template job manually, then use that config.xml as a template in your automated system.
As described in this previous answer, job configuration can be found in HUDSON_HOME/jobs/[name]/config.xml.