Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
MetadataValueAdapterTest.java
1 package org.bukkit.metadata;
2 
3 import static org.junit.Assert.assertEquals;
4 
5 import org.bukkit.plugin.Plugin;
7 import org.junit.Test;
8 
10  private TestPlugin plugin = new TestPlugin("x");
11 
12  @Test
13  public void testAdapterBasics() {
14  IncrementingMetaValue mv = new IncrementingMetaValue(plugin);
15  // check getOwningPlugin
16  assertEquals(mv.getOwningPlugin(), this.plugin);
17 
18  // Check value-getting and invalidation.
19  assertEquals(new Integer(1), mv.value());
20  assertEquals(new Integer(2), mv.value());
21  mv.invalidate();
22  assertEquals(new Integer(1), mv.value());
23  }
24 
25  @Test
26  public void testAdapterConversions() {
27  IncrementingMetaValue mv = new IncrementingMetaValue(plugin);
28 
29  assertEquals(1, mv.asInt());
30  assertEquals(2L, mv.asLong());
31  assertEquals(3.0, mv.asFloat(), 0.001);
32  assertEquals(4, mv.asByte());
33  assertEquals(5.0, mv.asDouble(), 0.001);
34  assertEquals(6, mv.asShort());
35  assertEquals("7", mv.asString());
36  }
37 
38  /** Boolean conversion is non-trivial, we want to test it thoroughly. */
39  @Test
40  public void testBooleanConversion() {
41  // null is False.
42  assertEquals(false, simpleValue(null).asBoolean());
43 
44  // String to boolean.
45  assertEquals(true, simpleValue("True").asBoolean());
46  assertEquals(true, simpleValue("TRUE").asBoolean());
47  assertEquals(false, simpleValue("false").asBoolean());
48 
49  // Number to boolean.
50  assertEquals(true, simpleValue(1).asBoolean());
51  assertEquals(true, simpleValue(5.0).asBoolean());
52  assertEquals(false, simpleValue(0).asBoolean());
53  assertEquals(false, simpleValue(0.1).asBoolean());
54 
55  // Boolean as boolean, of course.
56  assertEquals(true, simpleValue(Boolean.TRUE).asBoolean());
57  assertEquals(false, simpleValue(Boolean.FALSE).asBoolean());
58 
59  // any object that is not null and not a Boolean, String, or Number is true.
60  assertEquals(true, simpleValue(new Object()).asBoolean());
61  }
62 
63  /** Test String conversions return an empty string when given null. */
64  @Test
65  public void testStringConversionNull() {
66  assertEquals("", simpleValue(null).asString());
67  }
68 
69  /** Get a fixed value MetadataValue. */
70  private MetadataValue simpleValue(Object value) {
71  return new FixedMetadataValue(plugin, value);
72  }
73 
74  /**
75  * A sample non-trivial MetadataValueAdapter implementation.
76  *
77  * The rationale for implementing an incrementing value is to have a value
78  * which changes with every call to value(). This is important for testing
79  * because we want to make sure all the tested conversions are calling the
80  * value() method exactly once and no caching is going on.
81  */
82  class IncrementingMetaValue extends MetadataValueAdapter {
83  private int internalValue = 0;
84 
85  protected IncrementingMetaValue(Plugin owningPlugin) {
86  super(owningPlugin);
87  }
88 
89  public Object value() {
90  return ++internalValue;
91  }
92 
93  public void invalidate() {
94  internalValue = 0;
95  }
96  }
97 }