1 package ch.qos.logback.core.joran.action; 2 3 import static org.junit.Assert.assertEquals; 4 import static org.junit.Assert.assertTrue; 5 6 import java.util.Iterator; 7 8 import org.junit.After; 9 import org.junit.Before; 10 import org.junit.Test; 11 12 import ch.qos.logback.core.Context; 13 import ch.qos.logback.core.ContextBase; 14 import ch.qos.logback.core.joran.spi.InterpretationContext; 15 import ch.qos.logback.core.status.ErrorStatus; 16 import ch.qos.logback.core.util.Constants; 17 import ch.qos.logback.core.util.StatusPrinter; 18 19 public class PropertyActionTest { 20 21 Context context; 22 InterpretationContext ec; 23 PropertyAction spAction; 24 DummyAttributes atts = new DummyAttributes(); 25 26 @Before 27 public void setUp() throws Exception { 28 context = new ContextBase(); 29 ec = new InterpretationContext(context, null); 30 spAction = new PropertyAction(); 31 spAction.setContext(context); 32 } 33 34 @After 35 public void tearDown() throws Exception { 36 context = null; 37 spAction = null; 38 atts = null; 39 } 40 41 @Test 42 public void nameValuePair() { 43 atts.setValue("name", "v1"); 44 atts.setValue("value", "work"); 45 spAction.begin(ec, null, atts); 46 assertEquals("work", ec.getProperty("v1")); 47 } 48 49 @Test 50 public void nameValuePairWithPrerequisiteSubsitution() { 51 context.putProperty("w", "wor"); 52 atts.setValue("name", "v1"); 53 atts.setValue("value", "${w}k"); 54 spAction.begin(ec, null, atts); 55 assertEquals("work", ec.getProperty("v1")); 56 } 57 58 @Test 59 public void noValue() { 60 atts.setValue("name", "v1"); 61 spAction.begin(ec, null, atts); 62 assertEquals(1, context.getStatusManager().getCount()); 63 assertTrue(checkError()); 64 } 65 66 @Test 67 public void noName() { 68 atts.setValue("value", "v1"); 69 spAction.begin(ec, null, atts); 70 assertEquals(1, context.getStatusManager().getCount()); 71 assertTrue(checkError()); 72 } 73 74 @Test 75 public void noAttributes() { 76 spAction.begin(ec, null, atts); 77 assertEquals(1, context.getStatusManager().getCount()); 78 assertTrue(checkError()); 79 StatusPrinter.print(context); 80 } 81 82 @Test 83 public void testFileNotLoaded() { 84 atts.setValue("file", "toto"); 85 atts.setValue("value", "work"); 86 spAction.begin(ec, null, atts); 87 assertEquals(1, context.getStatusManager().getCount()); 88 assertTrue(checkError()); 89 } 90 91 @Test 92 public void testLoadFileWithPrerequisiteSubsitution() { 93 context.putProperty("STEM", Constants.TEST_DIR_PREFIX + "input/joran"); 94 atts.setValue("file", "${STEM}/propertyActionTest.properties"); 95 spAction.begin(ec, null, atts); 96 assertEquals("tata", ec.getProperty("v1")); 97 assertEquals("toto", ec.getProperty("v2")); 98 } 99 100 @Test 101 public void testLoadFile() { 102 atts.setValue("file", Constants.TEST_DIR_PREFIX + "input/joran/propertyActionTest.properties"); 103 spAction.begin(ec, null, atts); 104 assertEquals("tata", ec.getProperty("v1")); 105 assertEquals("toto", ec.getProperty("v2")); 106 } 107 108 @Test 109 public void testLoadResource() { 110 atts.setValue("resource", "asResource/joran/propertyActionTest.properties"); 111 spAction.begin(ec, null, atts); 112 assertEquals("tata", ec.getProperty("r1")); 113 assertEquals("toto", ec.getProperty("r2")); 114 } 115 116 @Test 117 public void testLoadResourceWithPrerequisiteSubsitution() { 118 context.putProperty("STEM", "asResource/joran"); 119 atts.setValue("resource", "${STEM}/propertyActionTest.properties"); 120 spAction.begin(ec, null, atts); 121 assertEquals("tata", ec.getProperty("r1")); 122 assertEquals("toto", ec.getProperty("r2")); 123 } 124 125 @Test 126 public void testLoadNotPossible() { 127 atts.setValue("file", "toto"); 128 spAction.begin(ec, null, atts); 129 assertEquals(1, context.getStatusManager().getCount()); 130 assertTrue(checkFileErrors()); 131 } 132 133 private boolean checkError() { 134 Iterator it = context.getStatusManager().getCopyOfStatusList().iterator(); 135 ErrorStatus es = (ErrorStatus)it.next(); 136 return PropertyAction.INVALID_ATTRIBUTES.equals(es.getMessage()); 137 } 138 139 private boolean checkFileErrors() { 140 Iterator it = context.getStatusManager().getCopyOfStatusList().iterator(); 141 ErrorStatus es1 = (ErrorStatus)it.next(); 142 return "Could not read properties file [toto].".equals(es1.getMessage()); 143 } 144 }