⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shpfilesteststream.java

📁 shape file read and write
💻 JAVA
字号:
package org.geotools.data.shapefile;

import static org.geotools.data.shapefile.ShpFileType.*;

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.Set;
import java.util.Map.Entry;

import org.geotools.TestData;

import junit.framework.TestCase;

public class ShpFilesTestStream extends TestCase implements
        org.geotools.data.shapefile.FileWriter {

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

    @Override
    protected 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();
            }
        }
    }

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

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

    public void testDelete() throws IOException {

        assertTrue(files.delete());

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

    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());
        }
    }

    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());
        }
    }

    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());
        }
    }

    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());
        }
    }
    
    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);
        }
    }

    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());
            }
        }
    }

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

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

    public void testGetReadChannelURL() throws IOException {
        ShpFiles files = new ShpFiles(TestData.url("shapes/statepop.shp"));
        
        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());
    }

    public void testGetWriteChannel() throws IOException {

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

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

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

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

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -