diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java index e6e4a3b203..1de772539a 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java @@ -47,6 +47,7 @@ import com.baidu.hugegraph.unit.id.EdgeIdTest; import com.baidu.hugegraph.unit.id.IdTest; import com.baidu.hugegraph.unit.id.IdUtilTest; +import com.baidu.hugegraph.unit.id.SplicingIdGeneratorTest; import com.baidu.hugegraph.unit.mysql.MysqlUtilTest; import com.baidu.hugegraph.unit.mysql.WhereBuilderTest; import com.baidu.hugegraph.unit.rocksdb.RocksDBCountersTest; @@ -84,6 +85,7 @@ IdTest.class, EdgeIdTest.class, IdUtilTest.class, + SplicingIdGeneratorTest.class, /* core */ LocksTableTest.class, diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/id/SplicingIdGeneratorTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/id/SplicingIdGeneratorTest.java new file mode 100644 index 0000000000..03451464e6 --- /dev/null +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/id/SplicingIdGeneratorTest.java @@ -0,0 +1,112 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.unit.id; + +import java.util.List; + +import org.junit.Test; +import org.mockito.Mockito; + +import com.baidu.hugegraph.backend.id.Id; +import com.baidu.hugegraph.backend.id.IdGenerator; +import com.baidu.hugegraph.backend.id.SplicingIdGenerator; +import com.baidu.hugegraph.schema.PropertyKey; +import com.baidu.hugegraph.schema.VertexLabel; +import com.baidu.hugegraph.structure.HugeVertex; +import com.baidu.hugegraph.testutil.Assert; +import com.baidu.hugegraph.type.define.IdStrategy; +import com.baidu.hugegraph.unit.FakeObjects; +import com.google.common.collect.ImmutableList; + +public class SplicingIdGeneratorTest { + + @Test + public void testGenerate() { + FakeObjects fakeObjects = new FakeObjects(); + PropertyKey name = fakeObjects.newPropertyKey(IdGenerator.of(1), + "name"); + VertexLabel vertexLabel = fakeObjects.newVertexLabel( + IdGenerator.of(1L), "fake", + IdStrategy.PRIMARY_KEY, name.id()); + HugeVertex vertex = Mockito.mock(HugeVertex.class); + Mockito.when(vertex.schemaLabel()).thenReturn(vertexLabel); + Mockito.when(vertex.name()).thenReturn("marko"); + Id vid = SplicingIdGenerator.instance().generate(vertex); + Assert.assertEquals(IdGenerator.of("1:marko"), vid); + } + + @Test + public void testConcatIds() { + Assert.assertEquals("a>b>c>d", + SplicingIdGenerator.concat("a", "b", "c", "d")); + Assert.assertEquals("a>`>>c>`>", + SplicingIdGenerator.concat("a", ">", "c", ">")); + Assert.assertEquals("a>b`>c>d", + SplicingIdGenerator.concat("a", "b>c", "d")); + } + + @Test + public void testSplitIds() { + Assert.assertArrayEquals(new String[]{"a", "b", "c", "d"}, + SplicingIdGenerator.split("a>b>c>d")); + Assert.assertArrayEquals(new String[]{"a", ">", "c", ">"}, + SplicingIdGenerator.split("a>`>>c>`>")); + Assert.assertArrayEquals(new String[]{"a", "b>c", "d"}, + SplicingIdGenerator.split("a>b`>c>d")); + } + + @Test + public void testConcatValues() { + Assert.assertEquals("a!1!c!d", + SplicingIdGenerator.concatValues("a", 1, 'c', "d")); + Assert.assertEquals("a!`!!1!d", + SplicingIdGenerator.concatValues("a", "!", 1, 'd')); + Assert.assertEquals("a!b`!c!d", + SplicingIdGenerator.concatValues("a", "b!c", "d")); + + List values = ImmutableList.of("a", 1, 'c', "d"); + Assert.assertEquals("a!1!c!d", + SplicingIdGenerator.concatValues(values)); + values = ImmutableList.of("a", "!", 1, 'd'); + Assert.assertEquals("a!`!!1!d", + SplicingIdGenerator.concatValues(values)); + values = ImmutableList.of("a", "b!c", "d"); + Assert.assertEquals("a!b`!c!d", + SplicingIdGenerator.concatValues(values)); + } + + @Test + public void testSplicing() { + Assert.assertEquals(IdGenerator.of("1:marko"), + SplicingIdGenerator.splicing("1", "marko")); + Assert.assertEquals(IdGenerator.of("book:c:2020"), + SplicingIdGenerator.splicing("book", "c", "2020")); + } + + @Test + public void testParse() { + Assert.assertArrayEquals(new String[]{"1", "marko"}, + SplicingIdGenerator.parse( + IdGenerator.of("1:marko"))); + Assert.assertArrayEquals(new String[]{"book", "c", "2020"}, + SplicingIdGenerator.parse( + IdGenerator.of("book:c:2020"))); + } +}