From 4c8e9097bd616a1ae4c611a542cbdea20ba871dc Mon Sep 17 00:00:00 2001 From: Stefan Miklosovic Date: Sat, 28 Jan 2023 16:18:51 +0100 Subject: [PATCH] Introduce check for names of test classes patch by Stefan Miklosovic; reviewed by Berenguer Blasi and David Capwell for CASSANDRA-17943 --- CHANGES.txt | 1 + build.xml | 17 +++- .../cassandra/anttasks/TestNameCheckTask.java | 85 +++++++++++++++++++ ...amples.java => ByteBuddyExamplesTest.java} | 2 +- ...ur.java => ReprepareOldBehaviourTest.java} | 2 +- .../cql3/{BatchTests.java => BatchTest.java} | 2 +- .../utils/{UUIDTests.java => UUIDTest.java} | 3 +- 7 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 test/anttasks/org/apache/cassandra/anttasks/TestNameCheckTask.java rename test/distributed/org/apache/cassandra/distributed/test/{ByteBuddyExamples.java => ByteBuddyExamplesTest.java} (98%) rename test/distributed/org/apache/cassandra/distributed/test/{ReprepareTestOldBehaviour.java => ReprepareOldBehaviourTest.java} (98%) rename test/unit/org/apache/cassandra/cql3/{BatchTests.java => BatchTest.java} (99%) rename test/unit/org/apache/cassandra/utils/{UUIDTests.java => UUIDTest.java} (97%) diff --git a/CHANGES.txt b/CHANGES.txt index 5600dc17fd..aceaa87b59 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 3.0.29 + * Introduce check for names of test classes (CASSANDRA-17964) * Suppress CVE-2021-1471, CVE-2021-3064, CVE-2021-4235 (CASSANDRA-18149) * Switch to snakeyaml's SafeConstructor (CASSANDRA-18150) * Expand build.dir property in rat targets (CASSANDRA-18183) diff --git a/build.xml b/build.xml index 965ff8640d..14f434cf7c 100644 --- a/build.xml +++ b/build.xml @@ -67,6 +67,7 @@ + @@ -360,7 +361,7 @@ - + @@ -1097,6 +1098,7 @@ + @@ -1104,12 +1106,25 @@ + + + + + + + + + + + + + diff --git a/test/anttasks/org/apache/cassandra/anttasks/TestNameCheckTask.java b/test/anttasks/org/apache/cassandra/anttasks/TestNameCheckTask.java new file mode 100644 index 0000000000..a5222261f2 --- /dev/null +++ b/test/anttasks/org/apache/cassandra/anttasks/TestNameCheckTask.java @@ -0,0 +1,85 @@ +/* + * 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.anttasks; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.List; +import java.util.Set; +import java.util.stream.Stream; + +import org.junit.Test; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; +import org.reflections.Reflections; +import org.reflections.scanners.Scanners; +import org.reflections.util.ConfigurationBuilder; + +import static java.util.stream.Collectors.toList; + +public class TestNameCheckTask extends Task +{ + private static final Reflections reflections = new Reflections(new ConfigurationBuilder() + .forPackage("org.apache.cassandra") + .setScanners(Scanners.MethodsAnnotated, Scanners.SubTypes) + .setExpandSuperTypes(true) + .setParallel(true)); + + public TestNameCheckTask() + { + } + + @Override + public void execute() throws BuildException + { + Set methodsAnnotatedWith = reflections.getMethodsAnnotatedWith(Test.class); + List testFiles = methodsAnnotatedWith.stream().map(Method::getDeclaringClass).distinct() + .flatMap(TestNameCheckTask::expand) + .map(TestNameCheckTask::normalize) + .map(Class::getCanonicalName) + .filter(s -> !s.endsWith("Test")) + .distinct().sorted() + .collect(toList()); + + if (!testFiles.isEmpty()) + throw new BuildException("Detected tests that have a bad naming convention. All tests have to end on 'Test': \n" + String.join("\n", testFiles)); + } + + private static Class normalize(Class klass) + { + for (; klass.getEnclosingClass() != null; klass = klass.getEnclosingClass()) + { + } + return klass; + } + + private static Stream> expand(Class klass) + { + Set> subTypes = reflections.getSubTypesOf(klass); + if (subTypes == null || subTypes.isEmpty()) + return Stream.of(klass); + Stream> subs = (Stream>) subTypes.stream(); + // assume we include if not abstract + if (!Modifier.isAbstract(klass.getModifiers())) + subs = Stream.concat(Stream.of(klass), subs); + return subs; + } + + +} diff --git a/test/distributed/org/apache/cassandra/distributed/test/ByteBuddyExamples.java b/test/distributed/org/apache/cassandra/distributed/test/ByteBuddyExamplesTest.java similarity index 98% rename from test/distributed/org/apache/cassandra/distributed/test/ByteBuddyExamples.java rename to test/distributed/org/apache/cassandra/distributed/test/ByteBuddyExamplesTest.java index b63b3e1c61..1934a2e67a 100644 --- a/test/distributed/org/apache/cassandra/distributed/test/ByteBuddyExamples.java +++ b/test/distributed/org/apache/cassandra/distributed/test/ByteBuddyExamplesTest.java @@ -41,7 +41,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -public class ByteBuddyExamples extends TestBaseImpl +public class ByteBuddyExamplesTest extends TestBaseImpl { @Test public void writeFailureTest() throws Throwable diff --git a/test/distributed/org/apache/cassandra/distributed/test/ReprepareTestOldBehaviour.java b/test/distributed/org/apache/cassandra/distributed/test/ReprepareOldBehaviourTest.java similarity index 98% rename from test/distributed/org/apache/cassandra/distributed/test/ReprepareTestOldBehaviour.java rename to test/distributed/org/apache/cassandra/distributed/test/ReprepareOldBehaviourTest.java index 9900febaea..2139485161 100644 --- a/test/distributed/org/apache/cassandra/distributed/test/ReprepareTestOldBehaviour.java +++ b/test/distributed/org/apache/cassandra/distributed/test/ReprepareOldBehaviourTest.java @@ -30,7 +30,7 @@ import static org.apache.cassandra.distributed.api.Feature.GOSSIP; import static org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL; import static org.apache.cassandra.distributed.api.Feature.NETWORK; -public class ReprepareTestOldBehaviour extends ReprepareTestBase +public class ReprepareOldBehaviourTest extends ReprepareTestBase { @Test public void testReprepareMixedVersion() throws Throwable diff --git a/test/unit/org/apache/cassandra/cql3/BatchTests.java b/test/unit/org/apache/cassandra/cql3/BatchTest.java similarity index 99% rename from test/unit/org/apache/cassandra/cql3/BatchTests.java rename to test/unit/org/apache/cassandra/cql3/BatchTest.java index 260db4eeed..28b0d5cbf7 100644 --- a/test/unit/org/apache/cassandra/cql3/BatchTests.java +++ b/test/unit/org/apache/cassandra/cql3/BatchTest.java @@ -30,7 +30,7 @@ import org.junit.Test; import java.io.IOException; -public class BatchTests extends CQLTester +public class BatchTest extends CQLTester { private static EmbeddedCassandraService cassandra; diff --git a/test/unit/org/apache/cassandra/utils/UUIDTests.java b/test/unit/org/apache/cassandra/utils/UUIDTest.java similarity index 97% rename from test/unit/org/apache/cassandra/utils/UUIDTests.java rename to test/unit/org/apache/cassandra/utils/UUIDTest.java index 83e421ad77..5043eabfa9 100644 --- a/test/unit/org/apache/cassandra/utils/UUIDTests.java +++ b/test/unit/org/apache/cassandra/utils/UUIDTest.java @@ -27,10 +27,9 @@ import java.util.UUID; import org.junit.Test; import org.apache.cassandra.db.marshal.TimeUUIDType; -import org.apache.cassandra.utils.UUIDGen; -public class UUIDTests +public class UUIDTest { @Test public void verifyType1()