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/06/04 06:42:23 UTC
svn commit: r662973 - in
/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule:
AndRuleTest.java EqualsRuleTest.java ExistsRuleTest.java
NotEqualsRuleTest.java NotRuleTest.java OrRuleTest.java
PartialTextMatchRuleTest.java
Author: carnold
Date: Tue Jun 3 21:42:22 2008
New Revision: 662973
URL: http://svn.apache.org/viewvc?rev=662973&view=rev
Log:
Bug 45029: Additional unit tests for rules
Added:
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/AndRuleTest.java
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/EqualsRuleTest.java
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/ExistsRuleTest.java
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/NotEqualsRuleTest.java
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/NotRuleTest.java
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/OrRuleTest.java
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/PartialTextMatchRuleTest.java
Added: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/AndRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/AndRuleTest.java?rev=662973&view=auto
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/AndRuleTest.java (added)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/AndRuleTest.java Tue Jun 3 21:42:22 2008
@@ -0,0 +1,154 @@
+/*
+ * 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.spi.LoggingEvent;
+import org.apache.log4j.util.SerializationTestHelper;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Stack;
+
+/**
+ * Test for AndRule.
+ */
+public class AndRuleTest extends TestCase {
+
+ /**
+ * Create new test.
+ *
+ * @param testName test name.
+ */
+ public AndRuleTest(final String testName) {
+ super(testName);
+ }
+
+ /**
+ * AndRule.getRule(Stack) throws exception if only one rule provided.
+ */
+ public void test1() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ try {
+ AndRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * AndRule.getRule(Stack) throws exception if non-rules are provided.
+ */
+ public void test2() {
+ Stack stack = new Stack();
+ stack.push("Hello");
+ stack.push("World");
+ try {
+ AndRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * Test And of Level and Time.
+ */
+ public void test3() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
+ Rule rule = AndRule.getRule(stack);
+ assertEquals(0, stack.size());
+ 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));
+ }
+
+ /**
+ * Test And of Level and Time when Level does not match.
+ */
+ public void test4() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
+ Rule rule = AndRule.getRule(stack);
+ assertEquals(0, stack.size());
+ 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));
+ }
+
+ /**
+ * Test And of Level and Time when Time does not match.
+ */
+ public void test5() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2009/05/21 00:44:45"));
+ Rule rule = AndRule.getRule(stack);
+ assertEquals(0, stack.size());
+ 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);
+ assertFalse(rule.evaluate(event));
+ }
+
+
+ /**
+ * Test deserialized And.
+ */
+ public void test6() throws IOException, ClassNotFoundException {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
+ Rule rule = (Rule) SerializationTestHelper.serializeClone(AndRule.getRule(stack));
+ assertEquals(0, stack.size());
+ 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));
+ }
+
+
+ /**
+ * Test deserialized And when Level doesn't match.
+ */
+ public void test7() throws IOException, ClassNotFoundException {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
+ Rule rule = (Rule) SerializationTestHelper.serializeClone(AndRule.getRule(stack));
+ assertEquals(0, stack.size());
+ 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));
+ }
+
+}
Added: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/EqualsRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/EqualsRuleTest.java?rev=662973&view=auto
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/EqualsRuleTest.java (added)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/EqualsRuleTest.java Tue Jun 3 21:42:22 2008
@@ -0,0 +1,150 @@
+/*
+ * 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.spi.LoggingEvent;
+import org.apache.log4j.util.SerializationTestHelper;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Stack;
+
+/**
+ * Test for EqualsRule.
+ */
+public class EqualsRuleTest extends TestCase {
+
+ /**
+ * Create new test.
+ *
+ * @param testName test name.
+ */
+ public EqualsRuleTest(final String testName) {
+ super(testName);
+ }
+
+ /**
+ * getRule() with only one entry on stack should throw IllegalArgumentException.
+ */
+ public void test1() {
+ Stack stack = new Stack();
+ stack.push("Hello");
+ try {
+ EqualsRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * getRule() with bad field name should throw IllegalArgumentException.
+ */
+ public void test2() {
+ Stack stack = new Stack();
+ stack.push("Hello");
+ stack.push("World");
+ try {
+ EqualsRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * getRule with "level" and "info" should return a LevelEqualsRule.
+ */
+ public void test3() {
+ Stack stack = new Stack();
+ stack.push("level");
+ stack.push("info");
+ Rule rule = EqualsRule.getRule(stack);
+ assertEquals(0, stack.size());
+ assertTrue(rule instanceof LevelEqualsRule);
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertTrue(rule.evaluate(event));
+ }
+
+ /**
+ * getRule with "timestamp" and time should return a TimestampEqualsRule.
+ */
+ public void test4() {
+ Stack stack = new Stack();
+ stack.push("timestamp");
+ stack.push("2008/05/21 00:45:44");
+ Rule rule = EqualsRule.getRule(stack);
+ assertEquals(0, stack.size());
+ assertTrue(rule instanceof TimestampEqualsRule);
+ 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));
+ }
+
+ /**
+ * getRule with "msg" should return an EqualsRule.
+ */
+ public void test5() {
+ Stack stack = new Stack();
+ stack.push("msg");
+ stack.push("Hello, World");
+ Rule rule = EqualsRule.getRule(stack);
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertTrue(rule.evaluate(event));
+ }
+
+ /**
+ * getRule with "msg" should return an EqualsRule.
+ */
+ public void test6() {
+ Stack stack = new Stack();
+ stack.push("msg");
+ stack.push("Bonjour, Monde");
+ Rule rule = EqualsRule.getRule(stack);
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertFalse(rule.evaluate(event));
+ }
+
+ /**
+ * Check EqualsRule serialization.
+ */
+ public void test7() throws IOException, ClassNotFoundException {
+ Stack stack = new Stack();
+ stack.push("msg");
+ stack.push("Hello, World");
+ Rule rule = (Rule) SerializationTestHelper.serializeClone(EqualsRule.getRule(stack));
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertTrue(rule.evaluate(event));
+ }
+
+}
Added: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/ExistsRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/ExistsRuleTest.java?rev=662973&view=auto
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/ExistsRuleTest.java (added)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/ExistsRuleTest.java Tue Jun 3 21:42:22 2008
@@ -0,0 +1,110 @@
+/*
+ * 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.spi.LoggingEvent;
+import org.apache.log4j.util.SerializationTestHelper;
+
+import java.io.IOException;
+import java.util.Stack;
+
+/**
+ * Test for ExistsRule.
+ */
+public class ExistsRuleTest extends TestCase {
+
+ /**
+ * Create new test.
+ *
+ * @param testName test name.
+ */
+ public ExistsRuleTest(final String testName) {
+ super(testName);
+ }
+
+ /**
+ * getRule() with no entry on stack should throw IllegalArgumentException.
+ */
+ public void test1() {
+ Stack stack = new Stack();
+ try {
+ ExistsRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * getRule() with bad field name should throw IllegalArgumentException.
+ */
+ public void test2() {
+ Stack stack = new Stack();
+ stack.push("Hello");
+ try {
+ ExistsRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * getRule with "msg".
+ */
+ public void test3() {
+ Stack stack = new Stack();
+ stack.push("msg");
+ Rule rule = ExistsRule.getRule(stack);
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertTrue(rule.evaluate(event));
+ }
+
+ /**
+ * getRule with "msg".
+ */
+ public void test4() {
+ Stack stack = new Stack();
+ stack.push("msg");
+ Rule rule = ExistsRule.getRule(stack);
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "", null);
+ assertFalse(rule.evaluate(event));
+ }
+
+ /**
+ * getRule with "msg".
+ */
+ public void test5() throws IOException, ClassNotFoundException {
+ Stack stack = new Stack();
+ stack.push("msg");
+ Rule rule = (Rule) SerializationTestHelper.serializeClone(ExistsRule.getRule(stack));
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertTrue(rule.evaluate(event));
+ }
+
+}
Added: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/NotEqualsRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/NotEqualsRuleTest.java?rev=662973&view=auto
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/NotEqualsRuleTest.java (added)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/NotEqualsRuleTest.java Tue Jun 3 21:42:22 2008
@@ -0,0 +1,148 @@
+/*
+ * 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.spi.LoggingEvent;
+import org.apache.log4j.util.SerializationTestHelper;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Stack;
+
+/**
+ * Test for NotEqualsRule.
+ */
+public class NotEqualsRuleTest extends TestCase {
+
+ /**
+ * Create new test.
+ *
+ * @param testName test name.
+ */
+ public NotEqualsRuleTest(final String testName) {
+ super(testName);
+ }
+
+ /**
+ * getRule() with only one entry on stack should throw IllegalArgumentException.
+ */
+ public void test1() {
+ Stack stack = new Stack();
+ stack.push("Hello");
+ try {
+ NotEqualsRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * getRule() with bad field name should throw IllegalArgumentException.
+ */
+ public void test2() {
+ Stack stack = new Stack();
+ stack.push("Hello");
+ stack.push("World");
+ try {
+ NotEqualsRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * getRule with "level" and "info".
+ */
+ public void test3() {
+ Stack stack = new Stack();
+ stack.push("level");
+ stack.push("info");
+ Rule rule = NotEqualsRule.getRule(stack);
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.WARN,
+ "Hello, World", null);
+ assertTrue(rule.evaluate(event));
+ }
+
+ /**
+ * getRule with "timestamp" and time.
+ */
+ public void test4() {
+ Stack stack = new Stack();
+ stack.push("timestamp");
+ stack.push("2008/05/21 00:45:44");
+ Rule rule = NotEqualsRule.getRule(stack);
+ assertEquals(0, stack.size());
+ Calendar cal = new GregorianCalendar(2009, 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));
+ }
+
+ /**
+ * getRule with "msg".
+ */
+ public void test5() {
+ Stack stack = new Stack();
+ stack.push("msg");
+ stack.push("Hello, World");
+ Rule rule = NotEqualsRule.getRule(stack);
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertFalse(rule.evaluate(event));
+ }
+
+ /**
+ * getRule with "msg".
+ */
+ public void test6() {
+ Stack stack = new Stack();
+ stack.push("msg");
+ stack.push("Bonjour, Monde");
+ Rule rule = NotEqualsRule.getRule(stack);
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertTrue(rule.evaluate(event));
+ }
+
+ /**
+ * Check NotEqualsRule serialization.
+ */
+ public void test7() throws IOException, ClassNotFoundException {
+ Stack stack = new Stack();
+ stack.push("msg");
+ stack.push("Hello, World");
+ Rule rule = (Rule) SerializationTestHelper.serializeClone(NotEqualsRule.getRule(stack));
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertFalse(rule.evaluate(event));
+ }
+
+}
Added: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/NotRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/NotRuleTest.java?rev=662973&view=auto
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/NotRuleTest.java (added)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/NotRuleTest.java Tue Jun 3 21:42:22 2008
@@ -0,0 +1,132 @@
+/*
+ * 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.spi.LoggingEvent;
+import org.apache.log4j.util.SerializationTestHelper;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Stack;
+
+/**
+ * Test for NotRule.
+ */
+public class NotRuleTest extends TestCase {
+
+ /**
+ * Create new test.
+ *
+ * @param testName test name.
+ */
+ public NotRuleTest(final String testName) {
+ super(testName);
+ }
+
+ /**
+ * NotRule.getRule(Stack) throws exception if only one rule provided.
+ */
+ public void test1() {
+ Stack stack = new Stack();
+ try {
+ NotRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * NotRule.getRule(Stack) throws exception if non-rules are provided.
+ */
+ public void test2() {
+ Stack stack = new Stack();
+ stack.push("Hello");
+ try {
+ NotRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * Test Not of LevelEqualsRule.
+ */
+ public void test3() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ Rule rule = NotRule.getRule(stack);
+ assertEquals(0, stack.size());
+ 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);
+ assertFalse(rule.evaluate(event));
+ }
+
+ /**
+ * Test Not of Level when Level does not match.
+ */
+ public void test4() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ Rule rule = NotRule.getRule(stack);
+ assertEquals(0, stack.size());
+ 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);
+ assertTrue(rule.evaluate(event));
+ }
+
+
+ /**
+ * Test deserialized Not.
+ */
+ public void test5() throws IOException, ClassNotFoundException {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ Rule rule = (Rule) SerializationTestHelper.serializeClone(NotRule.getRule(stack));
+ assertEquals(0, stack.size());
+ 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);
+ assertFalse(rule.evaluate(event));
+ }
+
+
+ /**
+ * Test deserialized Not.
+ */
+ public void test6() throws IOException, ClassNotFoundException {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ Rule rule = (Rule) SerializationTestHelper.serializeClone(NotRule.getRule(stack));
+ assertEquals(0, stack.size());
+ 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);
+ assertTrue(rule.evaluate(event));
+ }
+
+}
Added: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/OrRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/OrRuleTest.java?rev=662973&view=auto
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/OrRuleTest.java (added)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/OrRuleTest.java Tue Jun 3 21:42:22 2008
@@ -0,0 +1,170 @@
+/*
+ * 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.spi.LoggingEvent;
+import org.apache.log4j.util.SerializationTestHelper;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Stack;
+
+/**
+ * Test for OrRule.
+ */
+public class OrRuleTest extends TestCase {
+
+ /**
+ * Create new test.
+ *
+ * @param testName test name.
+ */
+ public OrRuleTest(final String testName) {
+ super(testName);
+ }
+
+ /**
+ * OrRule.getRule(Stack) throws exception if only one rule provided.
+ */
+ public void test1() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ try {
+ OrRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * OrRule.getRule(Stack) throws exception if non-rules are provided.
+ */
+ public void test2() {
+ Stack stack = new Stack();
+ stack.push("Hello");
+ stack.push("World");
+ try {
+ OrRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * Test Or of Level and Time.
+ */
+ public void test3() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
+ Rule rule = OrRule.getRule(stack);
+ assertEquals(0, stack.size());
+ 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));
+ }
+
+ /**
+ * Test Or of Level and Time when Level does not match.
+ */
+ public void test4() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
+ Rule rule = OrRule.getRule(stack);
+ assertEquals(0, stack.size());
+ 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);
+ assertTrue(rule.evaluate(event));
+ }
+
+ /**
+ * Test Or of Level and Time when Time does not match.
+ */
+ public void test5() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2009/05/21 00:44:45"));
+ Rule rule = OrRule.getRule(stack);
+ assertEquals(0, stack.size());
+ 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));
+ }
+
+
+ /**
+ * Test Or of Level and Time when Time and Level do not match.
+ */
+ public void test6() {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2009/05/21 00:44:45"));
+ Rule rule = OrRule.getRule(stack);
+ assertEquals(0, stack.size());
+ 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));
+ }
+
+ /**
+ * Test deserialized Or.
+ */
+ public void test7() throws IOException, ClassNotFoundException {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
+ Rule rule = (Rule) SerializationTestHelper.serializeClone(OrRule.getRule(stack));
+ assertEquals(0, stack.size());
+ 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));
+ }
+
+
+ /**
+ * Test deserialized Or when neither rule match.
+ */
+ public void test8() throws IOException, ClassNotFoundException {
+ Stack stack = new Stack();
+ stack.push(LevelEqualsRule.getRule("INFO"));
+ stack.push(TimestampInequalityRule.getRule(">=", "2009/05/21 00:44:45"));
+ Rule rule = (Rule) SerializationTestHelper.serializeClone(OrRule.getRule(stack));
+ assertEquals(0, stack.size());
+ 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));
+ }
+
+}
Added: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/PartialTextMatchRuleTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/PartialTextMatchRuleTest.java?rev=662973&view=auto
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/PartialTextMatchRuleTest.java (added)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/rule/PartialTextMatchRuleTest.java Tue Jun 3 21:42:22 2008
@@ -0,0 +1,132 @@
+/*
+ * 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.spi.LoggingEvent;
+import org.apache.log4j.util.SerializationTestHelper;
+
+import java.io.IOException;
+import java.util.Stack;
+
+/**
+ * Test for PartialTextMatchRule.
+ */
+public class PartialTextMatchRuleTest extends TestCase {
+
+ /**
+ * Create new test.
+ *
+ * @param testName test name.
+ */
+ public PartialTextMatchRuleTest(final String testName) {
+ super(testName);
+ }
+
+ /**
+ * getRule() with only one entry on stack should throw IllegalArgumentException.
+ */
+ public void test1() {
+ Stack stack = new Stack();
+ stack.push("Hello");
+ try {
+ PartialTextMatchRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * getRule() with bad field name should throw IllegalArgumentException.
+ */
+ public void test2() {
+ Stack stack = new Stack();
+ stack.push("Hello");
+ stack.push("World");
+ try {
+ PartialTextMatchRule.getRule(stack);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ /**
+ * getRule with "level" and "nfo" should return a LevelEqualsRule.
+ */
+ public void test3() {
+ Stack stack = new Stack();
+ stack.push("level");
+ stack.push("nfo");
+ Rule rule = PartialTextMatchRule.getRule(stack);
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertTrue(rule.evaluate(event));
+ }
+
+
+ /**
+ * getRule with "msg".
+ */
+ public void test4() {
+ Stack stack = new Stack();
+ stack.push("msg");
+ stack.push("World");
+ Rule rule = PartialTextMatchRule.getRule(stack);
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertTrue(rule.evaluate(event));
+ }
+
+ /**
+ * getRule with "msg".
+ */
+ public void test5() {
+ Stack stack = new Stack();
+ stack.push("msg");
+ stack.push("Bonjour, Monde");
+ Rule rule = PartialTextMatchRule.getRule(stack);
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertFalse(rule.evaluate(event));
+ }
+
+ /**
+ * Check PartailTextMatchRule serialization.
+ */
+ public void test6() throws IOException, ClassNotFoundException {
+ Stack stack = new Stack();
+ stack.push("msg");
+ stack.push("World");
+ Rule rule = (Rule) SerializationTestHelper.serializeClone(PartialTextMatchRule.getRule(stack));
+ assertEquals(0, stack.size());
+ LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
+ Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
+ "Hello, World", null);
+ assertTrue(rule.evaluate(event));
+ }
+
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org