From 5421b845c8b409723bd4bf005f37f15f8222b1c2 Mon Sep 17 00:00:00 2001 From: Michael Suzuki Date: Wed, 3 Sep 2014 11:15:47 +0100 Subject: [PATCH] seperated tests into unit and integration --- .../webscripts/webscript.get.html.ftl | 7 ++- .../org/alfresco/demoamp/DemoComponent.java | 29 ++++++++++ .../alfresco/demoamp/HelloWorldMessage.java | 58 +++++++++++++++++++ .../DemoComponentTest.java | 0 .../demoamp/unit/HelloWorldMessageTest.java | 50 ++++++++++++++++ 5 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/java/org/alfresco/demoamp/HelloWorldMessage.java rename archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/test/java/org/alfresco/demoamp/{test => integration}/DemoComponentTest.java (100%) create mode 100644 archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/test/java/org/alfresco/demoamp/unit/HelloWorldMessageTest.java diff --git a/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/amp/config/alfresco/extension/templates/webscripts/webscript.get.html.ftl b/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/amp/config/alfresco/extension/templates/webscripts/webscript.get.html.ftl index f4672ea6..ca0cf4e9 100644 --- a/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/amp/config/alfresco/extension/templates/webscripts/webscript.get.html.ftl +++ b/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/amp/config/alfresco/extension/templates/webscripts/webscript.get.html.ftl @@ -1 +1,6 @@ -Message: ${message} \ No newline at end of file + + +

Welcome to Demoamp

+

${demoMessage}

+ + \ No newline at end of file diff --git a/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/java/org/alfresco/demoamp/DemoComponent.java b/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/java/org/alfresco/demoamp/DemoComponent.java index 01027280..df873808 100644 --- a/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/java/org/alfresco/demoamp/DemoComponent.java +++ b/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/java/org/alfresco/demoamp/DemoComponent.java @@ -15,7 +15,18 @@ limitations under the License. */ package org.alfresco.demoamp; +import java.util.HashMap; +import java.util.Map; +import org.alfresco.model.ContentModel; +import org.alfresco.repo.nodelocator.NodeLocatorService; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.extensions.webscripts.DeclarativeWebScript; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptRequest; import org.alfresco.repo.module.AbstractModuleComponent; import org.alfresco.repo.nodelocator.NodeLocatorService; import org.alfresco.service.cmr.repository.NodeRef; @@ -23,6 +34,7 @@ import org.alfresco.service.cmr.repository.NodeService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + /** * A basic component that will be started for this module. * Uses the NodeLocatorService to easily find nodes and the @@ -30,6 +42,7 @@ import org.apache.commons.logging.LogFactory; * * @author Gabriele Columbro * @author Maurizio Pillitu + * @author Michael Suzuki */ public class DemoComponent extends AbstractModuleComponent { @@ -81,4 +94,20 @@ public class DemoComponent extends AbstractModuleComponent { return nodeLocatorService.getNode("companyhome", null, null); } + /** + * Binding data to webscript demoamp. + * @param req webscript request + * @param status + * @return {@link java.util.Map} data binding + */ + protected Map executeImpl(WebScriptRequest req, Status status ) + { + NodeRef companyHome = getCompanyHome(); + String companyHomeName = (String) nodeService.getProperty(companyHome, ContentModel.PROP_NAME); + int directoryCount = childNodesCount(companyHome); + HelloWorldMessage msg = new HelloWorldMessage(companyHomeName, directoryCount); + Map model = new HashMap(); + model.put("demoMessage", msg); + return model; + } } diff --git a/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/java/org/alfresco/demoamp/HelloWorldMessage.java b/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/java/org/alfresco/demoamp/HelloWorldMessage.java new file mode 100644 index 00000000..ed4faec8 --- /dev/null +++ b/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/main/java/org/alfresco/demoamp/HelloWorldMessage.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.demoamp; + +import com.sun.star.uno.RuntimeException; + +/** + * Simple pojo that holds directory name and + * the number of folders in the directory. + * + * This is used to create the message for the demoamp. + * + * @author Michael Suzuki + * + */ +public class HelloWorldMessage +{ + private final String directoryName; + private final int folders; + + public HelloWorldMessage(final String directoryName, final int folders) + { + if(directoryName == null || directoryName.isEmpty()) + { + throw new RuntimeException("Directory name is required"); + } + this.directoryName = directoryName; + this.folders = folders; + } + + public String toString() + { + return String.format("%s has %d folders", directoryName, folders); + } + +} diff --git a/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/test/java/org/alfresco/demoamp/test/DemoComponentTest.java b/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/test/java/org/alfresco/demoamp/integration/DemoComponentTest.java similarity index 100% rename from archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/test/java/org/alfresco/demoamp/test/DemoComponentTest.java rename to archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/test/java/org/alfresco/demoamp/integration/DemoComponentTest.java diff --git a/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/test/java/org/alfresco/demoamp/unit/HelloWorldMessageTest.java b/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/test/java/org/alfresco/demoamp/unit/HelloWorldMessageTest.java new file mode 100644 index 00000000..e67ef812 --- /dev/null +++ b/archetypes/alfresco-amp-archetype/src/main/resources/archetype-resources/src/test/java/org/alfresco/demoamp/unit/HelloWorldMessageTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2005-2012 Alfresco Software Limited. + * This file is part of Alfresco + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.demo.test.unit; + +import org.alfresco.demoamp.HelloWorldMessage; +import org.junit.Assert; +import org.junit.Test; + + +/**s + * Hello world demo unit test, show the basics of junit and unit testing. + * Create the message to display on hello world webscript. + * @author Michael Suzuki + * + */ +public class HelloWorldMessageTest +{ + @Test + public void init() + { + HelloWorldMessage msg = new HelloWorldMessage("bla", 0); + Assert.assertNotNull(msg); + } + + @Test(expected = RuntimeException.class) + public void initWithNull() + { + new HelloWorldMessage(null, 0); + } + @Test + public void toStringTest() + { + HelloWorldMessage msg = new HelloWorldMessage("Home", 10); + Assert.assertNotNull(msg); + Assert.assertEquals("Home has 10 folders",msg.toString()); + } + +}