Skip to content

Commit

Permalink
Improved Advice Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Paulsen committed Dec 5, 2024
1 parent 0086010 commit 8e21062
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,43 @@ 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<JavaInstrumentationAdviceToolbox> mockedToolbox = mockStatic(JavaInstrumentationAdviceToolbox.class)) {
// Arrange
SecurityException expectedViolation = null;
mockedToolbox.when(() -> JavaInstrumentationAdviceToolbox.checkFileSystemInteraction(
OPERATION,
CLASS_NAME,
METHOD_NAME,
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
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JavaInstrumentationAdviceToolbox> mockedToolbox = mockStatic(JavaInstrumentationAdviceToolbox.class)) {
// Arrange
Expand All @@ -38,7 +32,7 @@ void testOnEnter() throws IllegalAccessException {
CLASS_NAME,
METHOD_NAME,
METHOD_SIGNATURE,
attributes,
ATTRIBUTES,
PARAMETERS
)).thenAnswer(invocation -> null);

Expand All @@ -47,7 +41,7 @@ void testOnEnter() throws IllegalAccessException {
CLASS_NAME,
METHOD_NAME,
METHOD_SIGNATURE,
mockInstance,
INSTANCE,
PARAMETERS
);

Expand All @@ -57,7 +51,7 @@ void testOnEnter() throws IllegalAccessException {
CLASS_NAME,
METHOD_NAME,
METHOD_SIGNATURE,
attributes,
ATTRIBUTES,
PARAMETERS
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,44 @@ 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<JavaInstrumentationAdviceToolbox> 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
JavaInstrumentationOverwritePathMethodAdvice.onEnter(
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
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ void testOnEnter() {
try (MockedStatic<JavaInstrumentationAdviceToolbox> 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
Expand All @@ -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
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,44 @@ 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<JavaInstrumentationAdviceToolbox> 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
JavaInstrumentationReadPathMethodAdvice.onEnter(
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
));
}
}
Expand Down

This file was deleted.

0 comments on commit 8e21062

Please sign in to comment.