Package org.geotools.data.shapefile

Source Code of org.geotools.data.shapefile.ShpFilesTestStream

/*
*    GeoTools - The Open Source Java GIS Toolkit
*    http://geotools.org
*
*    (C) 2008, Open Source Geospatial Foundation (OSGeo)
*
*    This library is free software; you can redistribute it and/or
*    modify it under the terms of the GNU Lesser General Public
*    License as published by the Free Software Foundation;
*    version 2.1 of the License.
*
*    This library is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*    Lesser General Public License for more details.
*/
package org.geotools.data.shapefile;

import static org.geotools.data.shapefile.files.ShpFileType.PRJ;
import static org.geotools.data.shapefile.files.ShpFileType.SHP;
import static org.junit.Assert.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.geotools.TestData;
import org.geotools.data.shapefile.files.ShpFileType;
import org.geotools.data.shapefile.files.ShpFiles;
import org.geotools.data.shapefile.files.StorageFile;
import org.junit.Before;
import org.junit.Test;

/**
*
*
* @source $URL$
*/
public class ShpFilesTestStream implements
        org.geotools.data.shapefile.files.FileWriter {

    private String typeName;
    private Map<ShpFileType, File> map;
    private ShpFiles files;

    @Before
    public void setUp() throws Exception {
        map = ShpFilesTest.createFiles("shpFiles", ShpFileType.values(), false);

        typeName = map.get(SHP).getName();
        typeName = typeName.substring(0, typeName.lastIndexOf("."));

        files = new ShpFiles(map.get(SHP));
    }

    private void writeDataToFiles() throws IOException {
        Set<Entry<ShpFileType, File>> entries = map.entrySet();
        for (Entry<ShpFileType, File> entry : entries) {
            FileWriter out = new FileWriter(entry.getValue());
            try {
                out.write(entry.getKey().name());
            } finally {
                out.close();
            }
        }
    }

    @Test
    public void testIsLocalURL() throws IOException {
        ShpFiles files = new ShpFiles("http://someurl.com/file.shp");
        assertFalse(files.isLocal());
    }

    @Test
    public void testIsLocalFiles() throws IOException {
        assertTrue(files.isLocal());
    }

    @Test
    public void testDelete() throws IOException {

        assertTrue(files.delete());

        for (File file : map.values()) {
            assertFalse(file.exists());
        }
    }

    @Test
    public void testExceptionGetInputStream() throws Exception {
        ShpFiles shpFiles = new ShpFiles(new URL("http://blah/blah.shp"));
        try{
            shpFiles.getInputStream(SHP, this);
            fail("maybe test is bad?  We want an exception here");
        }catch(Throwable e){
            assertEquals(0, shpFiles.numberOfLocks());
        }
    }

    @Test
    public void testExceptionGetOutputStream() throws Exception {
        ShpFiles shpFiles = new ShpFiles(new URL("http://blah/blah.shp"));
        try{
            shpFiles.getOutputStream(SHP, this);
            fail("maybe test is bad?  We want an exception here");
        }catch(Throwable e){
            assertEquals(0, shpFiles.numberOfLocks());
        }
    }

    @Test
    public void testExceptionGetWriteChannel() throws Exception {
        ShpFiles shpFiles = new ShpFiles(new URL("http://blah/blah.shp"));
        try{
            shpFiles.getWriteChannel(SHP, this);
            fail("maybe test is bad?  We want an exception here");
        }catch(Throwable e){
            assertEquals(0, shpFiles.numberOfLocks());
        }
    }

    @Test
    public void testExceptionGetReadChannel() throws Exception {
        ShpFiles shpFiles = new ShpFiles(new URL("http://blah/blah.shp"));
        try{
            shpFiles.getReadChannel(SHP, this);
            fail("maybe test is bad?  We want an exception here");
        }catch(Throwable e){
            assertEquals(0, shpFiles.numberOfLocks());
        }
    }
   
    @Test
    public void testGetInputStream() throws IOException {
        writeDataToFiles();

        ShpFileType[] types = ShpFileType.values();
        for (ShpFileType shpFileType : types) {
            String read = "";
            InputStream in = files.getInputStream(shpFileType, this);
            InputStreamReader reader = new InputStreamReader(in);
            assertEquals(1, files.numberOfLocks());
            try {
                int current = reader.read();
                while (current != -1) {
                    read += (char) current;
                    current = reader.read();
                }
            } finally {
                reader.close();
                in.close();
                assertEquals(0, files.numberOfLocks());
            }
            assertEquals(shpFileType.name(), read);
        }
    }

    @Test
    public void testGetWriteStream() throws IOException {

        ShpFileType[] types = ShpFileType.values();
        for (ShpFileType shpFileType : types) {
           
            OutputStream out = files.getOutputStream(shpFileType, this);
            assertEquals(1, files.numberOfLocks());
            try {
                out.write((byte)2);
            } finally {
                out.close();
                assertEquals(0, files.numberOfLocks());
            }
        }
    }

    @Test
    public void testGetReadChannelFileChannel() throws IOException {
        writeDataToFiles();

        ShpFileType[] types = ShpFileType.values();
        for (ShpFileType shpFileType : types) {
            doRead(shpFileType);
        }
    }

    @Test
    public void testGetReadChannelURL() throws IOException {
        URL url = TestData.url("shapes/statepop.shp");
        ShpFiles files = new ShpFiles(url);
       
        assertFalse(files.isLocal());
       
        ReadableByteChannel read = files.getReadChannel(SHP, this);
       
        assertEquals(1, files.numberOfLocks());
       
        read.close();
       
        assertEquals(0, files.numberOfLocks());
    }
   
    private void doRead(ShpFileType shpFileType) throws IOException {
        ReadableByteChannel in = files.getReadChannel(shpFileType, this);
        assertEquals(1, files.numberOfLocks());
        assertTrue(in instanceof FileChannel);

        ByteBuffer buffer = ByteBuffer.allocate(10);
        in.read(buffer);
        buffer.flip();
        String read = "";
        try {
            while (buffer.hasRemaining()) {
                read += (char) buffer.get();
            }
        } finally {
            in.close();
            // verify that you can close multiple times without bad things
            // happening
            in.close();
        }
        assertEquals(0, files.numberOfLocks());
        assertEquals(shpFileType.name(), read);
    }

    private void doWrite(ShpFileType shpFileType) throws IOException {
        WritableByteChannel out = files.getWriteChannel(shpFileType, this);
        assertEquals(1, files.numberOfLocks());
        assertTrue(out instanceof FileChannel);

        try {
            ByteBuffer buffer = ByteBuffer.allocate(10);
            buffer.put(shpFileType.name().getBytes());
            buffer.flip();
            out.write(buffer);
        } finally {
            out.close();
            // verify that you can close multiple times without bad things
            // happening
            out.close();
        }
        assertEquals(0, files.numberOfLocks());
    }

    @Test
    public void testGetWriteChannel() throws IOException {

        ShpFileType[] types = ShpFileType.values();
        for (ShpFileType shpFileType : types) {
            doWrite(shpFileType);
            doRead(shpFileType);
        }
    }

    @Test
    public void testGetStorageFile() throws Exception {
        StorageFile prj = files.getStorageFile(PRJ);
        assertTrue(prj.getFile().getName().startsWith(typeName));
        assertTrue(prj.getFile().getName().endsWith(".prj"));
    }

    @Test
    public void testGetTypeName() throws Exception {
        assertEquals(typeName, files.getTypeName());
    }

    public String id() {
        return getClass().getName();
    }

}
TOP

Related Classes of org.geotools.data.shapefile.ShpFilesTestStream

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.