Ticket #10836: 10836_test.patch

File 10836_test.patch, 5.1 KB (added by Don-vip, 9 years ago)
  • test/unit/org/openstreetmap/josm/TestUtils.java

     
    2020public class TestUtils {
    2121
    2222    /**
     23     * Returns the path to images root directory.
     24     * @return path to images root directory
     25     */
     26    public static String getImagesRoot() {
     27        String imagesRoot = System.getProperty("josm.images");
     28        if (imagesRoot == null || imagesRoot.isEmpty()) {
     29            imagesRoot = "images";
     30            System.out.println("System property josm.images is not set, using '" + imagesRoot + "'");
     31        }
     32        return imagesRoot.endsWith("/") ? imagesRoot : imagesRoot + "/";
     33    }
     34
     35    /**
    2336     * Returns the path to test data root directory.
    2437     * @return path to test data root directory
    2538     */
  • test/unit/org/openstreetmap/josm/tools/ImageProviderTest.java

     
    55import static org.junit.Assert.assertThat;
    66
    77import java.awt.Transparency;
     8import java.awt.event.ActionEvent;
    89import java.io.File;
    910import java.io.IOException;
     11import java.nio.file.FileVisitResult;
     12import java.nio.file.FileVisitor;
     13import java.nio.file.Files;
     14import java.nio.file.Path;
     15import java.nio.file.Paths;
     16import java.nio.file.attribute.BasicFileAttributes;
     17import java.util.regex.Pattern;
     18
     19import javax.swing.AbstractAction;
    1020
     21import org.junit.BeforeClass;
    1122import org.junit.Test;
     23import org.openstreetmap.josm.JOSMFixture;
     24import org.openstreetmap.josm.Main;
    1225import org.openstreetmap.josm.TestUtils;
    1326
    1427/**
     
    1730public class ImageProviderTest {
    1831
    1932    /**
     33     * Setup test.
     34     */
     35    @BeforeClass
     36    public static void setUp() {
     37        JOSMFixture.createUnitTestFixture().init();
     38    }
     39
     40    /**
    2041     * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/9984">#9984</a>
    2142     * @throws IOException if an error occurs during reading
    2243     */
     
    3859        File file = new File(TestUtils.getRegressionDataFile(10030, "tile.jpg"));
    3960        ImageProvider.read(file, true, true);
    4061    }
     62
     63    /**
     64     * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/10836">#10836</a>
     65     * @throws IOException if an error occurs during reading
     66     */
     67    @Test
     68    public void testTicket10836() throws IOException {
     69        final Path rootDir = Paths.get(TestUtils.getImagesRoot());
     70        // Search for all SVG files
     71        Files.walkFileTree(rootDir, new FileVisitor<Path>() {
     72            private final Pattern pattern = Pattern.compile(".*\\.svg");
     73
     74            @Override
     75            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
     76                return FileVisitResult.CONTINUE;
     77            }
     78
     79            @Override
     80            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
     81                String path = file.toString();
     82                if (pattern.matcher(path).matches()) {
     83                    Main.info("Decoding SVG file: "+path);
     84                    // Validate SVG file in background, as done in TaggingPreset
     85                    new ImageProvider(path.replace(rootDir+File.separator, "")).getInBackground(
     86                            new ImageProvider.ImageResourceCallback() {
     87                        @Override
     88                        public void finished(ImageResource result) {
     89                            result.getImageIcon(new AbstractAction() {
     90                                @Override
     91                                public void actionPerformed(ActionEvent e) {
     92                                    // Do nothing
     93                                }
     94                            });
     95                        }
     96                    });
     97                }
     98                return FileVisitResult.CONTINUE;
     99            }
     100
     101            @Override
     102            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
     103                Main.error(exc);
     104                if (pattern.matcher(file.toString()).matches()) {
     105                    // If an SVG file has failed, make the test fail
     106                    throw exc;
     107                }
     108                return FileVisitResult.CONTINUE;
     109            }
     110
     111            @Override
     112            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
     113                if (exc != null) {
     114                    Main.error(exc);
     115                    if (dir.equals(rootDir)) {
     116                        // If the root directory has failed, make the test fail
     117                        throw exc;
     118                    }
     119                }
     120                return FileVisitResult.CONTINUE;
     121            }});
     122    }
    41123}