
Author: seb Date: Thu Jan 4 16:13:08 2007 New Revision: 1156 Added: logback/trunk/logback-core/src/test/input/joran/propertyActionTest.properties logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/DummyAttributes.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/PackageTest.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/PropertyActionTest.java Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/PackageTest.java Log: Added unit tests for PropertyAction class Added: logback/trunk/logback-core/src/test/input/joran/propertyActionTest.properties ============================================================================== --- (empty file) +++ logback/trunk/logback-core/src/test/input/joran/propertyActionTest.properties Thu Jan 4 16:13:08 2007 @@ -0,0 +1,2 @@ +v1=tata +v2=toto \ No newline at end of file Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/PackageTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/PackageTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/PackageTest.java Thu Jan 4 16:13:08 2007 @@ -19,6 +19,7 @@ TestSuite suite = new TestSuite(); suite.addTestSuite(SkippingInInterpreterTest.class); suite.addTestSuite(TrivialcConfiguratorTest.class); + suite.addTest(ch.qos.logback.core.joran.action.PackageTest.suite()); suite.addTest(ch.qos.logback.core.joran.event.PackageTest.suite()); suite.addTest(ch.qos.logback.core.joran.spi.PackageTest.suite()); suite.addTest(ch.qos.logback.core.joran.replay.PackageTest.suite()); Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/DummyAttributes.java ============================================================================== --- (empty file) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/DummyAttributes.java Thu Jan 4 16:13:08 2007 @@ -0,0 +1,63 @@ +package ch.qos.logback.core.joran.action; + +import java.util.HashMap; + +import org.xml.sax.Attributes; + +public class DummyAttributes implements Attributes { + + HashMap<String, String> atts = new HashMap<String, String>(); + + public int getIndex(String qName) { + return 0; + } + + public int getIndex(String uri, String localName) { + return 0; + } + + public int getLength() { + return 0; + } + + public String getLocalName(int index) { + return null; + } + + public String getQName(int index) { + return null; + } + + public String getType(int index) { + return null; + } + + public String getType(String qName) { + return null; + } + + public String getType(String uri, String localName) { + return null; + } + + public String getURI(int index) { + return null; + } + + public String getValue(int index) { + return null; + } + + public String getValue(String qName) { + return atts.get(qName); + } + + public void setValue(String key, String value) { + atts.put(key, value); + } + + public String getValue(String uri, String localName) { + return null; + } + +} Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/PackageTest.java ============================================================================== --- (empty file) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/PackageTest.java Thu Jan 4 16:13:08 2007 @@ -0,0 +1,23 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework for Java. + * + * Copyright (C) 2000-2006, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ +package ch.qos.logback.core.joran.action; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class PackageTest extends TestCase { + + public static Test suite() { + TestSuite suite = new TestSuite(); + suite.addTestSuite(PropertyActionTest.class); + return suite; + } +} Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/PropertyActionTest.java ============================================================================== --- (empty file) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/PropertyActionTest.java Thu Jan 4 16:13:08 2007 @@ -0,0 +1,101 @@ +package ch.qos.logback.core.joran.action; + +import java.util.Iterator; + +import junit.framework.TestCase; +import ch.qos.logback.core.Context; +import ch.qos.logback.core.ContextBase; +import ch.qos.logback.core.joran.spi.InterpretationContext; +import ch.qos.logback.core.status.ErrorStatus; +import ch.qos.logback.core.util.Constants; +import ch.qos.logback.core.util.StatusPrinter; + +public class PropertyActionTest extends TestCase { + + Context context; + InterpretationContext ec; + SubstitutionPropertyAction spAction; + DummyAttributes atts = new DummyAttributes(); + + @Override + protected void setUp() throws Exception { + context = new ContextBase(); + ec = new InterpretationContext(context, null); + spAction = new SubstitutionPropertyAction(); + spAction.setContext(context); + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + context = null; + spAction = null; + atts = null; + super.tearDown(); + } + + public void testBegin() { + atts.setValue("name", "v1"); + atts.setValue("value", "work"); + spAction.begin(ec, null, atts); + assertEquals("work", ec.getSubstitutionProperty("v1")); + } + + public void testBeginNoValue() { + atts.setValue("name", "v1"); + spAction.begin(ec, null, atts); + assertEquals(1, context.getStatusManager().getCount()); + assertTrue(checkError()); + } + + public void testBeginNoName() { + atts.setValue("value", "v1"); + spAction.begin(ec, null, atts); + assertEquals(1, context.getStatusManager().getCount()); + assertTrue(checkError()); + } + + public void testBeginNothing() { + spAction.begin(ec, null, atts); + assertEquals(1, context.getStatusManager().getCount()); + assertTrue(checkError()); + } + + public void testFileNotLoaded() { + atts.setValue("file", "toto"); + atts.setValue("value", "work"); + spAction.begin(ec, null, atts); + assertEquals(1, context.getStatusManager().getCount()); + assertTrue(checkError()); + } + + public void testLoadFile() { + atts.setValue("file", Constants.TEST_DIR_PREFIX + "input/joran/propertyActionTest.properties"); + spAction.begin(ec, null, atts); + assertEquals("tata", ec.getSubstitutionProperty("v1")); + assertEquals("toto", ec.getSubstitutionProperty("v2")); + } + + public void testLoadNotPossible() { + atts.setValue("file", "toto"); + spAction.begin(ec, null, atts); + StatusPrinter.print(context); + assertEquals(2, context.getStatusManager().getCount()); + assertTrue(checkFileErrors()); + } + + private boolean checkError() { + Iterator it = context.getStatusManager().iterator(); + ErrorStatus es = (ErrorStatus)it.next(); + return PropertyAction.INVALID_ATTRIBUTES.equals(es.getMessage()); + } + + private boolean checkFileErrors() { + Iterator it = context.getStatusManager().iterator(); + ErrorStatus es1 = (ErrorStatus)it.next(); + boolean result1 = "Could not read properties file [toto].".equals(es1.getMessage()); + ErrorStatus es2 = (ErrorStatus)it.next(); + boolean result2 = "Ignoring configuration file [toto].".equals(es2.getMessage()); + return result1 && result2; + } +}