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.surefire.api.report;
20  
21  import javax.annotation.Nonnegative;
22  import javax.annotation.Nonnull;
23  
24  import java.util.Collections;
25  import java.util.Map;
26  import java.util.Objects;
27  
28  import static org.apache.maven.surefire.shared.utils.StringUtils.isBlank;
29  
30  /**
31   * @author Kristian Rosenvold
32   */
33  public class CategorizedReportEntry extends SimpleReportEntry {
34      public static final String GROUP_PREFIX = " (of ";
35  
36      private static final String GROUP_SUFIX = ")";
37  
38      private final String group;
39  
40      public CategorizedReportEntry(
41              @Nonnull RunMode runMode, @Nonnegative Long testRunId, String source, String name, String group) {
42          this(runMode, testRunId, source, name, group, null, null);
43      }
44  
45      public CategorizedReportEntry(
46              @Nonnull RunMode runMode,
47              @Nonnegative Long testRunId,
48              String source,
49              String name,
50              String group,
51              StackTraceWriter stackTraceWriter,
52              Integer elapsed) {
53          super(runMode, testRunId, source, null, name, null, stackTraceWriter, elapsed);
54          this.group = group;
55      }
56  
57      public CategorizedReportEntry(
58              @Nonnull RunMode runMode,
59              @Nonnegative Long testRunId,
60              String source,
61              String name,
62              String group,
63              StackTraceWriter stackTraceWriter,
64              Integer elapsed,
65              String message) {
66          this(
67                  runMode,
68                  testRunId,
69                  source,
70                  null,
71                  name,
72                  null,
73                  group,
74                  stackTraceWriter,
75                  elapsed,
76                  message,
77                  Collections.<String, String>emptyMap());
78      }
79  
80      public CategorizedReportEntry(
81              @Nonnull RunMode runMode,
82              @Nonnegative Long testRunId,
83              String source,
84              String sourceText,
85              String name,
86              String nameText,
87              String group,
88              StackTraceWriter stackTraceWriter,
89              Integer elapsed,
90              String message,
91              Map<String, String> systemProperties) {
92          super(
93                  runMode,
94                  testRunId,
95                  source,
96                  sourceText,
97                  name,
98                  nameText,
99                  stackTraceWriter,
100                 elapsed,
101                 message,
102                 systemProperties);
103         this.group = group;
104     }
105 
106     public static TestSetReportEntry reportEntry(
107             @Nonnull RunMode runMode,
108             @Nonnegative Long testRunId,
109             String source,
110             String sourceText,
111             String name,
112             String nameText,
113             String group,
114             StackTraceWriter stackTraceWriter,
115             Integer elapsed,
116             String message,
117             Map<String, String> systemProperties) {
118         return group != null
119                 ? new CategorizedReportEntry(
120                         runMode,
121                         testRunId,
122                         source,
123                         sourceText,
124                         name,
125                         nameText,
126                         group,
127                         stackTraceWriter,
128                         elapsed,
129                         message,
130                         systemProperties)
131                 : new SimpleReportEntry(
132                         runMode,
133                         testRunId,
134                         source,
135                         sourceText,
136                         name,
137                         nameText,
138                         stackTraceWriter,
139                         elapsed,
140                         message,
141                         systemProperties);
142     }
143 
144     @Override
145     public String getGroup() {
146         return group;
147     }
148 
149     @Override
150     public String getNameWithGroup() {
151         return isNameWithGroup() ? getSourceName() + GROUP_PREFIX + getGroup() + GROUP_SUFIX : getSourceName();
152     }
153 
154     @Override
155     public String getReportNameWithGroup() {
156         String sourceText = getSourceText();
157 
158         if (isBlank(sourceText)) {
159             return null;
160         }
161 
162         return isNameWithGroup() ? sourceText + GROUP_PREFIX + getGroup() + GROUP_SUFIX : sourceText;
163     }
164 
165     @Override
166     public boolean equals(Object o) {
167         if (this == o) {
168             return true;
169         }
170         if (o == null || getClass() != o.getClass()) {
171             return false;
172         }
173         if (!super.equals(o)) {
174             return false;
175         }
176 
177         CategorizedReportEntry that = (CategorizedReportEntry) o;
178 
179         return Objects.equals(group, that.group);
180     }
181 
182     @Override
183     public int hashCode() {
184         int result = super.hashCode();
185         result = 31 * result + (group != null ? group.hashCode() : 0);
186         return result;
187     }
188 
189     private boolean isNameWithGroup() {
190         return getGroup() != null && !getGroup().equals(getSourceName());
191     }
192 }