2 * Copyright (c) 2017. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.snooker.json;
19 import java.io.BufferedReader;
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;
29 import com.google.gson.Gson;
31 import org.hedgecode.snooker.gson.SnookerGsonBuilder;
33 import org.junit.Assert;
36 * Abstract Test class with input JSON from file.
38 * @author Dmitry Samoshin aka gotty
40 public abstract class AbstractJsonTest extends Assert {
42 private static final String JSON_EXT = ".json";
43 private static final String SERIALIZE_EXT = ".ser";
45 protected static final Gson GSON = SnookerGsonBuilder.build();
47 private final File jsonFile =
49 getClass().getResource(
50 getClass().getSimpleName() + JSON_EXT
53 private final File serializeFile =
55 jsonFile.getParent() + File.separator
56 + getClass().getSimpleName() + SERIALIZE_EXT
59 public static void assertNull(JsonIdEntity expected, JsonIdEntity[] actuals) {
60 if (actuals.length == 1 && actuals[0] == null) {
65 protected String jsonFromFile() throws IOException {
66 StringBuilder sb = new StringBuilder();
67 try (BufferedReader br =
70 jsonFile.getAbsolutePath()
74 String line = br.readLine();
75 while (line != null) {
83 protected void serialize(Serializable serializeObject) throws IOException {
84 try (ObjectOutputStream oos =
85 new ObjectOutputStream(
87 serializeFile.getAbsolutePath()
91 oos.writeObject(serializeObject);
95 protected Serializable deserialize() throws IOException, ClassNotFoundException {
96 Serializable deserializeObject;
97 try (ObjectInputStream ois =
98 new ObjectInputStream(
100 serializeFile.getAbsolutePath()
104 deserializeObject = (Serializable) ois.readObject();
106 return deserializeObject;