Ticket #18200: 18200.patch
File 18200.patch, 7.7 KB (added by , 5 years ago) |
---|
-
ivy.xml
50 50 <artifact name="org.jacoco.ant" type="jar" maven:classifier="nodeps"/> 51 51 </dependency> 52 52 <!-- jmockit->default - Don't upgrade JMockit until https://josm.openstreetmap.de/ticket/18200 is resolved --> 53 <dependency conf="jmockit->default" org="org.jmockit" name="jmockit" rev="1.4 4"/>53 <dependency conf="jmockit->default" org="org.jmockit" name="jmockit" rev="1.49"/> 54 54 <!-- test->default --> 55 55 <dependency conf="test->default" org="com.github.spotbugs" name="spotbugs-annotations" rev="4.0.1"/> 56 56 <dependency conf="test->default" org="com.github.stefanbirkner" name="system-rules" rev="1.19.0"> -
src/org/openstreetmap/josm/gui/MapViewState.java
131 131 this(projecting, mvs.viewWidth, mvs.viewHeight, mvs.scale, mvs.topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen); 132 132 } 133 133 134 private static Point findTopLeftInWindow(JComponent position) { 134 /** 135 * This is visible for JMockit. 136 * 137 * @param position The component to get the top left position of its window 138 */ 139 static Point findTopLeftInWindow(JComponent position) { 135 140 Point result = new Point(); 136 141 // better than using swing utils, since this allows us to use the method if no screen is present. 137 142 Container component = position; … … 143 148 return result; 144 149 } 145 150 146 private static Point findTopLeftOnScreen(JComponent position) { 151 /** 152 * This is visible for JMockit. 153 * 154 * @param position The component to get the top left position of its screen 155 */ 156 static Point findTopLeftOnScreen(JComponent position) { 147 157 try { 148 158 return position.getLocationOnScreen(); 149 159 } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) { -
src/org/openstreetmap/josm/gui/util/GuiHelper.java
203 203 } 204 204 } 205 205 206 private static void handleEDTException(Throwable t) { 206 /** 207 * Handle exceptions in the EDT. This should only be used in {@link GuiHelper} 208 * and {@link org.openstreetmap.josm.testutils.mockers.EDTAssertionMocker}. 209 * 210 * @param t The throwable to handle 211 */ 212 static void handleEDTException(Throwable t) { 207 213 Logging.logWithStackTrace(Logging.LEVEL_ERROR, t, "Exception raised in EDT"); 208 214 } 209 215 -
test/unit/org/openstreetmap/josm/testutils/mockers/EDTAssertionMocker.java
13 13 */ 14 14 public class EDTAssertionMocker extends MockUp<GuiHelper> { 15 15 @Mock 16 private static void handleEDTException(final Invocation invocation, final Throwable t){16 static void handleEDTException(final Invocation invocation, final Throwable t) throws Throwable { 17 17 final Throwable cause = t.getCause(); 18 18 if (cause instanceof AssertionError) { 19 19 throw (AssertionError) cause; -
test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java
5 5 6 6 import java.awt.Component; 7 7 import java.awt.GraphicsEnvironment; 8 import java.lang.reflect.Field; 8 9 import java.util.Arrays; 9 10 import java.util.Map; 10 11 import java.util.Optional; 11 12 import java.util.WeakHashMap; 12 13 14 import org.junit.platform.commons.util.ReflectionUtils; 13 15 import org.openstreetmap.josm.TestUtils; 14 16 import org.openstreetmap.josm.gui.ExtendedDialog; 15 17 import org.openstreetmap.josm.tools.Logging; 16 18 17 import mockit.Deencapsulation;18 19 import mockit.Invocation; 19 20 import mockit.Mock; 20 import mockit.internal.reflection.FieldReflection;21 21 22 22 /** 23 23 * MockUp for {@link ExtendedDialog} allowing a test to pre-seed uses of {@link ExtendedDialog} … … 76 76 } 77 77 78 78 protected int getButtonPositionFromLabel(final ExtendedDialog instance, final String label) { 79 final String[] bTexts = Deencapsulation.getField(instance, "bTexts"); 79 final String[] bTexts = (String[]) ReflectionUtils.tryToReadFieldValue(ExtendedDialog.class, "bTexts", instance).toOptional() 80 .orElse(null); 80 81 final int position = Arrays.asList(bTexts).indexOf(label); 81 82 if (position == -1) { 82 83 fail("Unable to find button labeled \"" + label + "\". Instead found: " + Arrays.toString(bTexts)); … … 150 151 } 151 152 152 153 @Mock 153 private void setVisible(final Invocation invocation, final boolean value) {154 private void setVisible(final Invocation invocation, final boolean value) throws Throwable { 154 155 if (value == true) { 155 156 try { 156 157 final ExtendedDialog instance = invocation.getInvokedInstance(); … … 157 158 this.act(instance); 158 159 final int mockResult = this.getMockResult(instance); 159 160 // TODO check validity of mockResult? 160 FieldReflection.setField(instance.getClass(), instance, "result", mockResult); 161 Field resultField = instance.getClass().getDeclaredField("result"); 162 resultField.setAccessible(true); 163 resultField.set(instance, mockResult); 161 164 Logging.info( 162 165 "{0} answering {1} to ExtendedDialog with content {2}", 163 166 this.getClass().getName(), … … 165 168 this.getString(instance) 166 169 ); 167 170 this.getInvocationLogInternal().add(this.getInvocationLogEntry(instance, mockResult)); 168 } catch (AssertionError e) {171 } catch (AssertionError | NoSuchFieldException | IllegalAccessException e) { 169 172 // in case this exception gets ignored by the calling thread we want to signify this failure 170 173 // in the invocation log. it's hard to know what to add to the log in these cases as it's 171 174 // probably unsafe to call getInvocationLogEntry, so add the exception on its own. -
test/unit/org/openstreetmap/josm/testutils/mockers/WindowlessMapViewStateMocker.java
16 16 */ 17 17 public class WindowlessMapViewStateMocker extends MockUp<MapViewState> { 18 18 @Mock 19 privatestatic Point findTopLeftInWindow(JComponent position) {19 static Point findTopLeftInWindow(JComponent position) { 20 20 return new Point(); 21 21 } 22 22 23 23 @Mock 24 privatestatic Point findTopLeftOnScreen(JComponent position) {24 static Point findTopLeftOnScreen(JComponent position) { 25 25 // in our imaginary universe the window is always (10, 10) from the top left of the screen 26 26 Point topLeftInWindow = findTopLeftInWindow(position); 27 27 return new Point(topLeftInWindow.x + 10, topLeftInWindow.y + 10);