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;
28 import com.google.gson.Gson;
30 import org.hedgecode.snooker.gson.SnookerGsonBuilder;
32 import org.junit.Assert;
35 * Abstract Test class with input JSON from file.
37 * @author Dmitry Samoshin aka gotty
39 public abstract class AbstractJsonTest extends Assert {
41 private static final String JSON_EXT = ".json";
42 private static final String SERIALIZE_EXT = ".ser";
44 protected static final Gson GSON = SnookerGsonBuilder.build();
46 private final File jsonFile =
48 getClass().getResource(
49 getClass().getSimpleName() + JSON_EXT
52 private final File serFile =
54 jsonFile.getParent() + File.separator
55 + getClass().getSimpleName() + SERIALIZE_EXT
58 public static void assertNull(JsonIdEntity expected, JsonIdEntity[] actuals) {
59 if (actuals.length == 1 && actuals[0] == null) {
64 protected String jsonFromFile() throws IOException {
65 StringBuilder sb = new StringBuilder();
66 try (BufferedReader br =
69 jsonFile.getAbsolutePath()
73 String line = br.readLine();
74 while (line != null) {
82 protected void serialize(JsonSerializable jsonObject) throws IOException {
83 try (ObjectOutputStream oos =
84 new ObjectOutputStream(
86 serFile.getAbsolutePath()
90 oos.writeObject(jsonObject);
94 protected JsonSerializable deserialize() throws IOException, ClassNotFoundException {
95 JsonSerializable jsonObject;
96 try (ObjectInputStream ois =
97 new ObjectInputStream(
99 serFile.getAbsolutePath()
103 jsonObject = (JsonSerializable) ois.readObject();