Repository: kylin Updated Branches: refs/heads/master 82ae39aa2 -> 7c0c176b0
KYLIN-1541 add unit test Project: http://git-wip-us.apache.org/repos/asf/kylin/repo Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/7c0c176b Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/7c0c176b Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/7c0c176b Branch: refs/heads/master Commit: 7c0c176b07e0098f32007b98b16a1c7948be3664 Parents: 82ae39a Author: Li Yang <[email protected]> Authored: Mon Mar 28 18:03:30 2016 +0800 Committer: Li Yang <[email protected]> Committed: Mon Mar 28 18:03:30 2016 +0800 ---------------------------------------------------------------------- .../kylin/dimension/IntegerDimEncTest.java | 129 +++++++++++++++++++ 1 file changed, 129 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kylin/blob/7c0c176b/core-metadata/src/test/java/org/apache/kylin/dimension/IntegerDimEncTest.java ---------------------------------------------------------------------- diff --git a/core-metadata/src/test/java/org/apache/kylin/dimension/IntegerDimEncTest.java b/core-metadata/src/test/java/org/apache/kylin/dimension/IntegerDimEncTest.java new file mode 100644 index 0000000..2ed4fb3 --- /dev/null +++ b/core-metadata/src/test/java/org/apache/kylin/dimension/IntegerDimEncTest.java @@ -0,0 +1,129 @@ +/* + * 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 org.apache.kylin.dimension; + +import java.nio.ByteBuffer; + +import org.apache.kylin.common.util.Bytes; +import org.apache.kylin.dimension.DimensionEncoding; +import org.apache.kylin.dimension.IntegerDimEnc; +import org.apache.kylin.metadata.datatype.DataTypeSerializer; +import org.junit.Assert; +import org.junit.Test; + +public class IntegerDimEncTest { + + @Test + public void testConstructor() { + try { + new IntegerDimEnc(0); + Assert.fail(); + } catch (IllegalArgumentException e) { + // expect + } + try { + new IntegerDimEnc(9); + Assert.fail(); + } catch (IllegalArgumentException e) { + // expect + } + new IntegerDimEnc(8); + } + + @Test + public void testNull() { + for (int i = 1; i < 9; i++) { + IntegerDimEnc enc = new IntegerDimEnc(i); + + byte[] buf = new byte[enc.getLengthOfEncoding()]; + enc.encode(null, 0, buf, 0); + Assert.assertTrue(DimensionEncoding.isNull(buf, 0, buf.length)); + String decode = enc.decode(buf, 0, buf.length); + Assert.assertEquals(null, decode); + + buf = new byte[enc.getLengthOfEncoding()]; + DataTypeSerializer<Object> ser = enc.asDataTypeSerializer(); + ser.serialize(null, ByteBuffer.wrap(buf)); + Assert.assertTrue(DimensionEncoding.isNull(buf, 0, buf.length)); + decode = (String) ser.deserialize(ByteBuffer.wrap(buf)); + Assert.assertEquals(null, decode); + } + } + + @Test + public void testEncodeDecode() { + IntegerDimEnc enc = new IntegerDimEnc(2); + testEncodeDecode(enc, 0); + testEncodeDecode(enc, 100); + testEncodeDecode(enc, 10000); + testEncodeDecode(enc, 65534); + try { + testEncodeDecode(enc, 65535); + Assert.fail(); + } catch (Throwable e) { + Assert.assertEquals("expected:<65535> but was:<null>", e.getMessage()); + } + try { + testEncodeDecode(enc, 65536); + Assert.fail(); + } catch (Throwable e) { + Assert.assertEquals("expected:<[65536]> but was:<[0]>", e.getMessage()); + } + } + + private void testEncodeDecode(IntegerDimEnc enc, long value) { + byte[] buf = new byte[enc.getLengthOfEncoding()]; + String valueStr = "" + value; + byte[] bytes = Bytes.toBytes(valueStr); + enc.encode(bytes, bytes.length, buf, 0); + String decode = enc.decode(buf, 0, buf.length); + Assert.assertEquals(valueStr, decode); + } + + @Test + public void testSerDes() { + IntegerDimEnc enc = new IntegerDimEnc(2); + testSerDes(enc, 0); + testSerDes(enc, 100); + testSerDes(enc, 10000); + testSerDes(enc, 65534); + try { + testSerDes(enc, 65535); + Assert.fail(); + } catch (Throwable e) { + Assert.assertEquals("expected:<65535> but was:<null>", e.getMessage()); + } + try { + testSerDes(enc, 65536); + Assert.fail(); + } catch (Throwable e) { + Assert.assertEquals("expected:<[65536]> but was:<[0]>", e.getMessage()); + } + } + + private void testSerDes(IntegerDimEnc enc, long value) { + DataTypeSerializer<Object> ser = enc.asDataTypeSerializer(); + byte[] buf = new byte[enc.getLengthOfEncoding()]; + String valueStr = "" + value; + ser.serialize(valueStr, ByteBuffer.wrap(buf)); + String decode = (String) ser.deserialize(ByteBuffer.wrap(buf)); + Assert.assertEquals(valueStr, decode); + } + +}
