This is an automated email from the ASF dual-hosted git repository.

jiangtian pushed a commit to branch auto_detect_test_only
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 166118e6bb816ef2038f3791b0078a9a91b1ec0e
Author: Tian Jiang <[email protected]>
AuthorDate: Fri Nov 8 18:22:24 2024 +0800

    Support auto detecting the misuses of TestOnly annotation
---
 iotdb-core/datanode/pom.xml                        |  6 +++
 .../java/org/apache/iotdb/db/conf/IoTDBConfig.java |  1 -
 .../plan/planner/plan/node/write/InsertNode.java   |  1 -
 .../apache/iotdb/db/schemaengine/SchemaEngine.java |  1 -
 .../schemaengine/schemaregion/ISchemaRegion.java   |  1 -
 .../impl/pbtree/schemafile/WrappedSegment.java     |  2 -
 .../template/ClusterTemplateManager.java           |  1 -
 .../db/storageengine/dataregion/DataRegion.java    |  1 -
 .../dataregion/memtable/TsFileProcessor.java       |  1 -
 .../storageengine/dataregion/wal/WALManager.java   |  1 -
 .../wal/allocation/NodeAllocationStrategy.java     |  1 -
 .../iotdb/db/tools/utils/TsFileValidationScan.java |  1 -
 .../org/apache/iotdb/db/utils/AnnotationTest.java  | 56 ++++++++++++++++++++++
 .../org/apache/iotdb/commons/utils/TestOnly.java   |  2 +-
 14 files changed, 63 insertions(+), 13 deletions(-)

diff --git a/iotdb-core/datanode/pom.xml b/iotdb-core/datanode/pom.xml
index b85da147672..8e8e5f6e232 100644
--- a/iotdb-core/datanode/pom.xml
+++ b/iotdb-core/datanode/pom.xml
@@ -378,6 +378,12 @@
             <artifactId>awaitility</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.tngtech.archunit</groupId>
+            <artifactId>archunit</artifactId>
+            <version>1.3.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     <build>
         <plugins>
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
index 5b288d9120c..942fe993c56 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
@@ -3133,7 +3133,6 @@ public class IoTDBConfig {
     return cachedMNodeSizeInPBTreeMode;
   }
 
-  @TestOnly
   public void setCachedMNodeSizeInPBTreeMode(int cachedMNodeSizeInPBTreeMode) {
     this.cachedMNodeSizeInPBTreeMode = cachedMNodeSizeInPBTreeMode;
   }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertNode.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertNode.java
index 79c85994a0b..9693a95f6a6 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertNode.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertNode.java
@@ -294,7 +294,6 @@ public abstract class InsertNode extends SearchNode {
   public abstract long getMinTime();
 
   // region partial insert
-  @TestOnly
   public void markFailedMeasurement(int index) {
     throw new UnsupportedOperationException();
   }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/SchemaEngine.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/SchemaEngine.java
index cdbe91f4e02..9b26be6543b 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/SchemaEngine.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/SchemaEngine.java
@@ -444,7 +444,6 @@ public class SchemaEngine {
     }
   }
 
-  @TestOnly
   public ISchemaEngineStatistics getSchemaEngineStatistics() {
     return schemaEngineStatistics;
   }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/ISchemaRegion.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/ISchemaRegion.java
index a120443a086..cb1e022bb7d 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/ISchemaRegion.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/ISchemaRegion.java
@@ -94,7 +94,6 @@ public interface ISchemaRegion {
 
   void forceMlog();
 
-  @TestOnly
   ISchemaRegionStatistics getSchemaRegionStatistics();
 
   ISchemaRegionMetric getSchemaRegionMetric();
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/WrappedSegment.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/WrappedSegment.java
index e46f1aea025..8d9d4032883 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/WrappedSegment.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/WrappedSegment.java
@@ -902,7 +902,6 @@ public class WrappedSegment implements ISegment<ByteBuffer, 
ICachedMNode> {
     return null;
   }
 
-  @TestOnly
   public List<Pair<String, Short>> getKeyOffsetList() {
     short[] offsets = getOffsets();
     List<Pair<String, Short>> res = new ArrayList<>();
@@ -917,7 +916,6 @@ public class WrappedSegment implements ISegment<ByteBuffer, 
ICachedMNode> {
     return res;
   }
 
-  @TestOnly
   public short[] getOffsets() {
     ByteBuffer buf = this.buffer.asReadOnlyBuffer();
     short[] ofs = new short[recordNum];
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/template/ClusterTemplateManager.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/template/ClusterTemplateManager.java
index 4459f29ed6b..18229858ceb 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/template/ClusterTemplateManager.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/template/ClusterTemplateManager.java
@@ -651,7 +651,6 @@ public class ClusterTemplateManager implements 
ITemplateManager {
     templateNameMap.put(template.getName(), template.getId());
   }
 
-  @TestOnly
   public void clear() {
     templateIdMap.clear();
     templateNameMap.clear();
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java
index c73d93de5da..fddac6d7603 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java
@@ -3940,7 +3940,6 @@ public class DataRegion implements IDataRegionForQuery {
     return lastFlushTimeMap;
   }
 
-  @TestOnly
   public TsFileManager getTsFileManager() {
     return tsFileManager;
   }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java
index f1dde34972f..249d6f3d04a 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessor.java
@@ -2328,7 +2328,6 @@ public class TsFileProcessor {
         && (workMemTable == null || workMemTable.getTotalPointsNum() == 0);
   }
 
-  @TestOnly
   public IMemTable getWorkMemTable() {
     return workMemTable;
   }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/WALManager.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/WALManager.java
index 42f79ea310a..b3bca40ce29 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/WALManager.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/WALManager.java
@@ -309,7 +309,6 @@ public class WALManager implements IService {
     }
   }
 
-  @TestOnly
   public void clear() {
     totalDiskUsage.set(0);
     walNodesManager.clear();
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/allocation/NodeAllocationStrategy.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/allocation/NodeAllocationStrategy.java
index 3824bf3efd8..b7d9313a1fd 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/allocation/NodeAllocationStrategy.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/allocation/NodeAllocationStrategy.java
@@ -37,6 +37,5 @@ public interface NodeAllocationStrategy {
   /** Get all wal nodes num. Not thread-safe, used for metrics only. */
   int getNodesNum();
 
-  @TestOnly
   void clear();
 }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/tools/utils/TsFileValidationScan.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/tools/utils/TsFileValidationScan.java
index 1e6c6e2268e..6df98c5e792 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/tools/utils/TsFileValidationScan.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/tools/utils/TsFileValidationScan.java
@@ -352,7 +352,6 @@ public class TsFileValidationScan extends 
TsFileSequenceScan {
     return badFileNum;
   }
 
-  @TestOnly
   public void setBadFileNum(int badFileNum) {
     this.badFileNum = badFileNum;
   }
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/AnnotationTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/AnnotationTest.java
new file mode 100644
index 00000000000..080529e0d61
--- /dev/null
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/AnnotationTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.iotdb.db.utils;
+
+import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;
+
+import com.tngtech.archunit.core.domain.JavaClass;
+import com.tngtech.archunit.core.domain.JavaClasses;
+import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
+import com.tngtech.archunit.core.importer.ClassFileImporter;
+import com.tngtech.archunit.core.importer.ImportOption;
+import com.tngtech.archunit.core.importer.ImportOption.DoNotIncludeTests;
+import com.tngtech.archunit.lang.ArchRule;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.iotdb.commons.utils.TestOnly;
+import org.junit.Test;
+
+public class AnnotationTest {
+
+  @Test
+  public void checkTestOnly() {
+    JavaClasses productionClasses = new 
ClassFileImporter().withImportOption(new 
DoNotIncludeTests()).importPackages("org.apache.iotdb");
+    JavaClasses testClasses = new ClassFileImporter().withImportOption(new 
ImportOption.OnlyIncludeTests()).importPackages("org.apache.iotdb");
+
+    List<Class> testReflectedClasses = new ArrayList<>();
+    for (JavaClass testClass : testClasses) {
+      testReflectedClasses.add(testClass.reflect());
+    }
+
+    ArchRule rule = methods().that().areAnnotatedWith(TestOnly.class).
+        should().onlyBeCalled()
+        .byClassesThat().belongToAnyOf(testReflectedClasses.toArray(new 
Class[0])).
+        orShould().onlyBeCalled().byMethodsThat(
+            CanBeAnnotated.Predicates.annotatedWith(TestOnly.class)); // see 
next section
+
+    rule.check(productionClasses);
+  }
+}
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/TestOnly.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/TestOnly.java
index d31cbc83195..77c723deec5 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/TestOnly.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/TestOnly.java
@@ -30,5 +30,5 @@ import java.lang.annotation.Target;
  * functionality is not guaranteed and may interfere with the normal code.
  */
 @Target({ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR, 
ElementType.TYPE})
-@Retention(RetentionPolicy.SOURCE)
+@Retention(RetentionPolicy.CLASS)
 public @interface TestOnly {}

Reply via email to