001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.joran.action; 015 016 017import static org.junit.Assert.assertEquals; 018import static org.junit.Assert.assertNotNull; 019import static org.junit.Assert.assertNull; 020 021import java.util.HashMap; 022 023import org.junit.After; 024import org.junit.Before; 025import org.junit.Ignore; 026import org.junit.Test; 027 028import ch.qos.logback.core.Context; 029import ch.qos.logback.core.ContextBase; 030import ch.qos.logback.core.joran.SimpleConfigurator; 031import ch.qos.logback.core.joran.spi.ElementSelector; 032import ch.qos.logback.core.joran.spi.InterpretationContext; 033import ch.qos.logback.core.joran.spi.JoranException; 034import ch.qos.logback.core.status.Status; 035import ch.qos.logback.core.testUtil.CoreTestConstants; 036import ch.qos.logback.core.testUtil.StatusChecker; 037 038/** 039 * Test {@link DefinePropertyAction}. 040 * 041 * @author Aleksey Didik 042 */ 043public class DefinePropertyActionTest { 044 045 private static final String DEFINE_INPUT_DIR = CoreTestConstants.JORAN_INPUT_PREFIX + "define/"; 046 private static final String GOOD_XML = "good.xml"; 047 private static final String NONAME_XML = "noname.xml"; 048 private static final String NOCLASS_XML = "noclass.xml"; 049 private static final String BADCLASS_XML = "badclass.xml"; 050 051 SimpleConfigurator simpleConfigurator; 052 Context context = new ContextBase(); 053 DefinePropertyAction definerAction; 054 InterpretationContext ic; 055 StatusChecker checker = new StatusChecker(context); 056 057 @Before 058 public void setUp() throws Exception { 059 060 HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>(); 061 rulesMap.put(new ElementSelector("define"), new DefinePropertyAction()); 062 simpleConfigurator = new SimpleConfigurator(rulesMap); 063 simpleConfigurator.setContext(context); 064 } 065 066 @After 067 public void tearDown() throws Exception { 068 // StatusPrinter.printInCaseOfErrorsOrWarnings(context); 069 } 070 071 @Test 072 public void good() throws JoranException { 073 simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + GOOD_XML); 074 InterpretationContext ic = simpleConfigurator.getInterpreter().getInterpretationContext(); 075 String inContextFoo = ic.getProperty("foo"); 076 assertEquals("monster", inContextFoo); 077 } 078 079 @Test 080 public void noName() throws JoranException { 081 simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + NONAME_XML); 082 // get from context 083 String inContextFoo = context.getProperty("foo"); 084 assertNull(inContextFoo); 085 // check context errors 086 checker.assertContainsMatch(Status.ERROR, "Missing property name for property definer. Near \\[define\\] line 1"); 087 } 088 089 @Test 090 public void noClass() throws JoranException { 091 simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + NOCLASS_XML); 092 String inContextFoo = context.getProperty("foo"); 093 assertNull(inContextFoo); 094 checker.assertContainsMatch(Status.ERROR, "Missing class name for property definer. Near \\[define\\] line 1"); 095 } 096 097 @Test 098 public void testBadClass() throws JoranException { 099 simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + BADCLASS_XML); 100 // get from context 101 String inContextFoo = context.getProperty("foo"); 102 assertNull(inContextFoo); 103 // check context errors 104 checker.assertContainsMatch(Status.ERROR, "Could not create an PropertyDefiner of type"); 105 } 106 107 @Ignore // on certain hosts this test takes 5 seconds to complete 108 @Test 109 public void canonicalHostNameProperty() throws JoranException { 110 String configFileAsStr = DEFINE_INPUT_DIR + "canonicalHostname.xml"; 111 simpleConfigurator.doConfigure(configFileAsStr); 112 assertNotNull(context.getProperty("CANONICAL_HOST_NAME")); 113 } 114 115}