|
| 1 | +package aQute.tester.bundle.engine; |
| 2 | + |
| 3 | +import java.lang.reflect.Method; |
| 4 | +import java.lang.reflect.Modifier; |
| 5 | + |
| 6 | +import org.junit.platform.engine.ConfigurationParameters; |
| 7 | +import org.junit.platform.engine.EngineExecutionListener; |
| 8 | +import org.junit.platform.engine.ExecutionRequest; |
| 9 | +import org.junit.platform.engine.TestDescriptor; |
| 10 | + |
| 11 | +/** |
| 12 | + * Factory for creating ExecutionRequest instances that are compatible with both |
| 13 | + * JUnit Platform < 1.13 and >= 1.13. In JUnit Platform 1.13+, the internal |
| 14 | + * NamespacedHierarchicalStore must be properly propagated to child execution |
| 15 | + * requests. This class use reflection magic, but unfortunately this is |
| 16 | + * necessary to support wide range of runtime JUnit version where we need to use |
| 17 | + * internal API which drastically changes between versions. |
| 18 | + */ |
| 19 | +public class ExecutionRequestFactory { |
| 20 | + |
| 21 | + private static final boolean USE_REFLECTION; |
| 22 | + /** |
| 23 | + * Reference to the getStore() method (new in 1.13+) |
| 24 | + */ |
| 25 | + private static final Method GET_STORE_METHOD; |
| 26 | + |
| 27 | + /** |
| 28 | + * reference to static create() methods with 3-arguments |
| 29 | + * org.junit.platform.engine.ExecutionRequest.create(TestDescriptor, |
| 30 | + * EngineExecutionListener, ConfigurationParameters) |
| 31 | + */ |
| 32 | + static final Method CREATE_3; |
| 33 | + |
| 34 | + /** |
| 35 | + * reference to static create() methods with 5-arguments |
| 36 | + * org.junit.platform.engine.ExecutionRequest.create(TestDescriptor, |
| 37 | + * EngineExecutionListener, ConfigurationParameters) |
| 38 | + */ |
| 39 | + static final Method CREATE_5; |
| 40 | + |
| 41 | + |
| 42 | + static { |
| 43 | + boolean useReflection = false; |
| 44 | + Method getStoreMethod = null; |
| 45 | + Method c3 = null, c5 = null; |
| 46 | + |
| 47 | + try { |
| 48 | + |
| 49 | + // Look for 5-parameter create() method (1.13+): descriptor, |
| 50 | + // listener, params, outputProvider, store |
| 51 | + // or 4-parameter constructor (earlier versions): descriptor, |
| 52 | + // listener, params, store |
| 53 | + for (Method m : ExecutionRequest.class.getDeclaredMethods()) { |
| 54 | + if (!m.getName().equals("create") |
| 55 | + || !Modifier.isStatic(m.getModifiers()) |
| 56 | + || m.getReturnType() != ExecutionRequest.class) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + Class<?>[] p = m.getParameterTypes(); |
| 61 | + |
| 62 | + if (p.length == 3 |
| 63 | + && p[0] == TestDescriptor.class |
| 64 | + && p[1] == EngineExecutionListener.class |
| 65 | + && p[2] == ConfigurationParameters.class) { |
| 66 | + // this is the deprecated 3-arg |
| 67 | + c3 = m; |
| 68 | + } |
| 69 | + else if (p.length == 5 |
| 70 | + && p[0] == TestDescriptor.class |
| 71 | + && p[1] == EngineExecutionListener.class |
| 72 | + && p[2] == ConfigurationParameters.class) { |
| 73 | + // 5-arg version |
| 74 | + c5 = m; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + // Try to access the internal Store from ExecutionRequest |
| 80 | + // This accesses JUnit Platform's internal API which is necessary for |
| 81 | + // 1.13+ compatibility. |
| 82 | + // The Store class and getStore() method are internal implementation |
| 83 | + // details |
| 84 | + // that may change in future versions, but are required to properly |
| 85 | + // propagate |
| 86 | + // execution context to nested test engines. |
| 87 | + getStoreMethod = ExecutionRequest.class.getDeclaredMethod("getStore"); |
| 88 | + getStoreMethod.setAccessible(true); |
| 89 | + |
| 90 | + // Get the Store class from the method's return type |
| 91 | + Class<?> storeClass = getStoreMethod.getReturnType(); |
| 92 | + |
| 93 | + // Only use reflection if we found both the getter one of the create methods |
| 94 | + useReflection = (getStoreMethod != null && (c3 != null || c5 != null)); |
| 95 | + } catch (Exception e) { |
| 96 | + // Reflection not available or not needed, fall back to public |
| 97 | + // constructor |
| 98 | + } |
| 99 | + |
| 100 | + USE_REFLECTION = useReflection; |
| 101 | + GET_STORE_METHOD = getStoreMethod; |
| 102 | + CREATE_3 = c3; |
| 103 | + CREATE_5 = c5; |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Create a new ExecutionRequest for a child engine, properly propagating |
| 109 | + * the execution context from the parent request. |
| 110 | + * |
| 111 | + * @param descriptor The root test descriptor for the child engine |
| 112 | + * @param parentRequest The parent execution request to derive context from |
| 113 | + * @return A new ExecutionRequest for the child engine |
| 114 | + */ |
| 115 | + static ExecutionRequest createChildRequest(TestDescriptor descriptor, ExecutionRequest parentRequest) { |
| 116 | + EngineExecutionListener listener = parentRequest.getEngineExecutionListener(); |
| 117 | + ConfigurationParameters params = parentRequest.getConfigurationParameters(); |
| 118 | + |
| 119 | + if (USE_REFLECTION) { |
| 120 | + try { |
| 121 | + // Get the Store from the parent request |
| 122 | + Object store = GET_STORE_METHOD.invoke(parentRequest); |
| 123 | + |
| 124 | + // Create a new ExecutionRequest with the Store |
| 125 | + |
| 126 | + if (CREATE_5 != null) { |
| 127 | + // JUnit Platform 1.13+: need to pass |
| 128 | + // OutputDirectoryProvider as 4th param |
| 129 | + // Get the OutputDirectoryProvider from parent request |
| 130 | + // TODO from 1.14+ getOutputDirectoryCreator() replaces |
| 131 | + // "getOutputDirectoryProvider". So we may have to adopt |
| 132 | + // that in the future |
| 133 | + Method getOutputProvider = ExecutionRequest.class.getDeclaredMethod("getOutputDirectoryProvider"); |
| 134 | + getOutputProvider.setAccessible(true); |
| 135 | + Object outputProvider = getOutputProvider.invoke(parentRequest); |
| 136 | + |
| 137 | + return (ExecutionRequest) CREATE_5.invoke(parentRequest, descriptor, listener, params, |
| 138 | + outputProvider, |
| 139 | + store); |
| 140 | + } |
| 141 | + else { |
| 142 | + // Earlier versions: 3-param static create() method |
| 143 | + return (ExecutionRequest) CREATE_3.invoke(parentRequest, descriptor, listener, params); |
| 144 | + } |
| 145 | + } catch (Exception e) { |
| 146 | + // Fall back to public constructor if reflection fails |
| 147 | + // Using System.err because this is a test framework component that |
| 148 | + // needs |
| 149 | + // to report issues even when no logging framework is available |
| 150 | + System.err.println( |
| 151 | + "Warning: BundleEngine failed to propagate execution context for " + descriptor.getDisplayName() |
| 152 | + + " using reflection, falling back to public constructor. " + "JUnit Platform extensions may not work correctly. Cause: " |
| 153 | + + e.getClass() |
| 154 | + .getName() |
| 155 | + + ": " + e.getMessage()); |
| 156 | + e.printStackTrace(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Use the public constructor (works for JUnit Platform < 1.13) |
| 161 | + return new ExecutionRequest(descriptor, listener, params); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Check if this factory is using reflection to propagate execution context. |
| 166 | + * This is primarily for testing and debugging purposes. |
| 167 | + * |
| 168 | + * @return true if reflection is being used, false if using public API only |
| 169 | + */ |
| 170 | + static boolean isUsingReflection() { |
| 171 | + return USE_REFLECTION; |
| 172 | + } |
| 173 | +} |
0 commit comments