Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
LazyMetadataValueTest.java
1 package org.bukkit.metadata;
2 
4 import org.junit.Test;
5 
6 import java.util.concurrent.Callable;
7 
8 import static org.junit.Assert.*;
9 
10 public class LazyMetadataValueTest {
11  private LazyMetadataValue subject;
12  private TestPlugin plugin = new TestPlugin("x");
13 
14  @Test
15  public void testLazyInt() {
16  int value = 10;
17  subject = makeSimpleCallable(value);
18 
19  assertEquals(value, subject.value());
20  }
21 
22  @Test
23  public void testLazyDouble() {
24  double value = 10.5;
25  subject = makeSimpleCallable(value);
26 
27  assertEquals(value, (Double)subject.value(), 0.01);
28  }
29 
30  @Test
31  public void testLazyString() {
32  String value = "TEN";
33  subject = makeSimpleCallable(value);
34 
35  assertEquals(value, subject.value());
36  }
37 
38  @Test
39  public void testLazyBoolean() {
40  boolean value = false;
41  subject = makeSimpleCallable(value);
42 
43  assertEquals(value, subject.value());
44  }
45 
46  @Test(expected=MetadataEvaluationException.class)
47  public void testEvalException() {
48  subject = new LazyMetadataValue(plugin, LazyMetadataValue.CacheStrategy.CACHE_AFTER_FIRST_EVAL, new Callable<Object>() {
49  public Object call() throws Exception {
50  throw new RuntimeException("Gotcha!");
51  }
52  });
53  subject.value();
54  }
55 
56  @Test
57  public void testCacheStrategyCacheAfterFirstEval() {
58  final Counter counter = new Counter();
59  final int value = 10;
60  subject = new LazyMetadataValue(plugin, LazyMetadataValue.CacheStrategy.CACHE_AFTER_FIRST_EVAL, new Callable<Object>() {
61  public Object call() throws Exception {
62  counter.increment();
63  return value;
64  }
65  });
66 
67  subject.value();
68  subject.value();
69  assertEquals(value, subject.value());
70  assertEquals(1, counter.value());
71 
72  subject.invalidate();
73  subject.value();
74  assertEquals(2, counter.value());
75  }
76 
77  @Test
78  public void testCacheStrategyNeverCache() {
79  final Counter counter = new Counter();
80  final int value = 10;
81  subject = new LazyMetadataValue(plugin, LazyMetadataValue.CacheStrategy.NEVER_CACHE, new Callable<Object>() {
82  public Object call() throws Exception {
83  counter.increment();
84  return value;
85  }
86  });
87 
88  subject.value();
89  subject.value();
90  assertEquals(value, subject.value());
91  assertEquals(3, counter.value());
92  }
93 
94  @Test
95  public void testCacheStrategyEternally() {
96  final Counter counter = new Counter();
97  final int value = 10;
98  subject = new LazyMetadataValue(plugin, LazyMetadataValue.CacheStrategy.CACHE_ETERNALLY, new Callable<Object>() {
99  public Object call() throws Exception {
100  counter.increment();
101  return value;
102  }
103  });
104 
105  subject.value();
106  subject.value();
107  assertEquals(value, subject.value());
108  assertEquals(1, counter.value());
109 
110  subject.invalidate();
111  subject.value();
112  assertEquals(value, subject.value());
113  assertEquals(1, counter.value());
114  }
115 
116  private LazyMetadataValue makeSimpleCallable(final Object value) {
117  return new LazyMetadataValue(plugin, new Callable<Object>() {
118  public Object call() throws Exception {
119  return value;
120  }
121  });
122  }
123 
124  private class Counter {
125  private int c = 0;
126 
127  public void increment() {
128  c++;
129  }
130 
131  public int value() {
132  return c;
133  }
134  }
135 }