ceki 2004/04/21 02:45:10
Modified: tests/src/java/org/apache/log4j/util Filter.java
tests/witness customLogger.1
tests/src/java/org/apache/log4j/scheduler SchedulerTest.java
tests/src/java/org/apache/log4j/customLogger
XLoggerTestCase.java
Log:
Added a new JUnit filter in XLoggerTestCase.common method in order
to solve execution problems on a particular platform running Gump.
Revision Changes Path
1.6 +26 -15 logging-log4j/tests/src/java/org/apache/log4j/util/Filter.java
Index: Filter.java
===================================================================
RCS file: /home/cvs/logging-log4j/tests/src/java/org/apache/log4j/util/Filter.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Filter.java 25 Apr 2002 21:17:36 -0000 1.5
+++ Filter.java 21 Apr 2004 09:45:09 -0000 1.6
@@ -1,28 +1,39 @@
/*
- * Copyright (C) The Apache Software Foundation. All rights reserved.
+ * Copyright 1999,2004 The Apache Software Foundation.
*
- * This software is published under the terms of the Apache Software
- * License version 1.1, a copy of which has been included with this
- * distribution in the LICENSE.txt file. */
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package org.apache.log4j.util;
public interface Filter {
-
- final String BASIC_PAT = "\\[main\\] (FATAL|ERROR|WARN|INFO|DEBUG)";
- final String ISO8601_PAT = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}";
-
// 06 avr. 2002 18:36:32,036
// 18 fevr. 2002 20:05:36,222
- static public final String ABSOLUTE_DATE_AND_TIME_PAT =
- "^\\d{1,2} .{2,6}\\.? 200\\d
\\d{2}:\\d{2}:\\d{2},\\d{3}";
+ public static final String ABSOLUTE_DATE_AND_TIME_PAT =
+ "^\\d{1,2} .{2,6}\\.? 200\\d \\d{2}:\\d{2}:\\d{2},\\d{3}";
// 18:54:19,201
- static public final String ABSOLUTE_TIME_PAT =
- "^\\d{2}:\\d{2}:\\d{2},\\d{3}";
-
- static public final String RELATIVE_TIME_PAT = "^\\d{1,10}";
-
+ public static final String ABSOLUTE_TIME_PAT =
+ "^\\d{2}:\\d{2}:\\d{2},\\d{3}";
+ public static final String RELATIVE_TIME_PAT = "^\\d{1,10}";
+ final String BASIC_PAT = "\\[main\\] (FATAL|ERROR|WARN|INFO|DEBUG)";
+ final String ISO8601_PAT =
+ "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}";
+ /**
+ * This filter transforms the input string and returns the results as
+ * output. If the input should be ignored, this method returns null.
+ *
+ */
String filter(String in) throws UnexpectedFormatException;
}
1.4 +1 -3 logging-log4j/tests/witness/customLogger.1
Index: customLogger.1
===================================================================
RCS file: /home/cvs/logging-log4j/tests/witness/customLogger.1,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- customLogger.1 14 Nov 2002 08:48:40 -0000 1.3
+++ customLogger.1 21 Apr 2004 09:45:09 -0000 1.4
@@ -15,6 +15,4 @@
at junit.framework.TestResult.run(X)
at junit.framework.TestCase.run(X)
at junit.framework.TestSuite.runTest(X)
- at junit.framework.TestSuite.run(X)
- at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(X)
- at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(X)
+ at junit.framework.TestSuite.run(X)
\ No newline at end of file
1.3 +148 -35
logging-log4j/tests/src/java/org/apache/log4j/scheduler/SchedulerTest.java
Index: SchedulerTest.java
===================================================================
RCS file:
/home/cvs/logging-log4j/tests/src/java/org/apache/log4j/scheduler/SchedulerTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SchedulerTest.java 20 Apr 2004 18:47:59 -0000 1.2
+++ SchedulerTest.java 21 Apr 2004 09:45:09 -0000 1.3
@@ -33,7 +33,7 @@
static final long TOLERATED_GAP = 1000;
Random random = new Random(480361007);
final long START_TIME = System.currentTimeMillis();
-
+
public SchedulerTest(String arg0) {
super(arg0);
}
@@ -126,10 +126,12 @@
if (shouldDelete() && !jobVector.isEmpty()) {
int indexToDelete = getRandomIndexToDelete(jobVector.size());
CountingJob j = (CountingJob) jobVector.remove(indexToDelete);
+
if (j.count == 0) {
scheduler.delete(j);
deletedVector.add(j);
- if(j.count == 1) {
+
+ if (j.count == 1) {
fail("Error in the test code itself.");
}
}
@@ -140,26 +142,83 @@
scheduler.schedule(cj, expected);
}
}
+
long loopEnd = System.currentTimeMillis();
sleep(TEST_DURATION - (loopEnd - start) + 2000);
- if (deletedVector.size() > (MAX_OPS/2)) {
+ if (deletedVector.size() > (MAX_OPS / 2)) {
fail("too many deleted jobs: " + deletedVector.size());
}
- if (jobVector.size() < (MAX_OPS/2)) {
+
+ if (jobVector.size() < (MAX_OPS / 2)) {
fail("too few jobs: " + jobVector.size());
}
-
+
for (Iterator i = jobVector.iterator(); i.hasNext();) {
CountingJob cj = (CountingJob) i.next();
assertEquals(1, cj.count);
}
+
for (Iterator i = deletedVector.iterator(); i.hasNext();) {
CountingJob cj = (CountingJob) i.next();
assertEquals(0, cj.count);
}
}
+ public void testPeriodic() {
+ Scheduler scheduler = new Scheduler();
+ scheduler.start();
+
+ long now = System.currentTimeMillis();
+ long firstOn = now;
+ long period = 100;
+ PeriodicJob pj = new PeriodicJob(0, firstOn, period);
+ scheduler.schedule(pj, firstOn, period);
+
+ int NUM_PERIODS = 10;
+ sleep(period * NUM_PERIODS);
+
+ scheduler.shutdown();
+
+ if (pj.count < 10) {
+ fail(
+ "Periodic job executed only " + pj.count
+ + " times. Expected at least"+NUM_PERIODS);
+ }
+ pj.checkPeriods();
+ }
+
+ public void testMultiplePeriodic() {
+ Vector jobs = new Vector();
+ Scheduler scheduler = new Scheduler();
+ scheduler.start();
+
+ long now = System.currentTimeMillis();
+ long period = 100;
+ long runLen = 10;
+
+ for (int i = 0; i < runLen; i++) {
+ long firstOn = now + i;
+ PeriodicJob pj = new PeriodicJob(i, firstOn, period);
+ scheduler.schedule(pj, firstOn, period);
+ jobs.add(pj);
+ }
+
+ int NUM_PERIODS = 10;
+ sleep(period * NUM_PERIODS);
+ scheduler.shutdown();
+
+ for (int i = 0; i < runLen; i++) {
+ PeriodicJob pj = (PeriodicJob) jobs.get(i);
+ if (pj.count < NUM_PERIODS) {
+ fail(
+ "Periodic job executed only " + pj.count
+ + " times. Expected at least "+NUM_PERIODS);
+ }
+ pj.checkPeriods();
+ }
+ }
+
boolean shouldDelete() {
int r = random.nextInt(2);
@@ -172,12 +231,13 @@
// On average, make the index of 1 out of 5 deletes zero
int getRandomIndexToDelete(int range) {
- int r = random.nextInt(5);
- if(r == 0) {
- return 0;
- } else {
- return random.nextInt(range);
- }
+ int r = random.nextInt(5);
+
+ if (r == 0) {
+ return 0;
+ } else {
+ return random.nextInt(range);
+ }
}
void sleep(long duration) {
@@ -189,38 +249,91 @@
public static Test suite() {
TestSuite suite = new TestSuite();
- suite.addTest(new SchedulerTest("testRandom"));
+
+ //suite.addTest(new SchedulerTest("testRandom"));
+ //suite.addTest(new SchedulerTest("testPeriodic"));
+ suite.addTest(new SchedulerTest("testMultiplePeriodic"));
return suite;
}
-
-
-
- class CountingJob implements Job {
- int count = 0;
- int id;
- long scheduledTime;
- CountingJob(int id, long scheduledTime) {
- this.id = id;
- this.scheduledTime = scheduledTime;
- }
+ class CountingJob implements Job {
+ int count = 0;
+ int id;
+ long scheduledTime;
+
+ CountingJob(int id, long scheduledTime) {
+ this.id = id;
+ this.scheduledTime = scheduledTime;
+ }
- public void execute() {
- long now = System.currentTimeMillis();
- count++;
- if (now < scheduledTime) {
- throw new IllegalStateException("Job executed too early.");
- } else if ((now - scheduledTime) > SchedulerTest.TOLERATED_GAP) {
- String msg =
- "Job id "+id+" executed " + (now - scheduledTime) + " too late "+
- "diff "+(scheduledTime-SchedulerTest.this.START_TIME);
+ public void execute() {
+ long now = System.currentTimeMillis();
+ count++;
+
+ if (now < scheduledTime) {
+ throw new IllegalStateException("Job executed too early.");
+ } else if ((now - scheduledTime) > SchedulerTest.TOLERATED_GAP) {
+ String msg =
+ "Job id " + id + " executed " + (now - scheduledTime) + " too late "
+ + "diff " + (scheduledTime - SchedulerTest.this.START_TIME);
System.out.println(msg);
- throw new IllegalStateException(msg);
+ throw new IllegalStateException(msg);
+ }
}
}
-}
-}
+ class PeriodicJob implements Job {
+ int count = 0;
+ int id;
+ long period;
+ Vector desiredTimeVector;
+ Vector actualExecutionTime;
+
+ PeriodicJob(int id, long desiredExecutionTime, long period) {
+ this.id = id;
+ this.period = period;
+ actualExecutionTime = new Vector();
+ desiredTimeVector = new Vector();
+ desiredTimeVector.add(new Long(desiredExecutionTime));
+ }
+ public void execute() {
+ long now = System.currentTimeMillis();
+ count++;
+ System.out.println(id+" - execute called: count" + count + ", now=" + now);
+
+ long lastDesiredTime =
+ ((Long) desiredTimeVector.lastElement()).longValue();
+ desiredTimeVector.add(new Long(lastDesiredTime + period));
+ actualExecutionTime.add(new Long(now));
+
+ if (now < lastDesiredTime) {
+ throw new IllegalStateException("Job executed too early.");
+ } else if ((now - lastDesiredTime) > SchedulerTest.TOLERATED_GAP) {
+ String msg =
+ "Job id " + id + " executed " + (now - lastDesiredTime)
+ + " too late ";
+ System.out.println(msg);
+ throw new IllegalStateException(msg);
+ }
+ }
+ void checkPeriods() {
+ for (int i = 0; i < actualExecutionTime.size(); i++) {
+ long actual = ((Long) actualExecutionTime.get(i)).longValue();
+ long desired = ((Long) desiredTimeVector.get(i)).longValue();
+
+ if (actual < desired) {
+ throw new IllegalStateException("Job executed too early.");
+ } else if (
+ (actual - desired) > (SchedulerTest.TOLERATED_GAP * (i + 1))) {
+ String msg =
+ "Job id " + id + " executed " + (actual - desired) + " too late ";
+ System.out.println(msg);
+ throw new IllegalStateException(msg);
+ }
+ }
+ }
+ }
+}
1.5 +37 -20
logging-log4j/tests/src/java/org/apache/log4j/customLogger/XLoggerTestCase.java
Index: XLoggerTestCase.java
===================================================================
RCS file:
/home/cvs/logging-log4j/tests/src/java/org/apache/log4j/customLogger/XLoggerTestCase.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- XLoggerTestCase.java 13 May 2003 16:33:16 -0000 1.4
+++ XLoggerTestCase.java 21 Apr 2004 09:45:10 -0000 1.5
@@ -1,31 +1,40 @@
/*
- * Copyright (C) The Apache Software Foundation. All rights reserved.
+ * Copyright 1999,2004 The Apache Software Foundation.
*
- * This software is published under the terms of the Apache Software
- * License version 1.1, a copy of which has been included with this
- * distribution in the LICENSE.txt file. */
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package org.apache.log4j.customLogger;
-import org.apache.log4j.xml.DOMConfigurator;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
import org.apache.log4j.Logger;
import org.apache.log4j.util.*;
+import org.apache.log4j.xml.DOMConfigurator;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import junit.framework.Test;
/**
Tests handling of custom loggers.
-
+
@author Ceki Gülcü
*/
public class XLoggerTestCase extends TestCase {
-
static String FILTERED = "output/filtered";
static XLogger logger = (XLogger) XLogger.getLogger(XLoggerTestCase.class);
- public XLoggerTestCase(String name){
+ public XLoggerTestCase(String name) {
super(name);
}
@@ -33,28 +42,36 @@
logger.getLoggerRepository().resetConfiguration();
}
- public void test1() throws Exception { common(1); }
- public void test2() throws Exception { common(2); }
+ public void test1() throws Exception {
+ common(1);
+ }
+
+ public void test2() throws Exception {
+ common(2);
+ }
void common(int number) throws Exception {
- DOMConfigurator.configure("input/xml/customLogger"+number+".xml");
+ DOMConfigurator.configure("input/xml/customLogger" + number + ".xml");
int i = -1;
Logger root = Logger.getRootLogger();
logger.trace("Message " + ++i);
logger.debug("Message " + ++i);
- logger.warn ("Message " + ++i);
+ logger.warn("Message " + ++i);
logger.error("Message " + ++i);
logger.fatal("Message " + ++i);
+
Exception e = new Exception("Just testing");
logger.debug("Message " + ++i, e);
- Transformer.transform("output/temp", FILTERED, new Filter[] {
- new LineNumberFilter(),
- new SunReflectFilter()});
- assertTrue(Compare.compare(FILTERED, "witness/customLogger."+number));
-
+ Transformer.transform(
+ "output/temp", FILTERED,
+ new Filter[] {
+ new LineNumberFilter(), new SunReflectFilter(),
+ new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/customLogger." + number));
}
public static Test suite() {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]