[LIB-10] SerialVersionUID for Serializable classes
[snooker-score-api.git] / src / test / java / org / hedgecode / snooker / json / AbstractJsonTest.java
1 /*
2  * Copyright (c) 2017-2020. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.snooker.json;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.io.Serializable;
28
29 import com.google.gson.Gson;
30
31 import org.hedgecode.snooker.gson.SnookerGsonBuilder;
32
33 import org.junit.Assert;
34
35 /**
36  * Abstract Test class with input JSON from file.
37  *
38  * @author Dmitry Samoshin aka gotty
39  */
40 public abstract class AbstractJsonTest extends Assert {
41
42     private static final String JSON_EXT = ".json";
43     private static final String SERIALIZE_EXT = ".ser";
44
45     protected static final Gson GSON = SnookerGsonBuilder.build();
46
47     private final File jsonFile =
48             new File(
49                     getClass().getResource(
50                             getClass().getSimpleName() + JSON_EXT
51                     ).getFile()
52             );
53     private final File serializeFile =
54             new File(
55                     jsonFile.getParent() + File.separator
56                             + getClass().getSimpleName() + SERIALIZE_EXT
57             );
58
59     public static void assertNull(JsonIdEntity expected, JsonIdEntity[] actuals) {
60         if (actuals.length == 1 && actuals[0] == null) {
61                 assertNull(expected);
62         }
63     }
64
65     protected String jsonFromFile() throws IOException {
66         StringBuilder sb = new StringBuilder();
67         try (BufferedReader br =
68                      new BufferedReader(
69                              new FileReader(
70                                      jsonFile.getAbsolutePath()
71                              )
72                      )
73         ) {
74             String line = br.readLine();
75             while (line != null) {
76                 sb.append(line);
77                 line = br.readLine();
78             }
79         }
80         return sb.toString();
81     }
82
83     protected void verifySerialize(Serializable serializeObject) throws IOException {
84         if (!serializeFile.exists()) {
85             serialize(serializeObject);
86         }
87     }
88
89     protected void serialize(Serializable serializeObject) throws IOException {
90         try (ObjectOutputStream oos =
91                      new ObjectOutputStream(
92                              new FileOutputStream(
93                                      serializeFile.getAbsolutePath()
94                              )
95                      )
96         ) {
97             oos.writeObject(serializeObject);
98         }
99     }
100
101     protected Serializable deserialize() throws IOException, ClassNotFoundException {
102         Serializable deserializeObject;
103         try (ObjectInputStream ois =
104                      new ObjectInputStream(
105                              new FileInputStream(
106                                      serializeFile.getAbsolutePath()
107                              )
108                      )
109         ) {
110             deserializeObject = (Serializable) ois.readObject();
111         }
112         return deserializeObject;
113     }
114
115 }