Package: MessagesTest

MessagesTest

nameinstructionbranchcomplexitylinemethod
MessagesTest()
M: 134 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
partialArrayCopy(Object[], int, int)
M: 31 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
testFormattedGetStringInvalidKey()
M: 58 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
testFormattedGetStringNullKey()
M: 34 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
testFormattedGetStringValidKey()
M: 148 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 37 C: 0
0%
M: 1 C: 0
0%
testFormattedGetStringValidKeyNullParameter()
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
testUnFormattedGetStringInvalidKey()
M: 32 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
testUnFormattedGetStringNullKey()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
testUnFormattedGetStringValidKey()
M: 60 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2006, 2015 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.common.tests.unit.utils;
12:
13: import static org.junit.Assert.assertEquals;
14: import static org.junit.Assert.assertFalse;
15: import static org.junit.Assert.assertTrue;
16: import static org.junit.Assert.fail;
17:
18: import java.lang.reflect.Array;
19: import java.util.HashSet;
20: import java.util.Locale;
21:
22: import org.eclipse.acceleo.common.AcceleoCommonMessages;
23: import org.junit.Test;
24:
25: /**
26: * Tests Messages class. These tests' successful completion heavily depends on
27: * org.eclipse.acceleo.common.acceleocommonmessages.properties file contents.
28: *
29: * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
30: */
31: @SuppressWarnings({"unchecked", "nls", })
32: public class MessagesTest {
33:         /** Expected result of the parameterisable keys (only used if locale is en). */
34:         private final String[] expectedForParameterisable = {
35:                         "Couldn't load class {0}. Check that its containing package is exported.",
36:                         "No library connector to handle {0}",
37:                         "Unexpected exception while trying to install workspace-defined bundle {0} : {1}", };
38:
39:         /** Contains the expected results for the valid keys (only used if locale is en). */
40:         private final String[] expectedForValidKeys = {"Required element not found.",
41:                         "Status to be logged cannot be null.", "Path cannot be null or empty.",
42:                         "Couldn't load the Acceleo standard library.", };
43:
44:         /** These will be used when testing message retrieval with invalid keys. */
45:         private final String[] invalidKeys = {"invalidKey", "AcceleoEvaluationContext.CleanUpError1", "",
46:                         "\u00ec", };
47:
48:         /** Contains possible parameters for the messages. */
49:         private final Object[] messageParameters = {null, "", "Foehn", -1, new Long(10), '\u0043',
50:                         new HashSet<Object>(), "0x6c9a.^\\/", };
51:
52:         /**
53:          * These two are valid, parameterisable keys. See
54:          * org.eclipse.acceleo.common.acceleocommonmessages.properties.
55:          */
56:         private final String[] parameterisableKeys = {"BundleClassLookupFailure", "AcceleoLogger.MissingHandle",
57:                         "WorkspaceUtil.InstallationFailure", };
58:
59:         /**
60:          * These are valid, un-parameterisable keys. See
61:          * org.eclipse.acceleo.common.acceleocommonmessages.properties.
62:          */
63:         private final String[] validKeys = {"AcceleoLogger.ElementNotFound", "AcceleoLogger.LogNullStatus",
64:                         "ModelUtils.NullPath", "AcceleoStandardLibrary.LoadFailure", };
65:
66:         /**
67:          * Tests {@link AcceleoCommonMessages#getString(String, Object...)} with an invalid key. Expects the
68:          * String
69:          *
70:          * <pre>
71:          * "!" + key + "!"
72:          * </pre>
73:          *
74:          * to be returned. Parameters won't affect result here.
75:          */
76:         @Test
77:         public void testFormattedGetStringInvalidKey() {
78:•                for (int i = 0; i < messageParameters.length; i++) {
79:•                        for (int j = i; j < messageParameters.length; j++) {
80:                                 final Object[] parameters = partialArrayCopy(messageParameters, i, j);
81:•                                for (String invalidKey : invalidKeys) {
82:                                         assertEquals("Unexpected result of getString() with an invalid key.",
83:                                                         '!' + invalidKey + '!', AcceleoCommonMessages.getString(invalidKey, parameters));
84:                                 }
85:                         }
86:                 }
87:         }
88:
89:         /**
90:          * Tests {@link AcceleoCommonMessages#getString(String, Object...)} with <code>null</code> key. Expects a
91:          * NullPointerException to be thrown. Parameters won't affect result here.
92:          */
93:         @Test
94:         public void testFormattedGetStringNullKey() {
95:•                for (int i = 0; i < messageParameters.length; i++) {
96:•                        for (int j = i; j < messageParameters.length; j++) {
97:                                 final Object[] parameters = partialArrayCopy(messageParameters, i, j);
98:                                 try {
99:                                         AcceleoCommonMessages.getString(null, parameters);
100:                                         fail("Calling getString() with null key did not throw NullPointerException.");
101:                                 } catch (NullPointerException e) {
102:                                         // This was expected
103:                                 }
104:                         }
105:                 }
106:         }
107:
108:         /**
109:          * Tests {@link AcceleoCommonMessages#getString(String, Object...)} with valid keys.
110:          * <p>
111:          * If the System locale is configured for english language, expects the String associated to the key in
112:          * the properties file to be returned with all occurences of <code>"{[0-9]*}"</code> replaced by
113:          * the correct parameter if any. Otherwise, expects the key to have been found, and the parameters to be
114:          * correctly substituted.
115:          * </p>
116:          */
117:         @Test
118:         public void testFormattedGetStringValidKey() {
119:•                for (int i = 0; i < messageParameters.length; i++) {
120:•                        for (int j = i; j < messageParameters.length; j++) {
121:                                 final Object[] parameters = partialArrayCopy(messageParameters, i, j);
122:•                                for (int k = 0; k < parameterisableKeys.length; k++) {
123:                                         String expectedResult = expectedForParameterisable[k];
124:                                         int parameterCount = 0;
125:•                                        while (expectedResult.matches(".*\\{[0-9]+\\}.*") && parameterCount < parameters.length) {
126:•                                                if (parameters[parameterCount] == null) {
127:                                                         expectedResult = expectedResult.replaceFirst("\\{[0-9]+\\}", "null");
128:                                                 } else {
129:                                                         expectedResult = expectedResult.replaceFirst("\\{[0-9]+\\}",
130:                                                                         parameters[parameterCount].toString());
131:                                                 }
132:                                                 parameterCount++;
133:                                         }
134:                                         Locale previousLocale = null;
135:•                                        if (Locale.getDefault() != Locale.ENGLISH) {
136:                                                 previousLocale = Locale.getDefault();
137:                                         }
138:                                         Locale.setDefault(Locale.ENGLISH);
139:                                         assertEquals("Unexpected formatted String returned by getString(String, Object...).",
140:                                                         expectedResult, AcceleoCommonMessages.getString(parameterisableKeys[k],
141:                                                                         parameters));
142:•                                        if (previousLocale != null) {
143:                                                 Locale.setDefault(previousLocale);
144:                                         } else {
145:                                                 Locale.setDefault(Locale.FRENCH);
146:                                         }
147:                                         final String result = AcceleoCommonMessages.getString(parameterisableKeys[k], parameters);
148:                                         assertFalse("Message class did not find an existing parameterisable key.", result
149:                                                         .equals('!' + parameterisableKeys[k] + '!'));
150:•                                        for (int l = 0; l < parameterCount; l++) {
151:•                                                if (parameters[l] != null) {
152:                                                         assertTrue("Message class did not substitute the parameter in the result.",
153:                                                                         result.contains(parameters[l].toString()));
154:                                                 } else {
155:                                                         assertTrue("Message class did not substitute the parameter in the result.",
156:                                                                         result.contains("null"));
157:                                                 }
158:                                         }
159:•                                        if (previousLocale == null) {
160:                                                 Locale.setDefault(Locale.ENGLISH);
161:                                         }
162:                                 }
163:                         }
164:                 }
165:         }
166:
167:         /**
168:          * Tests {@link AcceleoCommonMessages#getString(String, Object...)} with valid keys and <code>null</code>
169:          * as formatting parameter. Expects the result to be the same as the
170:          * {@link AcceleoCommonMessages#getString(String)}.
171:          */
172:         @Test
173:         public void testFormattedGetStringValidKeyNullParameter() {
174:•                for (int i = 0; i < parameterisableKeys.length; i++) {
175:                         assertEquals("Unexpected formatted String returned by getString(String, Object...).",
176:                                         AcceleoCommonMessages.getString(parameterisableKeys[i]), AcceleoCommonMessages.getString(
177:                                                         parameterisableKeys[i], (Object[])null));
178:                 }
179:         }
180:
181:         /**
182:          * Tests {@link AcceleoCommonMessages#getString(String)} with an invalid key. Expects the String
183:          *
184:          * <pre>
185:          * "!" + key + "!"
186:          * </pre>
187:          *
188:          * to be returned.
189:          */
190:         @Test
191:         public void testUnFormattedGetStringInvalidKey() {
192:•                for (String invalidKey : invalidKeys) {
193:                         assertEquals("Unexpected result of getString() with an invalid key.", '!' + invalidKey + '!',
194:                                         AcceleoCommonMessages.getString(invalidKey));
195:                 }
196:         }
197:
198:         /**
199:          * Tests {@link AcceleoCommonMessages#getString(String)} with <code>null</code> argument. Expects a
200:          * NullPointerException to be thrown.
201:          */
202:         @Test
203:         public void testUnFormattedGetStringNullKey() {
204:                 try {
205:                         AcceleoCommonMessages.getString(null);
206:                         fail("Calling getString() with null argument did not throw NullPointerException.");
207:                 } catch (NullPointerException e) {
208:                         // This was expected
209:                 }
210:         }
211:
212:         /**
213:          * Tests {@link AcceleoCommonMessages#getString(String)} with valid keys. Expects the String associated to
214:          * the key in the properties file to be returned.
215:          */
216:         @Test
217:         public void testUnFormattedGetStringValidKey() {
218:•                for (int i = 0; i < validKeys.length; i++) {
219:                         Locale previousLocale = null;
220:•                        if (Locale.getDefault() != Locale.ENGLISH) {
221:                                 previousLocale = Locale.getDefault();
222:                         }
223:                         Locale.setDefault(Locale.ENGLISH);
224:                         String result = AcceleoCommonMessages.getString(validKeys[i]);
225:                         assertEquals("Unexpected String returned by getString(String).", expectedForValidKeys[i], result);
226:•                        if (previousLocale != null) {
227:                                 Locale.setDefault(previousLocale);
228:                         } else {
229:                                 Locale.setDefault(Locale.FRENCH);
230:                         }
231:                         assertFalse("Message class did not find an existing parameterisable key.", result
232:                                         .equals('!' + validKeys[i] + '!'));
233:•                        if (previousLocale == null) {
234:                                 Locale.setDefault(Locale.ENGLISH);
235:                         }
236:                 }
237:         }
238:
239:         /**
240:          * This will return a partial copy of an array.
241:          *
242:          * @param <T>
243:          * Type of the copied array.
244:          * @param original
245:          * Array to be copied.
246:          * @param start
247:          * starting index of the copy.
248:          * @param end
249:          * end index of the copy.
250:          * @return Array containing a copy of the given range from <code>original</code>.
251:          */
252:         private <T> T[] partialArrayCopy(T[] original, int start, int end) {
253:                 final int range = end - start;
254:•                if (range < 0) {
255:                         throw new IllegalArgumentException("Illegal copy range.");
256:                 }
257:                 final T[] copy = (T[])Array.newInstance(original.getClass().getComponentType(), range);
258:                 System.arraycopy(original, start, copy, 0, Math.min(original.length - start, range));
259:                 return copy;
260:         }
261: }