private void doTest(String resourceToBootstrap, boolean shouldBootstrap, boolean shouldExecuteDelegateTask) throws Exception { final MockSession session = SessionTestUtil.createSession("somerepo", SOME_PROPERTY); final InstallContext ctx = createStrictMock(InstallContext.class); final Task delegateTask = createStrictMock(Task.class); expect(ctx.getJCRSession("somerepo")).andReturn(session); if (shouldExecuteDelegateTask) { delegateTask.execute(ctx); } replay(ctx, delegateTask); final BootstrapConditionally task = new BootstrapConditionally("test", "test", resourceToBootstrap, delegateTask); try { task.execute(ctx); // TODO : ugly hack until BootstrapUtil is refactored / mockable / testable } catch (TaskExecutionException e) { if (shouldBootstrap) { assertEquals("Could not bootstrap: Can't find resource to bootstrap at /some-dir/test/foobar/somerepo.bleh.blih.xml", e.getMessage()); } else { fail("Failed! NullPointerException: " + e.getMessage() + " : " + ExceptionUtils.getStackTrace(e)); } } verify(ctx, delegateTask); }
@Test public void testThrowsExceptionIfPropertyMustExistAndDoesNot() throws Exception { try { doTest("nonexistingproperty", "bleh", true); fail(); } catch (TaskExecutionException e) { assertEquals("Property \"nonexistingproperty\" was expected to exist at /foo/bar", e.getMessage()); } }
@Test public void testUnknownRootThrowsException() throws Exception { NodeUtil.createPath(MgnlContext.getJCRSession("config").getRootNode(), "/hello/world", NodeTypes.Content.NAME, true); Task passing = new NodeBuilderTask("Passing", "This task should work since it uses a known root", ErrorHandling.strict, "config", "/hello/world", addProperty("foo", "bar")); Task failing = new NodeBuilderTask("Failing", "This task should not work since it uses an unknown root", ErrorHandling.strict, "config", "/hello/boo", addProperty("foo", "bar")); final InstallContextImpl ctx = new InstallContextImpl(null); passing.execute(ctx); try { failing.execute(ctx); fail("should have failed"); } catch (TaskExecutionException e) { assertEquals("Could not execute task: Path not found: /hello/boo", e.getMessage()); } }
@Test public void testUnknownRootThrowsException() throws Exception { ContentUtil.createPath(HierarchyManagerUtil.asHierarchyManager(MgnlContext.getJCRSession(RepositoryConstants.CONFIG)), "/hello/world", true); Task passing = new NodeBuilderTask("Passing", "This task should work since it uses a known root", ErrorHandling.strict, "config", "/hello/world", addProperty("foo", "bar")); Task failing = new NodeBuilderTask("Failing", "This task should not work since it uses an unknown root", ErrorHandling.strict, "config", "/hello/boo", addProperty("foo", "bar")); final InstallContextImpl ctx = new InstallContextImpl(null); passing.execute(ctx); try { failing.execute(ctx); fail("should have failed"); } catch (TaskExecutionException e) { assertEquals("Could not execute task: Path not found: hello/boo", e.getMessage()); } }