Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
BukkitObjectStreamTest.java
1 package org.bukkit.util.io;
2 
3 import static org.hamcrest.Matchers.*;
4 import static org.junit.Assert.*;
5 
6 import java.io.ByteArrayInputStream;
7 import java.io.ByteArrayOutputStream;
8 import java.io.IOException;
9 import java.io.ObjectInputStream;
10 import java.io.ObjectOutputStream;
11 import java.util.List;
12 
13 import org.bukkit.Color;
17 import org.bukkit.util.Vector;
18 import org.junit.Ignore;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.junit.runners.Parameterized;
22 import org.junit.runners.Parameterized.Parameter;
23 import org.junit.runners.Parameterized.Parameters;
24 import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
25 
26 import com.google.common.collect.ImmutableList;
27 
28 @RunWith(Parameterized.class)
29 public class BukkitObjectStreamTest {
30 
31  @Parameters(name= "{index}: {0}")
32  public static List<Object[]> data() {
33  return ImmutableList.<Object[]>of(
34  new Object[] {
35  Color.class.getName(),
36  "rO0ABXNyADZjb20uZ29vZ2xlLmNvbW1vbi5jb2xsZWN0LkltbXV0YWJsZUxpc3QkU2VyaWFsaXplZEZvcm0AAAAAAAAAAAIAAVsACGVsZW1lbnRzdAATW0xqYXZhL2xhbmcvT2JqZWN0O3hwdXIAE1tMamF2YS5sYW5nLk9iamVjdDuQzlifEHMpbAIAAHhwAAAABXNyABpvcmcuYnVra2l0LnV0aWwuaW8uV3JhcHBlcvJQR+zxEm8FAgABTAADbWFwdAAPTGphdmEvdXRpbC9NYXA7eHBzcgA1Y29tLmdvb2dsZS5jb21tb24uY29sbGVjdC5JbW11dGFibGVNYXAkU2VyaWFsaXplZEZvcm0AAAAAAAAAAAIAAlsABGtleXNxAH4AAVsABnZhbHVlc3EAfgABeHB1cQB+AAMAAAAEdAACPT10AANSRUR0AARCTFVFdAAFR1JFRU51cQB+AAMAAAAEdAAFQ29sb3JzcgARamF2YS5sYW5nLkludGVnZXIS4qCk94GHOAIAAUkABXZhbHVleHIAEGphdmEubGFuZy5OdW1iZXKGrJUdC5TgiwIAAHhwAAAA/3NxAH4AEQAAAP9zcQB+ABEAAAD/c3EAfgAFc3EAfgAIdXEAfgADAAAABHEAfgALcQB+AAxxAH4ADXEAfgAOdXEAfgADAAAABHEAfgAQc3EAfgARAAAAAHNxAH4AEQAAAIBzcQB+ABEAAACAc3EAfgAFc3EAfgAIdXEAfgADAAAABHEAfgALcQB+AAxxAH4ADXEAfgAOdXEAfgADAAAABHEAfgAQc3EAfgARAAAAgHNxAH4AEQAAAIBxAH4AGnNxAH4ABXNxAH4ACHVxAH4AAwAAAARxAH4AC3EAfgAMcQB+AA1xAH4ADnVxAH4AAwAAAARxAH4AEHNxAH4AEQAAAP9xAH4AGnEAfgAac3EAfgAFc3EAfgAIdXEAfgADAAAABHEAfgALcQB+AAxxAH4ADXEAfgAOdXEAfgADAAAABHEAfgAQc3EAfgARAAAA/3EAfgAac3EAfgARAAAApQ==",
37  ImmutableList.of(
38  Color.WHITE,
39  Color.TEAL,
40  Color.PURPLE,
41  Color.RED,
43  )
44  },
45  new Object[] {
46  FireworkEffect.class.getName(),
47  "rO0ABXNyADZjb20uZ29vZ2xlLmNvbW1vbi5jb2xsZWN0LkltbXV0YWJsZUxpc3QkU2VyaWFsaXplZEZvcm0AAAAAAAAAAAIAAVsACGVsZW1lbnRzdAATW0xqYXZhL2xhbmcvT2JqZWN0O3hwdXIAE1tMamF2YS5sYW5nLk9iamVjdDuQzlifEHMpbAIAAHhwAAAAA3NyABpvcmcuYnVra2l0LnV0aWwuaW8uV3JhcHBlcvJQR+zxEm8FAgABTAADbWFwdAAPTGphdmEvdXRpbC9NYXA7eHBzcgA1Y29tLmdvb2dsZS5jb21tb24uY29sbGVjdC5JbW11dGFibGVNYXAkU2VyaWFsaXplZEZvcm0AAAAAAAAAAAIAAlsABGtleXNxAH4AAVsABnZhbHVlc3EAfgABeHB1cQB+AAMAAAAGdAACPT10AAdmbGlja2VydAAFdHJhaWx0AAZjb2xvcn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AEcQB+AB9zcQB+ACAAAACAcQB+ACJxAH4AInEAfgA/dAAHQ1JFRVBFUg==",
48  ImmutableList.of(
50  .withColor(Color.BLACK, Color.SILVER)
51  .with(Type.BALL_LARGE)
52  .withFade(Color.WHITE)
53  .withFlicker()
54  .build(),
56  .withColor(Color.NAVY)
57  .build(),
59  .withColor(Color.MAROON)
60  .withTrail()
61  .withFlicker()
62  .with(Type.CREEPER)
63  .build()
64  ),
65  },
66  new Object[] {
67  Vector.class.getName(),
68  "rO0ABXNyADZjb20uZ29vZ2xlLmNvbW1vbi5jb2xsZWN0LkltbXV0YWJsZUxpc3QkU2VyaWFsaXplZEZvcm0AAAAAAAAAAAIAAVsACGVsZW1lbnRzdAATW0xqYXZhL2xhbmcvT2JqZWN0O3hwdXIAE1tMamF2YS5sYW5nLk9iamVjdDuQzlifEHMpbAIAAHhwAAAABHNyABpvcmcuYnVra2l0LnV0aWwuaW8uV3JhcHBlcvJQR+zxEm8FAgABTAADbWFwdAAPTGphdmEvdXRpbC9NYXA7eHBzcgA1Y29tLmdvb2dsZS5jb21tb24uY29sbGVjdC5JbW11dGFibGVNYXAkU2VyaWFsaXplZEZvcm0AAAAAAAAAAAIAAlsABGtleXNxAH4AAVsABnZhbHVlc3EAfgABeHB1cQB+AAMAAAAEdAACPT10AAF4dAABeXQAAXp1cQB+AAMAAAAEdAAGVmVjdG9yc3IAEGphdmEubGFuZy5Eb3VibGWAs8JKKWv7BAIAAUQABXZhbHVleHIAEGphdmEubGFuZy5OdW1iZXKGrJUdC5TgiwIAAHhwAAAAAAAAAABzcQB+ABEAAAAAAAAAAHNxAH4AEQAAAAAAAAAAc3EAfgAFc3EAfgAIdXEAfgADAAAABHEAfgALcQB+AAxxAH4ADXEAfgAOdXEAfgADAAAABHEAfgAQc3EAfgARQIOFwo9cKPZzcQB+ABFAtCKcKPXCj3NxAH4AEUBzrpeNT987c3EAfgAFc3EAfgAIdXEAfgADAAAABHEAfgALcQB+AAxxAH4ADXEAfgAOdXEAfgADAAAABHEAfgAQc3EAfgARwEQTMzMzMzNzcQB+ABFASYAAAAAAAHNxAH4AEcCjqG3UQTVUc3EAfgAFc3EAfgAIdXEAfgADAAAABHEAfgALcQB+AAxxAH4ADXEAfgAOdXEAfgADAAAABHEAfgAQc3EAfgARQd/////AAABzcQB+ABHB4AAAAAAAAHNxAH4AEQAAAAAAAAAA",
69  ImmutableList.of(
70  new Vector(0, 0, 0),
71  new Vector(624.72, 5154.61, 314.912),
72  new Vector(-40.15, 51, -2516.21451),
73  new Vector(Integer.MAX_VALUE, Integer.MIN_VALUE, 0)
74  )
75  });
76  }
77 
78  @Parameter(0)
79  public String className;
80 
81  @Parameter(1)
82  public String preEncoded;
83 
84  @Parameter(2)
85  public List<ConfigurationSerializable> object;
86 
87  @Test
88  public void checkSerlialization() throws Throwable {
89  // If this test fails, you may start your trek to debug by commenting the '@Ignore' on the next method
90  // (and of course, you would read those comments too)
91  final ByteArrayOutputStream out = new ByteArrayOutputStream();
92  ObjectOutputStream oos = null;
93  try {
94  oos = new BukkitObjectOutputStream(out);
95  oos.writeObject(object);
96  } finally {
97  if (oos != null) {
98  try {
99  oos.close();
100  } catch (IOException e) {
101  }
102  }
103  }
104 
105  final byte[] preEncodedArray = Base64Coder.decode(preEncoded);
106 
107  final Object readBack;
108  final Object preEncoded;
109 
110  ObjectInputStream ois = null;
111  ObjectInputStream preois = null;
112  try {
113  ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
114  ByteArrayInputStream preIn = new ByteArrayInputStream(preEncodedArray);
115  ois = new BukkitObjectInputStream(in);
116  preois = new BukkitObjectInputStream(preIn);
117 
118  readBack = ois.readObject();
119  preEncoded = preois.readObject();
120  } finally {
121  if (ois != null) {
122  try {
123  ois.close();
124  } catch (IOException ex) {
125  }
126  }
127  if (preois != null) {
128  try {
129  preois.close();
130  } catch (IOException ex) {
131  }
132  }
133  }
134 
135  assertThat(object, is(readBack));
136  assertThat(object, is(preEncoded));
137  }
138 
139  @Ignore
140  @Test
141  public void preEncoded() throws Throwable {
142  // This test is placed in the case that a necessary change is made to change the encoding format
143  // Just remove the ignore (or run manually) and it'll give you the new pre-encoded values
144 
145  // It really does not matter if the encoded array is different per system (hence why this test is set to not run),
146  // as long as all systems can deserialize it.
147 
148  // The entire reason the pre-encoded string was added is to make a build (test) fail if someone accidentally makes it not backward-compatible
149 
150  final ByteArrayOutputStream out = new ByteArrayOutputStream();
151  ObjectOutputStream oos = null;
152  try {
153  oos = new BukkitObjectOutputStream(out);
154  oos.writeObject(object);
155  oos.flush();
156  } finally {
157  if (oos != null) {
158  try {
159  oos.close();
160  } catch (IOException e) {
161  }
162  }
163  }
164 
165  final String string = new String(Base64Coder.encode(out.toByteArray()));
166  try {
167  assertThat(preEncoded, is(string));
168  } catch (Throwable t) {
169  System.out.println(className + ": \"" + string + "\"");
170  throw t;
171  }
172  }
173 }
static final Color SILVER
Definition: Color.java:28
static final Color RED
Definition: Color.java:43
static final Color BLACK
Definition: Color.java:38
static final Color PURPLE
Definition: Color.java:98
static final Color TEAL
Definition: Color.java:78
static final Color ORANGE
Definition: Color.java:103
static final Color NAVY
Definition: Color.java:88
static final Color WHITE
Definition: Color.java:23
static final Color MAROON
Definition: Color.java:48