1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, 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  
15  package ch.qos.logback.classic.blackbox.joran;
16  
17  import ch.qos.logback.classic.LoggerContext;
18  import ch.qos.logback.classic.blackbox.joran.spi.ConfigFileServlet;
19  import ch.qos.logback.classic.joran.JoranConfigurator;
20  import ch.qos.logback.core.CoreConstants;
21  import ch.qos.logback.core.joran.spi.HttpUtil;
22  import ch.qos.logback.core.joran.spi.JoranException;
23  import ch.qos.logback.core.testUtil.RandomUtil;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.InputStream;
27  import java.io.UnsupportedEncodingException;
28  import java.net.HttpURLConnection;
29  import java.net.MalformedURLException;
30  import java.util.concurrent.CountDownLatch;
31  
32  public class ReconfigureTaskTestSupport {
33  
34      protected int diff = RandomUtil.getPositiveInt();
35      protected LoggerContext loggerContext = new LoggerContext();
36  
37      protected void configure(InputStream is) throws JoranException {
38          JoranConfigurator jc = new JoranConfigurator();
39          jc.setContext(loggerContext);
40          jc.doConfigure(is);
41      }
42  
43      protected CountDownLatch registerChangeDetectedListener() {
44          CountDownLatch latch = new CountDownLatch(1);
45          ChangeDetectedListener changeDetectedListener = new ChangeDetectedListener(latch);
46          loggerContext.addConfigurationEventListener(changeDetectedListener);
47          return latch;
48      }
49  
50      protected static ByteArrayInputStream asBAIS(String configurationStr) throws UnsupportedEncodingException {
51          return new ByteArrayInputStream(configurationStr.getBytes("UTF-8"));
52      }
53  
54      protected String get(String urlString) throws MalformedURLException {
55          HttpUtil httpGetUtil = new HttpUtil(HttpUtil.RequestMethod.GET, urlString);
56          HttpURLConnection getConnection = httpGetUtil.connectTextTxt();
57          String response = httpGetUtil.readResponse(getConnection);
58          return response;
59      }
60  
61      protected String post(String urlString, String val) throws MalformedURLException {
62          HttpUtil httpPostUtil1 = new HttpUtil(HttpUtil.RequestMethod.POST, urlString);
63          HttpURLConnection postConnection1 = httpPostUtil1.connectTextTxt();
64          httpPostUtil1.post(postConnection1, ConfigFileServlet.CONTENT_KEY+ CoreConstants.EQUALS_CHAR+val);
65          String response = httpPostUtil1.readResponse(postConnection1);
66          return response;
67      }
68  
69      protected CountDownLatch registerPartialConfigurationEndedSuccessfullyEventListener() {
70          CountDownLatch latch = new CountDownLatch(1);
71          PartialConfigurationEndedSuccessfullyEventListener listener = new PartialConfigurationEndedSuccessfullyEventListener(latch);
72          loggerContext.addConfigurationEventListener(listener);
73          return latch;
74      }
75  }