Search in sources :

Example 6 with DatabaseFeatureClassNode

use of com.sldeditor.datasource.extension.filesystem.node.database.DatabaseFeatureClassNode in project sldeditor by robward-scisys.

the class DatabaseInputTest method testDatabaseInput.

/**
 * Test method for {@link com.sldeditor.extension.filesystem.database.DatabaseInput#DatabaseInput(com.sldeditor.common.ToolSelectionInterface)}.
 */
@Test
public void testDatabaseInput() {
    DatabaseInput input = new DatabaseInput(ToolManager.getInstance());
    input.readPropertyFile();
    FSTree tree = new FSTree();
    DefaultMutableTreeNode rootNode;
    try {
        rootNode = new DefaultMutableTreeNode("Root");
        DefaultTreeModel model = new DefaultTreeModel(rootNode);
        input.populate(tree, model, rootNode);
        List<SLDDataInterface> sldDataList = input.open(null);
        assertNull(sldDataList);
        DatabaseConnection connection1 = DatabaseConnectionFactory.createGeoPackage();
        String featureClassName = "test feature class";
        DatabaseFeatureClassNode fcTreeNode = new DatabaseFeatureClassNode(input, connection1, featureClassName);
        Map<String, String> connectionDataMap = new HashMap<String, String>();
        connectionDataMap.put(GeoPkgDataStoreFactory.DATABASE.key, "test.gpkg");
        connection1.setConnectionDataMap(connectionDataMap);
        // Try with no known database connections
        SelectedFiles actualSLDContents = input.getSLDContents(fcTreeNode);
        assertNotNull(actualSLDContents);
        assertTrue(actualSLDContents.isDataSource());
        // Add some database connections
        input.addNewConnection(connection1);
        DatabaseConnection connection2 = DatabaseConnectionFactory.createGeoPackage();
        Map<String, String> connectionDataMap2 = new HashMap<String, String>();
        connectionDataMap2.put(GeoPkgDataStoreFactory.DATABASE.key, "test2.gpkg");
        connection2.setConnectionDataMap(connectionDataMap2);
        input.addNewConnection(connection2);
        input.addNewConnection(null);
        List<SLDDataInterface> sldDataContentsList = input.getSLDContents(fcTreeNode).getSldData();
        assertEquals(0, sldDataContentsList.size());
        // Try saving a null object
        assertFalse(input.save(null));
        // Check how many connections we have
        assertEquals(2, input.getConnectionDetails().size());
    } catch (SecurityException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) HashMap(java.util.HashMap) FSTree(com.sldeditor.datasource.extension.filesystem.node.FSTree) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) DatabaseFeatureClassNode(com.sldeditor.datasource.extension.filesystem.node.database.DatabaseFeatureClassNode) SLDDataInterface(com.sldeditor.common.SLDDataInterface) SelectedFiles(com.sldeditor.common.filesystem.SelectedFiles) DatabaseInput(com.sldeditor.extension.filesystem.database.DatabaseInput) DatabaseConnection(com.sldeditor.common.data.DatabaseConnection) Test(org.junit.Test)

Example 7 with DatabaseFeatureClassNode

use of com.sldeditor.datasource.extension.filesystem.node.database.DatabaseFeatureClassNode in project sldeditor by robward-scisys.

the class VectorToolTest method testVectorToolDBDataSource.

@Test
public void testVectorToolDBDataSource() {
    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();
    }
    RenderPanelImpl.setUnderTest(true);
    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 {
        InputStream gpkgInputStream = VectorToolTest.class.getResourceAsStream("/test/sld_cookbook_polygon.gpkg");
        final File gpkgFile = new File(tempFolder, "sld_cookbook_polygon.gpkg");
        try (FileOutputStream out = new FileOutputStream(gpkgFile)) {
            IOUtils.copy(gpkgInputStream, out);
        }
        DatabaseConnection databaseConnection = DatabaseConnectionFactory.getConnection(gpkgFile.getAbsolutePath());
        DatabaseFeatureClassNode dbFCTreeNode = new DatabaseFeatureClassNode(null, databaseConnection, "sld_cookbook_polygon");
        DatabaseConnectionManager.getInstance().addNewConnection(null, databaseConnection);
        vectorTool.testSetDataSource(dbFCTreeNode);
        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("geometry");
        assertNotNull(geometry);
        AttributeDescriptor pop = (AttributeDescriptor) map.get("pop");
        assertNotNull(pop);
        // Create SLD from geopackage layer
        vectorTool.testImportFeatureClass(dbFCTreeNode);
        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("geometry");
        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) DataSourceInterface(com.sldeditor.datasource.DataSourceInterface) GeometryDescriptor(org.opengis.feature.type.GeometryDescriptor) DatabaseFeatureClassNode(com.sldeditor.datasource.extension.filesystem.node.database.DatabaseFeatureClassNode) JFrame(javax.swing.JFrame) FileOutputStream(java.io.FileOutputStream) WindowEvent(java.awt.event.WindowEvent) DatabaseConnection(com.sldeditor.common.data.DatabaseConnection) CheckAttributeInterface(com.sldeditor.datasource.checks.CheckAttributeInterface) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File) Test(org.junit.Test)

Aggregations

DatabaseFeatureClassNode (com.sldeditor.datasource.extension.filesystem.node.database.DatabaseFeatureClassNode)7 SLDDataInterface (com.sldeditor.common.SLDDataInterface)3 SLDEditorFile (com.sldeditor.datasource.SLDEditorFile)3 FileTreeNode (com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 NodeInterface (com.sldeditor.common.NodeInterface)2 DatabaseConnection (com.sldeditor.common.data.DatabaseConnection)2 SelectedFiles (com.sldeditor.common.filesystem.SelectedFiles)2 FileNotFoundException (java.io.FileNotFoundException)2 HashMap (java.util.HashMap)2 DataSourceInterface (com.sldeditor.datasource.DataSourceInterface)1 CheckAttributeInterface (com.sldeditor.datasource.checks.CheckAttributeInterface)1 FSTree (com.sldeditor.datasource.extension.filesystem.node.FSTree)1 GeoServerStyleHeadingNode (com.sldeditor.datasource.extension.filesystem.node.geoserver.GeoServerStyleHeadingNode)1 DatabaseInput (com.sldeditor.extension.filesystem.database.DatabaseInput)1 ToolButton (com.sldeditor.tool.ToolButton)1 VectorTool (com.sldeditor.tool.vector.VectorTool)1 Dimension (java.awt.Dimension)1