|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.tinkerpop.gremlin.process.traversal; |
| 20 | + |
| 21 | +import org.apache.tinkerpop.gremlin.process.traversal.step.GValue; |
| 22 | +import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | + |
| 32 | +/** |
| 33 | + * Test class for {@link P} with {@link BulkSet} literals ensure that if a {@link BulkSet} is used as a literal, |
| 34 | + * it is preserved in the {@link P} object's value, and that the {@link P} object can handle {@link BulkSet} literals |
| 35 | + * with {@link GValue} elements correctly. You get a {@link BulkSet} in {@link P} when you have some Gremlin like: |
| 36 | + * {@code aggreate('x')...where(within('x'))}. |
| 37 | + */ |
| 38 | +public class PBulkSetTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + public void shouldPreserveBulkSetInGetValue() { |
| 42 | + final BulkSet<String> bulkSet = new BulkSet<>(); |
| 43 | + bulkSet.add("a", 2); |
| 44 | + bulkSet.add("b", 1); |
| 45 | + |
| 46 | + final P p = P.within(bulkSet); |
| 47 | + final Object value = p.getValue(); |
| 48 | + |
| 49 | + assertTrue("Value should be a BulkSet but was " + value.getClass().getName(), value instanceof BulkSet); |
| 50 | + assertEquals(bulkSet, value); |
| 51 | + assertEquals(3, ((BulkSet) value).size()); |
| 52 | + assertEquals(2L, ((BulkSet) value).get("a")); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void shouldPreserveBulkSetInSetValueWithGValues() { |
| 57 | + final BulkSet<Object> bulkSet = new BulkSet<>(); |
| 58 | + bulkSet.add("a", 2); |
| 59 | + bulkSet.add(GValue.of("x", "b"), 1); |
| 60 | + |
| 61 | + final P p = P.within(Collections.emptyList()); |
| 62 | + p.setValue(bulkSet); |
| 63 | + |
| 64 | + final Object value = p.getValue(); |
| 65 | + // Currently this will fail and return an ArrayList (or a List from the stream) |
| 66 | + assertTrue("Value should be a BulkSet but was " + (value == null ? "null" : value.getClass().getName()), value instanceof BulkSet); |
| 67 | + assertEquals(3, ((BulkSet) value).size()); |
| 68 | + assertEquals(2L, ((BulkSet) value).get("a")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void shouldCloneBulkSetInGetValueWithVariables() { |
| 73 | + final BulkSet<Object> bulkSet = new BulkSet<>(); |
| 74 | + bulkSet.add("a", 2); |
| 75 | + |
| 76 | + // Subclass to access protected constructor and merge literals and variables |
| 77 | + final P p = new PSubWithVars(Compare.eq, bulkSet, Collections.singletonMap("var1", "b")); |
| 78 | + |
| 79 | + final Object value = p.getValue(); |
| 80 | + assertTrue("Value should be a BulkSet but was " + (value == null ? "null" : value.getClass().getName()), value instanceof BulkSet); |
| 81 | + assertEquals(3, ((BulkSet) value).size()); |
| 82 | + assertEquals(2L, ((BulkSet) value).get("a")); |
| 83 | + assertEquals(1L, ((BulkSet) value).get("b")); |
| 84 | + |
| 85 | + // Ensure original bulkSet was not modified (p.getValue() should have cloned) |
| 86 | + assertEquals(2, bulkSet.size()); |
| 87 | + assertEquals(2L, bulkSet.get("a")); |
| 88 | + assertEquals(0L, bulkSet.get("b")); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void shouldPreserveBulkSetInProtectedConstructor() throws Exception { |
| 93 | + final BulkSet<String> bulkSet = new BulkSet<>(); |
| 94 | + bulkSet.add("a", 2); |
| 95 | + |
| 96 | + final P p = new PSub(Compare.eq, bulkSet); |
| 97 | + |
| 98 | + final Object value = p.getValue(); |
| 99 | + assertTrue("Value should be a BulkSet but was " + (value == null ? "null" : value.getClass().getName()), value instanceof BulkSet); |
| 100 | + assertEquals(2, ((BulkSet) value).size()); |
| 101 | + assertEquals(2L, ((BulkSet) value).get("a")); |
| 102 | + } |
| 103 | + |
| 104 | + private static class PSub extends P { |
| 105 | + public PSub(final PBiPredicate biPredicate, final Collection literals) { |
| 106 | + super(biPredicate, literals, Collections.emptyMap(), true); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static class PSubWithVars extends P { |
| 111 | + public PSubWithVars(final PBiPredicate biPredicate, final Collection literals, final Map variables) { |
| 112 | + super(biPredicate, literals, variables, true); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments