001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * PolicyLoggerTest.java
039: * JUnit based test
040: *
041: * Created on February 21, 2007, 4:19 PM
042: */
043:
044: package com.sun.xml.ws.policy.privateutil;
045:
046: import com.sun.xml.ws.policy.PolicyException;
047: import junit.framework.*;
048: import java.util.logging.Level;
049: import static com.sun.xml.ws.policy.privateutil.PolicyUtils.Commons.getCallerMethodName;
050:
051: /**
052: *
053: * @author Marek Potociar (marek.potociar at sun.com)
054: */
055: public class PolicyLoggerTest extends TestCase {
056: private PolicyLogger instance;
057:
058: public PolicyLoggerTest(String testName) {
059: super (testName);
060: }
061:
062: protected void setUp() throws Exception {
063: instance = PolicyLogger.getLogger(PolicyLoggerTest.class);
064: }
065:
066: protected void tearDown() throws Exception {
067: }
068:
069: /**
070: * Test of getLogger method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
071: */
072: public void testGetLogger() {
073: PolicyLogger result = PolicyLogger
074: .getLogger(PolicyLoggerTest.class);
075: assertNotNull(result);
076:
077: try {
078: PolicyLogger.getLogger(null);
079: fail("NullPointerException expected");
080: } catch (NullPointerException e) { /* ok */
081: }
082: }
083:
084: /**
085: * Test of log method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
086: */
087: public void testLog() {
088: Level level = Level.FINEST;
089: String message = "Test";
090:
091: instance.log(level, message);
092: }
093:
094: /**
095: * Test of finest method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
096: */
097: public void testFinest() {
098: String message = "Test";
099:
100: instance.finest(message);
101: }
102:
103: /**
104: * Test of finer method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
105: */
106: public void testFiner() {
107: String message = "Test";
108:
109: instance.finer(message);
110: }
111:
112: /**
113: * Test of fine method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
114: */
115: public void testFine() {
116: String message = "Test";
117:
118: instance.fine(message);
119: }
120:
121: /**
122: * Test of info method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
123: */
124: public void testInfo() {
125: String message = "Test";
126:
127: instance.info(message);
128: }
129:
130: /**
131: * Test of config method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
132: */
133: public void testConfig() {
134: String message = "Test";
135:
136: instance.config(message);
137: }
138:
139: /**
140: * Test of warning method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
141: */
142: public void testWarning() {
143: String message = "Test";
144:
145: instance.warning(message);
146: }
147:
148: /**
149: * Test of severe method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
150: */
151: public void testSevere() {
152: String message = "Test";
153:
154: instance.severe(message);
155: }
156:
157: /**
158: * Test of isMethodCallLoggable method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
159: */
160: public void testIsMethodCallLoggable() {
161: boolean expResult = true;
162: instance.setLevel(Level.FINEST);
163: boolean result = instance.isMethodCallLoggable();
164: assertEquals(expResult, result);
165:
166: expResult = false;
167: instance.setLevel(Level.FINER);
168: result = instance.isMethodCallLoggable();
169: assertEquals(expResult, result);
170: }
171:
172: /**
173: * Test of isLoggable method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
174: */
175: public void testIsLoggable() {
176: boolean expResult, result;
177:
178: instance.setLevel(Level.ALL);
179: expResult = true;
180: result = instance.isLoggable(Level.FINEST);
181: assertEquals(expResult, result);
182: result = instance.isLoggable(Level.FINER);
183: assertEquals(expResult, result);
184: result = instance.isLoggable(Level.FINE);
185: assertEquals(expResult, result);
186: result = instance.isLoggable(Level.CONFIG);
187: assertEquals(expResult, result);
188: result = instance.isLoggable(Level.INFO);
189: assertEquals(expResult, result);
190: result = instance.isLoggable(Level.WARNING);
191: assertEquals(expResult, result);
192: result = instance.isLoggable(Level.SEVERE);
193: assertEquals(expResult, result);
194: result = instance.isLoggable(Level.ALL);
195: assertEquals(expResult, result);
196:
197: instance.setLevel(Level.OFF);
198: expResult = false;
199: result = instance.isLoggable(Level.FINEST);
200: assertEquals(expResult, result);
201: result = instance.isLoggable(Level.FINER);
202: assertEquals(expResult, result);
203: result = instance.isLoggable(Level.FINE);
204: assertEquals(expResult, result);
205: result = instance.isLoggable(Level.CONFIG);
206: assertEquals(expResult, result);
207: result = instance.isLoggable(Level.INFO);
208: assertEquals(expResult, result);
209: result = instance.isLoggable(Level.WARNING);
210: assertEquals(expResult, result);
211: result = instance.isLoggable(Level.SEVERE);
212: assertEquals(expResult, result);
213: result = instance.isLoggable(Level.ALL);
214: assertEquals(expResult, result);
215: }
216:
217: /**
218: * Test of setLevel method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
219: */
220: public void testSetLevel() {
221: instance.setLevel(Level.FINE);
222: assertFalse(instance.isLoggable(Level.FINER));
223: assertTrue(instance.isLoggable(Level.INFO));
224: }
225:
226: /**
227: * Test of entering method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
228: */
229: public void testEntering() {
230: instance.entering();
231: }
232:
233: /**
234: * Test of exiting method, of class com.sun.xml.ws.policy.privateutil.PolicyLogger.
235: */
236: public void testExiting() {
237: instance.exiting();
238: }
239:
240: /**
241: * Test of createAndLogException method, of class com.sun.xml.ws.policy.privateutil.PolicyUtils.Commons.
242: */
243: public void testCommonsCreateAndLogException() {
244: PolicyLogger logger = PolicyLogger
245: .getLogger(PolicyLoggerTest.class);
246: Throwable cause, result;
247: String message;
248:
249: cause = new Exception();
250: message = "Test message.";
251: result = logger.logSevereException(
252: new IllegalArgumentException(message), cause);
253: assertEquals(message, result.getMessage());
254: assertEquals(cause, result.getCause());
255: assertEquals("testCommonsCreateAndLogException", result
256: .getStackTrace()[0].getMethodName());
257:
258: message = "Test message.";
259: result = logger.logSevereException(new NullPointerException(
260: message), cause);
261: assertEquals(message, result.getMessage());
262: assertEquals(cause, result.getCause());
263: assertEquals("testCommonsCreateAndLogException", result
264: .getStackTrace()[0].getMethodName());
265:
266: message = null;
267: result = logger.logSevereException(
268: new PolicyException(message), true);
269: assertEquals(message, result.getMessage());
270: assertEquals(null, result.getCause());
271: assertEquals("testCommonsCreateAndLogException", result
272: .getStackTrace()[0].getMethodName());
273:
274: message = "Test message.";
275: result = logger.logSevereException(
276: new PolicyException(message), false);
277: assertEquals(message, result.getMessage());
278: assertEquals(null, result.getCause());
279: assertEquals("testCommonsCreateAndLogException", result
280: .getStackTrace()[0].getMethodName());
281:
282: cause = new NullPointerException("test");
283: message = null;
284: result = logger.logSevereException(new PolicyException(message,
285: cause), true);
286: assertEquals(message, result.getMessage());
287: assertEquals(cause, result.getCause());
288: assertEquals("testCommonsCreateAndLogException", result
289: .getStackTrace()[0].getMethodName());
290:
291: cause = new NullPointerException("test");
292: message = "Test message.";
293: result = logger.logSevereException(new PolicyException(message,
294: cause), false);
295: assertEquals(message, result.getMessage());
296: assertEquals(cause, result.getCause());
297: assertEquals("testCommonsCreateAndLogException", result
298: .getStackTrace()[0].getMethodName());
299:
300: message = null;
301: result = logger
302: .logSevereException(new PolicyException(message));
303: assertEquals(message, result.getMessage());
304: assertEquals(null, result.getCause());
305: assertEquals("testCommonsCreateAndLogException", result
306: .getStackTrace()[0].getMethodName());
307:
308: message = "Test message.";
309: result = logger
310: .logSevereException(new PolicyException(message));
311: assertEquals(message, result.getMessage());
312: assertEquals(null, result.getCause());
313: assertEquals("testCommonsCreateAndLogException", result
314: .getStackTrace()[0].getMethodName());
315:
316: cause = new NullPointerException("test");
317: message = null;
318: result = logger.logSevereException(new PolicyException(message,
319: cause));
320: assertEquals(message, result.getMessage());
321: assertEquals(cause, result.getCause());
322: assertEquals("testCommonsCreateAndLogException", result
323: .getStackTrace()[0].getMethodName());
324:
325: cause = new NullPointerException("test");
326: message = "Test message.";
327: result = logger.logSevereException(new PolicyException(message,
328: cause));
329: assertEquals(message, result.getMessage());
330: assertEquals(cause, result.getCause());
331: assertEquals("testCommonsCreateAndLogException", result
332: .getStackTrace()[0].getMethodName());
333: }
334: }
|