I am developing an eclipse plugin that shows the user links to test logs. In the current implementation we register a jUnit run listener that updates the view whenever a suite has been run and this works fine, problem is that the test framework now also support TestNG and we will need the equivalent functionality from the TestNG plugin if the user runs a TestNG testcase.
I found this feature request http://jira.opensymphony.com/browse/TESTNG-313 which suggests that the functionality I am looking for is there to use, at least that´s how I interpret it. Anyway, I can´t seem to get it to work. I try to create and define different objects in the view setup that should listen for ng-runs and calls to for example onFinish() but I have not found a way to "register" the listening class the way you are if you are defining a regular listener from the suite.xml or code. The TestNG class seems to be a singleton but only for every testrun, not for monitoring the plugin for whenever a suite is run. The TestNGPlugin class does not seem to have appropriate methods. Just implementing a TestListenerAdapter or ITestListener interface as a private class does not do the trick.
Does anyone know which is the most appropriate class or interface to implement for this and if needed, how they should be registered?
Btw I am using Eclipse 3.7.0 and TestNG 6.1.1.
I found another way around the problem, probably a better solution in the end. I monitor the org.eclipse.debug.core.DebugPlugin for any launch of any kind and do the update depending on that. This little piece of code made it work.
ILaunchesListener2 runListener = new ILaunchesListener2() {
#Override
public void launchesAdded(ILaunch[] arg0) {
}
#Override
public void launchesChanged(ILaunch[] arg0) {
}
#Override
public void launchesRemoved(ILaunch[] arg0) {
}
#Override
public void launchesTerminated(ILaunch[] arg0) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
updateTestRunList();
viewer.setInput(testRuns);
viewer.refresh();
}
});
}
};
DebugPlugin.getDefault().getLaunchManager().addLaunchListener(runListener);
Related
I work on an automated testing framework and I need to close the browser after passing tests on several test classes.
The package structure of the project is similar to this:
PackageA
SomeTestClass1
SomeTestClass2
...
PackageB
SomeOtherTestClass1
SomeOtherTestClass2
...
Note: There can be multiple levels of packages inside one another.
If I use #AfterClass or #AfterAll JUnit annotations I have to close the browser at the 'end' of each test class.
My framework requires launching the browser (as a static variable) and closing it only one time to speed up execution and avoid losing time for every browser launch and login, as I currently have a big number of tests that need to be run each night.
Is there any possiblity I could achieve this behaviour?
In JUnit 4, you could use the concept of Rule in conjunction with extending TestWatcher abstract class (org.junit.rules.TestWatcher).
Hence you won't have to close the browser in a method annotated with #AfterClass.
e.g.
public class StartFinishTestWatcher extends TestWatcher {
#Override
protected void starting(Description description) {
System.out.println("Starting test: " + description.getDisplayName());
}
#Override
protected void finished(Description description) {
System.out.println("Finishing test: " + description.getDisplayName());
// close the browser here
}
}
Then you would use this class in a RuleChain, in the test class(es) where you want to close the browser.
public class MyTestInWhichIWantBrowserToClose {
#Rule
public RuleChain ruleChain = buildTestRuleChain();
private RuleChain buildTestRuleChain() { // could be made protected and put in a superclass
return RuleChain.outerRule(new StartFinishTestWatcher());
}
}
In JUnit 5 you could create custom Test Suites and implement BeforeTestExecutionCallback and (in your case) AfterTestExecutionCallback interfaces in one of your base class.
You would then implement the custom logic which decides to close the browser in method public void afterTestExecution(ExtensionContext context).
I'm developing a java component that needs to run both on the command line and as a Eclipse job. The execution can take a few minutes, so I want to inform the user what's happening. I don't want to have a dependency to the Eclipse progress monitor package in my "pure" java code.
How can I provide progress monitoring in the Eclipse job without "polluting" my java code with Eclipse depenendcies?
Write your own progress monitor interface that you use in your code, something like:
interface MyProgressMonitor
{
public void beginTask(String name, int totalWork);
public void done();
public boolean isCanceled();
public void worked(int work);
}
For the Java application you can use an implementation of this that does nothing.
For the Eclipse job use an implementation that delegates to the Eclipse IProgressMonitor:
class EclipseMyProgressMonitor implements MyProgressMonitor
{
private final IProgressMonitor monitor;
EclipseMyProgressMonitor(IProgressMonitor theMonitor)
{
monitor = theMonitor;
}
public void beginTask(String name, int totalWork)
{
monitor.beginTask(name, totalWork);
}
public void done()
{
monitor.done();
}
public boolean isCanceled()
{
return monitor.isCanceled();
}
public void worked(int work)
{
monitor.worked(work);
}
}
It shouldn't be that difficult. Create the java component in a separate project using just pure java. Then create the eclipse functionality responsible for starting the job, see this article for assistance. You eclise code will start a job, this will in turn call your java component.
Issues that will be difficult to handle:
The user may want to abort the operation, you should try to handle this gracefully in your component. If you are just doing computations, you could kill the job, but if you do something that may require clean up, such as writing a file, you will need some other way to deal with it.
Ideally you would like to report a correct and realiable progress to the user, but that will introduce a dependency to org.eclipse.core.runtime.IProgressMonitor which you may not want.
I have an application and want to hook onto an existing command. But I don't want to replace any existing handlers, I want to add functionality (preferably after the default handler finished executing).
Is this possible? Or do I have to find another way to do this?
The scenario is this: I want to register when somebody copies stuff in Eclipse. Then I want do stuff with the things that the user has copied. Since the existing copy functionality is great I don't want to change that. But I would like to know when somebody has executed a copy command.
Note: This only needs to work inside Eclipse! ;) I don't need to monitor the clipboard outside of Eclipse.
I think I found a way to do this! After researching a lot I found this interface: IExecutionListener. The javadoc says:
A listener to the execution of commands. This listener will be notified if a command is about to execute, and when that execution completes. It is not possible for the listener to prevent the execution, only to respond to it in some way.
This is exactly what I want: Don't interfere with the existing command but get notified when it is executed.
So here's the code to add an execution listener to the default copy command:
public void addListenerToCopyCommand() {
ICommandService commandService = (ICommandService) PlatformUI
.getWorkbench().getAdapter(ICommandService.class);
Command defaultCopyCommand = commandService
.getCommand(org.eclipse.ui.IWorkbenchCommandConstants.EDIT_COPY);
defaultCopyCommand.addExecutionListener(new MyCopyListener());
}
public class MyCopyListener implements IExecutionListener {
#Override
public void preExecute(String commandId, ExecutionEvent event) {
// do nothing
}
#Override
public void postExecuteSuccess(String commandId, Object returnValue) {
// !!! Do stuff with the copied things here !!!!
System.out.println("copy command has executed");
}
#Override
public void postExecuteFailure(String commandId,
ExecutionException exception) {
// do nothing
}
#Override
public void notHandled(String commandId, NotHandledException exception) {
// do nothing
}
}
All I have to do now is add an Activator for my plugin that will run when the plugin is started and voila: My Plugin is notified whenever the user copies anything!
I am trying to build a Plugin System in java without resorting to special libraries.
As of now, I am able to load and process my plugins. However, I need to define a special Security Manager which disables any access to file deletion and others.
This Security Manager should only keep plugins in place, because the core of my system has to perform some actions plugins can't. As a result, I cannot set the Security Manager using the System class, or it will affect my system's core as well.
I've tried to "reset" the default Security Manager after loading the plugins, but I was unable to do that. Even if I was able, the plugins would end up executing the system's current Security Manager, not their own.
I would like to know if there is a way to encapsulate classes to specific Security Managers.
On top of my head, I was thinking on looking the stack trace and, if a plugin class is found as a caller, forbid the actions they should not perform, but I fear this will prove to be a little ineffective performance-wise.
As soon as I am able, I will post the code to my Security Manager in order to help with ideas.
Here is the code for my Security Manager:
package org.zeh.filemanager.core.controllers.util;
import java.io.FileDescriptor;
/**
* #author José Ricardo Carvalho Prado de Almeida
*/
public class PluginSecurityManager extends SecurityManager {
#Override
public void checkRead(FileDescriptor filedescriptor) {
super.checkRead(filedescriptor);
}
#Override
public void checkRead(String filename) {
super.checkRead(filename);
}
#Override
public void checkRead(String filename,
Object executionContext) {
//
super.checkRead(filename,
executionContext);
}
#Override
public void checkWrite(FileDescriptor filedescriptor) {
throw new SecurityException("Plugin is trying to write files.");
}
#Override
public void checkWrite(String filename) {
throw new SecurityException("Plugin is trying to write files.");
}
#Override
public void checkDelete(String file) {
super.checkDelete(file);
throw new SecurityException("Plugin is trying to delete files.");
}
#Override
public void checkExec(String cmd) {
super.checkExec(cmd);
throw new SecurityException("Plugin is trying to create subprocesses.");
}
#Override
public void checkExit(int status) {
super.checkExit(status);
throw new SecurityException("Plugin is trying to finalize the JVM.");
}
#Override
public void checkCreateClassLoader() {
super.checkCreateClassLoader();
throw new SecurityException("Plugin is trying to create ClassLoaders.");
}
}
I have found a question (How to implement Java plugin security safely?), that is very similar to mine but the answers do not really fit in my context, where external libraries are not desired.
I forgot to mention that I do have a custom Class Loader that is working perfectly as of now.
I've designed the program so each kind of plugin would have a special Class Loader and such loader would have it's own Security Manager, to restrict different plugins based on functionality.
The only reason this design does not work is because of those Security Managers. If I ignore the use of Security Managers, the system does everything it is supposed to, although it is not secure.
Does anyone have a probable solution for this problem?
Thank you.
I've created SEAM component which checks that at least one user with global admin rights exists and creates one if no.
#Name("installer")
#Stateless
public class InstallerBean implements Installer, Serializable{
#Observer("org.jboss.seam.postInitialization")
public void install() {
...
}
public boolean isInstalled() {
...
}
}
Now I need to test that installer works correctly. I need to check that isInstalled() returns true and check that correct users and roles are exists in the database. However SeamTest.ComponentTest.testComponents() is running before my installation compleate. I can see in the log that last messages from my installer appears in the middle of second test execution so my tests randomly fails.
I'm trying to test my installer in the following way:
public class InstallerTests extends SeamTest {
#Test
public void isInstalledTest() {
new ComponentTest() {
#Override
protected void testComponents() {
...
}
}
}
...
}
How can I make my test starting after my installation compleated?
I'm new to SEAM so maybe I'm doing all compleately wrong. Please tell me if there is a better way.
Maybe you already solved you problem. Do you call some methods asynchronously during the execution of install()? This could randomly delay completing the installation. A very pragmatic yet not the most clean solution is to use Thread.sleep(.) in your test case to wait for the installation to complete.