使用Maven解析器使用Arquillian创建耳朵测试

时间:2016-10-10 12:45:12

标签: java maven jboss-arquillian shrinkwrap

我需要使用Arquillian为我的EAR应用程序创建一个真正的IT测试。

由于ShrinkWrap方法 addClasses addPackages 在现实世界中无用 - 因为需要太多导入 - 我想使用 Maven.resolver() 适用于 pom.xml

因此我创建了一个类似的测试:

@RunWith(Arquillian.class)
public class ArqTest {

@Deployment
public static Archive<?> createTestArchive() {
    System.out.println("START CREATE ARCHIVE");

    //create ear archive  
    EnterpriseArchive ear = ShrinkWrap
            .create(EnterpriseArchive.class);  

    System.out.println("RESOLVING EJB DEPENDENCIES");

    // resolve and add ejbDependencies to ear lib  
    File[] ejbDependencies = Maven.resolver()
            .loadPomFromFile("../ejb/pom.xml")
            .importRuntimeDependencies()
            .resolve()
            .withTransitivity()
            .asFile();   

    System.out.println("ADDING DEPENDENCIES TO EAR");

    for (File archive : ejbDependencies) {  
        ear.addAsLibrary(archive);  
    }   

    System.out.println("START CREATE EJB");
    // resolve ejb (actually jar file cause as of now mavenimporter doesn't support ejb-types ony war and jars so don't be fooled by my naming convention)  
    // then add some external resources, and an empty beans.xml for CDI to work  
    JavaArchive ejb = ShrinkWrap                
            .create(MavenImporter.class)                
            .loadPomFromFile("../ejb/pom.xml")
            .importBuildOutput()                
            .as(JavaArchive.class)
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
            .addAsResource("app.properties")
            ;

    System.out.println("START CREATE WAR");
    //resolve war and add beans.xml and my html files manually  
    WebArchive war = ShrinkWrap
            .create(MavenImporter.class)
            .loadPomFromFile("pom.xml")
            .importBuildOutput()
            .as(WebArchive.class)               
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
            ;

    //add built modules to ear and my application.xml  
    ear.addAsModule(ejb);  
    ear.addAsModule(war);
    ear.setApplicationXML("application.xml");

    System.out.println("END CREATE ARCHIVE");

    return ear;
}

但是,在“解决EJB依赖关系”时无法创建存档并抛出此错误

Caused by: java.lang.IllegalArgumentException: No dependencies were set for resolution
at org.jboss.shrinkwrap.resolver.impl.maven.util.Validate.notEmpty(Validate.java:78)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenStrategyStageBaseImpl.using(MavenStrategyStageBaseImpl.java:65)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenStrategyStageBaseImpl.withTransitivity(MavenStrategyStageBaseImpl.java:49)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenStrategyStageBaseImpl.withTransitivity(MavenStrategyStageBaseImpl.java:38)
at ArqEjbTest.createTestArchive(ArqEjbTest.java:39)
... 61 more

任何人都可以成功创建存档吗?

1 个答案:

答案 0 :(得分:2)

我终于用以下代码解决了:

$webRequest = [System.Net.HttpWebRequest]::Create("$uriP/api/1.0/appliance-management/upgrade/uploadbundle/NSX")
$webRequest.Timeout = 66000
$webRequest.Method = "POST"
$webRequest.ContentType = "application/xml"
$webRequest.AllowWriteStreamBuffering=$false
$webRequest.SendChunked=$true
$webRequest.Credentials = $auth
$webRequest.Headers["Authorization"] = "Basic $auth"

$requestStream = $webRequest.GetRequestStream()
$fileStream = [System.IO.File]::OpenRead($nsxpath)
$chunk = New-Object byte[] $bufSize
  while( $bytesRead = $fileStream.Read($chunk,0,$bufsize) )
  {
    $requestStream.write($chunk, 0, $bytesRead)
    $requestStream.Flush()
  }

$responseStream = $webRequest.getresponse()

$FileStream.Close()
$requestStream.Close()
$responseStream.Close()

$responseStream
$responseStream.GetResponseHeader("Content-Length") 
$responseStream.StatusCode

其余的保持不变。

将此行添加到 arquillian.xml

System.out.println("RESOLVING EJB DEPENDENCIES");

// resolve and add ejbDependencies to ear lib  
File[] ejbDependencies = Maven.resolver()
    .loadPomFromFile("../project-ejb/pom.xml")
    .importRuntimeDependencies()
    .resolve()
    .withTransitivity()
    .asFile();   

for (File archive : ejbDependencies) {  
    ear.addAsLibrary(archive);  
}   

System.out.println("START CREATE EJB");
// resolve ejb (actually jar file cause as of now mavenimporter doesn't support ejb-types ony war and jars so don't be fooled by my naming convention)  
// then add some external resources, and an empty beans.xml for CDI to work  
JavaArchive ejb = ShrinkWrap
    .create(JavaArchive.class, "project-test-ejb.jar")              
    .addPackage("it.blah..")                
    .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
    ;

也有很多帮助,因为通过以上属性,arquillian创建了可以分析的结果包以消除错误。