|
18 | 18 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
19 | 19 | import static org.assertj.core.api.Assertions.fail; |
20 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
21 | | -import static org.junit.jupiter.api.Assertions.assertFalse; |
22 | 21 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
23 | 22 | import static org.junit.jupiter.api.Assertions.assertThrows; |
24 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | +import static org.mockito.Mockito.times; |
25 | 24 |
|
26 | 25 | import java.net.URL; |
27 | 26 | import java.util.AbstractCollection; |
|
33 | 32 | import java.util.LinkedList; |
34 | 33 | import java.util.List; |
35 | 34 |
|
36 | | -import org.junit.jupiter.api.Disabled; |
37 | 35 | import org.junit.jupiter.api.Test; |
| 36 | +import org.mockito.MockedStatic; |
| 37 | +import org.mockito.Mockito; |
38 | 38 | import org.osgi.framework.FrameworkUtil; |
39 | 39 |
|
40 | 40 | import org.w3c.dom.Document; |
|
72 | 72 | @SuppressWarnings("restriction") |
73 | 73 | public class ExpressionTests { |
74 | 74 |
|
75 | | - private static final int TYPE_ITERATIONS = 100000; |
| 75 | + /** |
| 76 | + * Used exclusively by {@link #testSubTypeTiming()} to provide a cache key type |
| 77 | + * unique to this test, avoiding interference with other tests. |
| 78 | + */ |
| 79 | + private static class CachingTestSet extends HashSet<Object> { |
| 80 | + private static final long serialVersionUID = 1L; |
| 81 | + } |
76 | 82 |
|
77 | 83 | public static class CollectionWrapper { |
78 | 84 | public Collection<String> collection; |
@@ -1014,28 +1020,32 @@ public void testSubType() throws Exception { |
1014 | 1020 | } |
1015 | 1021 |
|
1016 | 1022 | @Test |
1017 | | - @Disabled("CI test environment too unstable for performance tests") |
1018 | 1023 | public void testSubTypeTiming() throws Exception { |
1019 | | - HashSet<?> o1 = new HashSet<>(); |
1020 | | - |
1021 | | - System.gc(); |
1022 | | - long cachedStart= System.currentTimeMillis(); |
1023 | | - for (int i= 0; i < TYPE_ITERATIONS; i++) { |
1024 | | - assertTrue(Expressions.isInstanceOf(o1, "java.util.Set")); |
1025 | | - assertFalse(Expressions.isInstanceOf(o1, "java.util.List")); |
| 1024 | + CachingTestSet o = new CachingTestSet(); |
| 1025 | + |
| 1026 | + try (MockedStatic<Expressions> expressionsMock = Mockito.mockStatic(Expressions.class, |
| 1027 | + Mockito.CALLS_REAL_METHODS)) { |
| 1028 | + // First call (positive): cache miss — isSubtype must traverse the class |
| 1029 | + // hierarchy |
| 1030 | + assertThat(Expressions.isInstanceOf(o, "java.util.Set")).isTrue(); |
| 1031 | + expressionsMock.verify(() -> Expressions.uncachedIsSubtype(CachingTestSet.class, "java.util.Set"), |
| 1032 | + times(1)); |
| 1033 | + // Second call (positive): cache hit — uncachedIsSubtype must NOT be invoked |
| 1034 | + // again |
| 1035 | + assertThat(Expressions.isInstanceOf(o, "java.util.Set")).isTrue(); |
| 1036 | + expressionsMock.verify(() -> Expressions.uncachedIsSubtype(CachingTestSet.class, "java.util.Set"), |
| 1037 | + times(1)); |
| 1038 | + // First call (negative): cache miss — isSubtype must traverse the class |
| 1039 | + // hierarchy |
| 1040 | + assertThat(Expressions.isInstanceOf(o, "java.util.List")).isFalse(); |
| 1041 | + expressionsMock.verify(() -> Expressions.uncachedIsSubtype(CachingTestSet.class, "java.util.List"), |
| 1042 | + times(1)); |
| 1043 | + // Second call (negative): cache hit — uncachedIsSubtype must NOT be invoked |
| 1044 | + // again |
| 1045 | + assertThat(Expressions.isInstanceOf(o, "java.util.List")).isFalse(); |
| 1046 | + expressionsMock.verify(() -> Expressions.uncachedIsSubtype(CachingTestSet.class, "java.util.List"), |
| 1047 | + times(1)); |
1026 | 1048 | } |
1027 | | - long cachedDelta= System.currentTimeMillis() - cachedStart; |
1028 | | - |
1029 | | - System.gc(); |
1030 | | - long instanceStart= System.currentTimeMillis(); |
1031 | | - for (int i= 0; i < TYPE_ITERATIONS; i++) { |
1032 | | - assertTrue(Expressions.uncachedIsSubtype(o1.getClass(), "java.util.Set")); |
1033 | | - assertFalse(Expressions.uncachedIsSubtype(o1.getClass(), "java.util.List")); |
1034 | | - } |
1035 | | - long instanceDelta= System.currentTimeMillis() - instanceStart; |
1036 | | - |
1037 | | - assertThat(cachedDelta).as("assert cachedDelta is less than instanceDelta" + instanceDelta) |
1038 | | - .isLessThan(instanceDelta); |
1039 | 1049 | } |
1040 | 1050 |
|
1041 | 1051 | } |
0 commit comments