You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2008/05/21 18:07:18 UTC

svn commit: r658737 - in /logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule: LevelEqualsRuleTest.java LevelInequalityRuleTest.java TimestampEqualsRuleTest.java TimestampInequalityRuleTest.java

Author: carnold
Date: Wed May 21 09:07:18 2008
New Revision: 658737

URL: http://svn.apache.org/viewvc?rev=658737&view=rev
Log:
Bug 45029: Additional unit tests for rules

Added:
    logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/TimestampEqualsRuleTest.java
    logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/TimestampInequalityRuleTest.java
Modified:
    logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/LevelEqualsRuleTest.java
    logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/LevelInequalityRuleTest.java

Modified: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/LevelEqualsRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/LevelEqualsRuleTest.java?rev=658737&r1=658736&r2=658737&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/LevelEqualsRuleTest.java (original)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/LevelEqualsRuleTest.java Wed May 21 09:07:18 2008
@@ -75,7 +75,7 @@
     }
 
     /**
-     * Tests evaluate of a deserialized clone when levels are equal.
+     * Tests evaluate of a deserialized clone when levels are not equal.
      */
     public void test4() throws IOException, ClassNotFoundException {
         LevelEqualsRule rule = (LevelEqualsRule)

Modified: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/LevelInequalityRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/LevelInequalityRuleTest.java?rev=658737&r1=658736&r2=658737&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/LevelInequalityRuleTest.java (original)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/LevelInequalityRuleTest.java Wed May 21 09:07:18 2008
@@ -60,11 +60,11 @@
     }
 
     /**
-     * Tests evaluate of a deserialized clone when levels are equal.
+     * Tests evaluate of a deserialized clone when level satisfies rule.
      */
     public void test3() throws IOException, ClassNotFoundException {
-        LevelEqualsRule rule = (LevelEqualsRule)
-                SerializationTestHelper.serializeClone(LevelEqualsRule.getRule("info"));
+        Rule rule = (Rule)
+                SerializationTestHelper.serializeClone(LevelInequalityRule.getRule(">=", "info"));
         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
                 Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
                 "Hello, World", null);
@@ -72,22 +72,22 @@
     }
 
     /**
-     * Tests evaluate of a deserialized clone when levels are equal.
+     * Tests evaluate of a deserialized clone when level does not satisfy rule.
      */
     public void test4() throws IOException, ClassNotFoundException {
-        LevelEqualsRule rule = (LevelEqualsRule)
-                SerializationTestHelper.serializeClone(LevelEqualsRule.getRule("info"));
+        Rule rule = (Rule)
+                SerializationTestHelper.serializeClone(LevelInequalityRule.getRule("<", "info"));
         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
-                Logger.getRootLogger(), System.currentTimeMillis(), Level.WARN,
+                Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
                 "Hello, World", null);
         assertFalse(rule.evaluate(event));
     }
 
     /**
-     * Tests evaluate when levels are JDK 1.4 levels and equal.
+     * Tests evaluate when levels are JDK 1.4 levels and satisified.
      */
     public void test5() {
-        LevelEqualsRule rule = (LevelEqualsRule) LevelEqualsRule.getRule("finer");
+        Rule rule = (Rule) LevelInequalityRule.getRule(">=", "finer");
         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
                 Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINER,
                 "Hello, World", null);
@@ -98,19 +98,19 @@
      * Tests evaluate when levels are JDK 1.4 levels and not equal.
      */
     public void test6() {
-        LevelEqualsRule rule = (LevelEqualsRule) LevelEqualsRule.getRule("finer");
+        Rule rule = (Rule) LevelInequalityRule.getRule("<", "finer");
         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
-                Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINE,
+                Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINER,
                 "Hello, World", null);
         assertFalse(rule.evaluate(event));
     }
 
     /**
-     * Tests evaluate of a deserialized clone when levels are JDK 1.4 levels and equal.
+     * Tests evaluate of a deserialized clone when levels are JDK 1.4 levels and satisified.
      */
     public void test7() throws IOException, ClassNotFoundException {
-        LevelEqualsRule rule = (LevelEqualsRule)
-                SerializationTestHelper.serializeClone(LevelEqualsRule.getRule("finer"));
+        Rule rule = (Rule)
+                SerializationTestHelper.serializeClone(LevelInequalityRule.getRule(">=", "finer"));
         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
                 Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINER,
                 "Hello, World", null);
@@ -118,13 +118,13 @@
     }
 
     /**
-     * Tests evaluate of a deserialized clone when levels are JDK 1.4 levels and not equal.
+     * Tests evaluate of a deserialized clone when levels are JDK 1.4 levels and not satified.
      */
     public void test8() throws IOException, ClassNotFoundException {
-        LevelEqualsRule rule = (LevelEqualsRule)
-                SerializationTestHelper.serializeClone(LevelEqualsRule.getRule("finer"));
+        Rule rule = (Rule)
+                SerializationTestHelper.serializeClone(LevelInequalityRule.getRule("<", "finer"));
         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
-                Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINE,
+                Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINER,
                 "Hello, World", null);
         assertFalse(rule.evaluate(event));
     }

Added: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/TimestampEqualsRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/TimestampEqualsRuleTest.java?rev=658737&view=auto
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/TimestampEqualsRuleTest.java (added)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/TimestampEqualsRuleTest.java Wed May 21 09:07:18 2008
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.rule;
+
+
+import junit.framework.TestCase;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.helpers.UtilLoggingLevel;
+import org.apache.log4j.spi.LoggingEvent;
+import org.apache.log4j.util.SerializationTestHelper;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+
+/**
+ * Test for TimestampEqualsRule.
+ */
+public class TimestampEqualsRuleTest extends TestCase {
+
+    /**
+     * Create new test.
+     *
+     * @param testName test name.
+     */
+    public TimestampEqualsRuleTest(final String testName) {
+        super(testName);
+    }
+
+    /**
+     * Tests evaluate when timestamps are equal.
+     */
+    public void test1() {
+        TimestampEqualsRule rule = (TimestampEqualsRule) TimestampEqualsRule.getRule("2008/05/21 00:45:44");
+        Calendar cal = new GregorianCalendar(2008,04,21,00,45,44);
+        LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+                Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
+                "Hello, World", null);
+        assertTrue(rule.evaluate(event));
+    }
+
+    /**
+     * Tests evaluate when levels are not equal.
+     */
+    public void test2() {
+        TimestampEqualsRule rule = (TimestampEqualsRule) TimestampEqualsRule.getRule("2008/05/21 00:45:44");
+        Calendar cal = new GregorianCalendar(2008,04,21,00,46,44);
+        LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+                Logger.getRootLogger(), cal.getTimeInMillis(), Level.WARN,
+                "Hello, World", null);
+        assertFalse(rule.evaluate(event));
+    }
+
+    /**
+     * Tests evaluate of a deserialized clone when timestamps are equal.
+     */
+    public void test3() throws IOException, ClassNotFoundException {
+        TimestampEqualsRule rule = (TimestampEqualsRule)
+                SerializationTestHelper.serializeClone(TimestampEqualsRule.getRule("2008/05/21 00:45:44"));
+        Calendar cal = new GregorianCalendar(2008,04,21,00,45,44);
+        LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+                Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
+                "Hello, World", null);
+        assertTrue(rule.evaluate(event));
+    }
+
+    /**
+     * Tests evaluate of a deserialized clone when timestamps are not equal.
+     */
+    public void test4() throws IOException, ClassNotFoundException {
+        TimestampEqualsRule rule = (TimestampEqualsRule)
+                SerializationTestHelper.serializeClone(TimestampEqualsRule.getRule("2008/05/21 00:45:44"));
+        Calendar cal = new GregorianCalendar(2008,04,21,00,46,44);
+        LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+                Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
+                "Hello, World", null);
+        assertFalse(rule.evaluate(event));
+    }
+
+    /**
+     * Tests constructor will badly formed time specification.
+     */
+    public void test5() {
+        try {
+            TimestampEqualsRule.getRule("2008/May/21");
+            fail("IllegalArgumentException expected to be thrown");
+        } catch(IllegalArgumentException ex) {
+        }
+    }
+
+}

Added: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/TimestampInequalityRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/TimestampInequalityRuleTest.java?rev=658737&view=auto
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/TimestampInequalityRuleTest.java (added)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/TimestampInequalityRuleTest.java Wed May 21 09:07:18 2008
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.rule;
+
+
+import junit.framework.TestCase;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.helpers.UtilLoggingLevel;
+import org.apache.log4j.spi.LoggingEvent;
+import org.apache.log4j.util.SerializationTestHelper;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+
+/**
+ * Test for TimestampInequalityRule.
+ */
+public class TimestampInequalityRuleTest extends TestCase {
+
+    /**
+     * Create new test.
+     *
+     * @param testName test name.
+     */
+    public TimestampInequalityRuleTest(final String testName) {
+        super(testName);
+    }
+
+    /**
+     * Test construction when timestamp is unrecognized.
+     */
+    public void test1() {
+        try {
+            TimestampInequalityRule.getRule(">", "now");
+            fail("Expected IllegalArgumentException");
+        } catch(IllegalArgumentException ex) {
+        }
+    }
+
+    /**
+     * Tests construction when operator is unrecognized.
+     */
+    public void test2() {
+        //
+        //   unlike LevelInequalityRule, does not throw exception.  Resulting rule never satisified.
+        //
+        TimestampInequalityRule.getRule("~", "2008/05/21 00:45:46");
+    }
+
+    /**
+     * Tests evaluate of a deserialized clone when rule is satisified.
+     */
+    public void test3() throws IOException, ClassNotFoundException {
+        Rule rule = (Rule)
+                SerializationTestHelper.serializeClone(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
+        Calendar cal = new GregorianCalendar(2008,04,21,00,45,44);
+        LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+                Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
+                "Hello, World", null);
+        assertTrue(rule.evaluate(event));
+    }
+
+    /**
+     * Tests evaluate of a deserialized clone when rule is not satisfied.
+     */
+    public void test4() throws IOException, ClassNotFoundException {
+        Rule rule = (Rule)
+                SerializationTestHelper.serializeClone(TimestampInequalityRule.getRule("<", "2008/05/21 00:44:44"));
+        Calendar cal = new GregorianCalendar(2008,04,21,00,45,44);
+        LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+                Logger.getRootLogger(), cal.getTimeInMillis(), Level.WARN,
+                "Hello, World", null);
+        assertFalse(rule.evaluate(event));
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org