Ticket #21740: 21740-MainLayerManagerTest.patch

File 21740-MainLayerManagerTest.patch, 3.0 KB (added by Bjoeni, 4 years ago)
  • test/unit/org/openstreetmap/josm/gui/layer/MainLayerManagerTest.java

    ### Eclipse Workspace Patch 1.0
    #P josm-2
     
    22package org.openstreetmap.josm.gui.layer;
    33
    44import static org.junit.jupiter.api.Assertions.assertEquals;
    5 import static org.junit.jupiter.api.Assertions.assertFalse;
    65import static org.junit.jupiter.api.Assertions.assertNull;
    76import static org.junit.jupiter.api.Assertions.assertSame;
     7import static org.junit.jupiter.api.Assertions.assertTrue;
    88
     9import java.util.ArrayList;
    910import java.util.Arrays;
     11import java.util.List;
     12import java.util.logging.Handler;
     13import java.util.logging.LogRecord;
    1014
    1115import org.junit.jupiter.api.BeforeAll;
    1216import org.junit.jupiter.api.BeforeEach;
     
    5559        }
    5660    }
    5761
     62    protected static class LoggingHandler extends Handler {
     63
     64        private List<LogRecord> records = new ArrayList<>();
     65
     66        @Override
     67        public void publish(LogRecord record) {
     68            records.add(record);
     69        }
     70
     71        @Override
     72        public void flush() {}
     73
     74        @Override
     75        public void close() throws SecurityException {}
     76
     77        public List<LogRecord> getRecords() {
     78            return records;
     79        }
     80
     81    }
     82
    5883    @BeforeAll
    5984    public static void setUpClass() {
    6085        JOSMFixture.createUnitTestFixture().init();
     
    145170    @Test
    146171    void testAddActiveLayerChangeListenerTwice() {
    147172        CapturingActiveLayerChangeListener listener = new CapturingActiveLayerChangeListener();
     173        LoggingHandler handler = new LoggingHandler();
     174        Logging.getLogger().addHandler(handler);
     175
    148176        layerManagerWithActive.addActiveLayerChangeListener(listener);
    149         Logging.clearLastErrorAndWarnings();
     177        assertTrue(handler.getRecords().isEmpty());
     178
    150179        layerManagerWithActive.addActiveLayerChangeListener(listener);
    151         assertFalse(Logging.getLastErrorAndWarnings().isEmpty());
     180        assertTrue(handler.getRecords().get(1).getMessage().startsWith("Attempted to add listener that was already in list"));
     181
     182        Logging.getLogger().removeHandler(handler);
    152183    }
    153184
    154185    /**
     
    174205     */
    175206    @Test
    176207    void testRemoveActiveLayerChangeListenerNotInList() {
    177         Logging.clearLastErrorAndWarnings();
     208        LoggingHandler handler = new LoggingHandler();
     209        Logging.getLogger().addHandler(handler);
     210
    178211        layerManagerWithActive.removeActiveLayerChangeListener(new CapturingActiveLayerChangeListener());
    179         assertFalse(Logging.getLastErrorAndWarnings().isEmpty());
     212        assertTrue(handler.getRecords().get(1).getMessage().startsWith("Attempted to remove listener that was not in list"));
     213
     214        Logging.getLogger().removeHandler(handler);
    180215    }
    181216
    182217    /**