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
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.junit;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.IOException;
047: import java.io.PrintStream;
048: import java.util.ResourceBundle;
049: import java.util.logging.Level;
050: import java.util.logging.LogRecord;
051: import java.util.logging.Logger;
052: import junit.framework.TestFailure;
053: import junit.framework.TestResult;
054:
055: /** Checks that we can do proper logging.
056: *
057: * @author Jaroslav Tulach
058: */
059: public class LoggingTest extends NbTestCase {
060: private Throwable toThrow;
061: private Throwable toMsg;
062:
063: public LoggingTest(String testName) {
064: super (testName);
065: }
066:
067: public static NbTestSuite suite() {
068: // NbTestSuite suite = new NbTestSuite();
069: // // you can define order of test cases here
070: // suite.addTest(new LoggingTest("testLogFileName"));
071: // suite.addTest(new LoggingTest("testLogFileNameEqualsToNameOfTest"));
072: // suite.addTest(new LoggingTest("testTrimLogFiles"));
073:
074: NbTestSuite suite = new NbTestSuite(LoggingTest.class);
075: return suite;
076: }
077:
078: protected void setUp() throws Exception {
079: clearWorkDir();
080: }
081:
082: /** Used in testIOExceptionIsWrappedWithLogMsg. */
083: public void throwIOThrowable() throws Throwable {
084: Logger log = Logger.getLogger(getName());
085: if (toMsg != null) {
086: log.log(Level.WARNING, "Msg", toMsg);
087: }
088: log.warning("Going to throw: " + toThrow);
089: throw toThrow;
090: }
091:
092: /** Used in testMyExceptionIsWrappedWithLogMsg. It has to have different
093: * name because it cannot delete log file on 64 bit machine. */
094: public void throwMyThrowable() throws Throwable {
095: throwIOThrowable();
096: }
097:
098: public void testLogFileName() throws Exception {
099: PrintStream ps = getLog("ahoj");
100: File f = new File(getWorkDir(), "ahoj");
101: assertEquals("Log file exists", true, f.exists());
102: }
103:
104: public void testLogFileNameEqualsToNameOfTest() throws Exception {
105: PrintStream ps = getLog();
106: File f = new File(getWorkDir(), getName() + ".log");
107: assertEquals("Log file exists", true, f.exists());
108: }
109:
110: /** Test of NbTestCase#trimLogFiles method. It should trim size of all files
111: * in test case workdir to 1 MB. The method is called at the end of
112: * NbTestCase#run method.
113: */
114: public void testTrimLogFiles() throws IOException {
115: StringBuffer buff = new StringBuffer(1024);
116: for (int i = 0; i < 1024; i++) {
117: buff.append('A');
118: }
119: String string1kB = buff.toString();
120: for (int i = 0; i < 2024; i++) {
121: log(string1kB);
122: log("myLog", string1kB);
123: ref(string1kB);
124: }
125:
126: File trimmedDir = getWorkDir();
127: String[] filenames = { "testTrimLogFiles.log",
128: "testTrimLogFiles.ref", "myLog" };
129: for (int i = 0; i < filenames.length; i++) {
130: File file = new File(trimmedDir, "TRIMMED_" + filenames[i]);
131: assertTrue(file.getName() + " not exists.", file.exists());
132: assertTrue(file.getName() + " not trimmed to 1 MB.", file
133: .length() < 2097152L);
134: file = new File(trimmedDir, filenames[i]);
135: if (file.exists()) {
136: // original file exists only if cannot be deleted. Then it has minimal size.
137: assertTrue(file.getName() + " not trimmed."
138: + file.length(), file.length() < 1024 * 1024);
139: }
140: }
141: }
142:
143: protected Level logLevel() {
144: return Level.WARNING;
145: }
146:
147: public void testLoggingUtil() throws Exception {
148: doLoggingUtil(false);
149: }
150:
151: public void testLoggingUtilWithCleanOfUserDir() throws Exception {
152: doLoggingUtil(true);
153: }
154:
155: private void doLoggingUtil(boolean clean) throws Exception {
156: Logger log = Logger.getLogger(getName());
157:
158: log.log(Level.SEVERE, "Ahoj");
159:
160: if (clean) {
161: clearWorkDir();
162: log.log(Level.SEVERE, "Ahojky");
163: }
164:
165: log.log(Level.FINE, "Jardo");
166:
167: File f = new File(getWorkDir(), getName() + ".log");
168: assertEquals("Log file exists", true, f.exists());
169:
170: String s = readFile(f);
171: if (s.indexOf("Ahoj") == -1) {
172: fail("There should be Ahoj\n" + s);
173: }
174: assertEquals("Not logged for FINE: " + s, -1, s
175: .indexOf("Jardo"));
176: }
177:
178: static String readFile(File f) throws IOException {
179: byte[] arr = new byte[(int) f.length()];
180: FileInputStream is = new FileInputStream(f);
181: int l = is.read(arr);
182: assertEquals(l, arr.length);
183:
184: String s = new String(arr);
185: return s;
186: }
187:
188: public void testFmting() throws Exception {
189: Logger log = Logger.getLogger(getName());
190:
191: LogRecord rec = new LogRecord(Level.SEVERE, "LOG_SevereMsg");
192: rec.setResourceBundle(ResourceBundle
193: .getBundle("org.netbeans.junit.TestBundle"));
194: rec.setParameters(new Object[] { "Very" });
195: log.log(rec);
196:
197: File f = new File(getWorkDir(), getName() + ".log");
198: assertEquals("Log file exists", true, f.exists());
199:
200: byte[] arr = new byte[(int) f.length()];
201: FileInputStream is = new FileInputStream(f);
202: int l = is.read(arr);
203: assertEquals(l, arr.length);
204:
205: String s = new String(arr);
206: if (s.indexOf("Important message Very") == -1) {
207: fail("There should the message\n" + s);
208: }
209: }
210:
211: public void testIOExceptionIsWrappedWithLogMsg() throws Exception {
212:
213: LoggingTest inner = new LoggingTest("throwIOThrowable");
214: inner.toThrow = new IOException("Ahoj");
215:
216: TestResult res = inner.run();
217: assertEquals("One error", 1, res.errorCount());
218: assertEquals("No failure", 0, res.failureCount());
219:
220: TestFailure f = (TestFailure) res.errors().nextElement();
221:
222: if (f.exceptionMessage().indexOf("Going to throw") == -1) {
223: fail("There should be output of the log:\n"
224: + f.exceptionMessage());
225: }
226: }
227:
228: public void testMyExceptionIsWrappedWithLogMsg() throws Exception {
229: LoggingTest inner = new LoggingTest("throwMyThrowable");
230:
231: class MyEx extends Exception {
232: }
233:
234: inner.toThrow = new MyEx();
235:
236: TestResult res = inner.run();
237: assertEquals("One error", 1, res.errorCount());
238: assertEquals("No failure", 0, res.failureCount());
239:
240: TestFailure f = (TestFailure) res.errors().nextElement();
241:
242: if (f.exceptionMessage() == null
243: || f.exceptionMessage().indexOf("Going to throw") == -1) {
244: fail("There should be output of the log:\n"
245: + f.exceptionMessage());
246: }
247: }
248:
249: public void testMyExceptionWithStackTrace() throws Exception {
250: LoggingTest inner = new LoggingTest("throwMyThrowable");
251:
252: class MyEx extends Exception {
253: }
254:
255: inner.toThrow = new MyEx();
256: inner.toMsg = new MyEx();
257:
258: TestResult res = inner.run();
259: assertEquals("One error", 1, res.errorCount());
260: assertEquals("No failure", 0, res.failureCount());
261:
262: TestFailure f = (TestFailure) res.errors().nextElement();
263:
264: if (f.exceptionMessage() == null
265: || f.exceptionMessage().indexOf("Going to throw") == -1
266: || f.exceptionMessage().indexOf(
267: "testMyExceptionWithStackTrace") == -1) {
268: fail("There should be output of the log:\n"
269: + f.exceptionMessage());
270: }
271: }
272: }
|