Search in sources :

Example 31 with FileTreeNode

use of com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode in project sldeditor by robward-scisys.

the class VectorToolTest method testVectorToolFileDataSource.

/**
 * Test method for {@link com.sldeditor.tool.vector.VectorTool#VectorTool(com.sldeditor.common.SLDEditorInterface)}.
 */
@Test
public void testVectorToolFileDataSource() {
    TestMissingSLDAttributes testAttribute = new TestMissingSLDAttributes();
    List<CheckAttributeInterface> checkList = new ArrayList<CheckAttributeInterface>();
    checkList.add(testAttribute);
    CheckAttributeFactory.setOverideCheckList(checkList);
    String testsldfile = "/polygon/sld/polygon_polygonwithdefaultlabel.sld";
    TestSLDEditor testSLDEditor = null;
    try {
        testSLDEditor = TestSLDEditor.createAndShowGUI2(null, null, true, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    InputStream inputStream = VectorToolTest.class.getResourceAsStream(testsldfile);
    if (inputStream == null) {
        Assert.assertNotNull("Failed to find sld test file : " + testsldfile, inputStream);
    } else {
        File f = null;
        try {
            f = stream2file(inputStream);
            try {
                testSLDEditor.openFile(f.toURI().toURL());
            } catch (NullPointerException nullException) {
                nullException.printStackTrace();
                StackTraceElement[] stackTraceElements = nullException.getStackTrace();
                System.out.println(stackTraceElements[0].getMethodName());
            }
            f.delete();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    // Fields extracted from the SLD file
    DataSourceInterface dataSource = DataSourceFactory.createDataSource(null);
    Collection<PropertyDescriptor> propertyList = dataSource.getPropertyDescriptorList();
    assertEquals(2, propertyList.size());
    Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();
    for (PropertyDescriptor property : propertyList) {
        map.put(property.getName().getLocalPart(), property);
    }
    AttributeDescriptor name = (AttributeDescriptor) map.get("name");
    assertNotNull(name);
    GeometryDescriptor geometry = (GeometryDescriptor) map.get("geom");
    assertNotNull(geometry);
    File tempFolder = Files.createTempDir();
    TestVectorTool vectorTool = new TestVectorTool(testSLDEditor);
    try {
        // Set a shape file as a data source - that matches the SLD
        File matchingShpFile = extractShapeFile(tempFolder, "/test/sld_cookbook_polygon.zip");
        FileTreeNode fileTreeNode = new FileTreeNode(matchingShpFile.getParentFile(), matchingShpFile.getName());
        vectorTool.testSetDataSource(fileTreeNode);
        dataSource = DataSourceFactory.createDataSource(null);
        propertyList = dataSource.getPropertyDescriptorList();
        assertEquals(3, propertyList.size());
        map.clear();
        for (PropertyDescriptor property : propertyList) {
            map.put(property.getName().getLocalPart(), property);
        }
        name = (AttributeDescriptor) map.get("name");
        assertNotNull(name);
        geometry = (GeometryDescriptor) map.get("the_geom");
        assertNotNull(geometry);
        AttributeDescriptor pop = (AttributeDescriptor) map.get("pop");
        assertNotNull(pop);
        // Set a shape file as a data source - that does not match the SLD
        File nonMatchingShpFile = extractShapeFile(tempFolder, "/test/states.zip");
        FileTreeNode fileTreeNode2 = new FileTreeNode(nonMatchingShpFile.getParentFile(), nonMatchingShpFile.getName());
        vectorTool.testSetDataSource(fileTreeNode2);
        dataSource = DataSourceFactory.createDataSource(null);
        propertyList = dataSource.getPropertyDescriptorList();
        assertEquals(23, propertyList.size());
        map.clear();
        for (PropertyDescriptor property : propertyList) {
            map.put(property.getName().getLocalPart(), property);
        }
        name = (AttributeDescriptor) map.get("name");
        assertNull(name);
        geometry = (GeometryDescriptor) map.get("the_geom");
        assertNotNull(geometry);
        pop = (AttributeDescriptor) map.get("pop");
        assertNull(pop);
        assertEquals(1, testAttribute.getMissingFieldList().size());
        assertEquals("name", testAttribute.getMissingFieldList().get(0));
        // Create SLD from shape file
        vectorTool.testImportFile(fileTreeNode);
        dataSource = DataSourceFactory.createDataSource(null);
        propertyList = dataSource.getPropertyDescriptorList();
        assertEquals(3, propertyList.size());
        map.clear();
        for (PropertyDescriptor property : propertyList) {
            map.put(property.getName().getLocalPart(), property);
        }
        name = (AttributeDescriptor) map.get("name");
        assertNotNull(name);
        geometry = (GeometryDescriptor) map.get("the_geom");
        assertNotNull(geometry);
        pop = (AttributeDescriptor) map.get("pop");
        assertNotNull(pop);
        // Release locks
        dataSource.reset();
    } catch (IOException e) {
        e.printStackTrace();
        fail();
    }
    // Tidy up so the remaining unit tests are ok
    JFrame frame = testSLDEditor.getApplicationFrame();
    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
    testSLDEditor = null;
    clearDown();
    // Delete the shape files we extracted
    purgeDirectory(tempFolder);
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileTreeNode(com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode) DataSourceInterface(com.sldeditor.datasource.DataSourceInterface) GeometryDescriptor(org.opengis.feature.type.GeometryDescriptor) JFrame(javax.swing.JFrame) WindowEvent(java.awt.event.WindowEvent) CheckAttributeInterface(com.sldeditor.datasource.checks.CheckAttributeInterface) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File) Test(org.junit.Test)

Example 32 with FileTreeNode

use of com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode in project sldeditor by robward-scisys.

the class YSLDToolTest method testSupports.

/**
 * Test which file types the tool supports.
 */
@Test
public void testSupports() {
    YSLDTool tool = new YSLDTool();
    assertFalse(tool.supports(null, null, null));
    File testFile1 = null;
    File testFile2 = null;
    File testFile3 = null;
    try {
        testFile1 = File.createTempFile("invalid", ".tst");
        testFile2 = File.createTempFile("valid", ".sld");
        testFile3 = File.createTempFile("valid", ".ysld");
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    // Try with invalid file
    try {
        List<NodeInterface> nodeTypeList = new ArrayList<NodeInterface>();
        assertNotNull(testFile1);
        nodeTypeList.add(new FileTreeNode(testFile1.getParentFile(), testFile1.getName()));
        assertFalse(tool.supports(null, nodeTypeList, null));
    } catch (SecurityException | FileNotFoundException e) {
        e.printStackTrace();
    }
    // Try with valid sld file
    try {
        List<NodeInterface> nodeTypeList = new ArrayList<NodeInterface>();
        nodeTypeList.add(new FileTreeNode(testFile2.getParentFile(), testFile2.getName()));
        assertTrue(tool.supports(null, nodeTypeList, null));
    } catch (SecurityException | FileNotFoundException e) {
        e.printStackTrace();
    }
    // Try with valid ysld file
    try {
        List<NodeInterface> nodeTypeList = new ArrayList<NodeInterface>();
        nodeTypeList.add(new FileTreeNode(testFile3.getParentFile(), testFile3.getName()));
        assertTrue(tool.supports(null, nodeTypeList, null));
    } catch (SecurityException | FileNotFoundException e) {
        e.printStackTrace();
    }
    // Try with several files
    try {
        List<NodeInterface> nodeTypeList = new ArrayList<NodeInterface>();
        nodeTypeList.add(new FileTreeNode(testFile1.getParentFile(), testFile1.getName()));
        nodeTypeList.add(new FileTreeNode(testFile2.getParentFile(), testFile2.getName()));
        nodeTypeList.add(new FileTreeNode(testFile3.getParentFile(), testFile3.getName()));
        assertFalse(tool.supports(null, nodeTypeList, null));
    } catch (SecurityException | FileNotFoundException e) {
        e.printStackTrace();
    }
    testFile1.delete();
    testFile2.delete();
    testFile3.delete();
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) YSLDTool(com.sldeditor.tool.ysld.YSLDTool) NodeInterface(com.sldeditor.common.NodeInterface) FileTreeNode(com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode) Test(org.junit.Test)

Example 33 with FileTreeNode

use of com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode in project sldeditor by robward-scisys.

the class DatabaseConnectionToolTest method testDatabaseConnectionTool.

/**
 * Test method for
 * {@link com.sldeditor.tool.databaseconnection.DatabaseConnectionTool#DatabaseConnectionTool(com.sldeditor.tool.databaseconnection.DatabaseConnectStateInterface)}.
 */
@Test
public void testDatabaseConnectionTool() {
    TestDatabaseConnectState state = new TestDatabaseConnectState();
    TestDatabaseConnectionTool testObj = new TestDatabaseConnectionTool(state);
    testObj.setSelectedItems(null, null);
    List<NodeInterface> nodeTypeList = new ArrayList<NodeInterface>();
    DatabaseConnection connection = DatabaseConnectionFactory.createGeoPackage();
    nodeTypeList.add(new DatabaseNode(new DatabaseInput(ToolManager.getInstance()), connection));
    File parent = new File("");
    String filename = "test.gpkg";
    FileTreeNode fileTreeNode = null;
    try {
        fileTreeNode = new FileTreeNode(parent, filename);
        fileTreeNode.setFileCategory(FileTreeNodeTypeEnum.DATABASE);
        nodeTypeList.add(fileTreeNode);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    testObj.setSelectedItems(nodeTypeList, null);
    testObj.populateComplete(null);
    assertNotNull(testObj.getPanel());
    List<Class<?>> uniqueNodeTypeList = new ArrayList<Class<?>>();
    List<SLDDataInterface> sldDataList = null;
    assertFalse(testObj.supports(uniqueNodeTypeList, nodeTypeList, sldDataList));
    uniqueNodeTypeList.add(String.class);
    assertTrue(testObj.supports(uniqueNodeTypeList, nodeTypeList, sldDataList));
    nodeTypeList.remove(0);
    assertTrue(testObj.supports(uniqueNodeTypeList, nodeTypeList, sldDataList));
    testObj.testConnect();
    testObj.testDisconnect();
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) FileTreeNode(com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode) DatabaseNode(com.sldeditor.datasource.extension.filesystem.node.database.DatabaseNode) SLDDataInterface(com.sldeditor.common.SLDDataInterface) DatabaseConnection(com.sldeditor.common.data.DatabaseConnection) DatabaseInput(com.sldeditor.extension.filesystem.database.DatabaseInput) File(java.io.File) NodeInterface(com.sldeditor.common.NodeInterface) Test(org.junit.Test)

Aggregations

FileTreeNode (com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode)33 File (java.io.File)28 SLDDataInterface (com.sldeditor.common.SLDDataInterface)17 FileNotFoundException (java.io.FileNotFoundException)17 Test (org.junit.Test)17 IOException (java.io.IOException)13 URL (java.net.URL)10 NodeInterface (com.sldeditor.common.NodeInterface)9 URISyntaxException (java.net.URISyntaxException)9 SLDData (com.sldeditor.common.data.SLDData)8 SLDEditorFile (com.sldeditor.datasource.SLDEditorFile)7 ArrayList (java.util.ArrayList)7 ActionEvent (java.awt.event.ActionEvent)4 ActionListener (java.awt.event.ActionListener)4 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)4 DatabaseFeatureClassNode (com.sldeditor.datasource.extension.filesystem.node.database.DatabaseFeatureClassNode)3 FileHandlerInterface (com.sldeditor.datasource.extension.filesystem.node.file.FileHandlerInterface)3 SLDFileHandler (com.sldeditor.extension.filesystem.file.sld.SLDFileHandler)3 SLDFileHandlerTest (com.sldeditor.test.unit.extension.filesystem.file.sld.SLDFileHandlerTest)3 Path (java.nio.file.Path)3