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 016import static org.junit.Assert.assertEquals; 017import static org.junit.Assert.assertTrue; 018 019import java.util.Iterator; 020 021import org.junit.After; 022import org.junit.Before; 023import org.junit.Test; 024 025import ch.qos.logback.core.Context; 026import ch.qos.logback.core.ContextBase; 027import ch.qos.logback.core.joran.spi.InterpretationContext; 028import ch.qos.logback.core.status.ErrorStatus; 029import ch.qos.logback.core.status.Status; 030import ch.qos.logback.core.testUtil.CoreTestConstants; 031import ch.qos.logback.core.util.StatusPrinter; 032 033/** 034 * Test {@link PropertyAction}. 035 * @author Ceki Gülcü 036 */ 037public class PropertyActionTest { 038 039 Context context; 040 InterpretationContext ec; 041 PropertyAction propertyAction; 042 DummyAttributes atts = new DummyAttributes(); 043 044 @Before 045 public void setUp() throws Exception { 046 context = new ContextBase(); 047 ec = new InterpretationContext(context, null); 048 propertyAction = new PropertyAction(); 049 propertyAction.setContext(context); 050 } 051 052 @After 053 public void tearDown() throws Exception { 054 context = null; 055 propertyAction = null; 056 atts = null; 057 } 058 059 @Test 060 public void nameValuePair() { 061 atts.setValue("name", "v1"); 062 atts.setValue("value", "work"); 063 propertyAction.begin(ec, null, atts); 064 assertEquals("work", ec.getProperty("v1")); 065 } 066 067 @Test 068 public void nameValuePairWithPrerequisiteSubsitution() { 069 context.putProperty("w", "wor"); 070 atts.setValue("name", "v1"); 071 atts.setValue("value", "${w}k"); 072 propertyAction.begin(ec, null, atts); 073 assertEquals("work", ec.getProperty("v1")); 074 } 075 076 @Test 077 public void noValue() { 078 atts.setValue("name", "v1"); 079 propertyAction.begin(ec, null, atts); 080 assertEquals(1, context.getStatusManager().getCount()); 081 assertTrue(checkError()); 082 } 083 084 @Test 085 public void noName() { 086 atts.setValue("value", "v1"); 087 propertyAction.begin(ec, null, atts); 088 assertEquals(1, context.getStatusManager().getCount()); 089 assertTrue(checkError()); 090 } 091 092 @Test 093 public void noAttributes() { 094 propertyAction.begin(ec, null, atts); 095 assertEquals(1, context.getStatusManager().getCount()); 096 assertTrue(checkError()); 097 StatusPrinter.print(context); 098 } 099 100 @Test 101 public void testFileNotLoaded() { 102 atts.setValue("file", "toto"); 103 atts.setValue("value", "work"); 104 propertyAction.begin(ec, null, atts); 105 assertEquals(1, context.getStatusManager().getCount()); 106 assertTrue(checkError()); 107 } 108 109 @Test 110 public void testLoadFileWithPrerequisiteSubsitution() { 111 context.putProperty("STEM", CoreTestConstants.TEST_SRC_PREFIX + "input/joran"); 112 atts.setValue("file", "${STEM}/propertyActionTest.properties"); 113 propertyAction.begin(ec, null, atts); 114 assertEquals("tata", ec.getProperty("v1")); 115 assertEquals("toto", ec.getProperty("v2")); 116 } 117 118 @Test 119 public void testLoadFile() { 120 atts.setValue("file", CoreTestConstants.TEST_SRC_PREFIX + "input/joran/propertyActionTest.properties"); 121 propertyAction.begin(ec, null, atts); 122 assertEquals("tata", ec.getProperty("v1")); 123 assertEquals("toto", ec.getProperty("v2")); 124 } 125 126 @Test 127 public void testLoadResource() { 128 atts.setValue("resource", "asResource/joran/propertyActionTest.properties"); 129 propertyAction.begin(ec, null, atts); 130 assertEquals("tata", ec.getProperty("r1")); 131 assertEquals("toto", ec.getProperty("r2")); 132 } 133 134 @Test 135 public void testLoadResourceWithPrerequisiteSubsitution() { 136 context.putProperty("STEM", "asResource/joran"); 137 atts.setValue("resource", "${STEM}/propertyActionTest.properties"); 138 propertyAction.begin(ec, null, atts); 139 assertEquals("tata", ec.getProperty("r1")); 140 assertEquals("toto", ec.getProperty("r2")); 141 } 142 143 @Test 144 public void testLoadNotPossible() { 145 atts.setValue("file", "toto"); 146 propertyAction.begin(ec, null, atts); 147 assertEquals(1, context.getStatusManager().getCount()); 148 assertTrue(checkFileErrors()); 149 } 150 151 private boolean checkError() { 152 Iterator<Status> it = context.getStatusManager().getCopyOfStatusList().iterator(); 153 ErrorStatus es = (ErrorStatus) it.next(); 154 return PropertyAction.INVALID_ATTRIBUTES.equals(es.getMessage()); 155 } 156 157 private boolean checkFileErrors() { 158 Iterator<Status> it = context.getStatusManager().getCopyOfStatusList().iterator(); 159 ErrorStatus es1 = (ErrorStatus) it.next(); 160 return "Could not find properties file [toto].".equals(es1.getMessage()); 161 } 162}