org.softevo.ample
Class AmpleLauncher

java.lang.Object
  extended by org.softevo.ample.AmpleLauncher
All Implemented Interfaces:
org.eclipse.debug.core.IStreamListener

public class AmpleLauncher
extends java.lang.Object
implements org.eclipse.debug.core.IStreamListener

Control class for the instrumentation and launching the instrumented code.


Nested Class Summary
private  class AmpleLauncher.SysOutMonitor
          A ProgressMonitor that simply logs to System.out
private  class AmpleLauncher.TestListenerFaker
          A fake class that is used to convince the remote JUnit-Runner of Eclipse that everything is okay.
 
Field Summary
private  InstrumentationConfiguration config
          The instrumentation configuration to be used.
private  org.eclipse.debug.core.ILaunchConfiguration launchConfiguration
          The launchh configuration used to execute the virtual machine for running the tests.
private  java.util.ArrayList outputLocations
          Project's output directories stored as full path names.
private  java.io.File pluginDir
          Project-specific working area for the Ample plugin.
private  org.eclipse.jdt.core.IJavaProject project
          Java project targeted by the JUnits tests and analysed by Ample.
private  java.util.ArrayList requiredJars
          External JAR files required by the project stored as full path names.
 
Constructor Summary
AmpleLauncher(org.eclipse.jdt.core.IJavaProject project, InstrumentationConfiguration config, org.eclipse.debug.core.ILaunchConfiguration launchConfiguration)
          Initializes a new instance of AmpleLauncher.
 
Method Summary
private  void addJUnitJars(java.util.ArrayList jarsList)
          Adds to the given list the full names of the two JAR files that enable JUnit support in Eclipse.
 void cleanWorkingDir()
          Clears the contents of Ample's working directory.
protected  void copyAmpleRuntimeClasses(java.lang.String sourceDirectory)
          This method copies all runtime classes needed for the execution of AMPLE.
 void copyClassFiles()
          Copies all the output files of the targeted project into the working directory.
private  void copyDirectoryRecursively(java.io.File sourceDirectory, java.io.File targetDirectory)
           
private static void copyFile(java.io.File file, java.io.File targetDir)
          Copies a specified file to a given location.
protected  void copyFromJar(java.lang.String sourceDirectory)
          This method is currently not used.
private  java.util.HashMap createClassNameToJavaTypeMap(java.util.HashSet instrumentClassNames)
           
private  java.lang.String[] createClassPath()
          Computes the classpath using the directory with the instrumented .class files and the required JAR files.
protected  org.eclipse.jdt.launching.VMRunnerConfiguration createConfig(java.lang.String testName, int port)
          Creates a configuration with class path that includes org.eclipse.jdt.junit plugin.
protected  java.lang.String findJUnitJarInPlugins()
           
private  java.util.HashSet getInstrumentClassNames(org.softevo.sibrelib.source.ClassFileSource source, org.softevo.sibrelib.classfilter.ClassFilter filter)
          Calculates the names of all classes that shall be instrumented.
private  void initLists()
          Initializes the two fields - outputLocations and requiredJars.
 java.util.HashMap instrument()
          Instruments all the .class files in the working directory.
 AverageSequenceWeightEvaluator launch(org.eclipse.jdt.internal.junit.ui.TestRunInfo failingRun, org.eclipse.jdt.internal.junit.ui.TestRunInfo[] passingRuns, org.eclipse.core.runtime.IProgressMonitor monitor)
          Reruns the JUnit tests requested by the user on the instrumented byte code.
 void prepareRuntimeClasses()
          This method prepares and copies all runtime classes needed by ample.
protected  XMLPatternFileReader runTest(org.eclipse.jdt.internal.junit.ui.TestRunInfo testRun, org.eclipse.jdt.launching.IVMInstall vm)
          This method executes a junit test.
 void streamAppended(java.lang.String text, org.eclipse.debug.core.model.IStreamMonitor monitor)
          Implementation of IStreamListener.
protected  void writeOptionsClass()
          This method creates the options class and fills it with the values needed by the instrumentation configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

config

private InstrumentationConfiguration config
The instrumentation configuration to be used.


launchConfiguration

private org.eclipse.debug.core.ILaunchConfiguration launchConfiguration
The launchh configuration used to execute the virtual machine for running the tests.


project

private org.eclipse.jdt.core.IJavaProject project
Java project targeted by the JUnits tests and analysed by Ample.


pluginDir

private java.io.File pluginDir
Project-specific working area for the Ample plugin.

See Also:
project

outputLocations

private java.util.ArrayList outputLocations
Project's output directories stored as full path names.


requiredJars

private java.util.ArrayList requiredJars
External JAR files required by the project stored as full path names.

The last two entries of this list are the names of the two JAR files that enable JUnit support in Eclipse.

Constructor Detail

AmpleLauncher

public AmpleLauncher(org.eclipse.jdt.core.IJavaProject project,
                     InstrumentationConfiguration config,
                     org.eclipse.debug.core.ILaunchConfiguration launchConfiguration)
              throws org.eclipse.jdt.core.JavaModelException,
                     JUnitSupportException,
                     java.lang.NullPointerException
Initializes a new instance of AmpleLauncher.

Parameters:
project - Targeted Java project.
Throws:
org.eclipse.jdt.core.JavaModelException - If an error occured while accessing resources of the targeted Java project.
JUnitSupportException - If locating Eclipse's JUnit support (the JAR files) has failed.
java.lang.NullPointerException - ...
Method Detail

streamAppended

public void streamAppended(java.lang.String text,
                           org.eclipse.debug.core.model.IStreamMonitor monitor)
Implementation of IStreamListener.
This method is called whenever the vm that runs the tests writes to standard or error output streams.

Specified by:
streamAppended in interface org.eclipse.debug.core.IStreamListener

cleanWorkingDir

public void cleanWorkingDir()
Clears the contents of Ample's working directory.


getInstrumentClassNames

private java.util.HashSet getInstrumentClassNames(org.softevo.sibrelib.source.ClassFileSource source,
                                                  org.softevo.sibrelib.classfilter.ClassFilter filter)
Calculates the names of all classes that shall be instrumented.

Parameters:
source - the class file source
Returns:
the set of all classes in the source

createClassNameToJavaTypeMap

private java.util.HashMap createClassNameToJavaTypeMap(java.util.HashSet instrumentClassNames)
                                                throws org.eclipse.jdt.core.JavaModelException
Throws:
org.eclipse.jdt.core.JavaModelException

copyDirectoryRecursively

private void copyDirectoryRecursively(java.io.File sourceDirectory,
                                      java.io.File targetDirectory)
                               throws java.io.IOException
Throws:
java.io.IOException

copyClassFiles

public void copyClassFiles()
                    throws java.io.IOException
Copies all the output files of the targeted project into the working directory.

Throws:
java.io.IOException - If copying a file to Ample's working directory fails, e.g. due to name collision.

instrument

public java.util.HashMap instrument()
                             throws org.softevo.sibrelib.InstrumentationException,
                                    java.io.IOException,
                                    java.lang.ClassNotFoundException,
                                    org.eclipse.core.runtime.CoreException
Instruments all the .class files in the working directory.

Note that copyClassFiles() should be called prior to calling this method.

Returns:
a hashmap of class names mapped to their java types
Throws:
org.softevo.sibrelib.InstrumentationException
java.io.IOException
java.lang.ClassNotFoundException
org.eclipse.core.runtime.CoreException

writeOptionsClass

protected void writeOptionsClass()
                          throws java.io.IOException
This method creates the options class and fills it with the values needed by the instrumentation configuration.

Throws:
java.io.IOException - if an exception occurs writing the class

copyFromJar

protected void copyFromJar(java.lang.String sourceDirectory)
                    throws java.io.IOException
This method is currently not used.

Parameters:
sourceDirectory -
Throws:
java.io.IOException

copyAmpleRuntimeClasses

protected void copyAmpleRuntimeClasses(java.lang.String sourceDirectory)
                                throws java.io.IOException
This method copies all runtime classes needed for the execution of AMPLE.
This includes all sibrelib jars and required apache files. The method also adds entries for those jar files in the class path.

Parameters:
sourceDirectory - the directory where the classes are located (the plugin directory of ample)
Throws:
java.io.IOException - if an exception occurs copying classes

findJUnitJarInPlugins

protected java.lang.String findJUnitJarInPlugins()
                                          throws java.net.MalformedURLException,
                                                 java.io.IOException
Throws:
java.net.MalformedURLException
java.io.IOException

prepareRuntimeClasses

public void prepareRuntimeClasses()
                           throws java.io.IOException,
                                  java.net.MalformedURLException
This method prepares and copies all runtime classes needed by ample.

It should be called after instrument().

Throws:
java.io.IOException
java.net.MalformedURLException

runTest

protected XMLPatternFileReader runTest(org.eclipse.jdt.internal.junit.ui.TestRunInfo testRun,
                                       org.eclipse.jdt.launching.IVMInstall vm)
                                throws org.eclipse.core.runtime.CoreException,
                                       AnalysisException,
                                       java.util.MissingResourceException
This method executes a junit test.

Parameters:
testRun - the test to be executed
vm - the virtual machine to run the test
Returns:
the reader for the pattern file that was recorded or null if something went wrong
Throws:
org.eclipse.core.runtime.CoreException - if an exception occurs launching the process
AnalysisException - if an exception occurs reading the pattern file
java.util.MissingResourceException - if a workspace ressource cannot be found

launch

public AverageSequenceWeightEvaluator launch(org.eclipse.jdt.internal.junit.ui.TestRunInfo failingRun,
                                             org.eclipse.jdt.internal.junit.ui.TestRunInfo[] passingRuns,
                                             org.eclipse.core.runtime.IProgressMonitor monitor)
                                      throws org.eclipse.core.runtime.CoreException,
                                             java.util.MissingResourceException,
                                             AnalysisException
Reruns the JUnit tests requested by the user on the instrumented byte code.

Note that instrument() should be called prior to calling this method.

Parameters:
junitLaunchConfig - Launch configuration of the JUnit tests run before running Ample.
testRuns - Array of test runs to be launched.
Throws:
org.eclipse.core.runtime.CoreException - ...
java.util.MissingResourceException
AnalysisException

copyFile

private static void copyFile(java.io.File file,
                             java.io.File targetDir)
                      throws java.io.IOException
Copies a specified file to a given location.

Parameters:
file - File to be copied. If the specified file does not exist, calling this method has no effect.
targetDir - Location of the file.
Throws:
java.io.IOException - When I/O error occurs.

addJUnitJars

private void addJUnitJars(java.util.ArrayList jarsList)
                   throws JUnitSupportException
Adds to the given list the full names of the two JAR files that enable JUnit support in Eclipse.

Note: This method contains code copied from JUnitLaunchConfiguration.createClassPath(ILaunchConfiguration).

Parameters:
jarsList - List of JAR file names.
Throws:
JUnitSupportException - If locating any of the two JAR files has failed.
java.lang.NullPointerException - If jarsList is null.

initLists

private void initLists()
                throws org.eclipse.jdt.core.JavaModelException,
                       JUnitSupportException,
                       java.lang.NullPointerException
Initializes the two fields - outputLocations and requiredJars.

Throws:
org.eclipse.jdt.core.JavaModelException - If an error occured while accessing resources of the targeted Java project.
JUnitSupportException - If locating Eclipse's JUnit support (the JAR files) has failed.
java.lang.NullPointerException - ...

createClassPath

private java.lang.String[] createClassPath()
Computes the classpath using the directory with the instrumented .class files and the required JAR files.

Returns:
Classpath in the form of String array.

createConfig

protected org.eclipse.jdt.launching.VMRunnerConfiguration createConfig(java.lang.String testName,
                                                                       int port)
                                                                throws org.eclipse.core.runtime.CoreException,
                                                                       java.util.MissingResourceException
Creates a configuration with class path that includes org.eclipse.jdt.junit plugin. In addition it adds the port for the RemoteTestRunner as an argument.

Note: This method contains code copied from JUnitLaunchConfiguration.createVMRunner(ILaunchConfiguration, IType[], int).

Parameters:
testName - Name of test to be run. It has the form {classname}:{methodname}.
Throws:
org.eclipse.core.runtime.CoreException - ...
java.util.MissingResourceException - If no free port could be found.