Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some utilities for working with Arquillian
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
impl/src/test/java/org/jboss/weld/extensions/test/util/Deployments.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
impl/src/test/java/org/jboss/weld/extensions/test/util/MavenArtifactResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |