View Javadoc

1   package org.apache.maven.plugin.surefire.booterclient.output;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.jmock.core.constraint.IsEqual;
23  import org.jmock.core.matcher.InvokeOnceMatcher;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.InputStreamReader;
29  
30  /**
31   * Test for {@link FileOutputConsumerProxy}
32   *
33   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
34   * @version $Id: FileOutputConsumerProxyTest.java 1039320 2010-11-26 11:48:24Z krosenvold $
35   */
36  public class FileOutputConsumerProxyTest
37      extends OutputConsumerProxyTest
38  {
39  
40      private static final String USER_DIR = System.getProperty( "user.dir" );
41      File targetDir;
42  
43      protected void setUp()
44          throws Exception
45      {
46          super.setUp();
47          targetDir = new File ( USER_DIR, "target" );
48          if ( !targetDir.exists() ) targetDir = new File( USER_DIR );
49          setOutputConsumer( new FileOutputConsumerProxy( (OutputConsumer) getOutputConsumerMock().proxy(), targetDir ) );
50      }
51  
52      public void testConsumeOutputLine()
53          throws Exception
54      {
55          
56          File reportFile = new File( targetDir, getReportEntry().getName() + "-output.txt" );
57          reportFile.delete();
58  
59          getOutputConsumerMock().expects( new InvokeOnceMatcher() ).method( "testSetStarting" )
60              .with( new IsEqual( getReportEntry() ) );
61          getOutputConsumerMock().expects( new InvokeOnceMatcher() ).method( "testSetCompleted" );
62          getOutputConsumer().testSetStarting( getReportEntry() );
63          getOutputConsumer().consumeOutputLine( getLine() );
64          getOutputConsumer().testSetCompleted();
65          getOutputConsumerMock().verify();
66  
67          assertTrue( reportFile.exists() );
68  
69          BufferedReader in = null;
70          try
71          {
72              in = new BufferedReader( new InputStreamReader( new FileInputStream( reportFile ) ) );
73              String content = in.readLine();
74              assertEquals( getLine(), content );
75              assertNull( in.readLine() );
76          }
77          finally
78          {
79              if ( in != null )
80              {
81                  in.close();
82              }
83          }
84  
85          reportFile.deleteOnExit();
86      }
87  
88  }