Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ColorTest.java
1 package org.bukkit;
2 
3 import static org.hamcrest.Matchers.*;
4 import static org.junit.Assert.*;
5 
7 import org.junit.Test;
8 
9 @SuppressWarnings("javadoc")
10 public class ColorTest {
11  static class TestColor {
12  static int id = 0;
13  final String name;
14  final int rgb;
15  final int bgr;
16  final int r;
17  final int g;
18  final int b;
19 
20  TestColor(int rgb, int bgr, int r, int g, int b) {
21  this.rgb = rgb;
22  this.bgr = bgr;
23  this.r = r;
24  this.g = g;
25  this.b = b;
26  this.name = id + ":" + Integer.toHexString(rgb).toUpperCase() + "_" + Integer.toHexString(bgr).toUpperCase() + "-r" + Integer.toHexString(r).toUpperCase() + "-g" + Integer.toHexString(g).toUpperCase() + "-b" + Integer.toHexString(b).toUpperCase();
27  }
28  }
29 
30  static TestColor[] examples = new TestColor[] {
31  /* 0xRRGGBB, 0xBBGGRR, 0xRR, 0xGG, 0xBB */
32  new TestColor(0xFFFFFF, 0xFFFFFF, 0xFF, 0xFF, 0xFF),
33  new TestColor(0xFFFFAA, 0xAAFFFF, 0xFF, 0xFF, 0xAA),
34  new TestColor(0xFF00FF, 0xFF00FF, 0xFF, 0x00, 0xFF),
35  new TestColor(0x67FF22, 0x22FF67, 0x67, 0xFF, 0x22),
36  new TestColor(0x000000, 0x000000, 0x00, 0x00, 0x00)
37  };
38 
39  @Test
40  public void testSerialization() throws Throwable {
41  for (TestColor testColor : examples) {
42  Color base = Color.fromRGB(testColor.rgb);
43 
44  YamlConfiguration toSerialize = new YamlConfiguration();
45  toSerialize.set("color", base);
46  String serialized = toSerialize.saveToString();
47 
48  YamlConfiguration deserialized = new YamlConfiguration();
49  deserialized.loadFromString(serialized);
50 
51  assertThat(testColor.name + " on " + serialized, base, is(deserialized.getColor("color")));
52  }
53  }
54 
55  // Equality tests
56  @Test
57  public void testEqualities() {
58  for (TestColor testColor : examples) {
59  Color fromRGB = Color.fromRGB(testColor.rgb);
60  Color fromBGR = Color.fromBGR(testColor.bgr);
61  Color fromRGBs = Color.fromRGB(testColor.r, testColor.g, testColor.b);
62  Color fromBGRs = Color.fromBGR(testColor.b, testColor.g, testColor.r);
63 
64  assertThat(testColor.name, fromRGB, is(fromRGBs));
65  assertThat(testColor.name, fromRGB, is(fromBGR));
66  assertThat(testColor.name, fromRGB, is(fromBGRs));
67  assertThat(testColor.name, fromRGBs, is(fromBGR));
68  assertThat(testColor.name, fromRGBs, is(fromBGRs));
69  assertThat(testColor.name, fromBGR, is(fromBGRs));
70  }
71  }
72 
73  @Test
74  public void testInequalities() {
75  for (int i = 1; i < examples.length; i++) {
76  TestColor testFrom = examples[i];
77  Color from = Color.fromRGB(testFrom.rgb);
78  for (int j = i - 1; j >= 0; j--) {
79  TestColor testTo = examples[j];
80  Color to = Color.fromRGB(testTo.rgb);
81  String name = testFrom.name + " to " + testTo.name;
82  assertThat(name, from, is(not(to)));
83 
84  Color transform = from.setRed(testTo.r).setBlue(testTo.b).setGreen(testTo.g);
85  assertThat(name, transform, is(not(sameInstance(from))));
86  assertThat(name, transform, is(to));
87  }
88  }
89  }
90 
91  // RGB tests
92  @Test
93  public void testRGB() {
94  for (TestColor testColor : examples) {
95  assertThat(testColor.name, Color.fromRGB(testColor.rgb).asRGB(), is(testColor.rgb));
96  assertThat(testColor.name, Color.fromBGR(testColor.bgr).asRGB(), is(testColor.rgb));
97  assertThat(testColor.name, Color.fromRGB(testColor.r, testColor.g, testColor.b).asRGB(), is(testColor.rgb));
98  assertThat(testColor.name, Color.fromBGR(testColor.b, testColor.g, testColor.r).asRGB(), is(testColor.rgb));
99  }
100  }
101 
102  @Test(expected=IllegalArgumentException.class)
103  public void testInvalidRGB1() {
104  Color.fromRGB(0x01000000);
105  }
106 
107  @Test(expected=IllegalArgumentException.class)
108  public void testInvalidRGB2() {
109  Color.fromRGB(Integer.MIN_VALUE);
110  }
111 
112  @Test(expected=IllegalArgumentException.class)
113  public void testInvalidRGB3() {
114  Color.fromRGB(Integer.MAX_VALUE);
115  }
116 
117  @Test(expected=IllegalArgumentException.class)
118  public void testInvalidRGB4() {
119  Color.fromRGB(-1);
120  }
121 
122  // BGR tests
123  @Test
124  public void testBGR() {
125  for (TestColor testColor : examples) {
126  assertThat(testColor.name, Color.fromRGB(testColor.rgb).asBGR(), is(testColor.bgr));
127  assertThat(testColor.name, Color.fromBGR(testColor.bgr).asBGR(), is(testColor.bgr));
128  assertThat(testColor.name, Color.fromRGB(testColor.r, testColor.g, testColor.b).asBGR(), is(testColor.bgr));
129  assertThat(testColor.name, Color.fromBGR(testColor.b, testColor.g, testColor.r).asBGR(), is(testColor.bgr));
130  }
131  }
132 
133  @Test(expected=IllegalArgumentException.class)
134  public void testInvalidBGR1() {
135  Color.fromBGR(0x01000000);
136  }
137 
138  @Test(expected=IllegalArgumentException.class)
139  public void testInvalidBGR2() {
140  Color.fromBGR(Integer.MIN_VALUE);
141  }
142 
143  @Test(expected=IllegalArgumentException.class)
144  public void testInvalidBGR3() {
145  Color.fromBGR(Integer.MAX_VALUE);
146  }
147 
148  @Test(expected=IllegalArgumentException.class)
149  public void testInvalidBGR4() {
150  Color.fromBGR(-1);
151  }
152 
153  // Red tests
154  @Test
155  public void testRed() {
156  for (TestColor testColor : examples) {
157  assertThat(testColor.name, Color.fromRGB(testColor.rgb).getRed(), is(testColor.r));
158  assertThat(testColor.name, Color.fromBGR(testColor.bgr).getRed(), is(testColor.r));
159  assertThat(testColor.name, Color.fromRGB(testColor.r, testColor.g, testColor.b).getRed(), is(testColor.r));
160  assertThat(testColor.name, Color.fromBGR(testColor.b, testColor.g, testColor.r).getRed(), is(testColor.r));
161  }
162  }
163 
164  @Test(expected=IllegalArgumentException.class)
165  public void testInvalidR01() {
166  Color.fromRGB(-1, 0x00, 0x00);
167  }
168 
169  @Test(expected=IllegalArgumentException.class)
170  public void testInvalidR02() {
171  Color.fromRGB(Integer.MAX_VALUE, 0x00, 0x00);
172  }
173 
174  @Test(expected=IllegalArgumentException.class)
175  public void testInvalidR03() {
176  Color.fromRGB(Integer.MIN_VALUE, 0x00, 0x00);
177  }
178 
179  @Test(expected=IllegalArgumentException.class)
180  public void testInvalidR04() {
181  Color.fromRGB(0x100, 0x00, 0x00);
182  }
183 
184  @Test(expected=IllegalArgumentException.class)
185  public void testInvalidR05() {
186  Color.fromBGR(0x00, 0x00, -1);
187  }
188 
189  @Test(expected=IllegalArgumentException.class)
190  public void testInvalidR06() {
191  Color.fromBGR(0x00, 0x00, Integer.MAX_VALUE);
192  }
193 
194  @Test(expected=IllegalArgumentException.class)
195  public void testInvalidR07() {
196  Color.fromBGR(0x00, 0x00, Integer.MIN_VALUE);
197  }
198 
199  @Test(expected=IllegalArgumentException.class)
200  public void testInvalidR08() {
201  Color.fromBGR(0x00, 0x00, 0x100);
202  }
203 
204  @Test(expected=IllegalArgumentException.class)
205  public void testInvalidR09() {
206  Color.WHITE.setRed(-1);
207  }
208 
209  @Test(expected=IllegalArgumentException.class)
210  public void testInvalidR10() {
211  Color.WHITE.setRed(Integer.MAX_VALUE);
212  }
213 
214  @Test(expected=IllegalArgumentException.class)
215  public void testInvalidR11() {
216  Color.WHITE.setRed(Integer.MIN_VALUE);
217  }
218 
219  @Test(expected=IllegalArgumentException.class)
220  public void testInvalidR12() {
221  Color.WHITE.setRed(0x100);
222  }
223 
224  // Blue tests
225  @Test
226  public void testBlue() {
227  for (TestColor testColor : examples) {
228  assertThat(testColor.name, Color.fromRGB(testColor.rgb).getBlue(), is(testColor.b));
229  assertThat(testColor.name, Color.fromBGR(testColor.bgr).getBlue(), is(testColor.b));
230  assertThat(testColor.name, Color.fromRGB(testColor.r, testColor.g, testColor.b).getBlue(), is(testColor.b));
231  assertThat(testColor.name, Color.fromBGR(testColor.b, testColor.g, testColor.r).getBlue(), is(testColor.b));
232  }
233  }
234 
235  @Test(expected=IllegalArgumentException.class)
236  public void testInvalidB01() {
237  Color.fromRGB(0x00, 0x00, -1);
238  }
239 
240  @Test(expected=IllegalArgumentException.class)
241  public void testInvalidB02() {
242  Color.fromRGB(0x00, 0x00, Integer.MAX_VALUE);
243  }
244 
245  @Test(expected=IllegalArgumentException.class)
246  public void testInvalidB03() {
247  Color.fromRGB(0x00, 0x00, Integer.MIN_VALUE);
248  }
249 
250  @Test(expected=IllegalArgumentException.class)
251  public void testInvalidB04() {
252  Color.fromRGB(0x00, 0x00, 0x100);
253  }
254 
255  @Test(expected=IllegalArgumentException.class)
256  public void testInvalidB05() {
257  Color.fromBGR(-1, 0x00, 0x00);
258  }
259 
260  @Test(expected=IllegalArgumentException.class)
261  public void testInvalidB06() {
262  Color.fromBGR(Integer.MAX_VALUE, 0x00, 0x00);
263  }
264 
265  @Test(expected=IllegalArgumentException.class)
266  public void testInvalidB07() {
267  Color.fromBGR(Integer.MIN_VALUE, 0x00, 0x00);
268  }
269 
270  @Test(expected=IllegalArgumentException.class)
271  public void testInvalidB08() {
272  Color.fromBGR(0x100, 0x00, 0x00);
273  }
274 
275  @Test(expected=IllegalArgumentException.class)
276  public void testInvalidB09() {
277  Color.WHITE.setBlue(-1);
278  }
279 
280  @Test(expected=IllegalArgumentException.class)
281  public void testInvalidB10() {
282  Color.WHITE.setBlue(Integer.MAX_VALUE);
283  }
284 
285  @Test(expected=IllegalArgumentException.class)
286  public void testInvalidB11() {
287  Color.WHITE.setBlue(Integer.MIN_VALUE);
288  }
289 
290  @Test(expected=IllegalArgumentException.class)
291  public void testInvalidB12() {
292  Color.WHITE.setBlue(0x100);
293  }
294 
295  // Green tests
296  @Test
297  public void testGreen() {
298  for (TestColor testColor : examples) {
299  assertThat(testColor.name, Color.fromRGB(testColor.rgb).getGreen(), is(testColor.g));
300  assertThat(testColor.name, Color.fromBGR(testColor.bgr).getGreen(), is(testColor.g));
301  assertThat(testColor.name, Color.fromRGB(testColor.r, testColor.g, testColor.b).getGreen(), is(testColor.g));
302  assertThat(testColor.name, Color.fromBGR(testColor.b, testColor.g, testColor.r).getGreen(), is(testColor.g));
303  }
304  }
305 
306  @Test(expected=IllegalArgumentException.class)
307  public void testInvalidG01() {
308  Color.fromRGB(0x00, -1, 0x00);
309  }
310 
311  @Test(expected=IllegalArgumentException.class)
312  public void testInvalidG02() {
313  Color.fromRGB(0x00, Integer.MAX_VALUE, 0x00);
314  }
315 
316  @Test(expected=IllegalArgumentException.class)
317  public void testInvalidG03() {
318  Color.fromRGB(0x00, Integer.MIN_VALUE, 0x00);
319  }
320 
321  @Test(expected=IllegalArgumentException.class)
322  public void testInvalidG04() {
323  Color.fromRGB(0x00, 0x100, 0x00);
324  }
325 
326  @Test(expected=IllegalArgumentException.class)
327  public void testInvalidG05() {
328  Color.fromBGR(0x00, -1, 0x00);
329  }
330 
331  @Test(expected=IllegalArgumentException.class)
332  public void testInvalidG06() {
333  Color.fromBGR(0x00, Integer.MAX_VALUE, 0x00);
334  }
335 
336  @Test(expected=IllegalArgumentException.class)
337  public void testInvalidG07() {
338  Color.fromBGR(0x00, Integer.MIN_VALUE, 0x00);
339  }
340 
341  @Test(expected=IllegalArgumentException.class)
342  public void testInvalidG08() {
343  Color.fromBGR(0x00, 0x100, 0x00);
344  }
345 
346  @Test(expected=IllegalArgumentException.class)
347  public void testInvalidG09() {
348  Color.WHITE.setGreen(-1);
349  }
350 
351  @Test(expected=IllegalArgumentException.class)
352  public void testInvalidG10() {
353  Color.WHITE.setGreen(Integer.MAX_VALUE);
354  }
355 
356  @Test(expected=IllegalArgumentException.class)
357  public void testInvalidG11() {
358  Color.WHITE.setGreen(Integer.MIN_VALUE);
359  }
360 
361  @Test(expected=IllegalArgumentException.class)
362  public void testInvalidG12() {
363  Color.WHITE.setGreen(0x100);
364  }
365 }
Color setRed(int red)
Definition: Color.java:188
int getGreen()
Definition: Color.java:197
static Color fromRGB(int red, int green, int blue)
Definition: Color.java:118
Color setBlue(int blue)
Definition: Color.java:226
static Color fromBGR(int blue, int green, int red)
Definition: Color.java:131
void set(String path, Object value)
Color setGreen(int green)
Definition: Color.java:207
static final Color WHITE
Definition: Color.java:23