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