View Javadoc

1   package org.apache.maven.surefire.suite;
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 java.io.ByteArrayInputStream;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileNotFoundException;
26  import java.io.IOException;
27  import java.io.StringWriter;
28  import org.apache.maven.shared.utils.xml.PrettyPrintXMLWriter;
29  import org.apache.maven.shared.utils.xml.Xpp3DomWriter;
30  
31  import junit.framework.TestCase;
32  
33  /**
34   * @author Kristian Rosenvold
35   */
36  public class RunResultTest
37      extends TestCase
38  {
39  
40      public void testEmptySummaryShouldBeErrorFree()
41      {
42          RunResult summary = RunResult.noTestsRun();
43          assertTrue( summary.isErrorFree() );
44      }
45  
46      public void testFailuresInFirstRun()
47      {
48          RunResult resultOne = new RunResult( 10, 1, 3, 2 );
49          RunResult resultTwo = new RunResult( 20, 0, 0, 0 );
50          assertFalse( resultOne.aggregate( resultTwo ).isErrorFree() );
51      }
52  
53  
54      public void testAggregatedValues()
55      {
56          RunResult simple = getSimpleAggregate();
57          assertEquals( 20, simple.getCompletedCount() );
58          assertEquals( 3, simple.getErrors() );
59          assertEquals( 7, simple.getFailures() );
60          assertEquals( 4, simple.getSkipped() );
61  
62      }
63  
64      public void testSerialization()
65          throws FileNotFoundException
66      {
67          writeReadCheck( getSimpleAggregate() );
68      }
69  
70      public void testFailures()
71          throws FileNotFoundException
72      {
73          writeReadCheck( new RunResult( 0, 1, 2, 3, "stacktraceHere", false ) );
74      }
75  
76      public void testSkipped()
77          throws FileNotFoundException
78      {
79          writeReadCheck( new RunResult( 3, 2, 1, 0, null, true ) );
80      }
81  
82      public void testAppendSerialization()
83          throws IOException
84      {
85          RunResult simpleAggregate = getSimpleAggregate();
86          RunResult additional = new RunResult( 2, 1, 2, 2, null, true );
87          File summary = File.createTempFile( "failsafe", "test" );
88          simpleAggregate.writeSummary( summary, false, "utf-8" );
89          additional.writeSummary( summary, true, "utf-8" );
90  
91          RunResult actual = RunResult.fromInputStream( new FileInputStream( summary ), "utf-8" );
92          RunResult expected = simpleAggregate.aggregate( additional );
93          assertEquals( expected, actual );
94          //noinspection ResultOfMethodCallIgnored
95          summary.delete();
96  
97      }
98  
99      private void writeReadCheck( RunResult simpleAggregate )
100         throws FileNotFoundException
101     {
102         StringWriter writer = getStringWriter( simpleAggregate );
103 
104         RunResult actual =
105             RunResult.fromInputStream( new ByteArrayInputStream( writer.getBuffer().toString().getBytes() ), "UTF-8" );
106         assertEquals( simpleAggregate, actual );
107     }
108 
109     private StringWriter getStringWriter( RunResult simpleAggregate )
110     {
111         StringWriter writer = new StringWriter();
112         PrettyPrintXMLWriter wr = new PrettyPrintXMLWriter( writer );
113         Xpp3DomWriter.write( wr, simpleAggregate.asXpp3Dom() );
114         return writer;
115     }
116 
117     private RunResult getSimpleAggregate()
118     {
119         RunResult resultOne = new RunResult( 10, 1, 3, 2 );
120         RunResult resultTwo = new RunResult( 10, 2, 4, 2 );
121         return resultOne.aggregate( resultTwo );
122     }
123 }