-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathBytesRefsCollectionBuilderTests.java
82 lines (68 loc) · 3.79 KB
/
BytesRefsCollectionBuilderTests.java
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.index.mapper;
import org.apache.lucene.util.BytesRef;
import org.opensearch.test.OpenSearchTestCase;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.stream.Stream;
public class BytesRefsCollectionBuilderTests extends OpenSearchTestCase {
public void testBuildSortedNotSorted() {
String[] seedStrings = generateRandomStringArray(10, 10, false, true);
List<BytesRef> bytesRefList = Arrays.stream(seedStrings).map(BytesRef::new).toList();
List<BytesRef> sortedBytesRefs = bytesRefList.stream().sorted().toList();
Collection<BytesRef> sortedSet = assertCollectionBuilt(sortedBytesRefs);
assertCollectionBuilt(bytesRefList);
assertTrue(sortedSet instanceof SortedSet<BytesRef>);
assertNull(((SortedSet<BytesRef>) sortedSet).comparator());
}
public void testBuildFooBar() {
String[] reverseOrderStrings = new String[] { "foo", "bar" };
List<BytesRef> bytesRefList = Arrays.stream(reverseOrderStrings).map(BytesRef::new).toList();
List<BytesRef> sortedBytesRefs = bytesRefList.stream().sorted().toList();
Collection<BytesRef> sortedSet = assertCollectionBuilt(sortedBytesRefs);
Collection<BytesRef> reverseList = assertCollectionBuilt(bytesRefList);
assertTrue(sortedSet instanceof SortedSet<BytesRef>);
assertNull(((SortedSet<BytesRef>) sortedSet).comparator());
assertTrue(reverseList instanceof List<BytesRef>);
}
public void testFrozen() {
BytesRefsCollectionBuilder builder = new BytesRefsCollectionBuilder();
String[] seedStrings = generateRandomStringArray(5, 10, false, true);
Arrays.stream(seedStrings).map(BytesRef::new).forEachOrdered(builder);
Collection<BytesRef> bytesRefCollection = builder.get();
assertNotNull(bytesRefCollection);
assertEquals(seedStrings.length, bytesRefCollection.size());
assertThrows(IllegalStateException.class, () -> builder.accept(new BytesRef("illegal state")));
assertSame(bytesRefCollection, builder.get());
}
private static Collection<BytesRef> assertCollectionBuilt(List<BytesRef> sortedBytesRefs) {
BytesRefsCollectionBuilder builder = new BytesRefsCollectionBuilder();
sortedBytesRefs.stream().forEachOrdered(builder);
Collection<BytesRef> bytesRefCollection = builder.get();
assertEquals(bytesRefCollection.size(), sortedBytesRefs.size());
for (Iterator<BytesRef> iterator = bytesRefCollection.iterator(), iterator2 = sortedBytesRefs.iterator(); iterator.hasNext()
|| iterator2.hasNext();) {
assertTrue(iterator.next().bytesEquals(iterator2.next()));
}
return bytesRefCollection;
}
public void testCoverUnsupported() {
BytesRefsCollectionBuilder builder = new BytesRefsCollectionBuilder();
Stream.of("in", "order").map(BytesRef::new).forEachOrdered(builder);
SortedSet<BytesRef> bytesRefCollection = (SortedSet<BytesRef>) builder.get();
assertThrows(UnsupportedOperationException.class, () -> bytesRefCollection.subSet(new BytesRef("a"), new BytesRef("z")));
assertThrows(UnsupportedOperationException.class, () -> bytesRefCollection.headSet(new BytesRef("a")));
assertThrows(UnsupportedOperationException.class, () -> bytesRefCollection.tailSet(new BytesRef("a")));
assertThrows(UnsupportedOperationException.class, bytesRefCollection::first);
assertThrows(UnsupportedOperationException.class, bytesRefCollection::last);
}
}