View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.failsafe;
20  
21  import java.io.File;
22  import java.nio.charset.StandardCharsets;
23  import java.nio.file.Files;
24  import java.nio.file.StandardOpenOption;
25  import java.util.Locale;
26  
27  import org.apache.maven.plugin.failsafe.util.FailsafeSummaryXmlUtils;
28  import org.apache.maven.surefire.api.suite.RunResult;
29  import org.apache.maven.surefire.api.util.SureFireFileManager;
30  import org.junit.jupiter.api.Test;
31  
32  import static java.lang.String.format;
33  import static org.assertj.core.api.Assertions.assertThat;
34  
35  /**
36   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
37   * @since 2.20
38   */
39  @SuppressWarnings("checkstyle:magicnumber")
40  class RunResultTest {
41  
42      @Test
43      void testAggregatedValues() {
44          RunResult simple = getSimpleAggregate();
45  
46          assertThat(simple.getCompletedCount()).isEqualTo(20);
47  
48          assertThat(simple.getErrors()).isEqualTo(3);
49  
50          assertThat(simple.getFailures()).isEqualTo(7);
51  
52          assertThat(simple.getSkipped()).isEqualTo(4);
53  
54          assertThat(simple.getFlakes()).isEqualTo(2);
55      }
56  
57      @Test
58      void testSerialization() throws Exception {
59          writeReadCheck(getSimpleAggregate());
60      }
61  
62      @Test
63      void testFailures() throws Exception {
64          writeReadCheck(new RunResult(0, 1, 2, 3, "stacktraceHere", false));
65      }
66  
67      @Test
68      void testSkipped() throws Exception {
69          writeReadCheck(new RunResult(3, 2, 1, 0, null, true));
70      }
71  
72      @Test
73      void testFlakes() throws Exception {
74          writeReadCheck(new RunResult(3, 2, 1, 0, 2, null, true));
75      }
76  
77      @Test
78      void testLegacyDeserialization() throws Exception {
79          File legacySummary = SureFireFileManager.createTempFile("failsafe", "test");
80          String legacyFailsafeSummaryXmlTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
81                  + "<failsafe-summary xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
82                  + " xsi:noNamespaceSchemaLocation=\"https://maven.apache.org/surefire/maven-surefire-plugin/xsd/failsafe-summary.xsd\""
83                  + " result=\"%s\" timeout=\"%s\">\n"
84                  + "    <completed>%d</completed>\n"
85                  + "    <errors>%d</errors>\n"
86                  + "    <failures>%d</failures>\n"
87                  + "    <skipped>%d</skipped>\n"
88                  + "    %s\n"
89                  + "</failsafe-summary>";
90          String xml = format(Locale.ROOT, legacyFailsafeSummaryXmlTemplate, 0, false, 3, 2, 1, 0, "msg");
91          Files.write(
92                  legacySummary.toPath(),
93                  xml.getBytes(StandardCharsets.UTF_8),
94                  StandardOpenOption.CREATE,
95                  StandardOpenOption.TRUNCATE_EXISTING,
96                  StandardOpenOption.WRITE);
97  
98          // When the failsafe-summary.xml does not contain the <flakes> element, it should be considered as 0.
99          RunResult expected = new RunResult(3, 2, 1, 0, 0, null, false);
100         RunResult actual = FailsafeSummaryXmlUtils.toRunResult(legacySummary);
101 
102         assertThat(actual.getCompletedCount()).isEqualTo(expected.getCompletedCount());
103 
104         assertThat(actual.getErrors()).isEqualTo(expected.getErrors());
105 
106         assertThat(actual.getFailures()).isEqualTo(expected.getFailures());
107 
108         assertThat(actual.getSkipped()).isEqualTo(expected.getSkipped());
109 
110         assertThat(actual.getFlakes()).isEqualTo(expected.getFlakes());
111 
112         assertThat(actual).isEqualTo(expected);
113     }
114 
115     @Test
116     void testAppendSerialization() throws Exception {
117         RunResult simpleAggregate = getSimpleAggregate();
118         RunResult additional = new RunResult(2, 1, 2, 2, "msg " + ((char) 0x0E01), true);
119 
120         File summary = SureFireFileManager.createTempFile("failsafe", "test");
121         FailsafeSummaryXmlUtils.writeSummary(simpleAggregate, summary, false);
122         FailsafeSummaryXmlUtils.writeSummary(additional, summary, true);
123         RunResult actual = FailsafeSummaryXmlUtils.toRunResult(summary);
124         //noinspection ResultOfMethodCallIgnored
125         summary.delete();
126 
127         RunResult expected = simpleAggregate.aggregate(additional);
128 
129         assertThat(expected.getCompletedCount()).isEqualTo(22);
130 
131         assertThat(expected.getErrors()).isEqualTo(4);
132 
133         assertThat(expected.getFailures()).isEqualTo(9);
134 
135         assertThat(expected.getSkipped()).isEqualTo(6);
136 
137         assertThat(expected.getFlakes()).isEqualTo(2);
138 
139         assertThat(expected.getFailure()).isEqualTo("msg " + ((char) 0x0E01));
140 
141         assertThat(expected.isTimeout()).isTrue();
142 
143         assertThat(actual).isEqualTo(expected);
144     }
145 
146     private void writeReadCheck(RunResult expected) throws Exception {
147         File tmp = SureFireFileManager.createTempFile("test", "xml");
148         FailsafeSummaryXmlUtils.fromRunResultToFile(expected, tmp);
149 
150         RunResult actual = FailsafeSummaryXmlUtils.toRunResult(tmp);
151         //noinspection ResultOfMethodCallIgnored
152         tmp.delete();
153 
154         assertThat(actual).isEqualTo(expected);
155     }
156 
157     private RunResult getSimpleAggregate() {
158         RunResult resultOne = new RunResult(10, 1, 3, 2, 1);
159         RunResult resultTwo = new RunResult(10, 2, 4, 2, 1);
160         return resultOne.aggregate(resultTwo);
161     }
162 }