Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
MetadataConversionTest.java
1 // Copyright (C) 2011 Ryan Michela
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15 
16 package org.bukkit.metadata;
17 
18 import org.bukkit.plugin.Plugin;
20 import org.junit.Test;
21 
22 import static org.junit.Assert.assertEquals;
23 
24 /**
25  */
26 public class MetadataConversionTest {
27  private Plugin plugin = new TestPlugin("x");
28  private FixedMetadataValue subject;
29 
30  private void setSubject(Object value) {
31  subject = new FixedMetadataValue(plugin, value);
32  }
33 
34  @Test
35  public void testFromInt() {
36  setSubject(10);
37 
38  assertEquals(10, subject.asInt());
39  assertEquals(10, subject.asFloat(), 0.000001);
40  assertEquals(10, subject.asDouble(), 0.000001);
41  assertEquals(10, subject.asLong());
42  assertEquals(10, subject.asShort());
43  assertEquals(10, subject.asByte());
44  assertEquals(true, subject.asBoolean());
45  assertEquals("10", subject.asString());
46  }
47 
48  @Test
49  public void testFromFloat() {
50  setSubject(10.5);
51 
52  assertEquals(10, subject.asInt());
53  assertEquals(10.5, subject.asFloat(), 0.000001);
54  assertEquals(10.5, subject.asDouble(), 0.000001);
55  assertEquals(10, subject.asLong());
56  assertEquals(10, subject.asShort());
57  assertEquals(10, subject.asByte());
58  assertEquals(true, subject.asBoolean());
59  assertEquals("10.5", subject.asString());
60  }
61 
62  @Test
63  public void testFromNumericString() {
64  setSubject("10");
65 
66  assertEquals(10, subject.asInt());
67  assertEquals(10, subject.asFloat(), 0.000001);
68  assertEquals(10, subject.asDouble(), 0.000001);
69  assertEquals(10, subject.asLong());
70  assertEquals(10, subject.asShort());
71  assertEquals(10, subject.asByte());
72  assertEquals(false, subject.asBoolean());
73  assertEquals("10", subject.asString());
74  }
75 
76  @Test
77  public void testFromNonNumericString() {
78  setSubject("true");
79 
80  assertEquals(0, subject.asInt());
81  assertEquals(0, subject.asFloat(), 0.000001);
82  assertEquals(0, subject.asDouble(), 0.000001);
83  assertEquals(0, subject.asLong());
84  assertEquals(0, subject.asShort());
85  assertEquals(0, subject.asByte());
86  assertEquals(true, subject.asBoolean());
87  assertEquals("true", subject.asString());
88  }
89 
90  @Test
91  public void testFromNull() {
92  setSubject(null);
93 
94  assertEquals(0, subject.asInt());
95  assertEquals(0, subject.asFloat(), 0.000001);
96  assertEquals(0, subject.asDouble(), 0.000001);
97  assertEquals(0, subject.asLong());
98  assertEquals(0, subject.asShort());
99  assertEquals(0, subject.asByte());
100  assertEquals(false, subject.asBoolean());
101  assertEquals("", subject.asString());
102  }
103 }