2016-06-14 10 views
0

私はMavenCliを使って、ByteArrayOutpurStream -sをstdoutstderrとしています。 一部のレコードがこれらのストリームに書き込まれたことを確認するテストがあります。 私は1つのテストを持っている限り、すべてがOKです。 しかし、私が別のテストを導入すると、それは失敗します。何もstdoutに書き込まれないからです。私は"write something"テスト、"write something else"パスをコメントアウトした場合MavenCli - stdout、stderr

class MyIT extends SpecWithJUnit { 

    sequential 

    trait Ctx extends Scope { 
    val outStream = new ByteArrayOutpurStream() 
    val errStream = new ByteArrayOutpurStream() 
    val pomDir = Files.createTempDirectory("test_pom_").toFile 
    val mavenCli = new MavenCli() 

    def haveWrote(str: String): Matcher[Int] = { 
     contain(str) ^^ { (_: Int) => 
      new String(outStream.toByteArray, "UTF-8") aka "written records" } 
    } 

    def maven(pom: Elem, goals: String*): Int = { 
     val pomFile = new File(pomDir, "pom.xml") 

     XML.save(pomFile.getCanonicalPath, pom) 

     System.setProperty(
      "maven.multiModuleProjectDirectory", 
      pomDir.getCanonicalPath) 

     mavenCli.doMain(
      goals.toArray, 
      pomDir.getCanonicalPath, 
      new PrintStream(outStream), 
      new PrintStream(errStream)) 
    } 
    } 


    "test 1" should { 
    "write something" in new Ctx { 
     val pomXml = 
     <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://maven.apache.org/POM/4.0.0"> 
      <modelVersion>4.0.0</modelVersion> 

      <groupId>com.company</groupId> 
      <artifactId>some-artifact-id</artifactId> 
      <version>1.0.0-SNAPSHOT</version> 
      <description>some meaningful description</description> 
      <name>some catchy name</name> 
      ... 
     </project> 

     maven(pomXml, "clean" "validate") must haveWrote("something") 
    } 

    "write something else" in new Ctx { 
     val pomXml = 
     <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://maven.apache.org/POM/4.0.0"> 
      <modelVersion>4.0.0</modelVersion> 

      <groupId>com.company</groupId> 
      <artifactId>some-artifact-id</artifactId> 
      <version>1.0.0-SNAPSHOT</version> 
      <description>some meaningful description</description> 
      <name>some catchy name</name> 
      ... (different than the "write something" test) 
     </project> 

     maven(pomXml, "clean" "validate") must haveWrote("something else") 
    } 
    } 
} 

:ここ

はスニペット(スカラ)です。

私が"write something else"をコメントアウトして("write something"をコメント解除する)、"write something"が合格すると、

両方ともコメントが外されて実行された場合、1つは失敗します("write something else"、それは順次です)。

Mavenの実行後outStreamprintln -sを追加し、原因がoutStreamが空であることのようです...

私は回避する方法を、もっと重要なのはなぜ把握、またはすることはできません。

任意のアイデア?事前に...

おかげで、 オハッド

答えて

0

OK、私はMaven.execute(MavenExecutionRequest)の賛成でMavenCliアプローチをあきらめました。これだけでは問題は解決しませんでしたが、ExecutionEventLoggerの代わりにExecutionListenerを導入することができました。私の実行リスナーは、ログフレームワークを使用せずにレコードを直接記録しました。これは問題を解決するようです。

コードは今のようになります。

class MyIT extends SpecWithJUnit { 

    sequential 

    def plexusContainer = { 
    val containerConfig = new DefaultContainerConfiguration() 
     .setClassPathScanning(PlexusConstants.SCANNING_INDEX) 
     .setAutoWiring(true) 
     .setName("maven") 
    val container = new DefaultPlexusContainer(containerConfig) 

    container 
    } 

    def buildMavenRequest(pomFile: Path, 
         executionListener: ExecutionListener, 
         goals: String*): MavenExecutionRequest = { 
    val userProperties = new Properties() 
    val executionRequestPopulator = plexusContainer.lookup(classOf[MavenExecutionRequestPopulator]) 
    val req = new DefaultMavenExecutionRequest() 
     .setPom(pomFile.toFile) 
     .setBaseDirectory(pomFile.getParent.toFile) 
     .setGoals(goals) 
     .setInteractiveMode(false) 
     .setExecutionListener(executionListener) 
     .setUserProperties(userProperties) 

    executionRequestPopulator.populateDefaults(req) 
    } 

    def maven(pomFile: Path, 
      executionListener: ExecutionListener, 
      goals: String*): MavenExecutionResult = { 
    val mavenRequest = buildMavenRequest(pomFile, executionListener, goals: _*) 
    val mvn = plexusContainer.lookup(classOf[Maven]) 

    mvn.execute(mavenRequest) 
    } 

    def readFile(filename: Path): Seq[String] = { 
    Source.fromFile(filename.toString).getLines().foldLeft(Seq.empty[String]){ _ :+ _ } 
    } 


    trait Ctx extends Scope { 
    val pomDir = Files.createTempDirectory("test_pom_").toFile 
    val logFile = pomDir.resolve("test_log.log") 
    val myExecutionListener = new MyExecutionListener(logFile) 

    def haveWrote(str: String): Matcher[MavenExecutionResult] = { 
     contain(str) ^^ { (_: MavenExecutionResult) => 
     readFile(logFile) aka "written records" } 
    } 
    } 


    "test 1" should { 
    "write something" in new Ctx { 
     val pomXml = 
     <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://maven.apache.org/POM/4.0.0"> 
      <modelVersion>4.0.0</modelVersion> 

      <groupId>com.company</groupId> 
      <artifactId>some-artifact-id</artifactId> 
      <version>1.0.0-SNAPSHOT</version> 
      <description>some meaningful description</description> 
      <name>some catchy name</name> 
      ... 
     </project> 

     maven(pomXml, "clean" "validate") must haveWrote("something") 
    } 

    "write something else" in new Ctx { 
     val pomXml = 
     <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://maven.apache.org/POM/4.0.0"> 
      <modelVersion>4.0.0</modelVersion> 

      <groupId>com.company</groupId> 
      <artifactId>some-artifact-id</artifactId> 
      <version>1.0.0-SNAPSHOT</version> 
      <description>some meaningful description</description> 
      <name>some catchy name</name> 
      ... (different than the "write something" test) 
     </project> 

     maven(pomXml, "clean" "validate") must haveWrote("something else") 
    } 
    } 
} 
関連する問題