Class TestingWorkspace

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class TestingWorkspace
    extends Object
    implements org.junit.rules.TestRule
    Testing workspace rule allows to set up test projects for testing purposes. Provides a set of usefull methods such as project set up and workspace clean. Testing workspace cleans working workspace before and after test execution automatically. Typical test execution flow is next:
    • Test starts running in a clean workspace.
    • Test is responsible to set up the necessary workspace state.
    • After running a test, the workspace is cleaned up automatically.

    TestingWorkspace rule may provide its own testing workspace for each test or single testing workspace for all tests inside test class, based on @Rule or @ClassRule annotations.

    Example of rule usage with its own testing workspace for each test inside test class:

     public class TestingWorkspaceRuleExampleTest
     {
         @Rule
         public TestingWorkspace testingWorkspace = new TestingWorkspace();
    
         @Test
         public void testSomeProjectBehavior()
         {
             IProject project = testingWorkspace.setUpProject("Main", getClass());
    
             // we can use project for testing here, it is already built ...
         }
    
         @Test
         public void testSomeOtherProjectBehavior()
         {
             IProject project = testingWorkspace.setUpProject("Other", getClass());
    
             // we can use project for testing here, it is already built ...
         }
     }
     

    Example of rule usage with single testing workspace for all tests inside test class:

     public class TestingWorkspaceRuleClassExampleTest
     {
         @ClassRule
         public static TestingWorkspace testingWorkspace = new TestingWorkspace();
    
         private static IProject mainProject;
         private static IProject otherProject;
    
         @BeforeClass
         public static void setUp() throws Exception
         {
            mainProject = testingWorkspace.setUpProject("Main", TestingWorkspaceRuleClassExampleTest.class);
            otherProject = testingWorkspace.setUpProject("Other", TestingWorkspaceRuleClassExampleTest.class);
         }
    
         @Test
         public void testSomeProjectBehavior()
         {
             // we can use first and second project for testing here, they are already built ...
         }
    
         @Test
         public void testSomeOtherProjectBehavior()
         {
             // ... and here too
         }
     }
     

    Note, that clients need to use TestingWorkspace with @ClassRule rule annotation carefully: tests exectution may have side effect from one test to other, because they share the same workspace.

    • Constructor Detail

      • TestingWorkspace

        public TestingWorkspace()
        Creates an instance of TestingWorkspace with default parameters.
      • TestingWorkspace

        public TestingWorkspace​(boolean autoBuild,
                                boolean cleanUp)
        Creates an instance of TestingWorkspace.
        Parameters:
        autoBuild - the property "Build Automatically" value for the testing workspace
        cleanUp - whether need to clean up workspace automatically before and after each rule exetuion; may be usefull if client want to have manual workspace clean control
    • Method Detail

      • apply

        public org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement base,
                                                       org.junit.runner.Description description)
        Specified by:
        apply in interface org.junit.rules.TestRule
      • getWorkspace

        public org.eclipse.core.resources.IWorkspace getWorkspace()
        Returns the testing workspace. Shortcut to ResourcesPlugin.getWorkspace().
        Returns:
        the testing workspace, never null
      • getWorkspaceRoot

        public org.eclipse.core.resources.IWorkspaceRoot getWorkspaceRoot()
        Returns the testing workspace root. Shortcut to getWorkspace().getRoot().
      • getProject

        public org.eclipse.core.resources.IProject getProject​(String name)
        Returns the testing workspace project. Shortcut to getWorkspaceRoot().getProject(name).
        Parameters:
        name - the name of the project to get, cannot be null
        Returns:
        the project with the provided name, never null
      • buildWorkspace

        public void buildWorkspace()
                            throws org.eclipse.core.runtime.CoreException
        Builds the workspace and waits for build completion.
        Throws:
        org.eclipse.core.runtime.CoreException - if workspace build or wait error occurred
      • cleanUpWorkspace

        public void cleanUpWorkspace()
                              throws org.eclipse.core.runtime.CoreException
        Deletes all resources in the workspace.
        Throws:
        org.eclipse.core.runtime.CoreException - if workspace clean up error occurred
      • setUpProject

        public org.eclipse.core.resources.IProject setUpProject​(String name,
                                                                Class<?> sourceClass)
                                                         throws org.eclipse.core.runtime.CoreException
        Creates a new project in the workspace. The project will be copied from the OSGi-bundle of this test case if copyProjectOnSetUp flag set to true (default). The content must reside in the folder /workspace/<project-name> inside the bundle.
        Parameters:
        name - the name of the creating project and source project inside bundle to set up, cannot be null
        sourceClass - the source class to get OSGi-bundle for, e.g. test case, cannot be null
        Returns:
        the created and opened project never null
        Throws:
        org.eclipse.core.runtime.CoreException - if project creation error occurred
        IllegalArgumentException - if the project with the provided name not found in the source OSGi-bundle
      • setUpProject

        public org.eclipse.core.resources.IProject setUpProject​(String name,
                                                                String sourceName,
                                                                Class<?> sourceClass)
                                                         throws org.eclipse.core.runtime.CoreException
        Creates a new project in the workspace. The project will be copied from the OSGi-bundle of this test case if copyProjectOnSetUp flag set to true (default). The content must reside in the folder /workspace/<project-name> inside the bundle.
        Parameters:
        name - the name of the creating project, cannot be null
        sourceName - the source of source project inside bundle to set up, cannot be null
        sourceClass - the source class to get OSGi-bundle for, e.g. test case, cannot be null
        Returns:
        the created and opened project never null
        Throws:
        org.eclipse.core.runtime.CoreException - if project creation error occurred
        IllegalArgumentException - if the project with the provided name not found in the source OSGi-bundle
      • setUpProject

        public org.eclipse.core.resources.IProject setUpProject​(String name,
                                                                Path sourceRoot)
                                                         throws org.eclipse.core.runtime.CoreException
        Creates IProject.
        Parameters:
        name - the name of the creating project and source project in the source root, cannot be null
        sourceRoot - the location of the project parent directory, cannot be null
        Returns:
        the new project, never null
        Throws:
        org.eclipse.core.runtime.CoreException - if project creation error occurred
        IllegalArgumentException - if project with provided name not found in the source root
      • setUpProject

        public org.eclipse.core.resources.IProject setUpProject​(String name,
                                                                String sourceName,
                                                                Path sourceRoot)
                                                         throws org.eclipse.core.runtime.CoreException
        Creates IProject.
        Parameters:
        name - the name of the creating project, cannot be null
        sourceName - the source of source project in the source root, cannot be null
        sourceRoot - the location of the project parent directory, cannot be null
        Returns:
        the new project, never null
        Throws:
        org.eclipse.core.runtime.CoreException - if project creation error occurred
        IllegalArgumentException - if project with provided name not found in the source root
      • waitForBuildCompletion

        public void waitForBuildCompletion()
        Waits for the workspace build completion.
      • setCopyProjectOnSetUp

        public void setCopyProjectOnSetUp​(boolean value)
        Sets flag of copying project content into the workspace during the setup phase. Set this flag before any of #setUpProject() calls.
        Parameters:
        value - true if the project content should be copied into the workpace, false otherwise
      • before

        protected void before()
                       throws Exception
        Sets up the test execution. Method is executed before the rule statement.
        Throws:
        Throwable - if execution failed
        Exception
      • after

        protected void after()
                      throws Exception
        Tears down the test execution. Method is executed after the rule statement.
        Throws:
        Throwable - if execution failed
        Exception