<commit_before>package dev.osm.mapsplit;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;
import java.util.logging.Logger;

import org.imintel.mbtiles4j.MBTilesReadException;
import org.imintel.mbtiles4j.MBTilesReader;
import org.imintel.mbtiles4j.Tile;
import org.imintel.mbtiles4j.model.MetadataEntry;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SplitTest {

    private static final Logger LOGGER = Logger.getLogger(SplitTest.class.getName());

    private static final String MSF_OUTPUT_FILENAME = "build/tmp/tiles/liechtenstein.msf";

    /**
     * Pre-test setup
     */
    @Before
    public void setup() {
        File output = new File(MSF_OUTPUT_FILENAME);
        output.delete();
    }

    /**
     * Basic test to check that stuff happens
     */
    @Test
    public void split() {
        try {
            File output = new File(MSF_OUTPUT_FILENAME);
            Assert.assertFalse(output.exists());
            MapSplit.main(new String[] { "-mtvM", "-i", "test-data/liechtenstein-latest.osm.pbf", "-o", MSF_OUTPUT_FILENAME, "-f", "2000", "-z", "16", "-O",
                    "2000" });
            checkMetadata(12, 16);
            Tile t = getTile(15, 17250, 11506);
            Assert.assertNotNull(t);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }

    /**
     * Basic test to check that stuff happens, with full relations
     */
    @Test
    public void splitWithFullRelations() {
        try {
            File output = new File(MSF_OUTPUT_FILENAME);
            Assert.assertFalse(output.exists());
            MapSplit.main(new String[] { "-cmtvM", "-i", "test-data/liechtenstein-latest.osm.pbf", "-o", MSF_OUTPUT_FILENAME, "-f", "2000", "-z", "16", "-O",
                    "2000" });
            checkMetadata(12, 16);
            Tile t = getTile(16, 34501, 23013);
            Assert.assertNotNull(t);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }

    /**
     * Basic test, optimization turned of so that we can check clipping below
     */
    @Test
    public void splitNoOptimize() {
        try {
            File output = new File(MSF_OUTPUT_FILENAME);
            Assert.assertFalse(output.exists());
            MapSplit.main(new String[] { "-mtvM", "-i", "test-data/liechtenstein-latest.osm.pbf", "-o", MSF_OUTPUT_FILENAME, "-f", "2000", "-z", "16" });
            checkMetadata(16, 16);
            Tile t = getTile(16, 34496, 23000);
            Assert.assertNotNull(t); // should exist, is outside of Vaduz
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }

    /**
     * Basic test for clipping
     */
    @Test
    public void splitAndClip() {
        try {
            File output = new File(MSF_OUTPUT_FILENAME);
            Assert.assertFalse(output.exists());
            MapSplit.main(new String[] { "-mtvM", "-p", "test-data/vaduz.poly", "-i", "test-data/liechtenstein-latest.osm.pbf", "-o", MSF_OUTPUT_FILENAME, "-f",
                    "2000", "-z", "16" });
            checkMetadata(16, 16);
            Tile t = getTile(16, 33972, 23225);
            Assert.assertNull(t); // shouldn't exist, is outside of Vaduz
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }

    /**
     * Check that the metadata in the MBTiles is what we expect
     * 
     * @param min min zoom in metadata
     * @param max max zoom in metadata
     * @throws MBTilesReadException if we can't read or access the MBTiles file
     */
    private void checkMetadata(int min, int max) throws MBTilesReadException {
        MBTilesReader r = null;
        try {
            r = new MBTilesReader(new File(MSF_OUTPUT_FILENAME));
            MetadataEntry md = r.getMetadata();
            final Map<String, String> required = new HashMap<>();
            md.getRequiredKeyValuePairs().forEach(new Consumer<Entry<String, String>>() {

                @Override
                public void accept(Entry<String, String> e) {<commit_msg>Create output directory for tests<commit_after>package dev.osm.mapsplit;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;
import java.util.logging.Logger;

import org.imintel.mbtiles4j.MBTilesReadException;
import org.imintel.mbtiles4j.MBTilesReader;
import org.imintel.mbtiles4j.Tile;
import org.imintel.mbtiles4j.model.MetadataEntry;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SplitTest {

    private static final Logger LOGGER = Logger.getLogger(SplitTest.class.getName());

    private static final String MSF_OUTPUT_FILENAME = "build/tmp/tiles/liechtenstein.msf";
    private static final String MSF_OUTPUT_DIR      = "build/tmp/tiles/";
    private static final String TEST_DATA_PBF       = "test-data/liechtenstein-latest.osm.pbf";

    /**
     * Pre-test setup
     */
    @Before
    public void setup() {
        File output = new File(MSF_OUTPUT_FILENAME);
        output.delete();
        File path = new File(MSF_OUTPUT_DIR);
        path.mkdirs();
    }

    /**
     * Basic test to check that stuff happens
     */
    @Test
    public void split() {
        try {
            File output = new File(MSF_OUTPUT_FILENAME);
            Assert.assertFalse(output.exists());
            MapSplit.main(new String[] { "-mtvM", "-i", TEST_DATA_PBF, "-o", MSF_OUTPUT_FILENAME, "-f", "2000", "-z", "16", "-O", "2000" });
            checkMetadata(12, 16);
            Tile t = getTile(15, 17250, 11506);
            Assert.assertNotNull(t);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }

    /**
     * Basic test to check that stuff happens, with full relations
     */
    @Test
    public void splitWithFullRelations() {
        try {
            File output = new File(MSF_OUTPUT_FILENAME);
            Assert.assertFalse(output.exists());
            MapSplit.main(new String[] { "-cmtvM", "-i", TEST_DATA_PBF, "-o", MSF_OUTPUT_FILENAME, "-f", "2000", "-z", "16", "-O", "2000" });
            checkMetadata(12, 16);
            Tile t = getTile(16, 34501, 23013);
            Assert.assertNotNull(t);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }

    /**
     * Basic test, optimization turned of so that we can check clipping below
     */
    @Test
    public void splitNoOptimize() {
        try {
            File output = new File(MSF_OUTPUT_FILENAME);
            Assert.assertFalse(output.exists());
            MapSplit.main(new String[] { "-mtvM", "-i", TEST_DATA_PBF, "-o", MSF_OUTPUT_FILENAME, "-f", "2000", "-z", "16" });
            checkMetadata(16, 16);
            Tile t = getTile(16, 34496, 23000);
            Assert.assertNotNull(t); // should exist, is outside of Vaduz
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }

    /**
     * Basic test for clipping
     */
    @Test
    public void splitAndClip() {
        try {
            File output = new File(MSF_OUTPUT_FILENAME);
            Assert.assertFalse(output.exists());
            MapSplit.main(new String[] { "-mtvM", "-p", "test-data/vaduz.poly", "-i", TEST_DATA_PBF, "-o", MSF_OUTPUT_FILENAME, "-f", "2000", "-z", "16" });
            checkMetadata(16, 16);
            Tile t = getTile(16, 33972, 23225);
            Assert.assertNull(t); // shouldn't exist, is outside of Vaduz
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }

    /**
     * Check that the metadata in the MBTiles is what we expect
     * 
     * @param min min zoom in metadata
     * @param max max zoom in metadata
     * @throws MBTilesReadException if we can't read or access the MBTiles file
     */
    private void checkMetadata(int min, int max) throws MBTilesReadException {
        MBTilesReader r = null;
        try {
            r = new MBTilesReader(new File(MSF_OUTPUT_FILENAME));
            MetadataEntry md = r.getMetadata();
            final Map<String, String> required = new HashMap<>();
            md.getRequiredKeyValuePairs().forEach(new Consumer<Entry<String, String>>() {

                @Override
                public void accept(Entry<String, String> e) {
{'fraction_non_alphanumeric': 0.129601317182171, 'fraction_numerical': 0.02352111019640127, 'mean_word_length': 2.249522353840275, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3008894', 'n_tokens_mistral': 2755, 'n_tokens_neox': 2550, 'n_words': 635}