[LIB-2] Add snooker-score-api source files
[snooker-score-api.git] / src / test / java / org / hedgecode / snooker / json / AbstractJsonTest.java
1 /*
2  * Copyright (c) 2017. 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
28 import com.google.gson.Gson;
29
30 import org.hedgecode.snooker.gson.SnookerGsonBuilder;
31
32 import org.junit.Assert;
33
34 /**
35  * Abstract Test class with input JSON from file.
36  *
37  * @author Dmitry Samoshin aka gotty
38  */
39 public abstract class AbstractJsonTest extends Assert {
40
41     private static final String JSON_EXT = ".json";
42     private static final String SERIALIZE_EXT = ".ser";
43
44     protected static final Gson GSON = SnookerGsonBuilder.build();
45
46     private final File jsonFile =
47             new File(
48                     getClass().getResource(
49                             getClass().getSimpleName() + JSON_EXT
50                     ).getFile()
51             );
52     private final File serFile =
53             new File(
54                     jsonFile.getParent() + File.separator
55                             + getClass().getSimpleName() + SERIALIZE_EXT
56             );
57
58     public static void assertNull(JsonIdEntity expected, JsonIdEntity[] actuals) {
59         if (actuals.length == 1 && actuals[0] == null) {
60                 assertNull(expected);
61         }
62     }
63
64     protected String jsonFromFile() throws IOException {
65         StringBuilder sb = new StringBuilder();
66         try (BufferedReader br =
67                      new BufferedReader(
68                              new FileReader(
69                                      jsonFile.getAbsolutePath()
70                              )
71                      )
72         ) {
73             String line = br.readLine();
74             while (line != null) {
75                 sb.append(line);
76                 line = br.readLine();
77             }
78         }
79         return sb.toString();
80     }
81
82     protected void serialize(JsonSerializable jsonObject) throws IOException {
83         try (ObjectOutputStream oos =
84                      new ObjectOutputStream(
85                              new FileOutputStream(
86                                      serFile.getAbsolutePath()
87                              )
88                      )
89         ) {
90             oos.writeObject(jsonObject);
91         }
92     }
93
94     protected JsonSerializable deserialize() throws IOException, ClassNotFoundException {
95         JsonSerializable jsonObject;
96         try (ObjectInputStream ois =
97                      new ObjectInputStream(
98                              new FileInputStream(
99                                      serFile.getAbsolutePath()
100                              )
101                      )
102         ) {
103             jsonObject = (JsonSerializable) ois.readObject();
104         }
105         return jsonObject;
106     }
107
108 }