Package: AcceleoCommonPluginTest

AcceleoCommonPluginTest

nameinstructionbranchcomplexitylinemethod
AcceleoCommonPluginTest()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setUp()
M: 47 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 35 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
tearDown()
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
testLogExceptionArbitraryException()
M: 68 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
testLogExceptionCoreException()
M: 73 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
testLogExceptionNullException()
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%
testLogExceptionNullPointerException()
M: 68 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
testLogMessageNullMessage()
M: 57 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
testLogMessageValidMessage()
M: 62 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
testLogStatusNullStatus()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
testLogStatusValidStatus()
M: 69 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2009, 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.fail;
15:
16: import java.io.File;
17: import java.io.IOException;
18: import java.io.PrintStream;
19:
20: import org.eclipse.acceleo.common.AcceleoCommonMessages;
21: import org.eclipse.acceleo.common.AcceleoCommonPlugin;
22: import org.eclipse.acceleo.common.internal.utils.AcceleoLogger;
23: import org.eclipse.acceleo.common.tests.AcceleoCommonTestPlugin;
24: import org.eclipse.core.runtime.CoreException;
25: import org.eclipse.core.runtime.FileLocator;
26: import org.eclipse.core.runtime.ILogListener;
27: import org.eclipse.core.runtime.IStatus;
28: import org.eclipse.core.runtime.Status;
29: import org.junit.After;
30: import org.junit.Before;
31: import org.junit.Test;
32:
33: /**
34: * Tests for the common plug-in activator. Will mainly test behavior of utility methods it declares.
35: *
36: * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
37: */
38: @SuppressWarnings("nls")
39: public class AcceleoCommonPluginTest {
40:         /** Error messages to use for these tests. */
41:         private static final String[] ERROR_MESSAGES = {"NullPointerException has been thrown.",
42:                         "failed to build.", "\u00ec", "test", };
43:
44:         /** Possible severities for an exception. */
45:         private static final int[] ERROR_SEVERITIES = {IStatus.WARNING, IStatus.ERROR, IStatus.INFO, };
46:
47:         /** This will keep track of the last {@link IStatus} that has been logged. */
48:         protected IStatus loggedStatus;
49:
50:         /** This listener will be used to keep track of {@link IStatus}es logged. */
51:         private ILogListener logListener;
52:
53:         /** The output stream that will be used as a temporary System.err. */
54:         private PrintStream temporaryErr;
55:
56:         /** File that will serve as an output for the temporary System.err. */
57:         private File temporaryLog;
58:
59:         /**
60:          * Tests the behavior of {@link AcceleoCommonPlugin#log(Exception, boolean)} passing an arbitrary
61:          * exception other than {@link NullPointerException} and {@link CoreException} to be logged. Expects the
62:          * exception to be logged with the specified severity. The error message should be the one specified in
63:          * org.eclipse.acceleo.common.acceleocommonmessages.properties with key
64:          * "AcceleoLogger.JavaException".
65:          */
66:         @Test
67:         public void testLogExceptionArbitraryException() {
68:                 boolean blocker = false;
69:•                for (String message : ERROR_MESSAGES) {
70:                         final PrintStream systemErr = System.err;
71:                         // disables standard error to avoid all logged exception to be displayed in console.
72:                         final Exception exception = new IllegalArgumentException(message);
73:                         System.setErr(temporaryErr);
74:                         AcceleoLogger.log(exception, blocker);
75:                         System.setErr(systemErr);
76:
77:                         final int expectedSeverity;
78:•                        if (blocker) {
79:                                 expectedSeverity = IStatus.ERROR;
80:                         } else {
81:                                 expectedSeverity = IStatus.WARNING;
82:                         }
83:•                        blocker = !blocker;
84:
85:                         assertEquals("Unexpected message of the logged exception.", exception.getMessage(), loggedStatus
86:                                         .getMessage());
87:                         assertEquals("Unexpected severity of the logged exception.", expectedSeverity, loggedStatus
88:                                         .getSeverity());
89:                         assertEquals("Exception logged with unexpected plug-in ID.", AcceleoCommonPlugin.PLUGIN_ID,
90:                                         loggedStatus.getPlugin());
91:                 }
92:         }
93:
94:         /**
95:          * Tests the behavior of {@link AcceleoCommonPlugin#log(Exception, boolean)} passing a
96:          * {@link CoreException} to be logged. Expects the exception to be logged with the specified error message
97:          * and severity.
98:          */
99:         @Test
100:         public void testLogExceptionCoreException() {
101:•                for (int severity : ERROR_SEVERITIES) {
102:•                        for (String message : ERROR_MESSAGES) {
103:                                 final Status coreExceptionStatus = new Status(severity, AcceleoCommonTestPlugin.PLUGIN_ID,
104:                                                 message);
105:                                 // disables standard error to avoid all logged exception to be displayed in console.
106:                                 final PrintStream systemErr = System.err;
107:                                 System.setErr(temporaryErr);
108:                                 AcceleoLogger.log(new CoreException(coreExceptionStatus), true);
109:                                 System.setErr(systemErr);
110:
111:                                 assertEquals("Unexpected message of the logged core exception.", message, loggedStatus
112:                                                 .getMessage());
113:                                 assertEquals("Unexpected severity of the logged core exception.", severity, loggedStatus
114:                                                 .getSeverity());
115:                                 assertEquals("Core exception logged with unexpected plug-in ID.",
116:                                                 AcceleoCommonTestPlugin.PLUGIN_ID, loggedStatus.getPlugin());
117:                         }
118:                 }
119:         }
120:
121:         /**
122:          * Tests the behavior of {@link AcceleoCommonPlugin#log(Exception, boolean)} with <code>null</code> as the
123:          * exception argument. Expects a {@link NullPointerException} to be thrown.
124:          */
125:         @Test
126:         public void testLogExceptionNullException() {
127:                 try {
128:                         AcceleoLogger.log((Exception)null, true);
129:                         fail("Logging null didn't throw expected NullPointerException.");
130:                 } catch (NullPointerException e) {
131:                         // This was expected behavior
132:                 }
133:         }
134:
135:         /**
136:          * Tests the behavior of {@link AcceleoCommonPlugin#log(Exception, boolean)} passing a
137:          * {@link NullPointerException} to be logged. Expects the exception to be logged with the specified
138:          * severity. The error message should be the one specified in
139:          * org.eclipse.acceleo.common.acceleocommonmessages.properties with key
140:          * "AcceleoLogger.ElementNotFound".
141:          */
142:         @Test
143:         public void testLogExceptionNullPointerException() {
144:                 boolean blocker = false;
145:•                for (String message : ERROR_MESSAGES) {
146:                         // disables standard error to avoid all logged exception to be displayed in console.
147:                         final PrintStream systemErr = System.err;
148:                         System.setErr(temporaryErr);
149:                         AcceleoLogger.log(new NullPointerException(message), blocker);
150:                         System.setErr(systemErr);
151:
152:                         final String expectedMessage = AcceleoCommonMessages.getString("AcceleoLogger.ElementNotFound");
153:                         final int expectedSeverity;
154:•                        if (blocker) {
155:                                 expectedSeverity = IStatus.ERROR;
156:                         } else {
157:                                 expectedSeverity = IStatus.WARNING;
158:                         }
159:•                        blocker = !blocker;
160:
161:                         assertEquals("Unexpected message of the logged NullPointerException.", expectedMessage,
162:                                         loggedStatus.getMessage());
163:                         assertEquals("Unexpected severity of the logged NullPointerException.", expectedSeverity,
164:                                         loggedStatus.getSeverity());
165:                         assertEquals("NullPointerException logged with unexpected plug-in ID.",
166:                                         AcceleoCommonPlugin.PLUGIN_ID, loggedStatus.getPlugin());
167:                 }
168:         }
169:
170:         /**
171:          * Tests the behavior of {@link AcceleoCommonPlugin#log(String, boolean)} with <code>null</code> as the
172:          * message to be logged. Expects a new entry to be logged with the given severity and the message
173:          * specified in org.eclipse.acceleo.common.acceleocommonmessages.properties with key
174:          * "AcceleoLogger.UnexpectedException".
175:          */
176:         @Test
177:         public void testLogMessageNullMessage() {
178:                 boolean blocker = false;
179:•                for (int i = 0; i < ERROR_MESSAGES.length; i++) {
180:                         final PrintStream systemErr = System.err;
181:                         // disables standard error to avoid all logged exception to be displayed in console.
182:                         System.setErr(temporaryErr);
183:                         AcceleoLogger.log((String)null, blocker);
184:                         System.setErr(systemErr);
185:
186:                         final String expectedMessage = AcceleoCommonMessages
187:                                         .getString("AcceleoLogger.UnexpectedException");
188:                         final int expectedSeverity;
189:•                        if (blocker) {
190:                                 expectedSeverity = IStatus.ERROR;
191:                         } else {
192:                                 expectedSeverity = IStatus.WARNING;
193:                         }
194:•                        blocker = !blocker;
195:
196:                         assertEquals("Unexpected message of the logged message.", expectedMessage, loggedStatus
197:                                         .getMessage());
198:                         assertEquals("Unexpected severity of the logged message.", expectedSeverity, loggedStatus
199:                                         .getSeverity());
200:                         assertEquals("Message logged with unexpected plug-in ID.", AcceleoCommonPlugin.PLUGIN_ID,
201:                                         loggedStatus.getPlugin());
202:                 }
203:         }
204:
205:         /**
206:          * Tests the behavior of {@link AcceleoCommonPlugin#log(String, boolean)} with a valid message to be
207:          * logged. Expects a new entry to be logged with the given severity and message.
208:          */
209:         @Test
210:         public void testLogMessageValidMessage() {
211:                 boolean blocker = false;
212:•                for (String message : ERROR_MESSAGES) {
213:                         final PrintStream systemErr = System.err;
214:                         // disables standard error to avoid all logged exception to be displayed in console.
215:                         System.setErr(temporaryErr);
216:                         AcceleoLogger.log(message, blocker);
217:                         System.setErr(systemErr);
218:
219:                         final int expectedSeverity;
220:•                        if (blocker) {
221:                                 expectedSeverity = IStatus.ERROR;
222:                         } else {
223:                                 expectedSeverity = IStatus.WARNING;
224:                         }
225:•                        blocker = !blocker;
226:
227:                         assertEquals("Unexpected message logged.", message, loggedStatus.getMessage());
228:                         assertEquals("Unexpected severity of the logged message.", expectedSeverity, loggedStatus
229:                                         .getSeverity());
230:                         assertEquals("Message logged with unexpected plug-in ID.", AcceleoCommonPlugin.PLUGIN_ID,
231:                                         loggedStatus.getPlugin());
232:                 }
233:         }
234:
235:         /**
236:          * Tests the behavior of {@link AcceleoCommonPlugin#log(IStatus)} with <code>null</code> as the status to
237:          * be logged. Expects a {@link NullPointerException} to be thrown with the given status' error message.
238:          */
239:         @Test
240:         public void testLogStatusNullStatus() {
241:                 try {
242:                         AcceleoLogger.log(null);
243:                         fail("Logging null status didn't throw expected NullPointerException.");
244:                 } catch (NullPointerException e) {
245:                         // This ws expected behavior
246:                 }
247:         }
248:
249:         /**
250:          * Tests the behavior of {@link AcceleoCommonPlugin#log(IStatus)} with a valid status to be logged.
251:          * Expects the status to be logged with the specified severity, error message and source plugin.
252:          */
253:         @Test
254:         public void testLogStatusValidStatus() {
255:•                for (int severity : ERROR_SEVERITIES) {
256:•                        for (String message : ERROR_MESSAGES) {
257:                                 final Status status = new Status(severity, AcceleoCommonTestPlugin.PLUGIN_ID, message);
258:                                 final PrintStream systemErr = System.err;
259:                                 // disables standard error to avoid all logged exception to be displayed in console.
260:                                 System.setErr(temporaryErr);
261:                                 AcceleoLogger.log(status);
262:                                 System.setErr(systemErr);
263:
264:                                 assertEquals("Unexpected message of the logged exception.", message, loggedStatus
265:                                                 .getMessage());
266:                                 assertEquals("Unexpected severity of the logged exception.", severity, loggedStatus
267:                                                 .getSeverity());
268:                                 assertEquals("Exception logged with unexpected plug-in ID.",
269:                                                 AcceleoCommonTestPlugin.PLUGIN_ID, loggedStatus.getPlugin());
270:                         }
271:                 }
272:         }
273:
274:         /**
275:          * {@inheritDoc}
276:          *
277:          * @see junit.framework.TestCase#setUp()
278:          */
279:         @Before
280:         public void setUp() {
281:                 // Creates a log listener that will update the field loggedStatus as needed
282:                 logListener = new ILogListener() {
283:                         public void logging(IStatus status, String message) {
284:                                 loggedStatus = status;
285:                         }
286:                 };
287:                 // Then sets it to listen to the log
288:                 AcceleoCommonPlugin.getDefault().getLog().addLogListener(logListener);
289:
290:                 try {
291:                         // Creates temporary error log
292:                         final File dataDir = new File(FileLocator.toFileURL(
293:                                         AcceleoCommonTestPlugin.getDefault().getBundle().getEntry("/data")).getFile());
294:                         temporaryLog = new File(dataDir.getAbsolutePath() + "/testLogErrorLog");
295:                         temporaryErr = new PrintStream(temporaryLog);
296:                 } catch (IOException e) {
297:                         fail("Couldn't create temporary error log.");
298:                 }
299:         }
300:
301:         /**
302:          * {@inheritDoc}
303:          *
304:          * @see junit.framework.TestCase#tearDown()
305:          */
306:         @After
307:         public void tearDown() {
308:                 AcceleoCommonPlugin.getDefault().getLog().removeLogListener(logListener);
309:•                if (temporaryErr != null) {
310:                         temporaryErr.close();
311:                 }
312:•                if (temporaryLog.exists()) {
313:                         temporaryLog.delete();
314:                 }
315:         }
316: }