1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
package org.bukkit.craftbukkit.inventory;
import static org.bukkit.support.Matchers.sameHash;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.support.AbstractTestingBase;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
@RunWith(Parameterized.class)
public class ItemStackTest extends AbstractTestingBase {
static abstract class StackProvider {
final Material material;
StackProvider(Material material) {
this.material = material;
}
ItemStack bukkit() {
return operate(cleanStack(material, false));
}
ItemStack craft() {
return operate(cleanStack(material, true));
}
abstract ItemStack operate(ItemStack cleanStack);
static ItemStack cleanStack(Material material, boolean craft) {
final ItemStack stack = new ItemStack(material);
return craft ? CraftItemStack.asCraftCopy(stack) : stack;
}
@Override
public String toString() {
return material.toString();
}
/**
* For each item in parameterList, it will apply nameFormat at nameIndex.
* For each item in parameterList for each item in materials, it will create a stack provider at each array index that contains an Operator.
*
* @param parameterList
* @param nameFormat
* @param nameIndex
* @param materials
* @return
*/
static List<Object[]> compound(final List<Object[]> parameterList, final String nameFormat, final int nameIndex, final Material...materials) {
final List<Object[]> out = new ArrayList<Object[]>();
for (Object[] params : parameterList) {
final int len = params.length;
for (final Material material : materials) {
final Object[] paramsOut = params.clone();
for (int i = 0; i < len; i++) {
final Object param = paramsOut[i];
if (param instanceof Operator) {
final Operator operator = (Operator) param;
paramsOut[i] = new StackProvider(material) {
@Override
ItemStack operate(ItemStack cleanStack) {
return operator.operate(cleanStack);
}
};
}
}
paramsOut[nameIndex] = String.format(nameFormat, paramsOut[nameIndex], material);
out.add(paramsOut);
}
}
return out;
}
}
interface Operator {
ItemStack operate(ItemStack cleanStack);
}
static class CompoundOperator implements Operator {
static class RecursiveContainer {
final Joiner joiner;
final Object[] strings;
final int nameParameter;
final List<Object[]> stack;
final List<Object[]> out;
final List<Object[]>[] lists;
RecursiveContainer(Joiner joiner, Object[] strings, int nameParameter, List<Object[]> stack, List<Object[]> out, List<Object[]>[] lists) {
this.joiner = joiner;
this.strings = strings;
this.nameParameter = nameParameter;
this.stack = stack;
this.out = out;
this.lists = lists;
}
}
final Operator[] operators;
CompoundOperator(Operator...operators) {
this.operators = operators;
}
public ItemStack operate(ItemStack cleanStack) {
for (Operator operator : operators) {
operator.operate(cleanStack);
}
return cleanStack;
}
@Override
public String toString() {
return Arrays.toString(operators);
}
/**
* This combines different tests into one large collection, combining no two tests from the same list.
* @param joiner used to join names
* @param nameParameter index of the name parameter
* @param singletonBitmask a list of bits representing the 'singletons' located in your originalLists. Lowest order bits represent the first items in originalLists.
* Singletons are exponentially linked with each other, such that,
* the output will contain every unique subset of only items from the singletons,
* as well as every unique subset that contains at least one item from each non-singleton.
* @param originalLists
* @return
*/
static List<Object[]> compound(final Joiner joiner, final int nameParameter, final long singletonBitmask, final List<Object[]>...originalLists) {
final List<Object[]> out = new ArrayList<Object[]>();
final List<List<Object[]>> singletons = new ArrayList<List<Object[]>>();
final List<List<Object[]>> notSingletons = new ArrayList<List<Object[]>>();
{ // Separate and prime the 'singletons'
int i = 0;
for (List<Object[]> list : originalLists) {
(((singletonBitmask >>> i++) & 0x1) == 0x1 ? singletons : notSingletons).add(list);
}
}
for (final List<Object[]> primarySingleton : singletons) {
// Iterate over our singletons, to multiply the 'out' each time
for (final Object[] entry : out.toArray(EMPTY_ARRAY)) {
// Iterate over a snapshot of 'out' to prevent CMEs / infinite iteration
final int len = entry.length;
for (final Object[] singleton : primarySingleton) {
// Iterate over each item in our singleton for the current 'out' entry
final Object[] toOut = entry.clone();
for (int i = 0; i < len; i++) {
// Iterate over each parameter
if (i == nameParameter) {
toOut[i] = joiner.join(toOut[i], singleton[i]);
} else if (toOut[i] instanceof Operator) {
final Operator op1 = (Operator) toOut[i];
final Operator op2 = (Operator) singleton[i];
toOut[i] = new Operator() {
public ItemStack operate(final ItemStack cleanStack) {
return op2.operate(op1.operate(cleanStack));
}
};
}
}
out.add(toOut);
}
}
out.addAll(primarySingleton);
}
@SuppressWarnings("unchecked")
final List<Object[]>[] lists = new List[notSingletons.size() + 1];
notSingletons.toArray(lists);
lists[lists.length - 1] = out;
final RecursiveContainer methodParams = new RecursiveContainer(joiner, new Object[lists.length], nameParameter, new ArrayList<Object[]>(lists.length), new ArrayList<Object[]>(), lists);
recursivelyCompound(methodParams, 0);
methodParams.out.addAll(out);
return methodParams.out;
}
private static void recursivelyCompound(final RecursiveContainer methodParams, final int level) {
final List<Object[]> stack = methodParams.stack;
if (level == methodParams.lists.length) {
final Object[] firstParams = stack.get(0);
final int len = firstParams.length;
final int stackSize = stack.size();
final Object[] params = new Object[len];
for (int i = 0; i < len; i++) {
final Object firstParam = firstParams[i];
if (firstParam instanceof Operator) {
final Operator[] operators = new Operator[stackSize];
for (int j = 0; j < stackSize; j++) {
operators[j] = (Operator) stack.get(j)[i];
}
params[i] = new CompoundOperator(operators);
} else if (i == methodParams.nameParameter) {
final Object[] strings = methodParams.strings;
for (int j = 0; j < stackSize; j++) {
strings[j] = stack.get(j)[i];
}
params[i] = methodParams.joiner.join(strings);
} else {
params[i] = firstParam;
}
}
methodParams.out.add(params);
} else {
final int marker = stack.size();
for (final Object[] params : methodParams.lists[level]) {
stack.add(params);
recursivelyCompound(methodParams, level + 1);
stack.remove(marker);
}
}
}
}
interface StackWrapper {
ItemStack stack();
}
static class CraftWrapper implements StackWrapper {
final StackProvider provider;
CraftWrapper(StackProvider provider) {
this.provider = provider;
}
public ItemStack stack() {
return provider.craft();
}
@Override
public String toString() {
return "Craft " + provider;
}
}
static class BukkitWrapper implements StackWrapper {
final StackProvider provider;
BukkitWrapper(StackProvider provider) {
this.provider = provider;
}
public ItemStack stack() {
return provider.bukkit();
}
@Override
public String toString() {
return "Bukkit " + provider;
}
}
static class NoOpProvider extends StackProvider {
NoOpProvider(Material material) {
super(material);
}
@Override
ItemStack operate(ItemStack cleanStack) {
return cleanStack;
}
@Override
public String toString() {
return "NoOp " + super.toString();
}
}
@Parameters(name="[{index}]:{" + NAME_PARAMETER + "}")
public static List<Object[]> data() {
return ImmutableList.of(); // TODO, test basic durability issues
}
static final Object[][] EMPTY_ARRAY = new Object[0][];
/**
* Materials that generate unique item meta types.
*/
static final Material[] COMPOUND_MATERIALS;
static final int NAME_PARAMETER = 2;
static {
final ItemFactory factory = CraftItemFactory.instance();
final Map<Class<? extends ItemMeta>, Material> possibleMaterials = new HashMap<Class<? extends ItemMeta>, Material>();
ItemMeta meta;
for (final Material material : Material.values()) {
meta = factory.getItemMeta(material);
if (meta == null || possibleMaterials.containsKey(meta.getClass()))
continue;
possibleMaterials.put(meta.getClass(), material);
}
COMPOUND_MATERIALS = possibleMaterials.values().toArray(new Material[possibleMaterials.size()]);
}
@Parameter(0) public StackProvider provider;
@Parameter(1) public StackProvider unequalProvider;
@Parameter(NAME_PARAMETER) public String name;
@Test
public void testBukkitInequality() {
final StackWrapper bukkitWrapper = new CraftWrapper(provider);
testInequality(bukkitWrapper, new BukkitWrapper(unequalProvider));
testInequality(bukkitWrapper, new BukkitWrapper(new NoOpProvider(provider.material)));
}
@Test
public void testCraftInequality() {
final StackWrapper craftWrapper = new CraftWrapper(provider);
testInequality(craftWrapper, new CraftWrapper(unequalProvider));
testInequality(craftWrapper, new CraftWrapper(new NoOpProvider(provider.material)));
}
@Test
public void testMixedInequality() {
final StackWrapper craftWrapper = new CraftWrapper(provider);
testInequality(craftWrapper, new BukkitWrapper(unequalProvider));
testInequality(craftWrapper, new BukkitWrapper(new NoOpProvider(provider.material)));
final StackWrapper bukkitWrapper = new CraftWrapper(provider);
testInequality(bukkitWrapper, new CraftWrapper(unequalProvider));
testInequality(bukkitWrapper, new CraftWrapper(new NoOpProvider(provider.material)));
}
static void testInequality(StackWrapper provider, StackWrapper unequalProvider) {
final ItemStack stack = provider.stack();
final ItemStack stack2 = provider.stack();
assertThat(stack, allOf(equalTo(stack), sameHash(stack)));
assertThat(stack, is(not(sameInstance(stack2))));
assertThat(stack, allOf(equalTo(stack2), sameHash(stack2)));
final ItemStack unequalStack = unequalProvider.stack();
final ItemStack unequalStack2 = unequalProvider.stack();
assertThat(unequalStack, allOf(equalTo(unequalStack), sameHash(unequalStack)));
assertThat(unequalStack, is(not(sameInstance(unequalStack2))));
assertThat(unequalStack, allOf(equalTo(unequalStack2), sameHash(unequalStack2)));
assertThat(stack, is(not(unequalStack)));
assertThat(unequalStack, is(not(stack)));
final ItemStack newStack = new ItemStack(stack2);
assertThat(newStack, allOf(equalTo(stack), sameHash(stack)));
assertThat(newStack, is(not(unequalStack)));
assertThat(newStack.getItemMeta(), allOf(equalTo(stack.getItemMeta()), sameHash(stack.getItemMeta())));
assertThat(newStack.getItemMeta(), is(not(unequalStack.getItemMeta())));
final ItemStack craftStack = CraftItemStack.asCraftCopy(stack2);
assertThat(craftStack, allOf(equalTo(stack), sameHash(stack)));
assertThat(craftStack, is(not(unequalStack)));
assertThat(craftStack.getItemMeta(), allOf(equalTo(stack.getItemMeta()), sameHash(stack.getItemMeta())));
assertThat(craftStack.getItemMeta(), is(not(unequalStack.getItemMeta())));
final ItemStack newUnequalStack = new ItemStack(unequalStack2);
assertThat(newUnequalStack, allOf(equalTo(unequalStack), sameHash(unequalStack)));
assertThat(newUnequalStack, is(not(stack)));
assertThat(newUnequalStack.getItemMeta(), allOf(equalTo(unequalStack.getItemMeta()), sameHash(unequalStack.getItemMeta())));
assertThat(newUnequalStack.getItemMeta(), is(not(stack.getItemMeta())));
final ItemStack newUnequalCraftStack = CraftItemStack.asCraftCopy(unequalStack2);
assertThat(newUnequalCraftStack, allOf(equalTo(unequalStack), sameHash(unequalStack)));
assertThat(newUnequalCraftStack, is(not(stack)));
assertThat(newUnequalCraftStack.getItemMeta(), allOf(equalTo(unequalStack.getItemMeta()), sameHash(unequalStack.getItemMeta())));
assertThat(newUnequalCraftStack.getItemMeta(), is(not(stack.getItemMeta())));
}
@Test
public void testBukkitYamlDeserialize() throws Throwable {
testYamlDeserialize(new BukkitWrapper(provider), new BukkitWrapper(unequalProvider));
}
@Test
public void testCraftYamlDeserialize() throws Throwable {
testYamlDeserialize(new CraftWrapper(provider), new CraftWrapper(unequalProvider));
}
@Test
public void testBukkitStreamDeserialize() throws Throwable {
testStreamDeserialize(new BukkitWrapper(provider), new BukkitWrapper(unequalProvider));
}
@Test
public void testCraftStreamDeserialize() throws Throwable {
testStreamDeserialize(new CraftWrapper(provider), new CraftWrapper(unequalProvider));
}
static void testStreamDeserialize(StackWrapper provider, StackWrapper unequalProvider) throws Throwable {
final ItemStack stack = provider.stack();
final ItemStack unequalStack = unequalProvider.stack();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new BukkitObjectOutputStream(out);
oos.writeObject(stack);
oos.writeObject(unequalStack);
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException ex) {
}
}
}
final String data = new String(Base64Coder.encode(out.toByteArray()));
ObjectInputStream ois = null;
final ItemStack readFirst;
final ItemStack readSecond;
try {
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ois = new BukkitObjectInputStream(in);
readFirst = (ItemStack) ois.readObject();
readSecond = (ItemStack) ois.readObject();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException ex) {
}
}
}
testEqualities(data, readFirst, readSecond, stack, unequalStack);
}
static void testYamlDeserialize(StackWrapper provider, StackWrapper unequalProvider) {
final ItemStack stack = provider.stack();
final ItemStack unequalStack = unequalProvider.stack();
final YamlConfiguration configOut = new YamlConfiguration();
configOut.set("provider", stack);
configOut.set("unequal", unequalStack);
final String out = '\n' + configOut.saveToString();
final YamlConfiguration configIn = new YamlConfiguration();
try {
configIn.loadFromString(out);
} catch (InvalidConfigurationException ex) {
throw new RuntimeException(out, ex);
}
testEqualities(out, configIn.getItemStack("provider"), configIn.getItemStack("unequal"), stack, unequalStack);
}
static void testEqualities(String information, ItemStack primaryRead, ItemStack unequalRead, ItemStack primaryOriginal, ItemStack unequalOriginal) {
assertThat(information, primaryRead, allOf(equalTo(primaryOriginal), sameHash(primaryOriginal)));
assertThat(information, unequalRead, allOf(equalTo(unequalOriginal), sameHash(unequalOriginal)));
assertThat(information, primaryRead, is(not(unequalOriginal)));
assertThat(information, primaryRead, is(not(unequalRead)));
}
}
|