View Javadoc

1   package org.apache.maven.plugin.checkstyle;
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 com.puppycrawl.tools.checkstyle.api.AuditEvent;
23  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
24  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
25  import junit.framework.TestCase;
26  
27  import java.io.File;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   *
33   */
34  public class CheckstyleReportListenerMultiSourceTest
35      extends TestCase
36  {
37      private Map<SeverityLevel, CheckstyleReportListener> listenerMap;
38  
39      /**
40       * {@inheritDoc}
41       */
42      protected void setUp()
43          throws Exception
44      {
45          listenerMap = new HashMap<SeverityLevel, CheckstyleReportListener>();
46  
47          CheckstyleReportListener listener = new CheckstyleReportListener( new File( "/source/path" ) );
48          listener.addSourceDirectory( new File( "/source/path2" ) );
49          listener.setSeverityLevelFilter( SeverityLevel.INFO );
50          listenerMap.put( listener.getSeverityLevelFilter(), listener );
51  
52          listener = new CheckstyleReportListener( new File( "/source/path" ) );
53          listener.addSourceDirectory( new File( "/source/path2" ) );
54          listener.setSeverityLevelFilter( SeverityLevel.WARNING );
55          listenerMap.put( listener.getSeverityLevelFilter(), listener );
56  
57          listener = new CheckstyleReportListener( new File( "/source/path" ) );
58          listener.addSourceDirectory( new File( "/source/path2" ) );
59          listener.setSeverityLevelFilter( SeverityLevel.ERROR );
60          listenerMap.put( listener.getSeverityLevelFilter(), listener );
61  
62          listener = new CheckstyleReportListener( new File( "/source/path" ) );
63          listener.addSourceDirectory( new File( "/source/path2" ) );
64          listener.setSeverityLevelFilter( SeverityLevel.IGNORE );
65          listenerMap.put( listener.getSeverityLevelFilter(), listener );
66      }
67  
68      public void testListeners()
69      {
70          fireAuditStarted( null );
71  
72          AuditEvent event = new AuditEvent( this, "/source/path/file1", null );
73          fireFileStarted( event );
74          LocalizedMessage message =
75              new LocalizedMessage( 0, 0, "", "", null, SeverityLevel.INFO, null, getClass(), null );
76          fireAddError( new AuditEvent( this, "/source/path/file1", message ) );
77          fireFileFinished( event );
78  
79          event = new AuditEvent( this, "/source/path2/file2", null );
80          fireFileStarted( event );
81          message = new LocalizedMessage( 0, 0, "", "", null, SeverityLevel.WARNING, null, getClass(), null );
82          fireAddError( new AuditEvent( this, "/source/path2/file2", message ) );
83          fireAddError( new AuditEvent( this, "/source/path2/file2", message ) );
84          fireFileFinished( event );
85  
86          event = new AuditEvent( this, "/source/path/file3", null );
87          fireFileStarted( event );
88          message = new LocalizedMessage( 0, 0, "", "", null, SeverityLevel.ERROR, null, getClass(), null );
89          fireAddError( new AuditEvent( this, "/source/path/file3", message ) );
90          fireAddError( new AuditEvent( this, "/source/path/file3", message ) );
91          fireAddError( new AuditEvent( this, "/source/path/file3", message ) );
92          fireFileFinished( event );
93  
94          event = new AuditEvent( this, "/source/path2/file4", null );
95          fireFileStarted( event );
96          message = new LocalizedMessage( 0, 0, "", "", null, SeverityLevel.IGNORE, null, getClass(), null );
97          fireAddError( new AuditEvent( this, "/source/path2/file4", message ) );
98          fireAddError( new AuditEvent( this, "/source/path2/file4", message ) );
99          fireAddError( new AuditEvent( this, "/source/path2/file4", message ) );
100         fireAddError( new AuditEvent( this, "/source/path2/file4", message ) );
101         fireFileFinished( event );
102 
103         fireAuditFinished( null );
104 
105         CheckstyleReportListener listener = (CheckstyleReportListener) listenerMap.get( SeverityLevel.INFO );
106         CheckstyleResults results = listener.getResults();
107         assertEquals( "Test total files", 4, results.getFiles().size() );
108         assertEquals( "Test file count", 4, results.getFileCount() );
109         assertEquals( "test file violations", 1, results.getFileViolations( "file1" ).size() );
110         assertEquals( "test file severities", 1, results.getSeverityCount( "file1", SeverityLevel.INFO ) );
111         assertEquals( "test file severities", 0, results.getSeverityCount( "file1", SeverityLevel.WARNING ) );
112         assertEquals( "test file severities", 0, results.getSeverityCount( "file1", SeverityLevel.ERROR ) );
113         assertEquals( "test file severities", 0, results.getSeverityCount( "file1", SeverityLevel.IGNORE ) );
114 
115         listener = (CheckstyleReportListener) listenerMap.get( SeverityLevel.WARNING );
116         results = listener.getResults();
117         assertEquals( "Test total files", 4, results.getFiles().size() );
118         assertEquals( "Test file count", 4, results.getFileCount() );
119         assertEquals( "test file violations", 2, results.getFileViolations( "file2" ).size() );
120         assertEquals( "test file severities", 0, results.getSeverityCount( "file2", SeverityLevel.INFO ) );
121         assertEquals( "test file severities", 2, results.getSeverityCount( "file2", SeverityLevel.WARNING ) );
122         assertEquals( "test file severities", 0, results.getSeverityCount( "file2", SeverityLevel.ERROR ) );
123         assertEquals( "test file severities", 0, results.getSeverityCount( "file2", SeverityLevel.IGNORE ) );
124 
125         listener = (CheckstyleReportListener) listenerMap.get( SeverityLevel.ERROR );
126         results = listener.getResults();
127         assertEquals( "Test total files", 4, results.getFiles().size() );
128         assertEquals( "Test file count", 4, results.getFileCount() );
129         assertEquals( "test file violations", 3, results.getFileViolations( "file3" ).size() );
130         assertEquals( "test file severities", 0, results.getSeverityCount( "file3", SeverityLevel.INFO ) );
131         assertEquals( "test file severities", 0, results.getSeverityCount( "file3", SeverityLevel.WARNING ) );
132         assertEquals( "test file severities", 3, results.getSeverityCount( "file3", SeverityLevel.ERROR ) );
133         assertEquals( "test file severities", 0, results.getSeverityCount( "file3", SeverityLevel.IGNORE ) );
134 
135         listener = (CheckstyleReportListener) listenerMap.get( SeverityLevel.IGNORE );
136         results = listener.getResults();
137         assertEquals( "Test total files", 4, results.getFiles().size() );
138         assertEquals( "Test file count", 4, results.getFileCount() );
139         assertEquals( "test file violations", 0, results.getFileViolations( "file4" ).size() );
140         assertEquals( "test file severities", 0, results.getSeverityCount( "file4", SeverityLevel.INFO ) );
141         assertEquals( "test file severities", 0, results.getSeverityCount( "file4", SeverityLevel.WARNING ) );
142         assertEquals( "test file severities", 0, results.getSeverityCount( "file4", SeverityLevel.ERROR ) );
143         assertEquals( "test file severities", 0, results.getSeverityCount( "file4", SeverityLevel.IGNORE ) );
144     }
145 
146     private void fireAuditStarted( AuditEvent event )
147     {
148         for ( CheckstyleReportListener listener : listenerMap.values() )
149         {
150             listener.auditStarted( event );
151         }
152     }
153 
154     private void fireAuditFinished( AuditEvent event )
155     {
156         for ( CheckstyleReportListener listener : listenerMap.values() )
157         {
158             listener.auditFinished( event );
159         }
160     }
161 
162     private void fireFileStarted( AuditEvent event )
163     {
164         for ( CheckstyleReportListener listener : listenerMap.values() )
165         {
166             listener.fileStarted( event );
167         }
168     }
169 
170     private void fireFileFinished( AuditEvent event )
171     {
172         for ( CheckstyleReportListener listener : listenerMap.values() )
173         {
174             listener.fileFinished( event );
175         }
176     }
177 
178     private void fireAddError( AuditEvent event )
179     {
180         for ( CheckstyleReportListener listener : listenerMap.values() )
181         {
182             listener.addError( event );
183         }
184     }
185 }