From 8e210629516d080b43822bd530d39498499fc235 Mon Sep 17 00:00:00 2001 From: Markus Paulsen Date: Thu, 5 Dec 2024 20:33:15 +0100 Subject: [PATCH] Improved Advice Tests --- .../cit/ase/ares/api/aop/JavaAOPModeTest.java | 2 +- ...rumentationDeletePathMethodAdviceTest.java | 25 ++++++----- ...umentationExecutePathMethodAdviceTest.java | 22 ++++------ ...entationOverwritePathMethodAdviceTest.java | 43 ++++++++----------- ...entationReadPathConstructorAdviceTest.java | 24 +++++------ ...strumentationReadPathMethodAdviceTest.java | 43 ++++++++----------- ...avaArchitectureTestCaseCollectionTest.java | 30 ------------- 7 files changed, 71 insertions(+), 118 deletions(-) delete mode 100644 src/test/java/de/tum/cit/ase/ares/api/architecture/JavaArchitectureTestCaseCollectionTest.java diff --git a/src/test/java/de/tum/cit/ase/ares/api/aop/JavaAOPModeTest.java b/src/test/java/de/tum/cit/ase/ares/api/aop/JavaAOPModeTest.java index 62ee5964..5a71d37d 100644 --- a/src/test/java/de/tum/cit/ase/ares/api/aop/JavaAOPModeTest.java +++ b/src/test/java/de/tum/cit/ase/ares/api/aop/JavaAOPModeTest.java @@ -22,7 +22,7 @@ class JavaAOPModeTest { * - [List the specific files or types of files expected] */ private static final int INSTRUMENTATION_FILES_COUNT = 13; - private static final int INSTRUMENTATION_VALUES_COUNT = 13; + private static final int INSTRUMENTATION_VALUES_COUNT = 12; /** * Expected number of files to copy for AspectJ mode. diff --git a/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationDeletePathMethodAdviceTest.java b/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationDeletePathMethodAdviceTest.java index 07ad368d..f67a3feb 100644 --- a/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationDeletePathMethodAdviceTest.java +++ b/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationDeletePathMethodAdviceTest.java @@ -13,14 +13,17 @@ class JavaInstrumentationDeletePathMethodAdviceTest { private static final String CLASS_NAME = "de.tum.cit.ase.ares.api.aop.java.instrumentation.advice.JavaInstrumentationDeletePathMethodAdvice"; private static final String METHOD_NAME = "methodName"; private static final String METHOD_SIGNATURE = "methodSignature"; - private static final Object[] ATTRIBUTES = new Object[]{"context"}; - private static final Object[] PARAMETERS = new Object[]{"/test/file/path"}; + private static final Object[] ATTRIBUTES = new Object[]{"attrib1", "attrib2"}; + private static final Object[] PARAMETERS = new Object[]{"param1", "param2"}; + private static final Object INSTANCE = new Object() { + public final String attrib1 = "attrib1"; + public final String attrib2 = "attrib2"; + }; @Test void shouldCheckFileSystemInteraction_whenDeletingPath() { try (MockedStatic mockedToolbox = mockStatic(JavaInstrumentationAdviceToolbox.class)) { // Arrange - SecurityException expectedViolation = null; mockedToolbox.when(() -> JavaInstrumentationAdviceToolbox.checkFileSystemInteraction( OPERATION, CLASS_NAME, @@ -28,25 +31,25 @@ void shouldCheckFileSystemInteraction_whenDeletingPath() { METHOD_SIGNATURE, ATTRIBUTES, PARAMETERS - )).thenReturn(expectedViolation); + )).thenAnswer(invocation -> null); // Act JavaInstrumentationDeletePathMethodAdvice.onEnter( CLASS_NAME, METHOD_NAME, METHOD_SIGNATURE, - ATTRIBUTES, + INSTANCE, PARAMETERS ); // Assert mockedToolbox.verify(() -> JavaInstrumentationAdviceToolbox.checkFileSystemInteraction( - eq(OPERATION), - eq(CLASS_NAME), - eq(METHOD_NAME), - eq(METHOD_SIGNATURE), - eq(ATTRIBUTES), - eq(PARAMETERS) + OPERATION, + CLASS_NAME, + METHOD_NAME, + METHOD_SIGNATURE, + ATTRIBUTES, + PARAMETERS )); } } diff --git a/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationExecutePathMethodAdviceTest.java b/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationExecutePathMethodAdviceTest.java index 33944c36..c2f321ca 100644 --- a/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationExecutePathMethodAdviceTest.java +++ b/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationExecutePathMethodAdviceTest.java @@ -15,21 +15,15 @@ class JavaInstrumentationExecutePathMethodAdviceTest { private static final String CLASS_NAME = "de.tum.cit.ase.ares.api.aop.java.instrumentation.advice.JavaInstrumentationExecutePathMethodAdvice"; private static final String METHOD_NAME = "methodName"; private static final String METHOD_SIGNATURE = "methodSignature"; + private static final Object[] ATTRIBUTES = new Object[]{"attrib1", "attrib2"}; private static final Object[] PARAMETERS = new Object[]{"param1", "param2"}; + private static final Object INSTANCE = new Object() { + public final String attrib1 = "attrib1"; + public final String attrib2 = "attrib2"; + }; @Test void testOnEnter() throws IllegalAccessException { - Object mockInstance = new Object() { - public final String field1 = "value1"; - public final int field2 = 42; - }; - - Field[] fields = mockInstance.getClass().getDeclaredFields(); - Object[] attributes = new Object[fields.length]; - for (int i = 0; i < fields.length; i++) { - fields[i].setAccessible(true); - attributes[i] = fields[i].get(mockInstance); - } try (MockedStatic mockedToolbox = mockStatic(JavaInstrumentationAdviceToolbox.class)) { // Arrange @@ -38,7 +32,7 @@ void testOnEnter() throws IllegalAccessException { CLASS_NAME, METHOD_NAME, METHOD_SIGNATURE, - attributes, + ATTRIBUTES, PARAMETERS )).thenAnswer(invocation -> null); @@ -47,7 +41,7 @@ void testOnEnter() throws IllegalAccessException { CLASS_NAME, METHOD_NAME, METHOD_SIGNATURE, - mockInstance, + INSTANCE, PARAMETERS ); @@ -57,7 +51,7 @@ void testOnEnter() throws IllegalAccessException { CLASS_NAME, METHOD_NAME, METHOD_SIGNATURE, - attributes, + ATTRIBUTES, PARAMETERS )); } diff --git a/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationOverwritePathMethodAdviceTest.java b/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationOverwritePathMethodAdviceTest.java index 29cedf02..e4834e10 100644 --- a/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationOverwritePathMethodAdviceTest.java +++ b/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationOverwritePathMethodAdviceTest.java @@ -16,32 +16,25 @@ class JavaInstrumentationOverwritePathMethodAdviceTest { private static final String CLASS_NAME = "de.tum.cit.ase.ares.api.aop.java.instrumentation.advice.JavaInstrumentationOverwritePathMethodAdvice"; private static final String METHOD_NAME = "methodName"; private static final String METHOD_SIGNATURE = "methodSignature"; + private static final Object[] ATTRIBUTES = new Object[]{"attrib1", "attrib2"}; private static final Object[] PARAMETERS = new Object[]{"param1", "param2"}; + private static final Object INSTANCE = new Object() { + public final String attrib1 = "attrib1"; + public final String attrib2 = "attrib2"; + }; @Test void testOnEnter() throws Exception { - class MockClass { - private final String field1 = "value1"; - private final int field2 = 42; - } - - MockClass mockInstance = new MockClass(); - Field[] fields = mockInstance.getClass().getDeclaredFields(); - Object[] attributes = new Object[fields.length]; - for (int i = 0; i < fields.length; i++) { - fields[i].setAccessible(true); - attributes[i] = fields[i].get(mockInstance); - } try (MockedStatic mockedToolbox = mockStatic(JavaInstrumentationAdviceToolbox.class)) { // Arrange mockedToolbox.when(() -> JavaInstrumentationAdviceToolbox.checkFileSystemInteraction( - eq(OPERATION), - eq(CLASS_NAME), - eq(METHOD_NAME), - eq("methodSignature"), - aryEq(attributes), - aryEq(PARAMETERS) + OPERATION, + CLASS_NAME, + METHOD_NAME, + METHOD_SIGNATURE, + ATTRIBUTES, + PARAMETERS )).thenAnswer(invocation -> null); // Act @@ -49,18 +42,18 @@ class MockClass { CLASS_NAME, METHOD_NAME, METHOD_SIGNATURE, - mockInstance, + INSTANCE, PARAMETERS ); // Assert mockedToolbox.verify(() -> JavaInstrumentationAdviceToolbox.checkFileSystemInteraction( - eq(OPERATION), - eq(CLASS_NAME), - eq(METHOD_NAME), - eq(METHOD_SIGNATURE), - aryEq(attributes), - aryEq(PARAMETERS) + OPERATION, + CLASS_NAME, + METHOD_NAME, + METHOD_SIGNATURE, + ATTRIBUTES, + PARAMETERS )); } } diff --git a/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationReadPathConstructorAdviceTest.java b/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationReadPathConstructorAdviceTest.java index 78084c85..dfb6f052 100644 --- a/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationReadPathConstructorAdviceTest.java +++ b/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationReadPathConstructorAdviceTest.java @@ -22,12 +22,12 @@ void testOnEnter() { try (MockedStatic mockedToolbox = mockStatic(JavaInstrumentationAdviceToolbox.class)) { // Arrange mockedToolbox.when(() -> JavaInstrumentationAdviceToolbox.checkFileSystemInteraction( - eq(OPERATION), - eq(CLASS_NAME), - eq(METHOD_NAME), - eq(METHOD_SIGNATURE), - aryEq(ATTRIBUTES), - aryEq(PARAMETERS) + OPERATION, + CLASS_NAME, + METHOD_NAME, + METHOD_SIGNATURE, + ATTRIBUTES, + PARAMETERS )).thenAnswer(invocation -> null); // Act @@ -38,12 +38,12 @@ void testOnEnter() { // Assert mockedToolbox.verify(() -> JavaInstrumentationAdviceToolbox.checkFileSystemInteraction( - eq(OPERATION), - eq(CLASS_NAME), - eq(METHOD_NAME), - eq(METHOD_SIGNATURE), - aryEq(ATTRIBUTES), - aryEq(PARAMETERS) + OPERATION, + CLASS_NAME, + METHOD_NAME, + METHOD_SIGNATURE, + ATTRIBUTES, + PARAMETERS )); } } diff --git a/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationReadPathMethodAdviceTest.java b/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationReadPathMethodAdviceTest.java index 8efff66b..24cbaf68 100644 --- a/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationReadPathMethodAdviceTest.java +++ b/src/test/java/de/tum/cit/ase/ares/api/aop/instrumentation/advice/JavaInstrumentationReadPathMethodAdviceTest.java @@ -16,32 +16,25 @@ class JavaInstrumentationReadPathMethodAdviceTest { private static final String CLASS_NAME = "de.tum.cit.ase.ares.api.aop.java.instrumentation.advice.JavaInstrumentationReadPathMethodAdvice"; private static final String METHOD_NAME = "methodName"; private static final String METHOD_SIGNATURE = "methodSignature"; + private static final Object[] ATTRIBUTES = new Object[]{"attrib1", "attrib2"}; private static final Object[] PARAMETERS = new Object[]{"param1", "param2"}; + private static final Object INSTANCE = new Object() { + public final String attrib1 = "attrib1"; + public final String attrib2 = "attrib2"; + }; @Test void testOnEnter() throws Exception { - class MockClass { - private final String field1 = "value1"; - private final int field2 = 42; - } - - MockClass mockInstance = new MockClass(); - Field[] fields = mockInstance.getClass().getDeclaredFields(); - Object[] attributes = new Object[fields.length]; - for (int i = 0; i < fields.length; i++) { - fields[i].setAccessible(true); - attributes[i] = fields[i].get(mockInstance); - } try (MockedStatic mockedToolbox = mockStatic(JavaInstrumentationAdviceToolbox.class)) { // Arrange mockedToolbox.when(() -> JavaInstrumentationAdviceToolbox.checkFileSystemInteraction( - eq(OPERATION), - eq(CLASS_NAME), - eq(METHOD_NAME), - eq(METHOD_SIGNATURE), - aryEq(attributes), - aryEq(PARAMETERS) + OPERATION, + CLASS_NAME, + METHOD_NAME, + METHOD_SIGNATURE, + ATTRIBUTES, + PARAMETERS )).thenAnswer(invocation -> null); // Act @@ -49,18 +42,18 @@ class MockClass { CLASS_NAME, METHOD_NAME, METHOD_SIGNATURE, - mockInstance, + INSTANCE, PARAMETERS ); // Assert mockedToolbox.verify(() -> JavaInstrumentationAdviceToolbox.checkFileSystemInteraction( - eq(OPERATION), - eq(CLASS_NAME), - eq(METHOD_NAME), - eq(METHOD_SIGNATURE), - aryEq(attributes), - aryEq(PARAMETERS) + OPERATION, + CLASS_NAME, + METHOD_NAME, + METHOD_SIGNATURE, + ATTRIBUTES, + PARAMETERS )); } } diff --git a/src/test/java/de/tum/cit/ase/ares/api/architecture/JavaArchitectureTestCaseCollectionTest.java b/src/test/java/de/tum/cit/ase/ares/api/architecture/JavaArchitectureTestCaseCollectionTest.java deleted file mode 100644 index 877af440..00000000 --- a/src/test/java/de/tum/cit/ase/ares/api/architecture/JavaArchitectureTestCaseCollectionTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package de.tum.cit.ase.ares.api.architecture; - -import com.tngtech.archunit.core.domain.JavaClasses; -import com.tngtech.archunit.core.importer.ClassFileImporter; -import de.tum.cit.ase.ares.api.architecture.java.archunit.JavaArchUnitTestCaseCollection; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -class JavaArchitectureTestCaseCollectionTest { - - // TODO implement actual test cases for this :D - @Test - void testNoClassShouldAccessFileSystem() { - JavaClasses classes = new ClassFileImporter().importPackages("com.example"); - assertDoesNotThrow(() -> JavaArchUnitTestCaseCollection.NO_CLASS_SHOULD_ACCESS_FILE_SYSTEM.check(classes)); - } - - @Test - void testNoClassesShouldAccessNetwork() { - JavaClasses classes = new ClassFileImporter().importPackages("com.example"); - assertDoesNotThrow(() -> JavaArchUnitTestCaseCollection.NO_CLASSES_SHOULD_ACCESS_NETWORK.check(classes)); - } - - @Test - void testNoClassesShouldTerminateJvm() { - JavaClasses classes = new ClassFileImporter().importPackages("com.example"); - assertDoesNotThrow(() -> JavaArchUnitTestCaseCollection.NO_CLASSES_SHOULD_TERMINATE_JVM.check(classes)); - } -}