View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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.joran.serializedModel;
16  
17  import ch.qos.logback.classic.LoggerContext;
18  import ch.qos.logback.classic.model.ConfigurationModel;
19  import ch.qos.logback.classic.model.LoggerModel;
20  import ch.qos.logback.core.model.Model;
21  import ch.qos.logback.core.net.HardenedObjectInputStream;
22  import org.junit.jupiter.api.AfterEach;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  import java.io.IOException;
29  import java.io.ObjectOutputStream;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  
33  public class ModelSerializationTest {
34  
35  
36      ByteArrayOutputStream bos;
37      ObjectOutputStream oos;
38      HardenedObjectInputStream inputStream;
39  
40      LoggerContext loggerContext = new LoggerContext();
41  
42      //String[] whitelist = new String[] {  };
43  
44  
45      @BeforeEach
46      public void setUp() throws Exception {
47          bos = new ByteArrayOutputStream();
48          oos = new ObjectOutputStream(bos);
49      }
50  
51      @AfterEach
52      public void tearDown() throws Exception {
53      }
54  
55      @Test
56      public void smoke() throws ClassNotFoundException, IOException {
57          ConfigurationModel configurationModel = new ConfigurationModel();
58          configurationModel.setTag("configuration");
59          configurationModel.setDebugStr("true");
60  
61          LoggerModel loggerModel = new LoggerModel();
62          loggerModel.setTag("logger");
63          loggerModel.setLevel("DEBUG");
64          configurationModel.addSubModel(loggerModel);
65  
66          Model back = writeAndRead(configurationModel);
67          assertEquals(configurationModel, back);
68      }
69  
70      private Model writeAndRead(Model model) throws IOException, ClassNotFoundException {
71          writeObject(oos, model);
72          ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
73          inputStream = new HardenedModelInputStream(loggerContext, bis);
74          Model fooBack = (Model) inputStream.readObject();
75          inputStream.close();
76          return fooBack;
77      }
78  
79      private void writeObject(ObjectOutputStream oos, Object o) throws IOException {
80          oos.writeObject(o);
81          oos.flush();
82          oos.close();
83      }
84  }