Package com._1c.g5.v8.dt.testing
Class TestingWorkspace
java.lang.Object
com._1c.g5.v8.dt.testing.TestingWorkspace
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionCreates an instance ofTestingWorkspace
with default parameters.TestingWorkspace
(boolean autoBuild, boolean cleanUp) Creates an instance ofTestingWorkspace
. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
after()
Tears down the test execution.org.junit.runners.model.Statement
apply
(org.junit.runners.model.Statement base, org.junit.runner.Description description) protected void
before()
Sets up the test execution.void
Builds the workspace and waits for build completion.void
Deletes all resources in the workspace.getBundleEntry
(String path, Class<?> bundleClass) Returns bundle file entry by its path.org.eclipse.core.resources.IProject
getProject
(String name) Returns the testing workspace project.org.eclipse.core.resources.IWorkspace
Returns the testing workspace.org.eclipse.core.resources.IWorkspaceRoot
Returns the testing workspace root.void
setCopyProjectOnSetUp
(boolean value) Sets flag of copying project content into the workspace during the setup phase.void
setDeadlockDetectorThreshold
(long timeThreshold) Sets the deadlock detector reporting threshold (in milliseconds).org.eclipse.core.resources.IProject
setUpProject
(String name, Class<?> sourceClass) Creates a new project in the workspace.org.eclipse.core.resources.IProject
setUpProject
(String name, String sourceName, Class<?> sourceClass) Creates a new project in the workspace.org.eclipse.core.resources.IProject
setUpProject
(String name, String sourceName, Path sourceRoot) CreatesIProject
.org.eclipse.core.resources.IProject
setUpProject
(String name, Path sourceRoot) CreatesIProject
.void
Waits for the workspace build completion.
-
Constructor Details
-
TestingWorkspace
public TestingWorkspace()Creates an instance ofTestingWorkspace
with default parameters. -
TestingWorkspace
public TestingWorkspace(boolean autoBuild, boolean cleanUp) Creates an instance ofTestingWorkspace
.- Parameters:
autoBuild
- the property "Build Automatically" value for the testing workspacecleanUp
- 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 Details
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description) - Specified by:
apply
in interfaceorg.junit.rules.TestRule
-
getWorkspace
public org.eclipse.core.resources.IWorkspace getWorkspace()Returns the testing workspace. Shortcut toResourcesPlugin.getWorkspace()
.- Returns:
- the testing workspace, never
null
-
getWorkspaceRoot
public org.eclipse.core.resources.IWorkspaceRoot getWorkspaceRoot()Returns the testing workspace root. Shortcut togetWorkspace().getRoot()
. -
getProject
Returns the testing workspace project. Shortcut togetWorkspaceRoot().getProject(name)
.- Parameters:
name
- the name of the project to get, cannot benull
- Returns:
- the project with the provided name, never
null
-
buildWorkspace
public void buildWorkspace() throws org.eclipse.core.runtime.CoreExceptionBuilds 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.CoreExceptionDeletes 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 ifcopyProjectOnSetUp
flag set totrue
(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 benull
sourceClass
- the source class to get OSGi-bundle for, e.g. test case, cannot benull
- Returns:
- the created and opened project never
null
- Throws:
org.eclipse.core.runtime.CoreException
- if project creation error occurredIllegalArgumentException
- 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 ifcopyProjectOnSetUp
flag set totrue
(default). The content must reside in the folder/workspace/
<project-name> inside the bundle.- Parameters:
name
- the name of the creating project, cannot benull
sourceName
- the source of source project inside bundle to set up, cannot benull
sourceClass
- the source class to get OSGi-bundle for, e.g. test case, cannot benull
- Returns:
- the created and opened project never
null
- Throws:
org.eclipse.core.runtime.CoreException
- if project creation error occurredIllegalArgumentException
- 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 CreatesIProject
.- Parameters:
name
- the name of the creating project and source project in the source root, cannot benull
sourceRoot
- the location of the project parent directory, cannot benull
- Returns:
- the new project, never
null
- Throws:
org.eclipse.core.runtime.CoreException
- if project creation error occurredIllegalArgumentException
- 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 CreatesIProject
.- Parameters:
name
- the name of the creating project, cannot benull
sourceName
- the source of source project in the source root, cannot benull
sourceRoot
- the location of the project parent directory, cannot benull
- Returns:
- the new project, never
null
- Throws:
org.eclipse.core.runtime.CoreException
- if project creation error occurredIllegalArgumentException
- 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
-
getBundleEntry
public Optional<Path> getBundleEntry(String path, Class<?> bundleClass) throws org.eclipse.core.runtime.CoreException Returns bundle file entry by its path.- Parameters:
path
- path, cannot benull
bundleClass
- bundle class, cannot benull
- Returns:
- optional path, never
null
- Throws:
org.eclipse.core.runtime.CoreException
-
setDeadlockDetectorThreshold
public void setDeadlockDetectorThreshold(long timeThreshold) Sets the deadlock detector reporting threshold (in milliseconds). Applies to the wlole lifecycle of aTestingWorkspace
for a target test file- Parameters:
timeThreshold
- The time the detector will fire if test case will not be finished in the given time
-
before
Sets up the test execution. Method is executed before the rule statement. -
after
Tears down the test execution. Method is executed after the rule statement.
-