1
2
3
4
5
6
7
8
9
10
11
12
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
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 }