Skip to content

Commit

Permalink
Add some utilities for working with Arquillian
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Aug 12, 2010
1 parent 0c60f46 commit af3baf6
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
@@ -0,0 +1,15 @@
package org.jboss.weld.extensions.test.util;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;

public class Deployments
{

public static WebArchive baseDeployment()
{
return ShrinkWrap.create(WebArchive.class, "test.war").addLibrary(MavenArtifactResolver.resolve("org.jboss.weld", "weld-extensions")).addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
}

}
@@ -0,0 +1,95 @@
package org.jboss.weld.extensions.test.util;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Resolves a maven artifact present on the test classpath.
*
* @author Stuart Douglas
*
*/
public class MavenArtifactResolver
{

public static File resolve(String groupId, String artifactId)
{
String classPath = System.getProperty("java.class.path");
// first look for an artifact from the repo
String pathString = groupId.replace('.', File.separatorChar) + File.separatorChar + artifactId;
Pattern p = Pattern.compile("[^:]*" + Pattern.quote(pathString) + "[^:]*", Pattern.CASE_INSENSITIVE);
Matcher matches = p.matcher(classPath);

if (!matches.find())
{
// find a resource from the local build
String localClasses = Pattern.quote("target" + File.separatorChar + "classes");
Pattern localClassesPattern = Pattern.compile("[^:]*" + localClasses + "[^:]*", Pattern.CASE_INSENSITIVE);
Matcher localClassesMatcher = localClassesPattern.matcher(classPath);
if (!localClassesMatcher.find())
{
throw new IllegalArgumentException("Unable to find maven archive " + groupId + ":" + artifactId + " in the local build");
}
else
{
List<String> targetPaths = new ArrayList<String>();
do
{
String path = localClassesMatcher.group();
targetPaths.add(path.substring(0, path.length() - 8));
}
while (localClassesMatcher.find());
return new File(findBuiltArtifact(targetPaths, artifactId));
}
}
else
{
return new File(matches.group(0));
}
}

public static File resolve(String qualifiedArtifactId)
{
String[] segments = qualifiedArtifactId.split(":");
return resolve(segments[0], segments[1]);
}

public static String findBuiltArtifact(List<String> targetPaths, String artifactId)
{
final String regex = "^" + artifactId + "-[\\d+\\.]+(?:\\-\\p{Upper}*)?.jar$";
for (String targetPath : targetPaths)
{
File target = new File(targetPath);
if (target.exists())
{
if (!target.isDirectory())
{
throw new IllegalStateException("Found ${project.dir}/target/ but it is not a directory!");
}
String[] possibleFiles = target.list(new FilenameFilter()
{

public boolean accept(File dir, String name)
{
return name.matches(regex);
}

});
if (possibleFiles.length == 1)
{
return "target" + File.separatorChar + possibleFiles[0];
}
if (possibleFiles.length > 0)
{
throw new IllegalStateException("Found multiple matching files " + Arrays.asList(possibleFiles) + " for " + artifactId + " not sure which one to choose");
}
}
}
return null;
}
}

0 comments on commit af3baf6

Please sign in to comment.