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.api;
21  
22  import static com.puppycrawl.tools.checkstyle.utils.CommonUtils.EMPTY_BYTE_ARRAY;
23  import static com.puppycrawl.tools.checkstyle.utils.CommonUtils.EMPTY_OBJECT_ARRAY;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  import static org.mockito.Matchers.anyInt;
28  import static org.mockito.Matchers.anyObject;
29  import static org.mockito.Mockito.times;
30  import static org.mockito.Mockito.verify;
31  import static org.powermock.api.mockito.PowerMockito.mock;
32  import static org.powermock.api.mockito.PowerMockito.when;
33  
34  import java.io.ByteArrayInputStream;
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.net.URL;
38  import java.net.URLConnection;
39  import java.net.URLStreamHandler;
40  import java.util.Locale;
41  import java.util.Map;
42  import java.util.ResourceBundle;
43  
44  import org.junit.After;
45  import org.junit.Test;
46  import org.mockito.Mockito;
47  import org.powermock.reflect.Whitebox;
48  
49  import nl.jqno.equalsverifier.EqualsVerifier;
50  
51  public class LocalizedMessageTest {
52  
53      private static final Locale DEFAULT_LOCALE = Locale.getDefault();
54  
55      @Test
56      public void testEqualsAndHashCode() {
57          EqualsVerifier.forClass(LocalizedMessage.class).usingGetClass().verify();
58      }
59  
60      @Test
61      public void testGetModuleId() {
62          final LocalizedMessage localizedMessage = createSampleLocalizedMessage();
63  
64          assertEquals("Invalid module id", "module", localizedMessage.getModuleId());
65      }
66  
67      @Test
68      public void testMessageInEnglish() {
69          final LocalizedMessage localizedMessage = createSampleLocalizedMessage();
70          LocalizedMessage.setLocale(Locale.ENGLISH);
71  
72          assertEquals("Invalid message", "Empty statement.", localizedMessage.getMessage());
73      }
74  
75      @Test
76      public void testBundleReloadUrlNull() throws IOException {
77          final LocalizedMessage.Utf8Control control = new LocalizedMessage.Utf8Control();
78          final ResourceBundle bundle = control.newBundle(
79                  "com.puppycrawl.tools.checkstyle.checks.coding.messages",
80                  Locale.ENGLISH, "java.class",
81                  Thread.currentThread().getContextClassLoader(), true);
82          assertNull("Bundle should be null when reload is true and URL is null", bundle);
83      }
84  
85      @Test
86      public void testBundleReloadUrlNotNull() throws IOException {
87          final ClassLoader classloader = mock(ClassLoader.class);
88          final String resource =
89                  "com/puppycrawl/tools/checkstyle/checks/coding/messages_en.properties";
90          final URLConnection mockUrlCon = mock(URLConnection.class);
91          final URLStreamHandler stubUrlHandler = new URLStreamHandler() {
92              @Override
93              protected URLConnection openConnection(URL u) {
94                  return mockUrlCon;
95              }
96          };
97          final URL url = new URL("foo", "bar", 99, "/foobar", stubUrlHandler);
98          final InputStream inputStreamMock = mock(InputStream.class);
99          when(classloader.getResource(resource)).thenReturn(url);
100         when(mockUrlCon.getInputStream()).thenReturn(inputStreamMock);
101         when(inputStreamMock.read(anyObject(), anyInt(), anyInt())).thenReturn(-1);
102 
103         final LocalizedMessage.Utf8Control control = new LocalizedMessage.Utf8Control();
104         control.newBundle("com.puppycrawl.tools.checkstyle.checks.coding.messages",
105                 Locale.ENGLISH, "java.class",
106                 classloader, true);
107 
108         verify(mockUrlCon, times(1)).setUseCaches(false);
109         verify(inputStreamMock, times(1)).close();
110     }
111 
112     @Test
113     public void testBundleReloadUrlNotNullStreamNull() throws IOException {
114 
115         final ClassLoader classloader = mock(ClassLoader.class);
116         final String resource =
117             "com/puppycrawl/tools/checkstyle/checks/coding/messages_en.properties";
118 
119         final URL url = getMockUrl(null);
120         when(classloader.getResource(resource)).thenReturn(url);
121 
122         final LocalizedMessage.Utf8Control control = new LocalizedMessage.Utf8Control();
123         final ResourceBundle bundle = control.newBundle(
124                 "com.puppycrawl.tools.checkstyle.checks.coding.messages",
125                 Locale.ENGLISH, "java.class",
126                 classloader, true);
127         assertNull("Bundle should be null when stream is null", bundle);
128     }
129 
130     private static URL getMockUrl(final URLConnection connection) throws IOException {
131         final URLStreamHandler handler = new URLStreamHandler() {
132             @Override
133             protected URLConnection openConnection(final URL url) {
134                 return connection;
135             }
136         };
137         return new URL("http://foo.bar", "foo.bar", 80, "", handler);
138     }
139 
140     @Test
141     public void testMessageInFrench() {
142         final LocalizedMessage localizedMessage = createSampleLocalizedMessage();
143         LocalizedMessage.setLocale(Locale.FRENCH);
144 
145         assertEquals("Invalid message", "Instruction vide.", localizedMessage.getMessage());
146     }
147 
148     @Test
149     public void testEnforceEnglishLanguageBySettingUnitedStatesLocale() {
150         Locale.setDefault(Locale.FRENCH);
151         LocalizedMessage.setLocale(Locale.US);
152         final LocalizedMessage localizedMessage = createSampleLocalizedMessage();
153 
154         assertEquals("Invalid message", "Empty statement.", localizedMessage.getMessage());
155     }
156 
157     @Test
158     public void testEnforceEnglishLanguageBySettingRootLocale() {
159         Locale.setDefault(Locale.FRENCH);
160         LocalizedMessage.setLocale(Locale.ROOT);
161         final LocalizedMessage localizedMessage = createSampleLocalizedMessage();
162 
163         assertEquals("Invalid message", "Empty statement.", localizedMessage.getMessage());
164     }
165 
166     @Test
167     public void testBundleWithoutReload() throws IOException {
168         final ClassLoader classloader = mock(ClassLoader.class);
169         final URLConnection mockConnection = Mockito.mock(URLConnection.class);
170         when(mockConnection.getInputStream()).thenReturn(
171                 new ByteArrayInputStream(EMPTY_BYTE_ARRAY));
172 
173         final URL url = getMockUrl(mockConnection);
174         final String resource =
175                 "com/puppycrawl/tools/checkstyle/checks/coding/messages_en.properties";
176         when(classloader.getResource(resource)).thenReturn(url);
177 
178         final LocalizedMessage.Utf8Control control = new LocalizedMessage.Utf8Control();
179         final ResourceBundle resourceBundle = control.newBundle(
180                 "com.puppycrawl.tools.checkstyle.checks.coding.messages",
181                 Locale.ENGLISH, "java.class", classloader, false);
182 
183         assertNull("Resource bundle should not be null", resourceBundle);
184     }
185 
186     @Test
187     public void testGetKey() {
188         Locale.setDefault(Locale.FRENCH);
189         LocalizedMessage.setLocale(Locale.US);
190         final LocalizedMessage localizedMessage = createSampleLocalizedMessage();
191 
192         assertEquals("Invalid message key", "empty.statement", localizedMessage.getKey());
193     }
194 
195     @Test
196     public void testCleatBundleCache() {
197         Locale.setDefault(Locale.FRENCH);
198         LocalizedMessage.setLocale(Locale.ROOT);
199         final LocalizedMessage localizedMessage = createSampleLocalizedMessage();
200 
201         assertEquals("Invalid message", "Empty statement.", localizedMessage.getMessage());
202 
203         final Map<String, ResourceBundle> bundleCache =
204                 Whitebox.getInternalState(LocalizedMessage.class, "BUNDLE_CACHE");
205 
206         assertEquals("Invalid bundle cache size", 1, bundleCache.size());
207 
208         LocalizedMessage.setLocale(Locale.CHINA);
209 
210         assertEquals("Invalid bundle cache size", 0, bundleCache.size());
211     }
212 
213     @Test
214     public void testTokenType() {
215         final LocalizedMessage localizedMessage1 = new LocalizedMessage(1, 1, TokenTypes.CLASS_DEF,
216                 "messages.properties", "key", null, SeverityLevel.ERROR, null,
217                 getClass(), null);
218         final LocalizedMessage localizedMessage2 = new LocalizedMessage(1, 1, TokenTypes.OBJBLOCK,
219                 "messages.properties", "key", EMPTY_OBJECT_ARRAY, SeverityLevel.ERROR, null,
220                 getClass(), null);
221 
222         assertEquals("Invalid token type", TokenTypes.CLASS_DEF, localizedMessage1.getTokenType());
223         assertEquals("Invalid token type", TokenTypes.OBJBLOCK, localizedMessage2.getTokenType());
224     }
225 
226     @Test
227     public void testGetColumnCharIndex() {
228         final LocalizedMessage localizedMessage1 = new LocalizedMessage(1, 1, 123,
229                 TokenTypes.CLASS_DEF, "messages.properties", "key", null, SeverityLevel.ERROR,
230                 null, getClass(), null);
231 
232         assertEquals("Invalid column char index", 123, localizedMessage1.getColumnCharIndex());
233     }
234 
235     @Test
236     public void testCompareToWithDifferentModuleId() {
237         final LocalizedMessage message1 = createSampleLocalizedMessageWithId("module1");
238         final LocalizedMessage message2 = createSampleLocalizedMessageWithId("module2");
239         final LocalizedMessage messageNull = createSampleLocalizedMessageWithId(null);
240 
241         assertTrue("Invalid comparing result", message1.compareTo(messageNull) > 0);
242         assertTrue("Invalid comparing result", messageNull.compareTo(message1) < 0);
243         assertTrue("Invalid comparing result", message1.compareTo(message2) < 0);
244     }
245 
246     private static LocalizedMessage createSampleLocalizedMessage() {
247         return createSampleLocalizedMessageWithId("module");
248     }
249 
250     private static LocalizedMessage createSampleLocalizedMessageWithId(String id) {
251         return new LocalizedMessage(0, "com.puppycrawl.tools.checkstyle.checks.coding.messages",
252                 "empty.statement", EMPTY_OBJECT_ARRAY, id, LocalizedMessage.class, null);
253     }
254 
255     @After
256     public void tearDown() {
257         Locale.setDefault(DEFAULT_LOCALE);
258         LocalizedMessage.clearCache();
259         LocalizedMessage.setLocale(DEFAULT_LOCALE);
260     }
261 }