|
17 | 17 | import ch.qos.logback.classic.Level;
|
18 | 18 | import ch.qos.logback.classic.Logger;
|
19 | 19 | import ch.qos.logback.classic.LoggerContext;
|
20 |
| -import ch.qos.logback.classic.joran.JoranConfigurator; |
21 |
| -//import ch.qos.logback.classic.blackbox.joran.ReconfigureOnChangeTaskTest; |
22 |
| -//import ch.qos.logback.classic.blackbox.joran.ReconfigureOnChangeTaskTest; |
| 20 | +import ch.qos.logback.classic.blackbox.joran.ReconfigureOnChangeTaskTest; |
| 21 | +import ch.qos.logback.classic.blackbox.joran.ReconfigureTaskTestSupport; |
23 | 22 | import ch.qos.logback.core.CoreConstants;
|
24 | 23 | import ch.qos.logback.core.joran.spi.ConfigurationWatchList;
|
25 | 24 | import ch.qos.logback.core.joran.spi.HttpUtil;
|
|
32 | 31 | import org.junit.jupiter.api.Test;
|
33 | 32 | import org.slf4j.LoggerFactory;
|
34 | 33 |
|
35 |
| -import java.io.ByteArrayInputStream; |
36 |
| -import java.io.InputStream; |
37 | 34 | import java.io.UnsupportedEncodingException;
|
38 | 35 | import java.net.HttpURLConnection;
|
39 | 36 | import java.net.MalformedURLException;
|
40 | 37 | import java.net.URL;
|
41 | 38 | import java.util.concurrent.CountDownLatch;
|
| 39 | +import java.util.concurrent.TimeUnit; |
42 | 40 |
|
43 | 41 | import static org.junit.jupiter.api.Assertions.*;
|
44 | 42 |
|
45 |
| -public class ConfigurationWatchListTest { |
46 |
| - |
47 |
| - |
48 |
| -// static String BAZINGA_LOGGER_0 = "logback.logger.com.bazinga=WARN"; |
49 |
| -// static String BAZINGA_LOGGER_1 = "logback.logger.com.bazinga=ERROR" |
50 |
| -// |
51 |
| -// int randomPort = RandomUtil.getRandomServerPort(); |
52 |
| -// ConfigEmbeddedJetty configEmbeddedJetty; |
53 |
| -// LoggerContext loggerContext = new LoggerContext(); |
54 |
| -// ConfigurationWatchList cwl = new ConfigurationWatchList(); |
55 |
| -// String urlString = "http://127.0.0.1:"+randomPort+"/"; |
56 |
| -// |
57 |
| -// @BeforeEach |
58 |
| -// public void setUp() throws Exception { |
59 |
| -// Logger rootLogger = (Logger) LoggerFactory.getLogger( Logger.ROOT_LOGGER_NAME ); |
60 |
| -// rootLogger.setLevel(Level.INFO); |
61 |
| -// |
62 |
| -// configEmbeddedJetty = new ConfigEmbeddedJetty(randomPort); |
63 |
| -// |
64 |
| -// cwl.setContext(loggerContext); |
65 |
| -// |
66 |
| -// HttpServlet configServlet = new ConfigFileServlet(); |
67 |
| -// configEmbeddedJetty.getServletMap().put("/", configServlet); |
68 |
| -// //configEmbeddedJetty.getServletMap().put("/mod", configServlet); |
69 |
| -// |
70 |
| -// configEmbeddedJetty.init(); |
71 |
| -// |
72 |
| -// } |
73 |
| -// |
74 |
| -// @AfterEach |
75 |
| -// public void tearDown() throws Exception { |
76 |
| -// configEmbeddedJetty.stop(); |
77 |
| -// } |
78 |
| -// |
79 |
| -// @Test |
80 |
| -// public void testInfrastructure() throws MalformedURLException { |
81 |
| -// HttpUtil httpGetUtil0 = new HttpUtil(HttpUtil.RequestMethod.GET, urlString); |
82 |
| -// |
83 |
| -// HttpURLConnection getConnection0 = httpGetUtil0.connectTextTxt(); |
84 |
| -// String response = httpGetUtil0.readResponse(getConnection0); |
85 |
| -// assertNotNull(response); |
86 |
| -// Assertions.assertEquals(ConfigFileServlet.DEFAULT_CONTENT, response); |
87 |
| -// |
88 |
| -// HttpUtil httpPostUtil1 = new HttpUtil(HttpUtil.RequestMethod.POST, urlString); |
89 |
| -// HttpURLConnection postConnection1 = httpPostUtil1.connectTextTxt(); |
90 |
| -// String setResponse1 = "bla bla"; |
91 |
| -// httpPostUtil1.post(postConnection1, ConfigFileServlet.CONTENT_KEY+ CoreConstants.EQUALS_CHAR+setResponse1); |
92 |
| -// |
93 |
| -// String response1 = httpPostUtil1.readResponse(postConnection1); |
94 |
| -// assertEquals(response1, setResponse1); |
95 |
| -// //System.out.println( "POST response1="+response1); |
96 |
| -// HttpUtil httpGetUtil2 = new HttpUtil(HttpUtil.RequestMethod.GET, urlString); |
97 |
| -// |
98 |
| -// HttpURLConnection getConnection2 = httpGetUtil2.connectTextTxt(); |
99 |
| -// String response2 = httpGetUtil2.readResponse(getConnection2); |
100 |
| -// assertEquals(response1, response2); |
101 |
| -// |
102 |
| -// } |
103 |
| -// |
104 |
| -// @Test |
105 |
| -// public void smoke() throws MalformedURLException { |
106 |
| -// URL url = new URL(urlString); |
107 |
| -// cwl.addToWatchList(url); |
108 |
| -// URL changedURL0 = cwl.changeDetectedInURL(); |
109 |
| -// assertNull(changedURL0); |
110 |
| -// HttpUtil httpPostUtil1 = new HttpUtil(HttpUtil.RequestMethod.POST, urlString); |
111 |
| -// HttpURLConnection postConnection1 = httpPostUtil1.connectTextTxt(); |
112 |
| -// String setResponse1 = "bla bla"; |
113 |
| -// httpPostUtil1.post(postConnection1, ConfigFileServlet.CONTENT_KEY+ CoreConstants.EQUALS_CHAR+setResponse1); |
114 |
| -// |
115 |
| -// String response1 = httpPostUtil1.readResponse(postConnection1); |
116 |
| -// assertEquals(response1, setResponse1); |
117 |
| -// URL changedURL1 = cwl.changeDetectedInURL(); |
118 |
| -// assertEquals(urlString, changedURL1.toString()); |
119 |
| -// |
120 |
| -// URL changedURL2 = cwl.changeDetectedInURL(); |
121 |
| -// assertNull(changedURL2); |
122 |
| -// |
123 |
| -// URL changedURL3 = cwl.changeDetectedInURL(); |
124 |
| -// assertNull(changedURL3); |
125 |
| -// } |
126 |
| -// |
127 |
| -// |
128 |
| -// @Test |
129 |
| -// public void propertiesFromHTTP() throws UnsupportedEncodingException, JoranException { |
130 |
| -// String loggerName = "com.bazinga"; |
131 |
| -// String propertiesURLStr = "https://127.0.0.1:"+randomPort+"/"; |
132 |
| -// Logger aLogger = loggerContext.getLogger(loggerName); |
133 |
| -// String configurationStr = "<configuration debug=\"true\" scan=\"true\" scanPeriod=\"10 millisecond\"><propertiesConfigurator url=\"" + propertiesURLStr + "\"/></configuration>"; |
134 |
| -// |
135 |
| -// configure(asBAIS(configurationStr)); |
136 |
| -// |
137 |
| -// assertEquals(Level.WARN, aLogger.getLevel()); |
138 |
| -// System.out.println("first phase OK"); |
139 |
| -// CountDownLatch changeDetectedLatch0 = registerChangeDetectedListener(); |
140 |
| -// CountDownLatch configurationDoneLatch0 = registerPartialConfigurationEndedSuccessfullyEventListener(); |
141 |
| -// |
142 |
| -// changeDetectedLatch0.await(); |
143 |
| -// System.out.println("after changeDetectedLatch0.await();"); |
144 |
| -// configurationDoneLatch0.await(); |
145 |
| -// assertEquals(Level.ERROR, aLogger.getLevel()); |
146 |
| -// } |
147 |
| -// |
| 43 | +public class ConfigurationWatchListTest extends ReconfigureTaskTestSupport { |
| 44 | + |
| 45 | + static String BAZINGA_LOGGER_NAME = "com.bazinga"; |
| 46 | + static String BAZINGA_LOGGER_SETUP_0 = "logback.logger."+BAZINGA_LOGGER_NAME+"=WARN"; |
| 47 | + static String BAZINGA_LOGGER_SETUP_1 = "logback.logger."+BAZINGA_LOGGER_NAME+"=ERROR"; |
| 48 | + |
| 49 | + int randomPort = RandomUtil.getRandomServerPort(); |
| 50 | + ConfigEmbeddedJetty configEmbeddedJetty; |
| 51 | + ConfigurationWatchList cwl = new ConfigurationWatchList(); |
| 52 | + static String FOO_PROPERTIES = "/foo.properties"; |
| 53 | + String urlString = "http://127.0.0.1:"+randomPort+FOO_PROPERTIES; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + public void setUp() throws Exception { |
| 57 | + Logger rootLogger = (Logger) LoggerFactory.getLogger( Logger.ROOT_LOGGER_NAME ); |
| 58 | + rootLogger.setLevel(Level.INFO); |
| 59 | + |
| 60 | + configEmbeddedJetty = new ConfigEmbeddedJetty(randomPort); |
| 61 | + |
| 62 | + cwl.setContext(loggerContext); |
| 63 | + |
| 64 | + HttpServlet configServlet = new ConfigFileServlet(BAZINGA_LOGGER_SETUP_0); |
| 65 | + configEmbeddedJetty.getServletMap().put(FOO_PROPERTIES, configServlet); |
| 66 | + |
| 67 | + configEmbeddedJetty.init(); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @AfterEach |
| 72 | + public void tearDown() throws Exception { |
| 73 | + configEmbeddedJetty.stop(); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testInfrastructure() throws MalformedURLException { |
| 80 | + String response = get(urlString); |
| 81 | + assertNotNull(response); |
| 82 | + Assertions.assertEquals(BAZINGA_LOGGER_SETUP_0, response); |
| 83 | + |
| 84 | + String setResponse1 = "bla bla"; |
| 85 | + String response1 = post(urlString, setResponse1); |
| 86 | + assertEquals(response1, setResponse1); |
| 87 | + |
| 88 | + String response2 = get(urlString); |
| 89 | + assertEquals(response1, response2); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void smoke() throws MalformedURLException { |
| 94 | + URL url = new URL(urlString); |
| 95 | + cwl.addToWatchList(url); |
| 96 | + URL changedURL0 = cwl.changeDetectedInURL(); |
| 97 | + assertNull(changedURL0); |
| 98 | + |
| 99 | + String setResponse1 = "bla bla"; |
| 100 | + String response1 = post(urlString, setResponse1); |
| 101 | + assertEquals(response1, setResponse1); |
| 102 | + |
| 103 | + URL changedURL1 = cwl.changeDetectedInURL(); |
| 104 | + assertEquals(urlString, changedURL1.toString()); |
| 105 | + |
| 106 | + URL changedURL2 = cwl.changeDetectedInURL(); |
| 107 | + assertNull(changedURL2); |
| 108 | + |
| 109 | + URL changedURL3 = cwl.changeDetectedInURL(); |
| 110 | + assertNull(changedURL3); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void propertiesFromHTTP() throws UnsupportedEncodingException, JoranException, InterruptedException, MalformedURLException { |
| 115 | + |
| 116 | + String propertiesURLStr = urlString; |
| 117 | + Logger bazingaLogger = loggerContext.getLogger(BAZINGA_LOGGER_NAME); |
| 118 | + |
| 119 | + assertEquals(BAZINGA_LOGGER_SETUP_0, get(urlString)); |
| 120 | + |
| 121 | + String configurationStr = "<configuration debug=\"true\" scan=\"true\" scanPeriod=\"1 millisecond\"><propertiesConfigurator url=\"" + propertiesURLStr + "\"/></configuration>"; |
| 122 | + |
| 123 | + configure(asBAIS(configurationStr)); |
| 124 | + |
| 125 | + // allow for the first update |
| 126 | + Thread.sleep(50); |
| 127 | + assertEquals(Level.WARN, bazingaLogger.getLevel()); |
| 128 | + System.out.println("first test passed with success"); |
| 129 | + |
| 130 | + CountDownLatch changeDetectedLatch0 = registerChangeDetectedListener(); |
| 131 | + CountDownLatch configurationDoneLatch0 = registerPartialConfigurationEndedSuccessfullyEventListener(); |
| 132 | + |
| 133 | + String response1 = post(urlString, BAZINGA_LOGGER_SETUP_1); |
| 134 | + assertEquals(BAZINGA_LOGGER_SETUP_1, get(urlString)); |
| 135 | + |
| 136 | + changeDetectedLatch0.await(100, TimeUnit.MICROSECONDS); |
| 137 | + configurationDoneLatch0.await(100, TimeUnit.MICROSECONDS); |
| 138 | + assertEquals(Level.ERROR, bazingaLogger.getLevel()); |
| 139 | + } |
148 | 140 |
|
149 | 141 | }
|
0 commit comments