dcapwell commented on code in PR #2310: URL: https://github.com/apache/cassandra/pull/2310#discussion_r1186556542
########## test/unit/org/apache/cassandra/db/marshal/AbstractTypeTest.java: ########## @@ -0,0 +1,439 @@ +/* + * 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.cassandra.db.marshal; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.lang.reflect.Modifier; +import java.net.InetAddress; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import com.google.common.collect.Sets; +import org.junit.Test; + +import org.apache.cassandra.cql3.AssignmentTestable; +import org.apache.cassandra.cql3.ColumnIdentifier; +import org.apache.cassandra.cql3.ColumnSpecification; +import org.apache.cassandra.cql3.Duration; +import org.apache.cassandra.cql3.Json; +import org.apache.cassandra.cql3.VariableSpecifications; +import org.apache.cassandra.io.util.DataInputBuffer; +import org.apache.cassandra.io.util.DataInputPlus; +import org.apache.cassandra.io.util.DataOutputBuffer; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.schema.CQLTypeParser; +import org.apache.cassandra.schema.ColumnMetadata; +import org.apache.cassandra.schema.Types; +import org.apache.cassandra.transport.ProtocolVersion; +import org.apache.cassandra.utils.AbstractTypeGenerators; +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.FastByteOperations; +import org.apache.cassandra.utils.bytecomparable.ByteComparable; +import org.apache.cassandra.utils.bytecomparable.ByteSource; +import org.quicktheories.core.Gen; +import org.reflections.Reflections; +import org.reflections.scanners.Scanners; +import org.reflections.util.ConfigurationBuilder; + +import static org.apache.cassandra.utils.AbstractTypeGenerators.extractUDTs; +import static org.assertj.core.api.Assertions.assertThat; +import static org.quicktheories.QuickTheory.qt; + +public class AbstractTypeTest +{ + //TODO + // isCompatibleWith/isValueCompatibleWith/isSerializationCompatibleWith, + // withUpdatedUserType/expandUserTypes/referencesDuration - types that recursive check types + // getMaskedValue + + @Test + public void allTypesCovered() + { + // this test just makes sure that all types are covered and no new type is left out + Reflections reflections = new Reflections(new ConfigurationBuilder() + .forPackage("org.apache.cassandra") + .setScanners(Scanners.SubTypes) + .setExpandSuperTypes(true) + .setParallel(true)); + Set<Class<? extends AbstractType>> subTypes = reflections.getSubTypesOf(AbstractType.class); + Set<Class<? extends AbstractType>> coverage = AbstractTypeGenerators.knownTypes(); + StringBuilder sb = new StringBuilder(); + for (Class<? extends AbstractType> klass : Sets.difference(subTypes, coverage)) + { + if (Modifier.isAbstract(klass.getModifiers())) + continue; + if ("test".equals(new File(klass.getProtectionDomain().getCodeSource().getLocation().getPath()).name())) + continue; + String name = klass.getCanonicalName(); + if (name == null) + name = klass.getName(); + sb.append(name).append('\n'); + } + if (sb.length() > 0) + throw new AssertionError("Uncovered types:\n" + sb); + } + + @Test + public void comparableBytes() + { + qt().withShrinkCycles(0).forAll(examples(1)).checkAssert(example -> { + AbstractType type = example.type; + for (Object value : example.samples) + { + ByteBuffer bb = type.decompose(value); + for (ByteComparable.Version bcv : ByteComparable.Version.values()) + { + // LEGACY, // Encoding used in legacy sstable format; forward (value to byte-comparable) translation only + // Legacy is a one-way conversion, so for this test ignore + if (bcv == ByteComparable.Version.LEGACY) + continue; + ByteSource.Peekable comparable = ByteSource.peekable(type.asComparableBytes(bb, bcv)); + if (comparable == null) + throw new NullPointerException(); + ByteBuffer read; + try + { + read = type.fromComparableBytes(comparable, bcv); + } + catch (Exception | Error e) + { + throw new AssertionError(String.format("Unable to parse comparable bytes for type %s and version %s; value %s", type.asCQL3Type(), bcv, type.toCQLString(bb)), e); + } + assertThat(read).isEqualTo(bb); + } + } + }); + } + + @Test + public void knowThySelf() + { + qt().withShrinkCycles(0).forAll(AbstractTypeGenerators.typeGen()).checkAssert(type -> { + assertThat(type.testAssignment(null, new ColumnSpecification(null, null, null, type))).isEqualTo(AssignmentTestable.TestResult.EXACT_MATCH); + assertThat(type.testAssignment(type)).isEqualTo(AssignmentTestable.TestResult.EXACT_MATCH); + }); + } + + @Test + public void json() + { + qt().withShrinkCycles(0).forAll(examples(1)).checkAssert(es -> { + AbstractType type = es.type; + for (Object example : es.samples) + { + ByteBuffer bb = type.decompose(example); + String json = type.toJSONString(bb, ProtocolVersion.CURRENT); + ColumnMetadata column = fake(type); + String cqlJson = "{\"" + column.name + "\": " + json + "}"; + try + { + Json.Prepared prepared = new Json.Literal(cqlJson).prepareAndCollectMarkers(null, Collections.singletonList(column), VariableSpecifications.empty()); + } + catch (Exception e) + { + throw new AssertionError("Unable to parse JSON for " + json + "; type " + type.asCQL3Type(), e); + } + } + }); + } + + @Test + public void nested() + { + qt().withShrinkCycles(0).forAll(AbstractTypeGenerators.builder().withoutTypeKinds(AbstractTypeGenerators.TypeKind.PRIMITIVE).build()).checkAssert(type -> { + List<AbstractType<?>> subtypes = type.subTypes(); + assertThat(subtypes).hasSize(type instanceof MapType ? 2 : type instanceof TupleType ? ((TupleType) type).size() : 1); + }); + } + + @Test + public void serde() + { + Gen<AbstractType<?>> typeGen = genBuilder() + // fromCQL(toCQL()) does not work + .withoutPrimitive(DurationType.instance) + .build(); + qt().withShrinkCycles(0).forAll(examples(1, typeGen)).checkAssert(example -> { + AbstractType type = example.type; + + // to -> from cql + String cqlType = type.asCQL3Type().toString(); + assertThat(CQLTypeParser.parse(null, cqlType, toTypes(extractUDTs(type)))).describedAs("CQL type %s parse did not match the expected type", cqlType).isEqualTo(type); + + for (Object expected : example.samples) + { + ByteBuffer bb = type.decompose(expected); + int position = bb.position(); + type.validate(bb); + Object read = type.compose(bb); + assertThat(bb.position()).describedAs("ByteBuffer was mutated by %s", type).isEqualTo(position); + assertThat(read).isEqualTo(expected); + + String str = type.getString(bb); + assertThat(type.fromString(str)).describedAs("fromString(getString(bb)) != bb; %s", str).isEqualTo(bb); + + String literal = type.asCQL3Type().toCQLLiteral(bb, ProtocolVersion.CURRENT); + ByteBuffer cqlBB = parseLiteralType(type, literal); + assertThat(ByteBufferUtil.bytesToHex(cqlBB)).describedAs("Deserializing literal %s did not match expected bytes", literal).isEqualTo(ByteBufferUtil.bytesToHex(bb)); + + try (DataOutputBuffer out = DataOutputBuffer.scratchBuffer.get()) + { + type.writeValue(bb, out); + ByteBuffer written = out.unsafeGetBufferAndFlip(); + DataInputPlus in = new DataInputBuffer(written, true); + assertThat(type.readBuffer(in)).isEqualTo(bb); + in = new DataInputBuffer(written, false); + type.skipValue(in); + assertThat(written.remaining()).isEqualTo(0); + } + catch (IOException e) + { + throw new UncheckedIOException(e); + } + } + }); + } + + private static ColumnMetadata fake(AbstractType<?> type) + { + return new ColumnMetadata(null, null, new ColumnIdentifier("", true), type, 0, ColumnMetadata.Kind.PARTITION_KEY, null); + } + + private static ByteBuffer parseLiteralType(AbstractType<?> type, String literal) + { + try + { + return type.asCQL3Type().fromCQLLiteral(literal); + } + catch (Exception e) + { + throw new AssertionError(String.format("Unable to parse CQL literal %s from type %s", literal, type.asCQL3Type())); + } + } + + private static Types toTypes(Set<UserType> udts) + { + if (udts.isEmpty()) + return Types.none(); + Types.Builder builder = Types.builder(); + for (UserType udt : udts) + builder.add(udt.unfreeze()); + return builder.build(); + } + + private static ByteComparable fromBytes(AbstractType<?> type, ByteBuffer bb) + { + return version -> type.asComparableBytes(bb, version); + } + + @Test + public void ordering() + { + Gen<AbstractType<?>> types = genBuilder() + .withoutPrimitive(DurationType.instance) // this uses byte ordering and vint, which makes the ordering effectivlly random from a user's point of view + .build(); + qt().withShrinkCycles(0).forAll(examples(10, types)).checkAssert(example -> { + AbstractType type = example.type; + List<ByteBuffer> actual = decompose(type, example.samples); + Collections.sort(actual, type); + List<ByteBuffer>[] byteOrdered = new List[ByteComparable.Version.values().length]; + for (int i = 0; i < byteOrdered.length; i++) + { + byteOrdered[i] = new ArrayList<>(actual); + ByteComparable.Version version = ByteComparable.Version.values()[i]; + Collections.sort(byteOrdered[i], (a, b) -> ByteComparable.compare(fromBytes(type, a), fromBytes(type, b), version)); + } + + Collections.sort(example.samples, comparator(type)); + List<Object> real = new ArrayList<>(actual.size()); + for (ByteBuffer bb : actual) + real.add(type.compose(bb)); + assertThat(real).isEqualTo(example.samples); + List<Object>[] realBytesOrder = new List[byteOrdered.length]; + for (int i = 0; i < realBytesOrder.length; i++) + { + List<ByteBuffer> ordered = byteOrdered[i]; + List<Object> realOrdered = new ArrayList<>(ordered.size()); + for (ByteBuffer bb : ordered) + realOrdered.add(type.compose(bb)); + assertThat(real).describedAs("Bad ordering for type %s", ByteComparable.Version.values()[i]).isEqualTo(realOrdered); + } + }); + } + + private static Comparator<Object> comparator(AbstractType<?> type) Review Comment: TODO move to abstract type gen's support? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

