Index: magnolia-core/src/test/java/info/magnolia/nodebuilder/task/NodeBuilderTaskTest.java =================================================================== --- magnolia-core/src/test/java/info/magnolia/nodebuilder/task/NodeBuilderTaskTest.java (revision 48529) +++ magnolia-core/src/test/java/info/magnolia/nodebuilder/task/NodeBuilderTaskTest.java (working copy) @@ -37,12 +37,14 @@ import info.magnolia.context.MgnlContext; import info.magnolia.module.InstallContextImpl; import info.magnolia.module.delta.Task; -import static info.magnolia.nodebuilder.Ops.*; +import info.magnolia.module.delta.TaskExecutionException; import info.magnolia.test.RepositoryTestCase; import javax.jcr.PathNotFoundException; import javax.jcr.RepositoryException; +import static info.magnolia.nodebuilder.Ops.*; + /** * * @author gjoseph @@ -50,14 +52,19 @@ */ public class NodeBuilderTaskTest extends RepositoryTestCase { public void testUnknownRootThrowsException() throws Exception { - MgnlContext.getHierarchyManager("config").getRoot().createContent("hello").createContent("world"); + ContentUtil.createPath(MgnlContext.getHierarchyManager("config"), "/hello/world", true); - MgnlContext.getHierarchyManager("config").getContent("/hello"); - MgnlContext.getHierarchyManager("config").getContent("/hello/world"); + 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")); - assertPathNotFoundExceptionFor("boo"); - assertPathNotFoundExceptionFor("hello/woohoo"); - assertPathNotFoundExceptionFor("hello/world/blah"); + final InstallContextImpl ctx = new InstallContextImpl(); + passing.execute(ctx); + try { + failing.execute(ctx); + fail("should have failed"); + } catch (TaskExecutionException e) { + assertEquals("Could not execute task: hello/boo", e.getMessage()); + } } public void testSyntax() throws Exception { @@ -90,14 +97,4 @@ configurateTemplateStkSection.execute(new InstallContextImpl()); } - private void assertPathNotFoundExceptionFor(final String path) throws RepositoryException { - try { - MgnlContext.getHierarchyManager("config").getContent(path); - fail("should have failed"); - } catch (PathNotFoundException e) { - assertEquals(path, e.getMessage()); - } - } - - } Index: magnolia-core/src/test/java/info/magnolia/nodebuilder/task/ModuleNodeBuilderTaskTest.java =================================================================== --- magnolia-core/src/test/java/info/magnolia/nodebuilder/task/ModuleNodeBuilderTaskTest.java (revision 48529) +++ magnolia-core/src/test/java/info/magnolia/nodebuilder/task/ModuleNodeBuilderTaskTest.java (working copy) @@ -36,58 +36,51 @@ import info.magnolia.cms.util.ContentUtil; import info.magnolia.context.MgnlContext; import info.magnolia.module.InstallContextImpl; -import info.magnolia.module.delta.Task; -import static info.magnolia.nodebuilder.Ops.*; +import info.magnolia.module.model.ModuleDefinition; import info.magnolia.test.RepositoryTestCase; import javax.jcr.PathNotFoundException; import javax.jcr.RepositoryException; +import static info.magnolia.nodebuilder.Ops.addProperty; + /** * * @author gjoseph * @version $Revision: $ ($Author: $) */ -public class NodeBuilderTaskTest extends RepositoryTestCase { - public void testUnknownRootThrowsException() throws Exception { - MgnlContext.getHierarchyManager("config").getRoot().createContent("hello").createContent("world"); +public class ModuleNodeBuilderTaskTest extends RepositoryTestCase { + public void testModuleNodeIsCreatedIfNeeded() throws Exception { + // given + ContentUtil.createPath(MgnlContext.getHierarchyManager("config"), "/modules", true); + assertPathNotFoundExceptionFor("modules/test-module"); + final ModuleNodeBuilderTask task = new ModuleNodeBuilderTask("Test", "Tests", ErrorHandling.strict, + addProperty("Hello", "World") + ); - MgnlContext.getHierarchyManager("config").getContent("/hello"); - MgnlContext.getHierarchyManager("config").getContent("/hello/world"); + // when + final InstallContextImpl ctx = new InstallContextImpl(); + ctx.setCurrentModule(new ModuleDefinition("test-module", null, null, null)); + task.execute(ctx); - assertPathNotFoundExceptionFor("boo"); - assertPathNotFoundExceptionFor("hello/woohoo"); - assertPathNotFoundExceptionFor("hello/world/blah"); + // then + assertEquals("World", MgnlContext.getHierarchyManager("config").getNodeData("/modules/test-module/Hello").getString()); } - public void testSyntax() throws Exception { - ContentUtil.createPath(MgnlContext.getHierarchyManager("config"), "/modules/stk/templates/stkSection/mainArea/opener/paragraphs/stkTeaserOpener"); + public void testUsesExistingModuleNode() throws Exception { + // given + ContentUtil.createPath(MgnlContext.getHierarchyManager("config"), "/modules/test-module", true); + final ModuleNodeBuilderTask task = new ModuleNodeBuilderTask("Test", "Tests", ErrorHandling.strict, + addProperty("Hello", "World") + ); - final Task configurateTemplateStkSection = new NodeBuilderTask("Config Task stkSection", "Configure stkSection template.", - ErrorHandling.logging, - "config", "/modules/stk/templates", + // when + final InstallContextImpl ctx = new InstallContextImpl(); + ctx.setCurrentModule(new ModuleDefinition("test-module", null, null, null)); + task.execute(ctx); - getNode("stkSection/mainArea").then( - addProperty("template", "/templates/xxx/pages/section/pageIntroMainArea.ftl"), - addNode("floating").then( - addProperty("columns", "2"), - addProperty("enabled", "true")), - addNode("opener/paragraphs/xxxTeaserOpener").then( - addProperty("name", "xxxTeaserOpener")), - addNode("paragraphs").then( - addNode("xxxSingleLink").then( - addProperty("name", "xxxSingleLink")), - addNode("stkTeaserFingerTabbed").then( - addProperty("name", "stkTeaserFingerTabbed")), - addNode("xxxTeaserNewsList").then( - addProperty("name", "xxxTeaserNewsList")), - addNode("xxxExternalTeaser").then( - addProperty("name", "xxxExternalTeaser")), - addNode("xxxChronicleTeaser").then( - addProperty("name", "xxxChronicleTeaser"))), - remove("opener/paragraphs/stkTeaserOpener")) - ); - configurateTemplateStkSection.execute(new InstallContextImpl()); + // then + assertEquals("World", MgnlContext.getHierarchyManager("config").getNodeData("/modules/test-module/Hello").getString()); } private void assertPathNotFoundExceptionFor(final String path) throws RepositoryException { @@ -99,5 +92,4 @@ } } - } Index: magnolia-core/src/test/java/info/magnolia/nodebuilder/task/ModuleConfigNodeBuilderTaskTest.java =================================================================== --- magnolia-core/src/test/java/info/magnolia/nodebuilder/task/ModuleConfigNodeBuilderTaskTest.java (revision 0) +++ magnolia-core/src/test/java/info/magnolia/nodebuilder/task/ModuleConfigNodeBuilderTaskTest.java (revision 0) @@ -0,0 +1,112 @@ +/** + * This file Copyright (c) 2009-2011 Magnolia International + * Ltd. (http://www.magnolia-cms.com). All rights reserved. + * + * + * This file is dual-licensed under both the Magnolia + * Network Agreement and the GNU General Public License. + * You may elect to use one or the other of these licenses. + * + * This file is distributed in the hope that it will be + * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. + * Redistribution, except as permitted by whichever of the GPL + * or MNA you select, is prohibited. + * + * 1. For the GPL license (GPL), you can redistribute and/or + * modify this file under the terms of the GNU General + * Public License, Version 3, as published by the Free Software + * Foundation. You should have received a copy of the GNU + * General Public License, Version 3 along with this program; + * if not, write to the Free Software Foundation, Inc., 51 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * 2. For the Magnolia Network Agreement (MNA), this file + * and the accompanying materials are made available under the + * terms of the MNA which accompanies this distribution, and + * is available at http://www.magnolia-cms.com/mna.html + * + * Any modifications to this file must keep this entire header + * intact. + * + */ +package info.magnolia.nodebuilder.task; + +import info.magnolia.cms.util.ContentUtil; +import info.magnolia.context.MgnlContext; +import info.magnolia.module.InstallContextImpl; +import info.magnolia.module.model.ModuleDefinition; +import info.magnolia.test.RepositoryTestCase; + +import javax.jcr.PathNotFoundException; +import javax.jcr.RepositoryException; + +import static info.magnolia.nodebuilder.Ops.addProperty; + +/** + * + * @author gjoseph + * @version $Revision: $ ($Author: $) + */ +public class ModuleConfigNodeBuilderTaskTest extends RepositoryTestCase { + public void testModuleConfigNodeIsCreatedIfNeeded() throws Exception { + // given + ContentUtil.createPath(MgnlContext.getHierarchyManager("config"), "/modules/test-module", true); + assertPathNotFoundExceptionFor("modules/test-module/config"); + final ModuleConfigNodeBuilderTask task = new ModuleConfigNodeBuilderTask("Test", "Tests", ErrorHandling.strict, + addProperty("Hello", "World") + ); + + // when + final InstallContextImpl ctx = new InstallContextImpl(); + ctx.setCurrentModule(new ModuleDefinition("test-module", null, null, null)); + task.execute(ctx); + + // then + assertEquals("World", MgnlContext.getHierarchyManager("config").getNodeData("/modules/test-module/config/Hello").getString()); + } + + public void testModuleNodeIsCreatedIfNeeded() throws Exception { + // given + ContentUtil.createPath(MgnlContext.getHierarchyManager("config"), "/modules", true); + assertPathNotFoundExceptionFor("modules/test-module"); + final ModuleConfigNodeBuilderTask task = new ModuleConfigNodeBuilderTask("Test", "Tests", ErrorHandling.strict, + addProperty("Hello", "World") + ); + + // when + final InstallContextImpl ctx = new InstallContextImpl(); + ctx.setCurrentModule(new ModuleDefinition("test-module", null, null, null)); + task.execute(ctx); + + // then + assertEquals("World", MgnlContext.getHierarchyManager("config").getNodeData("/modules/test-module/config/Hello").getString()); + } + + public void testUsesExistingModuleConfigNode() throws Exception { + // given + ContentUtil.createPath(MgnlContext.getHierarchyManager("config"), "/modules/test-module/config", true); + final ModuleConfigNodeBuilderTask task = new ModuleConfigNodeBuilderTask("Test", "Tests", ErrorHandling.strict, + addProperty("Hello", "World") + ); + + // when + final InstallContextImpl ctx = new InstallContextImpl(); + ctx.setCurrentModule(new ModuleDefinition("test-module", null, null, null)); + task.execute(ctx); + + // then + assertEquals("World", MgnlContext.getHierarchyManager("config").getNodeData("/modules/test-module/config/Hello").getString()); + } + + private void assertPathNotFoundExceptionFor(final String path) throws RepositoryException { + try { + MgnlContext.getHierarchyManager("config").getContent(path); + fail("should have failed"); + } catch (PathNotFoundException e) { + assertEquals(path, e.getMessage()); + } + } + +}