From 19c5b6d11a70ed3cbbd92111dfe20e3d12764d39 Mon Sep 17 00:00:00 2001 From: Gaurav Arora Date: Thu, 28 Mar 2013 08:54:51 +0530 Subject: [PATCH] testdoc goal implementation --- .gitignore | 4 +- pom.xml | 2 +- .../mavenplugin/TestDocConfiguration.java | 112 ++++++++++++++++++ .../mavenplugin/TestDocMojo.java | 99 ++++++++++++++++ .../mavenplugin/AbstractRFMojoTestCase.java | 32 +++++ .../mavenplugin/LibDocMojoTest.java | 36 +----- .../mavenplugin/TestDocMojoTest.java | 56 +++++++++ .../resources/pom-testdoc-txtfile-doc.xml | 27 +++++ .../resources/pom-testdoc-txtfile-name.xml | 27 +++++ .../resources/pom-testdoc-txtfile-title.xml | 27 +++++ src/test/resources/pom-testdoc-txtfile.xml | 26 ++++ .../resources/robot-testdoc/invalid_login.txt | 31 +++++ 12 files changed, 447 insertions(+), 32 deletions(-) create mode 100644 src/main/java/org/robotframework/mavenplugin/TestDocConfiguration.java create mode 100644 src/main/java/org/robotframework/mavenplugin/TestDocMojo.java create mode 100644 src/test/java/org/robotframework/mavenplugin/AbstractRFMojoTestCase.java create mode 100644 src/test/java/org/robotframework/mavenplugin/TestDocMojoTest.java create mode 100644 src/test/resources/pom-testdoc-txtfile-doc.xml create mode 100644 src/test/resources/pom-testdoc-txtfile-name.xml create mode 100644 src/test/resources/pom-testdoc-txtfile-title.xml create mode 100644 src/test/resources/pom-testdoc-txtfile.xml create mode 100644 src/test/resources/robot-testdoc/invalid_login.txt diff --git a/.gitignore b/.gitignore index 6fd4122..1b9a3aa 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,6 @@ target .DS_store src/test/projects/acceptance-and-verify/log.txt docs/_build - +.classpath +.settings/ +.project diff --git a/pom.xml b/pom.xml index dbbd085..d1c5c74 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.robotframework robotframework-maven-plugin maven-plugin - 1.0.8 + 1.0.9-SNAPSHOT Robot Framework Maven Plugin Maven plugin for the Robot Framework. diff --git a/src/main/java/org/robotframework/mavenplugin/TestDocConfiguration.java b/src/main/java/org/robotframework/mavenplugin/TestDocConfiguration.java new file mode 100644 index 0000000..c79ae77 --- /dev/null +++ b/src/main/java/org/robotframework/mavenplugin/TestDocConfiguration.java @@ -0,0 +1,112 @@ +package org.robotframework.mavenplugin; + +/* + * Copyright 2011 Michael Mallete, Dietrich Schulten + * Copyright 2012 Nokia Siemens Networks Oyj + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; +import java.io.IOException; + +import org.codehaus.plexus.util.StringUtils; + + +public class TestDocConfiguration { + + public String[] generateRunArguments() throws IOException { + Arguments generatedArguments = new Arguments(); + generatedArguments.add("testdoc"); + generatedArguments.addNonEmptyStringToArguments(title, "--title"); + generatedArguments.addNonEmptyStringToArguments(name, "--name"); + generatedArguments.addNonEmptyStringToArguments(doc, "--doc"); + generatedArguments.add(getDatasourceFile()); + generatedArguments.add(getOutputPath()); + return generatedArguments.toArray(); + } + + private String getDatasourceFile() throws IOException { + File libOrResource = new File(dataSourceFile); + if (!libOrResource.exists()) { + throw new IOException("Data source file " + dataSourceFile + " does not exist"); + } + return libOrResource.getAbsolutePath(); + } + + private String getOutputPath() { + return outputDirectory + File.separator + outputFile.getName(); + } + + public void ensureOutputDirectoryExists() + throws IOException { + if (outputDirectory == null) { + String baseDir = System.getProperty("basedir"); + // FIXME: this is to get around the problem that default-value does not currently work + if (baseDir == null) + baseDir = "."; + outputDirectory = new File(joinPaths(baseDir, "target", "robotframework", "testdoc")); + } + if (!outputDirectory.exists() && !outputDirectory.mkdirs()) { + throw new IOException("Target output directory cannot be created: " + outputDirectory.getAbsolutePath()); + } + } + + private String joinPaths(String... parts) { + return StringUtils.join(parts, File.separator); + } + + public void populateDefaults(TestDocMojo defaults) { + if (this.outputDirectory == null) + this.outputDirectory = defaults.defaultTestdocOutputDirectory; + } + + /** + * Specifies the directory where documentation files are written. Considered to be relative to the ${basedir} of the + * project. + */ + private File outputDirectory; + + /** + * Specifies the filename of the created documentation. Considered to be relative to the {@link #outputDirectory} of + * the project. + */ + private File outputFile; + + /** + * Name or path of the documented library or resource file. + *

+ * Name must be in the same format as when used in Robot Framework test data, for example BuiltIn or + * com.acme.FooLibrary. When name is used, the library is imported the same as when running the tests. + * Use {@link #extraPathDirectories} to set PYTHONPATH/CLASSPATH accordingly. + *

+ * Paths are considered relative to the location of pom.xml and must point to a valid Python/Java + * source file or a resource file. For example src/main/java/com/test/ExampleLib.java + */ + private String dataSourceFile; + + /** + * Set the title of the generated documentation. Underscores in the title are converted to spaces. The default title is the name of the top level suite. + */ + private String title; + + /** + * Override the name of the top level test suite. + */ + private String name; + + /** + * Override the documentation of the top level test suite. + */ + private String doc; +} diff --git a/src/main/java/org/robotframework/mavenplugin/TestDocMojo.java b/src/main/java/org/robotframework/mavenplugin/TestDocMojo.java new file mode 100644 index 0000000..bd232b5 --- /dev/null +++ b/src/main/java/org/robotframework/mavenplugin/TestDocMojo.java @@ -0,0 +1,99 @@ +package org.robotframework.mavenplugin; + +/* + * Copyright 2011 Michael Mallete, Dietrich Schulten + * Copyright 2012 Nokia Siemens Networks Oyj + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; + +import java.io.IOException; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.robotframework.RobotFramework; + +/** + * Create documentation of test libraries or resource files using the Robot Framework libdoc tool. + *

+ * Uses the libdoc bundled in Robot Framework jar distribution. For more help see + * libdoc documentation. + * + * @goal testdoc + * @requiresDependencyResolution test + */ +public class TestDocMojo + extends AbstractMojoWithLoadedClasspath { + + protected void subclassExecute() + throws MojoExecutionException, MojoFailureException { + try { + runTestDoc(); + } catch (IOException e) { + throw new MojoExecutionException("Failed to execute libdoc script: " + e.getMessage()); + } + } + + public void runTestDoc() + throws IOException { + testdoc.populateDefaults(this); + testdoc.ensureOutputDirectoryExists(); + String[] args = testdoc.generateRunArguments(); + getLog().debug("Run arguments -> " + args); + RobotFramework.run(args); + } + + /** + * Test case documentation configuration. + * + * Required settings: + *

+ *

+ * Paths are considered relative to the location of pom.xml and must point to a valid test case file. + * For example src/main/test/ExampleTest.txt + * Optional settings: + *

+ * + * Example: + *

+     *      MyTests.html
+     *      src/test/resources/MyTests.txt
+     * ]]>
+ * + * @parameter + * @required + */ + private TestDocConfiguration testdoc; + + /** + * Default output directory. Effective if outputDirectory is empty. Cannot be overridden. + * + * @parameter default-value="${project.build.directory}/robotframework/testdoc" + * @readonly + */ + File defaultTestdocOutputDirectory; +} diff --git a/src/test/java/org/robotframework/mavenplugin/AbstractRFMojoTestCase.java b/src/test/java/org/robotframework/mavenplugin/AbstractRFMojoTestCase.java new file mode 100644 index 0000000..74a184c --- /dev/null +++ b/src/test/java/org/robotframework/mavenplugin/AbstractRFMojoTestCase.java @@ -0,0 +1,32 @@ +package org.robotframework.mavenplugin; + +import java.io.File; + +import org.apache.maven.plugin.Mojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; + +/** + * @author g.arora@iontrading.com + * @version $Id$ + * @since 0.7.6 + */ +public abstract class AbstractRFMojoTestCase extends AbstractMojoTestCase { + + void executeLibdocWithPom(String goal, String pathToPom) throws Exception, MojoExecutionException, MojoFailureException { + File pom = getTestFile(pathToPom); + Mojo mojo = lookupMojo(goal, pom); + mojo.execute(); + } + + void deleteDocument(String documentation) throws Exception { + File document = new File(documentation); + if (document.exists()) { + boolean deleted = document.delete(); + if (!deleted) { + throw new Exception("Cannot delete existing document."); + } + } + } +} diff --git a/src/test/java/org/robotframework/mavenplugin/LibDocMojoTest.java b/src/test/java/org/robotframework/mavenplugin/LibDocMojoTest.java index 104dea7..fdf24cd 100644 --- a/src/test/java/org/robotframework/mavenplugin/LibDocMojoTest.java +++ b/src/test/java/org/robotframework/mavenplugin/LibDocMojoTest.java @@ -2,13 +2,9 @@ import java.io.File; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; - public class LibDocMojoTest - extends AbstractMojoTestCase { + extends AbstractRFMojoTestCase { private final String outputDirectory = "target/robotframework/libdoc/"; private final String htmlResourceLibDoc = outputDirectory + "html_resource.html"; @@ -29,14 +25,13 @@ protected void setUp() public void testLibDocForJavaResource() throws Exception { - executeLibdocWithPom("src/test/resources/pom-libdoc.xml"); + executeLibdocWithPom("libdoc", "src/test/resources/pom-libdoc.xml"); assertTrue(javalibLibDoc + " not found", new File(javalibLibDoc).exists()); - } public void testLibDocForTxtResource() throws Exception { - executeLibdocWithPom("src/test/resources/pom-libdoc-txtfile.xml"); + executeLibdocWithPom("libdoc", "src/test/resources/pom-libdoc-txtfile.xml"); assertTrue(htmlResourceLibDoc + " not found", new File(htmlResourceLibDoc).exists()); } @@ -44,41 +39,22 @@ public void testLibDocForTxtResource() public void testLibDocForLibraryNamePython() throws Exception { - executeLibdocWithPom("src/test/resources/pom-libdoc-libraryname-python.xml"); + executeLibdocWithPom("libdoc", "src/test/resources/pom-libdoc-libraryname-python.xml"); assertTrue(mylibLibDoc + " not found", new File(mylibLibDoc).exists()); } public void testLibDocForLibraryNamePythonWithPackage() throws Exception { - executeLibdocWithPom("src/test/resources/pom-libdoc-libraryname-python-subpackage.xml"); + executeLibdocWithPom("libdoc", "src/test/resources/pom-libdoc-libraryname-python-subpackage.xml"); assertTrue(mypackageMylibLibDoc + " not found", new File(mypackageMylibLibDoc).exists()); } public void testLibDocForLibraryNameJava() throws Exception { - executeLibdocWithPom("src/test/resources/pom-libdoc-libraryname-java.xml"); + executeLibdocWithPom("libdoc", "src/test/resources/pom-libdoc-libraryname-java.xml"); assertTrue(javalibLibDoc + " not found", new File(javalibLibDoc).exists()); } - - private void executeLibdocWithPom(String pathToPom) throws Exception, MojoExecutionException, - MojoFailureException { - File pom = getTestFile(pathToPom); - LibDocMojo mojo = (LibDocMojo) lookupMojo("libdoc", pom); - mojo.execute(); - } - - private void deleteDocument(String documentation) - throws Exception { - File document = new File(documentation); - if (document.exists()) { - boolean deleted = document.delete(); - if (!deleted) { - throw new Exception("Cannot delete existing document."); - } - } - } - } diff --git a/src/test/java/org/robotframework/mavenplugin/TestDocMojoTest.java b/src/test/java/org/robotframework/mavenplugin/TestDocMojoTest.java new file mode 100644 index 0000000..29ec48b --- /dev/null +++ b/src/test/java/org/robotframework/mavenplugin/TestDocMojoTest.java @@ -0,0 +1,56 @@ +package org.robotframework.mavenplugin; + +import java.io.File; + +import org.apache.maven.it.util.FileUtils; + +import static org.mockito.Mockito.*; + + +public class TestDocMojoTest + extends AbstractRFMojoTestCase { + + private final String outputDirectory = "target/robotframework/testdoc/"; + + protected void setUp() + throws Exception { + super.setUp(); + File outputDir = new File(outputDirectory); + outputDir.mkdirs(); + } + + public void testTestDocForTxtResource() + throws Exception { + executeLibdocWithPom("testdoc", "src/test/resources/pom-testdoc-txtfile.xml"); + String txtResourceTestDoc = outputDirectory + "invalid.html"; + assertTrue(txtResourceTestDoc + " not found", new File(txtResourceTestDoc).exists()); + } + + public void testTestDocForTxtResourceWithTitle() + throws Exception { + executeLibdocWithPom("testdoc", "src/test/resources/pom-testdoc-txtfile-title.xml"); + String txtResourceWithTitleTestDoc = outputDirectory + "invalid_login_title.html"; + assertTrue(txtResourceWithTitleTestDoc + " not found", new File(txtResourceWithTitleTestDoc).exists()); + String contents = FileUtils.fileRead(txtResourceWithTitleTestDoc); + System.out.println(contents); + assertTrue(contents.contains("\"title\":\"Custom Title\"")); + } + + public void testTestDocForTxtResourceWithName() + throws Exception { + executeLibdocWithPom("testdoc", "src/test/resources/pom-testdoc-txtfile-name.xml"); + String txtResourceWithNameTestDoc = outputDirectory + "invalid_login_name.html"; + assertTrue(txtResourceWithNameTestDoc + " not found", new File(txtResourceWithNameTestDoc).exists()); + String contents = FileUtils.fileRead(txtResourceWithNameTestDoc); + assertTrue(contents.contains("\"fullName\":\"Custom name\"")); + } + + public void testTestDocForTxtResourceWithDoc() + throws Exception { + executeLibdocWithPom("testdoc", "src/test/resources/pom-testdoc-txtfile-doc.xml"); + String txtResourceWithDocTestDoc = outputDirectory + "invalid_login_doc.html"; + assertTrue(txtResourceWithDocTestDoc + " not found", new File(txtResourceWithDocTestDoc).exists()); + String contents = FileUtils.fileRead(txtResourceWithDocTestDoc); + assertTrue(contents.contains("\"doc\":\"

Custom documentation

\"")); + } +} diff --git a/src/test/resources/pom-testdoc-txtfile-doc.xml b/src/test/resources/pom-testdoc-txtfile-doc.xml new file mode 100644 index 0000000..1144bf8 --- /dev/null +++ b/src/test/resources/pom-testdoc-txtfile-doc.xml @@ -0,0 +1,27 @@ + + 4.0.0 + + org.robotframework + robotframework-maven-plugin-test + 1.0-SNAPSHOT + jar + Test Mojo + + + + + org.robotframework + robotframework-maven-plugin + + + src/test/resources/robot-testdoc/invalid_login.txt + invalid_login_doc.html + Custom documentation + + + + + + diff --git a/src/test/resources/pom-testdoc-txtfile-name.xml b/src/test/resources/pom-testdoc-txtfile-name.xml new file mode 100644 index 0000000..396a437 --- /dev/null +++ b/src/test/resources/pom-testdoc-txtfile-name.xml @@ -0,0 +1,27 @@ + + 4.0.0 + + org.robotframework + robotframework-maven-plugin-test + 1.0-SNAPSHOT + jar + Test Mojo + + + + + org.robotframework + robotframework-maven-plugin + + + src/test/resources/robot-testdoc/invalid_login.txt + invalid_login_name.html + Custom name + + + + + + diff --git a/src/test/resources/pom-testdoc-txtfile-title.xml b/src/test/resources/pom-testdoc-txtfile-title.xml new file mode 100644 index 0000000..6e9b35c --- /dev/null +++ b/src/test/resources/pom-testdoc-txtfile-title.xml @@ -0,0 +1,27 @@ + + 4.0.0 + + org.robotframework + robotframework-maven-plugin-test + 1.0-SNAPSHOT + jar + Test Mojo + + + + + org.robotframework + robotframework-maven-plugin + + + src/test/resources/robot-testdoc/invalid_login.txt + invalid_login_title.html + Custom Title + + + + + + diff --git a/src/test/resources/pom-testdoc-txtfile.xml b/src/test/resources/pom-testdoc-txtfile.xml new file mode 100644 index 0000000..2bb261b --- /dev/null +++ b/src/test/resources/pom-testdoc-txtfile.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + org.robotframework + robotframework-maven-plugin-test + 1.0-SNAPSHOT + jar + Test Mojo + + + + + org.robotframework + robotframework-maven-plugin + + + src/test/resources/robot-testdoc/invalid_login.txt + invalid.html + + + + + + diff --git a/src/test/resources/robot-testdoc/invalid_login.txt b/src/test/resources/robot-testdoc/invalid_login.txt new file mode 100644 index 0000000..450c9de --- /dev/null +++ b/src/test/resources/robot-testdoc/invalid_login.txt @@ -0,0 +1,31 @@ +*** Settings *** + +Documentation A test suite containing tests related to invalid login. These +... tests are data-driven by they nature. They use a single +... keyword, specified with Test Template setting, that is called +... with different arguments to cover different scenarios. +Suite Setup Open Browser To Login Page +Test Setup Go To Login Page +Test Template Login With Invalid Credentials Should Fail +Suite Teardown Close Browser +Resource common_resource.txt + + +*** Test Cases *** User Name Password + +Invalid Username invalid ${VALID PASSWD} +Invalid Password ${VALID USER} invalid +Invalid Username And Password invalid whatever +Empty Username ${EMPTY} ${VALID PASSWD} +Empty Password ${VALID USER} ${EMPTY} +Empty Username And Password ${EMPTY} ${EMPTY} + + +*** Keywords *** + +Login With Invalid Credentials Should Fail + [Arguments] ${username} ${password} + Input Username ${username} + Input Password ${password} + Submit Credentials + Login Should Have Failed