Search in sources :

Example 1 with DatabaseConnectionName

use of com.sldeditor.tool.dbconnectionlist.DatabaseConnectionName in project sldeditor by robward-scisys.

the class DatabaseConnectionTest method testDatabaseConnection.

/**
 * Test method for
 * {@link com.sldeditor.common.data.DatabaseConnection#DatabaseConnection(com.sldeditor.common.data.DatabaseConnection)}.
 */
@Test
public void testDatabaseConnection() {
    Param param = GeoPkgDataStoreFactory.DBTYPE;
    List<DatabaseConnectionField> expectedDetailList = new ArrayList<DatabaseConnectionField>();
    expectedDetailList.add(new DatabaseConnectionField(GeoPkgDataStoreFactory.DATABASE));
    expectedDetailList.add(new DatabaseConnectionField(GeoPkgDataStoreFactory.USER));
    String expectedDatabaseTypeLabel = "GeoPackage";
    boolean expectedSupportsDuplication = false;
    DatabaseConnection test = new DatabaseConnection(param, expectedDatabaseTypeLabel, expectedSupportsDuplication, expectedDetailList, new DatabaseConnectionName() {

        @Override
        public String getConnectionName(String duplicatePrefix, int noOfDuplicates, Map<String, String> properties) {
            String connectionName = Localisation.getString(DatabaseConnectionTest.class, "common.notSet");
            String databaseName = properties.get(GeoPkgDataStoreFactory.DATABASE.key);
            if (databaseName != null) {
                File f = new File(databaseName);
                if (f.isFile()) {
                    connectionName = f.getName();
                }
            }
            for (int i = 0; i < noOfDuplicates; i++) {
                connectionName = duplicatePrefix + connectionName;
            }
            return connectionName;
        }
    });
    Map<String, String> connectionDataMap = new HashMap<String, String>();
    connectionDataMap.put(GeoPkgDataStoreFactory.DATABASE.key, "test datatbase");
    test.setConnectionDataMap(connectionDataMap);
    assertEquals(test.isSupportsDuplication(), expectedSupportsDuplication);
    assertNotNull(test.getConnectionName());
    assertNotNull(test.getDatabaseConnectionName());
    assertEquals(test.getDetailList(), expectedDetailList);
    assertEquals(test.getDatabaseTypeLabel(), expectedDatabaseTypeLabel);
    assertTrue(test.hashCode() != 0);
    assertEquals(1, test.getExpectedKeys().size());
    assertEquals(GeoPkgDataStoreFactory.DATABASE.key, test.getExpectedKeys().get(0));
    assertEquals(test.getDatabaseType(), param.sample);
    assertEquals(test.getConnectionDataMap().get(GeoPkgDataStoreFactory.DATABASE.key), connectionDataMap.get(GeoPkgDataStoreFactory.DATABASE.key));
    assertEquals(test.getConnectionDataMap().get(GeoPkgDataStoreFactory.DBTYPE.key), GeoPkgDataStoreFactory.DBTYPE.sample);
    String expectedUserName = "test user";
    test.setUserName(expectedUserName);
    assertEquals(test.getUserName(), expectedUserName);
    assertNotNull(test.getDBConnectionParams());
    String expectedPassword = "password123";
    test.setPassword(expectedPassword);
    assertEquals(test.getPassword(), expectedPassword);
    // Duplicate
    DatabaseConnection testCopy = test.duplicate();
    assertTrue(testCopy.compareTo(test) < 0);
    testCopy.update(test);
    assertEquals(testCopy, test);
    assertEquals(testCopy, testCopy);
    testCopy.setUserName("new username");
    assertFalse(testCopy.equals(test));
    testCopy.update(test);
    assertTrue(testCopy.equals(test));
    String actualString = test.encodeAsString();
    DatabaseConnection newTest = DatabaseConnection.decodeString(actualString);
    assertTrue(newTest.equals(test));
}
Also used : DatabaseConnectionField(com.sldeditor.common.data.DatabaseConnectionField) DatabaseConnectionName(com.sldeditor.tool.dbconnectionlist.DatabaseConnectionName) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Param(org.geotools.data.DataAccessFactory.Param) DatabaseConnection(com.sldeditor.common.data.DatabaseConnection) File(java.io.File) Test(org.junit.Test)

Aggregations

DatabaseConnection (com.sldeditor.common.data.DatabaseConnection)1 DatabaseConnectionField (com.sldeditor.common.data.DatabaseConnectionField)1 DatabaseConnectionName (com.sldeditor.tool.dbconnectionlist.DatabaseConnectionName)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Param (org.geotools.data.DataAccessFactory.Param)1 Test (org.junit.Test)1