Skip to content

Commit

Permalink
testdoc goal implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravarora committed Mar 28, 2013
1 parent 411845b commit 19c5b6d
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ target
.DS_store
src/test/projects/acceptance-and-verify/log.txt
docs/_build

.classpath
.settings/
.project
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<groupId>org.robotframework</groupId>
<artifactId>robotframework-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0.8</version>
<version>1.0.9-SNAPSHOT</version>
<name>Robot Framework Maven Plugin</name>
<description>
Maven plugin for the Robot Framework.
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/org/robotframework/mavenplugin/TestDocConfiguration.java
Original file line number Diff line number Diff line change
@@ -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.
* <p/>
* Name must be in the same format as when used in Robot Framework test data, for example <code>BuiltIn</code> or
* <code>com.acme.FooLibrary</code>. When name is used, the library is imported the same as when running the tests.
* Use {@link #extraPathDirectories} to set PYTHONPATH/CLASSPATH accordingly.
* <p/>
* Paths are considered relative to the location of <code>pom.xml</code> and must point to a valid Python/Java
* source file or a resource file. For example <code>src/main/java/com/test/ExampleLib.java</code>
*/
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;
}
99 changes: 99 additions & 0 deletions src/main/java/org/robotframework/mavenplugin/TestDocMojo.java
Original file line number Diff line number Diff line change
@@ -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 <code>libdoc</code> tool.
* <p/>
* Uses the <code>libdoc</code> bundled in Robot Framework jar distribution. For more help see
* <a href="http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html#library-documentation-tool-libdoc">libdoc documentation</a>.
*
* @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:
* <ul>
* <li><code>outputFile</code> The name for the output file.</li>
* <li><code>dataSourceFile</code> Name or path of the documented test case(s).</li>
* </ul>
* <p/>
* Paths are considered relative to the location of <code>pom.xml</code> and must point to a valid test case file.
* For example <code>src/main/test/ExampleTest.txt</code>
* Optional settings:
* <ul>
* <li><code>outputDirectory</code> Specifies the directory where documentation files are written.
* Considered to be relative to the ${basedir} of the project.
* Default ${project.build.directory}/robotframework/testdoc</li>
* <li><code>title</code> 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.</li>
* <li><code>name</code> Override the name of the top level test suite.</li>
* <li><code>doc</code> Override the name of the top level test suite.</li>
* </ul>
*
* Example:
* <pre><![CDATA[<libdoc>
* <outputFile>MyTests.html</outputFile>
* <dataSourceFile>src/test/resources/MyTests.txt</dataSourceFile>
* </libdoc>]]></pre>
*
* @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;
}
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @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.");
}
}
}
}
36 changes: 6 additions & 30 deletions src/test/java/org/robotframework/mavenplugin/LibDocMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -29,56 +25,36 @@ 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());

}

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.");
}
}
}

}
56 changes: 56 additions & 0 deletions src/test/java/org/robotframework/mavenplugin/TestDocMojoTest.java
Original file line number Diff line number Diff line change
@@ -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\":\"<p>Custom documentation</p>\""));
}
}
Loading

0 comments on commit 19c5b6d

Please sign in to comment.