View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.mockito.Mockito.when;
24  
25  import java.lang.reflect.Method;
26  
27  import org.junit.Test;
28  import org.junit.runner.RunWith;
29  import org.powermock.api.mockito.PowerMockito;
30  import org.powermock.core.classloader.annotations.PrepareForTest;
31  import org.powermock.modules.junit4.PowerMockRunner;
32  import org.powermock.reflect.Whitebox;
33  
34  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
35  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
36  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
37  
38  @RunWith(PowerMockRunner.class)
39  @PrepareForTest(AuditEvent.class)
40  public class AuditEventDefaultFormatterTest {
41  
42      @Test
43      public void testFormatFullyQualifiedModuleNameContainsCheckSuffix() {
44          final LocalizedMessage message = new LocalizedMessage(1, 1, null, null, null,
45                  SeverityLevel.WARNING, null, TestModuleCheck.class, "Mocked message.");
46          final AuditEvent event = new AuditEvent("", "InputMockFile.java", message);
47          final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
48  
49          final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. "
50                  + "[AuditEventDefaultFormatterTest$TestModule]";
51  
52          assertEquals("Invalid format", expected, formatter.format(event));
53      }
54  
55      @Test
56      public void testFormatFullyQualifiedModuleNameDoesNotContainCheckSuffix() {
57          final LocalizedMessage message = new LocalizedMessage(1, 1, null, null, null,
58                  SeverityLevel.WARNING, null, TestModule.class, "Mocked message.");
59          final AuditEvent event = new AuditEvent("", "InputMockFile.java", message);
60          final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
61  
62          final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. "
63                  + "[AuditEventDefaultFormatterTest$TestModule]";
64  
65          assertEquals("Invalid format", expected, formatter.format(event));
66      }
67  
68      @Test
69      public void testFormatModuleNameContainsCheckSuffix() {
70          final AuditEvent mock = PowerMockito.mock(AuditEvent.class);
71          when(mock.getSourceName()).thenReturn("TestModuleCheck");
72          when(mock.getSeverityLevel()).thenReturn(SeverityLevel.WARNING);
73          when(mock.getLine()).thenReturn(1);
74          when(mock.getColumn()).thenReturn(1);
75          when(mock.getMessage()).thenReturn("Mocked message.");
76          when(mock.getFileName()).thenReturn("InputMockFile.java");
77          final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
78  
79          final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. [TestModule]";
80  
81          assertEquals("Invalid format", expected, formatter.format(mock));
82      }
83  
84      @Test
85      public void testFormatModuleNameDoesNotContainCheckSuffix() {
86          final AuditEvent mock = PowerMockito.mock(AuditEvent.class);
87          when(mock.getSourceName()).thenReturn("TestModule");
88          when(mock.getSeverityLevel()).thenReturn(SeverityLevel.WARNING);
89          when(mock.getLine()).thenReturn(1);
90          when(mock.getColumn()).thenReturn(1);
91          when(mock.getMessage()).thenReturn("Mocked message.");
92          when(mock.getFileName()).thenReturn("InputMockFile.java");
93          final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
94  
95          final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. [TestModule]";
96  
97          assertEquals("Invalid format", expected, formatter.format(mock));
98      }
99  
100     @Test
101     public void testFormatModuleWithModuleId() {
102         final LocalizedMessage message = new LocalizedMessage(1, 1, null, null, null,
103                 SeverityLevel.WARNING, "ModuleId", TestModule.class, "Mocked message.");
104         final AuditEvent event = new AuditEvent("", "InputMockFile.java", message);
105         final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
106 
107         final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. [ModuleId]";
108 
109         assertEquals("Invalid format", expected, formatter.format(event));
110     }
111 
112     @Test
113     public void testCalculateBufferLength() throws Exception {
114         final Method calculateBufferLengthMethod =
115                 Whitebox.getMethod(AuditEventDefaultFormatter.class,
116                         "calculateBufferLength", AuditEvent.class, int.class);
117         final LocalizedMessage localizedMessage = new LocalizedMessage(1, 1,
118                 "messages.properties", "key", null, SeverityLevel.ERROR, null,
119                 getClass(), null);
120         final AuditEvent auditEvent = new AuditEvent(new Object(), "fileName", localizedMessage);
121         final int result = (int) calculateBufferLengthMethod.invoke(null,
122                 auditEvent, SeverityLevel.ERROR.ordinal());
123 
124         assertEquals("Buffer length is not expected", 54, result);
125     }
126 
127     private static class TestModuleCheck {
128         // no code
129     }
130 
131     private static class TestModule {
132         // no code
133     }
134 }