View Javadoc
1   package org.apache.maven.plugin.checkstyle.exec;
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 junit.framework.TestCase;
23  
24  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
25  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
26  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
27  
28  import java.util.Collections;
29  import java.util.Map;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.ArrayList;
33  
34  import org.apache.maven.plugin.checkstyle.exec.CheckstyleResults;
35  
36  /**
37   * @author Edwin Punzalan
38   * @version $Id: CheckstyleResultsTest.html 937799 2015-01-27 00:57:50Z hboutemy $
39   */
40  public class CheckstyleResultsTest
41      extends TestCase
42  {
43      private CheckstyleResults results;
44  
45      /** {@inheritDoc} */
46      protected void setUp()
47          throws Exception
48      {
49          results = new CheckstyleResults();
50      }
51  
52      /** {@inheritDoc} */
53      protected void tearDown()
54          throws Exception
55      {
56          results = null;
57      }
58  
59      public void testEmptyResults()
60      {
61          assertEquals( "test total files", 0, results.getFiles().size() );
62  
63          assertEquals( "test file count", 0, results.getFileCount() );
64  
65          assertEquals( "test zero file violations", 0, results.getFileViolations( "filename" ).size() );
66  
67          assertEquals( "test INFO severity count", 0, results.getSeverityCount( SeverityLevel.INFO ) );
68  
69          assertEquals( "test WARNING severity count", 0, results.getSeverityCount( SeverityLevel.WARNING ) );
70  
71          assertEquals( "test ERROR severity count", 0, results.getSeverityCount( SeverityLevel.ERROR ) );
72  
73          assertEquals( "test IGNORE severity count", 0, results.getSeverityCount( SeverityLevel.IGNORE ) );
74      }
75  
76      public void testResults()
77      {
78          Map<String, List<AuditEvent>> files = new HashMap<String, List<AuditEvent>>();
79  
80          LocalizedMessage message = new LocalizedMessage( 0, 0, "", "", null, SeverityLevel.INFO, null, getClass(), null );
81          AuditEvent event = new AuditEvent( this, "file1", message );
82          files.put( "file1", Collections.singletonList( event ) );
83  
84          message = new LocalizedMessage( 0, 0, "", "", null, SeverityLevel.WARNING, null, getClass(), null );
85          List<AuditEvent> events = new ArrayList<AuditEvent>();
86          events.add( new AuditEvent( this, "file2", message ) );
87          events.add( new AuditEvent( this, "file2", message ) );
88          files.put( "file2", events );
89  
90          message = new LocalizedMessage( 0, 0, "", "", null, SeverityLevel.ERROR, null, getClass(), null );
91          events = new ArrayList<AuditEvent>();
92          events.add( new AuditEvent( this, "file3", message ) );
93          events.add( new AuditEvent( this, "file3", message ) );
94          events.add( new AuditEvent( this, "file3", message ) );
95          files.put( "file3", events );
96  
97          message = new LocalizedMessage( 0, 0, "", "", null, SeverityLevel.IGNORE, null, getClass(), null );
98          events = new ArrayList<AuditEvent>();
99          events.add( new AuditEvent( this, "file4", message ) );
100         events.add( new AuditEvent( this, "file4", message ) );
101         events.add( new AuditEvent( this, "file4", message ) );
102         events.add( new AuditEvent( this, "file4", message ) );
103         files.put( "file4", events );
104 
105         results.setFiles( files );
106 
107         assertEquals( "test total files", 4, results.getFiles().size() );
108         assertEquals( "test file count", 4, results.getFileCount() );
109 
110         assertEquals( "test file severities", 0, results.getSeverityCount( "file0", SeverityLevel.INFO ) );
111         assertEquals( "test file severities", 0, results.getSeverityCount( "file0", SeverityLevel.WARNING ) );
112         assertEquals( "test file severities", 0, results.getSeverityCount( "file0", SeverityLevel.ERROR ) );
113         assertEquals( "test file severities", 0, results.getSeverityCount( "file0", SeverityLevel.IGNORE ) );
114 
115         assertEquals( "test file violations", 1, results.getFileViolations( "file1" ).size() );
116         assertEquals( "test file severities", 1, results.getSeverityCount( "file1", SeverityLevel.INFO ) );
117         assertEquals( "test file severities", 0, results.getSeverityCount( "file1", SeverityLevel.WARNING ) );
118         assertEquals( "test file severities", 0, results.getSeverityCount( "file1", SeverityLevel.ERROR ) );
119         assertEquals( "test file severities", 0, results.getSeverityCount( "file1", SeverityLevel.IGNORE ) );
120 
121         assertEquals( "test file violations", 2, results.getFileViolations( "file2" ).size() );
122         assertEquals( "test file severities", 0, results.getSeverityCount( "file2", SeverityLevel.INFO ) );
123         assertEquals( "test file severities", 2, results.getSeverityCount( "file2", SeverityLevel.WARNING ) );
124         assertEquals( "test file severities", 0, results.getSeverityCount( "file2", SeverityLevel.ERROR ) );
125         assertEquals( "test file severities", 0, results.getSeverityCount( "file2", SeverityLevel.IGNORE ) );
126 
127         assertEquals( "test file violations", 3, results.getFileViolations( "file3" ).size() );
128         assertEquals( "test file severities", 0, results.getSeverityCount( "file3", SeverityLevel.INFO ) );
129         assertEquals( "test file severities", 0, results.getSeverityCount( "file3", SeverityLevel.WARNING ) );
130         assertEquals( "test file severities", 3, results.getSeverityCount( "file3", SeverityLevel.ERROR ) );
131         assertEquals( "test file severities", 0, results.getSeverityCount( "file3", SeverityLevel.IGNORE ) );
132 
133         assertEquals( "test file violations", 4, results.getFileViolations( "file4" ).size() );
134         assertEquals( "test file severities", 0, results.getSeverityCount( "file4", SeverityLevel.INFO ) );
135         assertEquals( "test file severities", 0, results.getSeverityCount( "file4", SeverityLevel.WARNING ) );
136         assertEquals( "test file severities", 0, results.getSeverityCount( "file4", SeverityLevel.ERROR ) );
137         assertEquals( "test file severities", 4, results.getSeverityCount( "file4", SeverityLevel.IGNORE ) );
138 
139         assertEquals( "test INFO severity count", 1, results.getSeverityCount( SeverityLevel.INFO ) );
140         assertEquals( "test WARNING severity count", 2, results.getSeverityCount( SeverityLevel.WARNING ) );
141         assertEquals( "test ERROR severity count", 3, results.getSeverityCount( SeverityLevel.ERROR ) );
142         assertEquals( "test IGNORE severity count", 4, results.getSeverityCount( SeverityLevel.IGNORE ) );
143 
144         results.setFileViolations( "file", Collections.EMPTY_LIST );
145         assertEquals( "test file violations", 0, results.getFileViolations( "file" ).size() );
146     }
147 }