001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003:
004: package fitnesse.components;
005:
006: import java.io.BufferedReader;
007: import java.io.File;
008: import java.io.FileReader;
009: import java.text.DateFormat;
010: import java.text.SimpleDateFormat;
011: import java.util.Calendar;
012: import java.util.GregorianCalendar;
013: import java.util.TimeZone;
014:
015: import junit.framework.TestCase;
016: import fitnesse.util.FileUtil;
017:
018: public class LoggerTest extends TestCase {
019: private final String dirPath = "testLogs";
020:
021: private Logger l;
022:
023: private LogData ld;
024:
025: private String filename = "fitnesse20030306134205.log";
026:
027: private String logLine = "myHost - - [06/Mar/2003:13:42:05 -0100] \"request\" 42 666";
028:
029: public void setUp() throws Exception {
030: l = new Logger(dirPath);
031: ld = new LogData();
032: ld.host = "myHost";
033: ld.requestLine = "request";
034: ld.size = 666;
035: ld.status = 42;
036: TimeZone z = TimeZone.getTimeZone("GMT-1:00");
037: ld.time = new GregorianCalendar(2003, 2, 6, 13, 42, 5);
038: ld.time.setTimeZone(z);
039: }
040:
041: public void tearDown() throws Exception {
042: l.close();
043: FileUtil.deleteFileSystemDirectory(dirPath);
044: }
045:
046: public void testTimeZoneHandling() {
047: // let's figure out how this stuff works...
048: Calendar calendar = new GregorianCalendar(2003, 0, 2, 3, 4, 5);
049: calendar.setTimeZone(TimeZone.getTimeZone("GMT+2"));
050:
051: DateFormat format = new SimpleDateFormat(
052: "MMM dd, yyyy hh:mm:ss Z");
053:
054: DateFormat format2 = (DateFormat) format.clone();
055: format2.setTimeZone(calendar.getTimeZone());
056:
057: assertEquals("Jan 02, 2003 03:04:05 +0200", format2
058: .format(calendar.getTime()));
059: }
060:
061: public void testConstruction() throws Exception {
062: assertEquals(dirPath, l.getDirectory().getName());
063: File dir = new File(dirPath);
064: assertEquals(true, dir.exists());
065: assertEquals(true, dir.isDirectory());
066: }
067:
068: public void testLogFormat() throws Exception {
069: String line = l.formatLogLine(ld);
070: assertEquals(logLine, line);
071: }
072:
073: public void testLogFileName() throws Exception {
074: String logName = Logger.makeLogFileName(ld.time);
075: assertEquals(filename, logName);
076: }
077:
078: public void testLoggingOneLineInNewFile() throws Exception {
079: l.log(ld);
080: l.close();
081: File dir = l.getDirectory();
082: File file = new File(dir, filename);
083: assertTrue(file.exists());
084: String contents = FileUtil.getFileContent(file);
085: assertEquals(logLine + System.getProperty("line.separator"),
086: contents);
087: }
088:
089: public void testLogSecondLineInSameFile() throws Exception {
090: l.log(ld);
091: LogData ld2 = (LogData) ld.clone();
092: ld2.host = "newHost";
093: l.log(ld2);
094: File dir = l.getDirectory();
095: File file = new File(dir, filename);
096: BufferedReader br = new BufferedReader(new FileReader(file));
097: assertEquals(logLine, br.readLine());
098: assertEquals(
099: "newHost - - [06/Mar/2003:13:42:05 -0100] \"request\" 42 666",
100: br.readLine());
101: assertTrue(br.readLine() == null);
102: br.close();
103: }
104:
105: public void testLogLineInNewFile() throws Exception {
106: LogData nextDay = (LogData) ld.clone();
107: nextDay.time.add(Calendar.DATE, 1);
108: l.log(ld);
109: l.log(nextDay);
110: l.close();
111: File firstFile = getLogFileFor(ld);
112: File secondFile = getLogFileFor(nextDay);
113: assertTrue(firstFile.exists());
114: assertTrue(secondFile.exists());
115: String firstContent = FileUtil.getFileContent(firstFile);
116: assertEquals(l.formatLogLine(ld)
117: + System.getProperty("line.separator"), firstContent);
118: String secondContent = FileUtil.getFileContent(secondFile);
119: assertEquals(l.formatLogLine(nextDay)
120: + System.getProperty("line.separator"), secondContent);
121: }
122:
123: private File getLogFileFor(LogData data) {
124: return new File(l.getDirectory(), Logger
125: .makeLogFileName(data.time));
126: }
127: }
|