|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Vogella GmbH and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Lars Vogel <Lars.Vogel@vogella.com> - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.core.tests.internal.events; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 18 | + |
| 19 | +import java.lang.reflect.Field; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.lang.reflect.Proxy; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.eclipse.core.internal.events.BuildManager; |
| 25 | +import org.eclipse.core.internal.events.InternalBuilder; |
| 26 | +import org.eclipse.core.resources.IBuildConfiguration; |
| 27 | +import org.eclipse.core.resources.IProject; |
| 28 | +import org.eclipse.core.resources.IncrementalProjectBuilder; |
| 29 | +import org.eclipse.core.runtime.IPath; |
| 30 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +/** |
| 34 | + * Verifies the format of the enhanced {@link IllegalArgumentException} that |
| 35 | + * {@code BuildManager} throws when a builder hits a scheduling-rule conflict |
| 36 | + * during {@code beginRule} or {@code endRule}. The wrapped message has to |
| 37 | + * identify the offending builder so it can be diagnosed from a stack trace. |
| 38 | + */ |
| 39 | +public class BuildManagerRuleConflictMessageTest { |
| 40 | + |
| 41 | + private static final String BUILDER_LABEL = "My Awesome Builder"; |
| 42 | + private static final String PLUGIN_ID = "com.example.builders"; |
| 43 | + private static final String PROJECT_PATH = "/MyProject"; |
| 44 | + |
| 45 | + @Test |
| 46 | + public void beginRuleConflictMessageIdentifiesOffendingBuilder() throws Exception { |
| 47 | + InternalBuilder builder = newStubBuilder(stubProject(PROJECT_PATH)); |
| 48 | + IllegalArgumentException original = new IllegalArgumentException( |
| 49 | + "Attempted to beginRule: P/MyProject/foo, does not match outer scope rule: P/Other"); |
| 50 | + |
| 51 | + IllegalArgumentException result = invokeHandleRuleConflict(true, builder, original); |
| 52 | + |
| 53 | + assertThat(result.getMessage()) // |
| 54 | + .startsWith("beginRule failed for builder " + builder.getClass().getName()) // |
| 55 | + .contains("'" + BUILDER_LABEL + "'") // |
| 56 | + .contains("plugin " + PLUGIN_ID) // |
| 57 | + .contains("on project " + PROJECT_PATH) // |
| 58 | + .endsWith(": " + original.getMessage()); |
| 59 | + assertSame(original, result.getCause()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void endRuleConflictMessageIdentifiesOffendingBuilder() throws Exception { |
| 64 | + InternalBuilder builder = newStubBuilder(stubProject(PROJECT_PATH)); |
| 65 | + IllegalArgumentException original = new IllegalArgumentException( |
| 66 | + "Attempted to endRule: P/MyProject/foo, does not match the rule of current entry: P/MyProject"); |
| 67 | + |
| 68 | + IllegalArgumentException result = invokeHandleRuleConflict(false, builder, original); |
| 69 | + |
| 70 | + assertThat(result.getMessage()) // |
| 71 | + .startsWith("endRule failed for builder " + builder.getClass().getName()) // |
| 72 | + .contains("'" + BUILDER_LABEL + "'") // |
| 73 | + .contains("plugin " + PLUGIN_ID) // |
| 74 | + .contains("on project " + PROJECT_PATH) // |
| 75 | + .endsWith(": " + original.getMessage()); |
| 76 | + assertSame(original, result.getCause()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void messageFallsBackToPlaceholdersWhenBuilderMetadataMissing() throws Exception { |
| 81 | + InternalBuilder builder = newStubBuilder(null); |
| 82 | + IllegalArgumentException original = new IllegalArgumentException("boom"); |
| 83 | + setInternalBuilderField(builder, "label", null); |
| 84 | + setInternalBuilderField(builder, "pluginId", null); |
| 85 | + |
| 86 | + IllegalArgumentException result = invokeHandleRuleConflict(true, builder, original); |
| 87 | + |
| 88 | + assertThat(result.getMessage()) // |
| 89 | + .contains("'<unknown>'") // |
| 90 | + .contains("plugin <unknown>") // |
| 91 | + .contains("on project <unknown>"); |
| 92 | + } |
| 93 | + |
| 94 | + private static InternalBuilder newStubBuilder(IProject project) throws Exception { |
| 95 | + IncrementalProjectBuilder builder = new IncrementalProjectBuilder() { |
| 96 | + @Override |
| 97 | + protected IProject[] build(int kind, Map<String, String> args, IProgressMonitor monitor) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + }; |
| 101 | + setInternalBuilderField(builder, "label", BUILDER_LABEL); |
| 102 | + setInternalBuilderField(builder, "pluginId", PLUGIN_ID); |
| 103 | + setInternalBuilderField(builder, "buildConfiguration", stubBuildConfiguration(project)); |
| 104 | + return builder; |
| 105 | + } |
| 106 | + |
| 107 | + private static IBuildConfiguration stubBuildConfiguration(IProject project) { |
| 108 | + return (IBuildConfiguration) Proxy.newProxyInstance(IBuildConfiguration.class.getClassLoader(), |
| 109 | + new Class<?>[] { IBuildConfiguration.class }, (proxy, method, args) -> { |
| 110 | + if ("getProject".equals(method.getName())) { |
| 111 | + return project; |
| 112 | + } |
| 113 | + return defaultReturnValue(method.getReturnType()); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + private static IProject stubProject(String path) { |
| 118 | + IPath fullPath = IPath.fromPortableString(path); |
| 119 | + return (IProject) Proxy.newProxyInstance(IProject.class.getClassLoader(), |
| 120 | + new Class<?>[] { IProject.class }, (proxy, method, args) -> { |
| 121 | + if ("getFullPath".equals(method.getName())) { |
| 122 | + return fullPath; |
| 123 | + } |
| 124 | + return defaultReturnValue(method.getReturnType()); |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + private static Object defaultReturnValue(Class<?> returnType) { |
| 129 | + if (!returnType.isPrimitive()) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + if (returnType == boolean.class) { |
| 133 | + return Boolean.FALSE; |
| 134 | + } |
| 135 | + if (returnType == void.class) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + return 0; |
| 139 | + } |
| 140 | + |
| 141 | + private static void setInternalBuilderField(InternalBuilder target, String name, Object value) throws Exception { |
| 142 | + Field field = InternalBuilder.class.getDeclaredField(name); |
| 143 | + field.setAccessible(true); |
| 144 | + field.set(target, value); |
| 145 | + } |
| 146 | + |
| 147 | + private static IllegalArgumentException invokeHandleRuleConflict(boolean beginRule, InternalBuilder builder, |
| 148 | + IllegalArgumentException original) throws Exception { |
| 149 | + Method method = BuildManager.class.getDeclaredMethod("handleRuleConflict", boolean.class, InternalBuilder.class, |
| 150 | + IllegalArgumentException.class); |
| 151 | + method.setAccessible(true); |
| 152 | + try { |
| 153 | + return (IllegalArgumentException) method.invoke(null, beginRule, builder, original); |
| 154 | + } catch (java.lang.reflect.InvocationTargetException e) { |
| 155 | + Throwable cause = e.getCause(); |
| 156 | + if (cause instanceof RuntimeException re) { |
| 157 | + throw re; |
| 158 | + } |
| 159 | + throw new RuntimeException(cause); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments