2016-06-14 7 views
0

Я пытался использовать MavenCli, что дало ByteArrayOutpurStream -s как stdout и stderr. У меня есть тесты, которые проверяют, что некоторые записи были записаны в эти потоки. Пока у меня есть один тест, все в порядке. Но, если я представим еще один тест, он терпит неудачу, так как в stdout ничего не записывается.MavenCli - stdout, stderr

Вот отрывок (Scala):

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" тест, "write something else" пассы.

Если я прокомментирую "write something else" тест (и расторжение "write something"), "write something" проходит.

Если обе раскоментированы и выполнены, один сбой (, так как он является последовательным).

Добавление println -s из outStream после выполнения Maven, причина, кажется, что outStream пусто ...

Я не могу понять, почему, или что еще более важно, как временное решение.

Любые идеи? ...

Спасибо заранее, Ohad

ответ

0

ОК, я испустил MavenCli подход в пользу Maven.execute(MavenExecutionRequest). Это само по себе не помогло решить проблему, но я смог ввести свой собственный ExecutionListener вместо стандартного ExecutionEventLogger. Мой исполнитель прослушивает записи напрямую, без использования каких-либо систем ведения журнала, которые, похоже, решают проблему.

Код выглядит что-то вроде:

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") 
    } 
    } 
}