View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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.jar.JarOutputStream;
28  import java.util.zip.ZipEntry;
29  
30  import org.junit.Test;
31  
32  import ch.qos.logback.core.Context;
33  import ch.qos.logback.core.ContextBase;
34  import ch.qos.logback.core.CoreConstants;
35  import ch.qos.logback.core.joran.action.Action;
36  import ch.qos.logback.core.joran.action.ext.IncAction;
37  import ch.qos.logback.core.joran.spi.ElementSelector;
38  import ch.qos.logback.core.joran.spi.JoranException;
39  import ch.qos.logback.core.status.Status;
40  import ch.qos.logback.core.testUtil.CoreTestConstants;
41  import ch.qos.logback.core.testUtil.RandomUtil;
42  import ch.qos.logback.core.testUtil.TrivialStatusListener;
43  
44  public class TrivialConfiguratorTest {
45  
46      Context context = new ContextBase();
47      HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>();
48  
49      public void doTest(String filename) throws Exception {
50  
51          // rule store is case insensitve
52          rulesMap.put(new ElementSelector("x/inc"), new IncAction());
53  
54          TrivialConfigurator trivialConfigurator = new TrivialConfigurator(rulesMap);
55  
56          trivialConfigurator.setContext(context);
57          trivialConfigurator.doConfigure(filename);
58      }
59  
60      @Test
61      public void smoke() throws Exception {
62          int oldBeginCount = IncAction.beginCount;
63          int oldEndCount = IncAction.endCount;
64          int oldErrorCount = IncAction.errorCount;
65          doTest(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + "inc.xml");
66          assertEquals(oldErrorCount, IncAction.errorCount);
67          assertEquals(oldBeginCount + 1, IncAction.beginCount);
68          assertEquals(oldEndCount + 1, IncAction.endCount);
69      }
70  
71      @Test
72      public void inexistentFile() {
73          TrivialStatusListener tsl = new TrivialStatusListener();
74          tsl.start();
75          String filename = CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + "nothereBLAH.xml";
76          context.getStatusManager().add(tsl);
77          try {
78              doTest(filename);
79          } catch (Exception e) {
80              assertTrue(e.getMessage().startsWith("Could not open ["));
81          }
82          assertTrue(tsl.list.size() + " should be greater than or equal to 1", tsl.list.size() >= 1);
83          Status s0 = tsl.list.get(0);
84          assertTrue(s0.getMessage().startsWith("Could not open ["));
85      }
86  
87      @Test
88      public void illFormedXML() {
89          TrivialStatusListener tsl = new TrivialStatusListener();
90          tsl.start();
91          String filename = CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + "illformed.xml";
92          context.getStatusManager().add(tsl);
93          try {
94              doTest(filename);
95          } catch (Exception e) {
96          }
97          assertEquals(2, tsl.list.size());
98          Status s0 = tsl.list.get(0);
99          assertTrue(s0.getMessage().startsWith(CoreConstants.XML_PARSING));
100     }
101 
102     @Test
103     public void lbcore105() throws IOException, JoranException {
104         String jarEntry = "buzz.xml";
105         File jarFile = makeRandomJarFile();
106         fillInJarFile(jarFile, jarEntry);
107         URL url = asURL(jarFile, jarEntry);
108         TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
109         tc.setContext(context);
110         tc.doConfigure(url);
111         // deleting an open file fails
112         assertTrue(jarFile.delete());
113         assertFalse(jarFile.exists());
114     }
115 
116     @Test
117     public void lbcore127() throws IOException, JoranException {
118         String jarEntry = "buzz.xml";
119         String jarEntry2 = "lightyear.xml";
120 
121         File jarFile = makeRandomJarFile();
122         fillInJarFile(jarFile, jarEntry, jarEntry2);
123 
124         URL url1 = asURL(jarFile, jarEntry);
125         URL url2 = asURL(jarFile, jarEntry2);
126 
127         URLConnection urlConnection2 = url2.openConnection();
128         urlConnection2.setUseCaches(false);
129         InputStream is = urlConnection2.getInputStream();
130 
131         TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
132         tc.setContext(context);
133         tc.doConfigure(url1);
134 
135         is.read();
136         is.close();
137 
138         // deleting an open file fails
139         assertTrue(jarFile.delete());
140         assertFalse(jarFile.exists());
141     }
142 
143     File makeRandomJarFile() {
144         File outputDir = new File(CoreTestConstants.OUTPUT_DIR_PREFIX);
145         outputDir.mkdirs();
146         int randomPart = RandomUtil.getPositiveInt();
147         return new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "foo-" + randomPart + ".jar");
148     }
149 
150     private void fillInJarFile(File jarFile, String jarEntryName) throws IOException {
151         fillInJarFile(jarFile, jarEntryName, null);
152     }
153 
154     private void fillInJarFile(File jarFile, String jarEntryName1, String jarEntryName2) throws IOException {
155         JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile));
156         jos.putNextEntry(new ZipEntry(jarEntryName1));
157         jos.write("<x/>".getBytes());
158         jos.closeEntry();
159         if (jarEntryName2 != null) {
160             jos.putNextEntry(new ZipEntry(jarEntryName2));
161             jos.write("<y/>".getBytes());
162             jos.closeEntry();
163         }
164         jos.close();
165     }
166 
167     URL asURL(File jarFile, String jarEntryName) throws IOException {
168         URL innerURL = jarFile.toURI().toURL();
169         return new URL("jar:" + innerURL + "!/" + jarEntryName);
170     }
171 
172 }