All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.java.de.k3b.geo.io.gpx.GpxReaderTest Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2015-2016 by k3b.
 *
 * This file is part of k3b-geoHelper library.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 package de.k3b.geo.io.gpx;

import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.InputSource;

import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import de.k3b.geo.api.GeoPointDto;
import de.k3b.geo.api.IGeoInfoHandler;
import de.k3b.geo.api.IGeoPointInfo;

/**
 * Created by k3b on 14.06.2014.
 */
public class GpxReaderTest {
    /** format for gpx v1.0 */
    String xmlMinimal_gpx_v10 = "262:3:562:54989type: cell, accuracy: 1640, confidence: 75geo:0,0?q=12.34,56.78(name)\n";

    /** format for gpx v1.1 */
    String xmlMinimal_gpx_v11 = "262:3:562:54989type: cell, accuracy: 1640, confidence: 75\n";
    String xmlFull_gpx_v11 = ""+
            "" + xmlMinimal_gpx_v11 + "";

    /** format for gpx v1.1 using xml namespace g: */
    String xmlMinimal_gpx_v11_withNS = "2014-12-19T21:13:21Z262:3:562:54989type: cell, accuracy: 1640, confidence: 75geo:0,0?q=12.34,56.78(name)\n";
    String xmlFull_gpx_v11withNS = ""+
            "" + xmlMinimal_gpx_v11_withNS + "";

    /** illegal kml format but containing the essential fields understood by the parser */
    String xmlMinimal_kml = "262:3:562:54989type: cell, accuracy: 1640, confidence: 758.7178206,53.1099972,02014-12-19T21:13:21Z\n";

    @Test
    public void parseFormatGpx11ShortTest() throws IOException {
        GpxReader reader = new GpxReader(null);
        IGeoPointInfo location = reader.getTracks(new InputSource(new StringReader(xmlMinimal_gpx_v11))).get(0);
        String formatted = GpxFormatter.toGpx(new StringBuffer(), location).toString();

        Assert.assertEquals(xmlMinimal_gpx_v11, formatted);
    }

    @Test
    public void parseFormatGpx11FullTest() throws IOException {
        GpxReader reader = new GpxReader(null);
        IGeoPointInfo location = reader.getTracks(new InputSource(new StringReader(xmlFull_gpx_v11))).get(0);
        String formatted = GpxFormatter.toGpx(new StringBuffer(), location, location.getDescription(), location.getLink()).toString();

        Assert.assertEquals(xmlMinimal_gpx_v11, formatted);
    }

    @Test
    public void parseFormatGpx11WithNamespaceTest() throws IOException {
        GpxReader reader = new GpxReader(null);
        IGeoPointInfo location = reader.getTracks(new InputSource(new StringReader(xmlFull_gpx_v11withNS))).get(0);
        String formatted = GpxFormatter.toGpx(new StringBuffer(), location, location.getDescription(), location.getLink()).toString();

        Assert.assertEquals(xmlMinimal_gpx_v11, formatted);
    }

    @Test
    public void parseFormatGpx10ShortTest() throws IOException {
        GpxReader reader = new GpxReader(null);
        IGeoPointInfo location = reader.getTracks(new InputSource(new StringReader(xmlMinimal_gpx_v10))).get(0);
        String formatted = GpxFormatter.toGpx(new StringBuffer(), location, location.getDescription(), location.getLink()).toString();

        Assert.assertEquals(xmlMinimal_gpx_v11, formatted);
    }

    @Test
    public void parseFormatKmlShortTest() throws IOException {
        GpxReader reader = new GpxReader(new GeoPointDto());
        GeoPointDto location = (GeoPointDto) reader.getTracks(new InputSource(new StringReader(xmlMinimal_kml))).get(0);

        // uri not supported by kml
        location.setLink("geo:0,0?q=12.34,56.78(name)");

        String formatted = GpxFormatter.toGpx(new StringBuffer(), location, location.getDescription(), location.getLink()).toString();

        Assert.assertEquals(xmlMinimal_gpx_v11, formatted);
    }

    // used to test files that do not work
    // @Test
    public void parseFormatFileTest() throws IOException {
        // from android-s MyTracks
        final String fullPathToParserInputFile = "D:\\prj\\eve\\android\\prj\\LocationMapViewer.wrk\\download\\Hotel-Conrad-10_02_2015 09_01.kml";

        if (fullPathToParserInputFile != null) {
            GpxReader reader = new GpxReader(null);

            List locations = reader.getTracks(new InputSource(new FileReader(fullPathToParserInputFile)));

            final StringBuffer result = new StringBuffer();
            for (IGeoPointInfo location : locations) {
                GpxFormatter.toGpx(result, location, location.getDescription(), location.getLink());
            }
            System.out.print(result.toString());
        }
    }


    @Test
    public void shouldNotCrashWithEllegalCharsGpxFormatter() throws IOException {
        String forbidden1 = "charsNotAllowdInXml:=><& '\"";
        String forbidden = forbidden1 + "\n\r\t";
        GeoPointDto geo = new GeoPointDto()
                .setLongitude(1).setLongitude(1)
                .setName(forbidden)
                .setDescription(forbidden1) // space/cr/lf my change so test without them
                .setId(forbidden)
                .setSymbol(forbidden)
                .setLink(forbidden)
                ;

        String xmlFragment = GpxFormatter.toGpx(new StringBuffer(), geo).toString();

        GeoXmlOrTextParser parser = new GeoXmlOrTextParser<>();
        List result = parser.get(xmlFragment);
        Assert.assertEquals("#items", 1, result.size());
		
		// space/cr/lf my change so test without them
        Assert.assertEquals("item[0].Description", forbidden1, result.get(0).getDescription());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy