Skip to content
This repository has been archived by the owner on Jan 29, 2018. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Vonk committed Oct 17, 2009
1 parent 807cb5b commit 269cb4e
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.classpath
.project
target
.settings
68 changes: 68 additions & 0 deletions pom.xml
@@ -0,0 +1,68 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fitnesse.plugins</groupId>
<artifactId>maven-classpath-plugin</artifactId>
<name>Maven Classpath Plugin</name>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>3.0-alpha-2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.fitnesse</groupId>
<artifactId>fitnesse</artifactId>
<version>20090818</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.6</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>neuri</id>
<url>http://maven.neuri.com/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
142 changes: 142 additions & 0 deletions src/main/java/fitnesse/wikitext/widgets/MavenClasspathWidget.java
@@ -0,0 +1,142 @@
package fitnesse.wikitext.widgets;

import static org.apache.maven.embedder.MavenEmbedder.validateConfiguration;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.embedder.Configuration;
import org.apache.maven.embedder.ConfigurationValidationResult;
import org.apache.maven.embedder.DefaultConfiguration;
import org.apache.maven.embedder.MavenEmbedder;
import org.apache.maven.embedder.MavenEmbedderConsoleLogger;
import org.apache.maven.embedder.MavenEmbedderException;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;

import fitnesse.html.HtmlUtil;
import fitnesse.wiki.PageData;

public class MavenClasspathWidget extends ParentWidget implements WidgetWithTextArgument {

static {
PageData.classpathWidgetBuilder.addWidgetClass(MavenClasspathWidget.class);
}

private String pomFile;
public static final String REGEXP = "^!pomFile [^\r\n]*";
private static final Pattern pattern = Pattern.compile("^!pomFile (.*)");

public MavenClasspathWidget(ParentWidget parent, String text) {
super(parent);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
this.pomFile = matcher.group(1);
ensurePomFileExists();
} else {
throw new IllegalArgumentException("no pom file specified.");
}
}

private void ensurePomFileExists() {
if(!new File(pomFile).exists()) {
throw new IllegalArgumentException(pomFile + " does not exist");
}
}

@Override
public String asWikiText() throws Exception {
return "!pomFile " + pomFile;
}

public String getText() throws Exception {
List<String> classpathElements = getMavenClasspath();
return createClasspath(classpathElements);
}

private List<String> getMavenClasspath() throws MavenEmbedderException,
DependencyResolutionRequiredException {
Configuration configuration = mavenConfiguration();
ensureMavenConfigurationIsValid(configuration);
MavenExecutionRequest request = createExecutionRequest(projectRootDirectory());
List<String> classpathElements = getClasspathElements(configuration, request);
return classpathElements;
}

@Override
public String render() throws Exception {
List<String> classpathElements = getMavenClasspath();

String classpathForRender = "";
for (String element : classpathElements) {
classpathForRender += HtmlUtil.metaText("classpath: " + element) + HtmlUtil.BRtag;

}
return classpathForRender;

}

private void ensureMavenConfigurationIsValid(Configuration configuration) {
ConfigurationValidationResult validationResult = validateConfiguration(configuration);
if (!validationResult.isValid()) {
throw new IllegalStateException("Unable to create valid Maven Configuration.");
}
}

private String createClasspath(List<String> classpathElements) {
String classpath = "";
for (String element : classpathElements) {
classpath += element + File.pathSeparator;
}
return removeTrailingPathSeparator(classpath);
}

private String removeTrailingPathSeparator(String classpath) {
return classpath.substring(0, classpath.length() - 1);
}

private List<String> getClasspathElements(Configuration configuration, MavenExecutionRequest request)
throws MavenEmbedderException, DependencyResolutionRequiredException {
MavenEmbedder embedder = new MavenEmbedder(configuration);
MavenExecutionResult executionResult = embedder.readProjectWithDependencies(request);
List<String> classpathElements = executionResult.getProject().getCompileClasspathElements();
return classpathElements;
}

private File projectRootDirectory() {
String root = pomFile.substring(0, pomFile.lastIndexOf("/"));
File projectDirectory = new File(root);
return projectDirectory;
}

private MavenExecutionRequest createExecutionRequest(File projectDirectory) {
MavenExecutionRequest request = new DefaultMavenExecutionRequest().setBaseDirectory(projectDirectory).setPomFile(
pomFile);
return request;
}

private Configuration mavenConfiguration() {
Configuration configuration = new DefaultConfiguration().setClassLoader(Thread.currentThread()
.getContextClassLoader()).setMavenEmbedderLogger(new MavenEmbedderConsoleLogger());
if(hasNonDefaultLocalRepository()) {
configuration.setLocalRepository(getLocalRepository());
}
return configuration;
}

private boolean hasNonDefaultLocalRepository() {
return getLocalRepository() != null;
}
/*
* can be overridden for test purposes.
*/
protected File getLocalRepository() {
return null;
}


}
@@ -0,0 +1,92 @@
package fitnesse.wikitext.widgets;

import static fitnesse.html.HtmlUtil.BRtag;
import static fitnesse.html.HtmlUtil.metaText;

import java.io.File;

import org.codehaus.plexus.util.FileUtils;

import fitnesse.wiki.PageData;

public class MavenClasspathWidgetTest extends WidgetTestCase {
private static final String TEST_PROJECT_ROOT = new File("src/test/resources/MavenClasspathWidget").getAbsolutePath();
private final static String TEST_POM_FILE = "src/test/resources/MavenClasspathWidget/pom.xml";
private File mavenLocalRepo = new File(System.getProperty("java.io.tmpdir"), "MavenClasspathWidgetTest/m2/repo");
private String path = mavenLocalRepo.getAbsolutePath();

private MavenClasspathWidget widget;
@Override
protected void setUp() throws Exception {
super.setUp();
widget = new MavenClasspathWidget(new MockWidgetRoot(), "!pomFile " + TEST_POM_FILE) {
@Override
protected File getLocalRepository() {
return mavenLocalRepo;
}
};
mavenLocalRepo.mkdirs();
FileUtils.copyDirectoryStructure(new File(TEST_PROJECT_ROOT, "repository"), mavenLocalRepo);
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
FileUtils.deleteDirectory(mavenLocalRepo);
}

@Override
protected String getRegexp() {
return MavenClasspathWidget.REGEXP;
}


public void testThatMavenClasspathWidgetIsAddedToTheClasspathWidgetBuilder() throws Exception {
assertEquals(MavenClasspathWidget.class, PageData.classpathWidgetBuilder.findWidgetClassMatching("!pomFile pom.xml"));
}

public void testRegexp() throws Exception {
assertMatchEquals("!pomFile pom.xml", "!pomFile pom.xml");
}

public void testAsWikiText() throws Exception {
assertEquals("!pomFile " + TEST_POM_FILE, widget.asWikiText());
}

public void testRender() throws Exception {
String actual = widget.render();
String expected = metaText("classpath: " + TEST_PROJECT_ROOT + "/target/classes") + BRtag +
metaText(classpathElementForRender("/fitnesse/fitnesse-dep/1.0/fitnesse-dep-1.0.jar")) + BRtag +
metaText(classpathElementForRender("/fitnesse/fitnesse-subdep/1.0/fitnesse-subdep-1.0.jar")) + BRtag;
assertEquals(expected, actual);
}

public void testGetText() throws Exception {
String actual = widget.getText();
String expected = TEST_PROJECT_ROOT + "/target/classes"
+ classpathElementForText("/fitnesse/fitnesse-dep/1.0/fitnesse-dep-1.0.jar")
+ classpathElementForText("/fitnesse/fitnesse-subdep/1.0/fitnesse-subdep-1.0.jar");
assertEquals(expected, actual);
}

public void testFailFastWhenPomFileDoesNotExist() throws Exception {
try {
new MavenClasspathWidget(new MockWidgetRoot(), "!pomFile /non/existing/pom.xml");
fail("should have thrown IllegalArgumentException");
} catch(IllegalArgumentException expected) {}
}

public void testFailFastWhenPomFileIsNotSpecified() throws Exception {
try {
new MavenClasspathWidget(new MockWidgetRoot(), "!pomFile");
fail("should have thrown IllegalArgumentException");
} catch(IllegalArgumentException expected) {}
}

private String classpathElementForRender(String file) {
return "classpath: " + path + file;
}
private String classpathElementForText(String file) {
return File.pathSeparator + path + file;
}
}
26 changes: 26 additions & 0 deletions src/test/resources/MavenClasspathWidget/pom.xml
@@ -0,0 +1,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fitnesse</groupId>
<artifactId>fitnesse-maven-widget</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>fitnesse</groupId>
<artifactId>fitnesse-dep</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>local</id>
<url>file://./test-resources/MavenClasspathWidget/repository</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
@@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fitnesse</groupId>
<artifactId>fitnesse-dep</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>fitnesse</groupId>
<artifactId>fitnesse-subdep</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fitnesse</groupId>
<artifactId>fitnesse-subdep</artifactId>
<version>1.0</version>
</project>

0 comments on commit 269cb4e

Please sign in to comment.