View Javadoc
1   package org.apache.maven.surefire.api.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 javax.annotation.Nonnegative;
23  import javax.annotation.Nonnull;
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
34      extends SimpleReportEntry
35  {
36      public static final String GROUP_PREFIX = " (of ";
37  
38      private static final String GROUP_SUFIX = ")";
39  
40      private final String group;
41  
42      public CategorizedReportEntry( @Nonnull RunMode runMode, @Nonnegative Long testRunId,
43                                     String source, String name, String group )
44      {
45          this( runMode, testRunId, source, name, group, null, null );
46      }
47  
48      public CategorizedReportEntry( @Nonnull RunMode runMode, @Nonnegative Long testRunId,
49                                     String source, String name, String group, StackTraceWriter stackTraceWriter,
50                                     Integer elapsed )
51      {
52          super( runMode, testRunId, source, null, name, null, stackTraceWriter, elapsed );
53          this.group = group;
54      }
55  
56      public CategorizedReportEntry( @Nonnull RunMode runMode, @Nonnegative Long testRunId,
57                                     String source, String name, String group, StackTraceWriter stackTraceWriter,
58                                     Integer elapsed, String message )
59      {
60          this( runMode, testRunId, source, null, name, null,
61                  group, stackTraceWriter, elapsed, message, Collections.<String, String>emptyMap() );
62      }
63  
64      public CategorizedReportEntry( @Nonnull RunMode runMode, @Nonnegative Long testRunId,
65                                     String source, String sourceText, String name, String nameText,
66                                     String group, StackTraceWriter stackTraceWriter,
67                                     Integer elapsed, String message, Map<String, String> systemProperties )
68      {
69          super( runMode, testRunId, source, sourceText, name, nameText, stackTraceWriter, elapsed, message,
70              systemProperties );
71          this.group = group;
72      }
73  
74      public static TestSetReportEntry reportEntry( @Nonnull RunMode runMode, @Nonnegative Long testRunId,
75                                                    String source, String sourceText, String name, String nameText,
76                                                    String group,
77                                                    StackTraceWriter stackTraceWriter, Integer elapsed, String message,
78                                                    Map<String, String> systemProperties )
79      {
80          return group != null
81              ? new CategorizedReportEntry( runMode, testRunId, source, sourceText, name, nameText,
82                  group, stackTraceWriter, elapsed, message, systemProperties )
83              : new SimpleReportEntry( runMode, testRunId, source, sourceText, name, nameText,
84                  stackTraceWriter, elapsed, message, systemProperties );
85      }
86  
87      @Override
88      public String getGroup()
89      {
90          return group;
91      }
92  
93      @Override
94      public String getNameWithGroup()
95      {
96          return isNameWithGroup() ? getSourceName() + GROUP_PREFIX + getGroup() + GROUP_SUFIX : getSourceName();
97      }
98  
99      @Override
100     public String getReportNameWithGroup()
101     {
102         String sourceText = getSourceText();
103 
104         if ( isBlank ( sourceText ) )
105         {
106             return null;
107         }
108 
109         return isNameWithGroup() ? sourceText + GROUP_PREFIX + getGroup() + GROUP_SUFIX : sourceText;
110     }
111 
112     @Override
113     public boolean equals( Object o )
114     {
115         if ( this == o )
116         {
117             return true;
118         }
119         if ( o == null || getClass() != o.getClass() )
120         {
121             return false;
122         }
123         if ( !super.equals( o ) )
124         {
125             return false;
126         }
127 
128         CategorizedReportEntry that = (CategorizedReportEntry) o;
129 
130         return Objects.equals( group, that.group );
131     }
132 
133     @Override
134     public int hashCode()
135     {
136         int result = super.hashCode();
137         result = 31 * result + ( group != null ? group.hashCode() : 0 );
138         return result;
139     }
140 
141     private boolean isNameWithGroup()
142     {
143         return getGroup() != null && !getGroup().equals( getSourceName() );
144     }
145 }