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          assertEquals( 2, simple.getFlakes() );
62  
63      }
64  
65      public void testSerialization()
66          throws FileNotFoundException
67      {
68          writeReadCheck( getSimpleAggregate() );
69      }
70  
71      public void testFailures()
72          throws FileNotFoundException
73      {
74          writeReadCheck( new RunResult( 0, 1, 2, 3, "stacktraceHere", false ) );
75      }
76  
77      public void testSkipped()
78          throws FileNotFoundException
79      {
80          writeReadCheck( new RunResult( 3, 2, 1, 0, null, true ) );
81      }
82  
83      public void testAppendSerialization()
84          throws IOException
85      {
86          RunResult simpleAggregate = getSimpleAggregate();
87          RunResult additional = new RunResult( 2, 1, 2, 2, null, true );
88          File summary = File.createTempFile( "failsafe", "test" );
89          simpleAggregate.writeSummary( summary, false, "utf-8" );
90          additional.writeSummary( summary, true, "utf-8" );
91  
92          RunResult actual = RunResult.fromInputStream( new FileInputStream( summary ), "utf-8" );
93          RunResult expected = simpleAggregate.aggregate( additional );
94          assertEquals( expected, actual );
95          //noinspection ResultOfMethodCallIgnored
96          summary.delete();
97  
98      }
99  
100     private void writeReadCheck( RunResult simpleAggregate )
101         throws FileNotFoundException
102     {
103         StringWriter writer = getStringWriter( simpleAggregate );
104 
105         RunResult actual =
106             RunResult.fromInputStream( new ByteArrayInputStream( writer.getBuffer().toString().getBytes() ), "UTF-8" );
107         assertEquals( simpleAggregate, actual );
108     }
109 
110     private StringWriter getStringWriter( RunResult simpleAggregate )
111     {
112         StringWriter writer = new StringWriter();
113         PrettyPrintXMLWriter wr = new PrettyPrintXMLWriter( writer );
114         Xpp3DomWriter.write( wr, simpleAggregate.asXpp3Dom() );
115         return writer;
116     }
117 
118     private RunResult getSimpleAggregate()
119     {
120         RunResult resultOne = new RunResult( 10, 1, 3, 2, 1 );
121         RunResult resultTwo = new RunResult( 10, 2, 4, 2, 1 );
122         return resultOne.aggregate( resultTwo );
123     }
124 }