Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
MetadataStoreTest.java
1 package org.bukkit.metadata;
2 
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 
7 import java.util.List;
8 import java.util.concurrent.Callable;
9 
10 import org.bukkit.plugin.Plugin;
12 import org.junit.Test;
13 
14 public class MetadataStoreTest {
15  private Plugin pluginX = new TestPlugin("x");
16  private Plugin pluginY = new TestPlugin("y");
17 
18  StringMetadataStore subject = new StringMetadataStore();
19 
20  @Test
21  public void testMetadataStore() {
22  subject.setMetadata("subject", "key", new FixedMetadataValue(pluginX, 10));
23 
24  assertTrue(subject.hasMetadata("subject", "key"));
25  List<MetadataValue> values = subject.getMetadata("subject", "key");
26  assertEquals(10, values.get(0).value());
27  }
28 
29  @Test
30  public void testMetadataNotPresent() {
31  assertFalse(subject.hasMetadata("subject", "key"));
32  List<MetadataValue> values = subject.getMetadata("subject", "key");
33  assertTrue(values.isEmpty());
34  }
35 
36  @Test
37  public void testInvalidateAll() {
38  final Counter counter = new Counter();
39 
40  subject.setMetadata("subject", "key", new LazyMetadataValue(pluginX, new Callable<Object>() {
41  public Object call() throws Exception {
42  counter.increment();
43  return 10;
44  }
45  }));
46 
47  assertTrue(subject.hasMetadata("subject", "key"));
48  subject.getMetadata("subject", "key").get(0).value();
49  subject.invalidateAll(pluginX);
50  subject.getMetadata("subject", "key").get(0).value();
51  assertEquals(2, counter.value());
52  }
53 
54  @Test
55  public void testInvalidateAllButActuallyNothing() {
56  final Counter counter = new Counter();
57 
58  subject.setMetadata("subject", "key", new LazyMetadataValue(pluginX, new Callable<Object>() {
59  public Object call() throws Exception {
60  counter.increment();
61  return 10;
62  }
63  }));
64 
65  assertTrue(subject.hasMetadata("subject", "key"));
66  subject.getMetadata("subject", "key").get(0).value();
67  subject.invalidateAll(pluginY);
68  subject.getMetadata("subject", "key").get(0).value();
69  assertEquals(1, counter.value());
70  }
71 
72  @Test
73  public void testMetadataReplace() {
74  subject.setMetadata("subject", "key", new FixedMetadataValue(pluginX, 10));
75  subject.setMetadata("subject", "key", new FixedMetadataValue(pluginY, 10));
76  subject.setMetadata("subject", "key", new FixedMetadataValue(pluginX, 20));
77 
78  for (MetadataValue mv : subject.getMetadata("subject", "key")) {
79  if (mv.getOwningPlugin().equals(pluginX)) {
80  assertEquals(20, mv.value());
81  }
82  if (mv.getOwningPlugin().equals(pluginY)) {
83  assertEquals(10, mv.value());
84  }
85  }
86  }
87 
88  @Test
89  public void testMetadataRemove() {
90  subject.setMetadata("subject", "key", new FixedMetadataValue(pluginX, 10));
91  subject.setMetadata("subject", "key", new FixedMetadataValue(pluginY, 20));
92  subject.removeMetadata("subject", "key", pluginX);
93 
94  assertTrue(subject.hasMetadata("subject", "key"));
95  assertEquals(1, subject.getMetadata("subject", "key").size());
96  assertEquals(20, subject.getMetadata("subject", "key").get(0).value());
97  }
98 
99  @Test
100  public void testMetadataRemoveLast() {
101  subject.setMetadata("subject", "key", new FixedMetadataValue(pluginX, 10));
102  subject.removeMetadata("subject", "key", pluginX);
103 
104  assertFalse(subject.hasMetadata("subject", "key"));
105  assertEquals(0, subject.getMetadata("subject", "key").size());
106  }
107 
108  @Test
109  public void testMetadataRemoveForNonExistingPlugin() {
110  subject.setMetadata("subject", "key", new FixedMetadataValue(pluginX, 10));
111  subject.removeMetadata("subject", "key", pluginY);
112 
113  assertTrue(subject.hasMetadata("subject", "key"));
114  assertEquals(1, subject.getMetadata("subject", "key").size());
115  assertEquals(10, subject.getMetadata("subject", "key").get(0).value());
116  }
117 
118  @Test
119  public void testHasMetadata() {
120  subject.setMetadata("subject", "key", new FixedMetadataValue(pluginX, 10));
121  assertTrue(subject.hasMetadata("subject", "key"));
122  assertFalse(subject.hasMetadata("subject", "otherKey"));
123  }
124 
125  private class StringMetadataStore extends MetadataStoreBase<String> implements MetadataStore<String> {
126  @Override
127  protected String disambiguate(String subject, String metadataKey) {
128  return subject + ":" + metadataKey;
129  }
130  }
131 
132  private class Counter {
133  int c = 0;
134 
135  public void increment() {
136  c++;
137  }
138 
139  public int value() {
140  return c;
141  }
142  }
143 }