1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.joran;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertTrue;
19
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URL;
25 import java.net.URLConnection;
26 import java.util.HashMap;
27 import java.util.function.Supplier;
28 import java.util.jar.JarOutputStream;
29 import java.util.zip.ZipEntry;
30
31 import org.junit.Before;
32 import org.junit.Test;
33
34 import ch.qos.logback.core.Context;
35 import ch.qos.logback.core.ContextBase;
36 import ch.qos.logback.core.CoreConstants;
37 import ch.qos.logback.core.joran.action.Action;
38 import ch.qos.logback.core.joran.action.TopElementAction;
39 import ch.qos.logback.core.joran.action.ext.IncAction;
40 import ch.qos.logback.core.joran.spi.ElementSelector;
41 import ch.qos.logback.core.joran.spi.JoranException;
42 import ch.qos.logback.core.status.Status;
43 import ch.qos.logback.core.testUtil.CoreTestConstants;
44 import ch.qos.logback.core.testUtil.RandomUtil;
45 import ch.qos.logback.core.testUtil.TrivialStatusListener;
46
47 public class TrivialConfiguratorTest {
48
49 Context context = new ContextBase();
50 HashMap<ElementSelector, Supplier<Action>> rulesMap = new HashMap<>();
51
52 @Before
53 public void setUp() {
54
55 rulesMap.put(new ElementSelector("x"), () -> new TopElementAction());
56 rulesMap.put(new ElementSelector("x/inc"), () -> new IncAction());
57
58 }
59
60 public void doTest(String filename) throws Exception {
61 TrivialConfigurator trivialConfigurator = new TrivialConfigurator(rulesMap);
62
63 trivialConfigurator.setContext(context);
64 trivialConfigurator.doConfigure(filename);
65 }
66
67 @Test
68 public void smoke() throws Exception {
69 int oldBeginCount = IncAction.beginCount;
70 int oldEndCount = IncAction.endCount;
71 int oldErrorCount = IncAction.errorCount;
72 doTest(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + "inc.xml");
73 assertEquals(oldErrorCount, IncAction.errorCount);
74 assertEquals(oldBeginCount + 1, IncAction.beginCount);
75 assertEquals(oldEndCount + 1, IncAction.endCount);
76 }
77
78 @Test
79 public void inexistentFile() {
80 TrivialStatusListener tsl = new TrivialStatusListener();
81 tsl.start();
82 String filename = CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + "nothereBLAH.xml";
83 context.getStatusManager().add(tsl);
84 try {
85 doTest(filename);
86 } catch (Exception e) {
87 assertTrue(e.getMessage().startsWith("Could not open ["));
88 }
89 assertTrue(tsl.list.size() + " should be greater than or equal to 1", tsl.list.size() >= 1);
90 Status s0 = tsl.list.get(0);
91 assertTrue(s0.getMessage().startsWith("Could not open ["));
92 }
93
94 @Test
95 public void illFormedXML() {
96 TrivialStatusListener tsl = new TrivialStatusListener();
97 tsl.start();
98 String filename = CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + "illformed.xml";
99 context.getStatusManager().add(tsl);
100 try {
101 doTest(filename);
102 } catch (Exception e) {
103 }
104 assertEquals(2, tsl.list.size());
105 Status s0 = tsl.list.get(0);
106 assertTrue(s0.getMessage().startsWith(CoreConstants.XML_PARSING));
107 }
108
109 @Test
110 public void lbcore105() throws IOException, JoranException {
111 String jarEntry = "buzz.xml";
112 File jarFile = makeRandomJarFile();
113 fillInJarFile(jarFile, jarEntry);
114 URL url = asURL(jarFile, jarEntry);
115 TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
116 tc.setContext(context);
117 tc.doConfigure(url);
118
119 assertTrue(jarFile.delete());
120 assertFalse(jarFile.exists());
121 }
122
123 @Test
124 public void lbcore127() throws IOException, JoranException {
125 String jarEntry = "buzz.xml";
126 String jarEntry2 = "lightyear.xml";
127
128 File jarFile = makeRandomJarFile();
129 fillInJarFile(jarFile, jarEntry, jarEntry2);
130
131 URL url1 = asURL(jarFile, jarEntry);
132 URL url2 = asURL(jarFile, jarEntry2);
133
134 URLConnection urlConnection2 = url2.openConnection();
135 urlConnection2.setUseCaches(false);
136 InputStream is = urlConnection2.getInputStream();
137
138 TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
139 tc.setContext(context);
140 tc.doConfigure(url1);
141
142 is.read();
143 is.close();
144
145
146 assertTrue(jarFile.delete());
147 assertFalse(jarFile.exists());
148 }
149
150 File makeRandomJarFile() {
151 File outputDir = new File(CoreTestConstants.OUTPUT_DIR_PREFIX);
152 outputDir.mkdirs();
153 int randomPart = RandomUtil.getPositiveInt();
154 return new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "foo-" + randomPart + ".jar");
155 }
156
157 private void fillInJarFile(File jarFile, String jarEntryName) throws IOException {
158 fillInJarFile(jarFile, jarEntryName, null);
159 }
160
161 private void fillInJarFile(File jarFile, String jarEntryName1, String jarEntryName2) throws IOException {
162 JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile));
163 jos.putNextEntry(new ZipEntry(jarEntryName1));
164 jos.write("<x/>".getBytes());
165 jos.closeEntry();
166 if (jarEntryName2 != null) {
167 jos.putNextEntry(new ZipEntry(jarEntryName2));
168 jos.write("<y/>".getBytes());
169 jos.closeEntry();
170 }
171 jos.close();
172 }
173
174 URL asURL(File jarFile, String jarEntryName) throws IOException {
175 URL innerURL = jarFile.toURI().toURL();
176 return new URL("jar:" + innerURL + "!/" + jarEntryName);
177 }
178
179 }