View Javadoc

1   package org.apache.maven.plugin.surefire.report;
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.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStreamReader;
26  
27  import junit.framework.AssertionFailedError;
28  import junit.framework.TestCase;
29  
30  import org.apache.maven.plugin.surefire.booterclient.output.DeserializedStacktraceWriter;
31  import org.apache.maven.shared.utils.StringUtils;
32  import org.apache.maven.shared.utils.xml.Xpp3Dom;
33  import org.apache.maven.shared.utils.xml.Xpp3DomBuilder;
34  import org.apache.maven.surefire.report.LegacyPojoStackTraceWriter;
35  import org.apache.maven.surefire.report.ReportEntry;
36  import org.apache.maven.surefire.report.SimpleReportEntry;
37  import org.apache.maven.surefire.report.StackTraceWriter;
38  
39  @SuppressWarnings( "ResultOfMethodCallIgnored" )
40  public class StatelessXMLReporterTest
41      extends TestCase
42  {
43  
44      private StatelessXmlReporter reporter = new StatelessXmlReporter( new File( "." ), null, false );
45  
46      private ReportEntry reportEntry;
47  
48      private TestSetStats stats;
49  
50      private File expectedReportFile;
51  
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp();
56          reportEntry = new SimpleReportEntry( this.getClass().getName(), "StatelessXMLReporterTest",
57                                               new LegacyPojoStackTraceWriter( "", "", new AssertionFailedError() ), 17 );
58          stats = new TestSetStats( false, true );
59      }
60  
61      @Override protected void tearDown()
62          throws Exception
63      {
64          super.tearDown();
65  
66          if ( expectedReportFile != null )
67          {
68              expectedReportFile.delete();
69          }
70      }
71  
72      public void testFileNameWithoutSuffix()
73      {
74          File reportDir = new File( "." );
75          String testName = "org.apache.maven.plugin.surefire.report.StatelessXMLReporterTest";
76          reportEntry = new SimpleReportEntry( this.getClass().getName(), testName, 12 );
77          WrappedReportEntry testSetReportEntry =
78              new WrappedReportEntry( reportEntry, ReportEntryType.success, 12, null, null );
79          stats.testSucceeded( testSetReportEntry );
80          reporter.testSetCompleted( testSetReportEntry, stats );
81  
82          expectedReportFile = new File( reportDir, "TEST-" + testName + ".xml" );
83          assertTrue( "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
84                      expectedReportFile.exists() );
85      }
86  
87  
88      public void testAllFieldsSerialized()
89          throws IOException
90      {
91          File reportDir = new File( "." );
92          String testName = "aTestMethod";
93          String testName2 = "bTestMethod";
94          reportEntry = new SimpleReportEntry( this.getClass().getName(), testName, 12 );
95          WrappedReportEntry testSetReportEntry =
96              new WrappedReportEntry( reportEntry, ReportEntryType.success, 12, null, null );
97          expectedReportFile = new File( reportDir, "TEST-" + testName + ".xml" );
98  
99          stats.testSucceeded( testSetReportEntry );
100         StackTraceWriter stackTraceWriter = new DeserializedStacktraceWriter( "A fud msg", "trimmed", "fail at foo" );
101         Utf8RecodingDeferredFileOutputStream stdOut = new Utf8RecodingDeferredFileOutputStream( "fds" );
102         String stdOutPrefix;
103         String stdErrPrefix;
104         if ( defaultCharsetSupportsSpecialChar() )
105         {
106             stdErrPrefix = "std-\u0115rr";
107             stdOutPrefix = "st]]>d-o\u00DCt";
108         }
109         else
110         {
111             stdErrPrefix = "std-err";
112             stdOutPrefix = "st]]>d-out";
113         }
114 
115         byte[] stdOutBytes = (stdOutPrefix + "<null>!\u0020\u0000\u001F").getBytes();
116         stdOut.write( stdOutBytes, 0, stdOutBytes.length );
117 
118         Utf8RecodingDeferredFileOutputStream stdErr = new Utf8RecodingDeferredFileOutputStream( "fds" );
119 
120 
121         byte[] stdErrBytes = (stdErrPrefix + "?&-&amp;&#163;\u0020\u0000\u001F").getBytes();
122         stdErr.write( stdErrBytes, 0, stdErrBytes.length );
123         WrappedReportEntry t2 =
124             new WrappedReportEntry( new SimpleReportEntry( Inner.class.getName(), testName2, stackTraceWriter, 13 ),
125                                     ReportEntryType.error, 13, stdOut, stdErr );
126 
127         stats.testSucceeded( t2 );
128         StatelessXmlReporter reporter = new StatelessXmlReporter( new File( "." ), null, false );
129         reporter.testSetCompleted( testSetReportEntry, stats );
130 
131         FileInputStream fileInputStream = new FileInputStream( expectedReportFile );
132 
133         Xpp3Dom testSuite = Xpp3DomBuilder.build( new InputStreamReader( fileInputStream, "UTF-8") );
134         assertEquals( "testsuite", testSuite.getName() );
135         Xpp3Dom properties = testSuite.getChild( "properties" );
136         assertEquals( System.getProperties().size(), properties.getChildCount() );
137         Xpp3Dom child = properties.getChild( 1 );
138         assertFalse( StringUtils.isEmpty( child.getAttribute( "value" ) ) );
139         assertFalse( StringUtils.isEmpty( child.getAttribute( "name" ) ) );
140 
141         Xpp3Dom[] testcase = testSuite.getChildren( "testcase" );
142         Xpp3Dom tca = testcase[0];
143         assertEquals( testName, tca.getAttribute( "name" ) ); // Hopefully same order on jdk5
144         assertEquals( "0.012", tca.getAttribute( "time" ) );
145         assertEquals( this.getClass().getName(), tca.getAttribute( "classname" ) );
146 
147         Xpp3Dom tcb = testcase[1];
148         assertEquals( testName2, tcb.getAttribute( "name" ) );
149         assertEquals( "0.013", tcb.getAttribute( "time" ) );
150         assertEquals( Inner.class.getName(), tcb.getAttribute( "classname" ) );
151         Xpp3Dom errorNode = tcb.getChild( "error" );
152         assertNotNull( errorNode );
153         assertEquals( "A fud msg", errorNode.getAttribute( "message" ) );
154         assertEquals( "fail at foo", errorNode.getAttribute( "type" ) );
155         assertEquals( stdOutPrefix + "<null>! &amp#0;&amp#31;", tcb.getChild( "system-out" ).getValue() );
156 
157 
158         assertEquals( stdErrPrefix + "?&-&amp;&#163; &amp#0;&amp#31;", tcb.getChild( "system-err" ).getValue() );
159     }
160 
161     private boolean defaultCharsetSupportsSpecialChar()
162     {
163         // some charsets are not able to deal with \u0115 on both ways of the conversion
164         return "\u0115\u00DC".equals( new String( "\u0115\u00DC".getBytes() ) );
165     }
166 
167     class Inner
168     {
169 
170     }
171 
172 }