Example #1
0
  private void testAddField(String dsName, Type type) throws Exception {
    DataSource d = dsf.getDataSource(dsName, DataSourceFactory.EDITABLE);

    d.open();
    Metadata m = d.getMetadata();
    int fc = m.getFieldCount();
    String name = "toto";
    int i = 0;
    while (m.getFieldIndex(name + "_" + i) != -1) {
      i++;
    }
    //                System.out.println(fc + " fields");
    //                System.out.println("adding " + name + "_" + i);
    d.addField(name + "_" + i, type);
    d.commit();
    d.close();
    d.open();
    assertEquals(fc + 1, d.getMetadata().getFieldCount());
    //                System.out.println(d.getMetadata().getFieldCount() + " fields");
    //                for (int j = 0; j < d.getFieldCount(); j++) {
    //                        System.out.println(j + " field " + d.getFieldName(j));
    //                }
    //                System.out.println(d.getFieldName(fc));
    assertEquals(d.getFieldName(fc), name + "_" + i);
    assertEquals(d.getFieldType(fc).getTypeCode(), Type.STRING);

    assertNull(d.getFieldType(fc).getConstraintValue(Constraint.PK));
    assertNull(d.getFieldType(fc).getConstraintValue(Constraint.READONLY));
    d.close();
  }
Example #2
0
  @Test
  public void testModifyField() throws Exception {
    sm.register("big", getTempCopyOf(getAnyNonSpatialResource()));
    DataSource d = dsf.getDataSource("big");

    d.open();
    d.getMetadata();
    d.setFieldName(1, "nuevo");
    d.commit();
    d.close();
    d.open();
    assertEquals(d.getMetadata().getFieldName(1), "nuevo");
    d.close();
  }
Example #3
0
  @Test
  public void testDeleteField() throws Exception {
    sm.register(
        "big", getTempCopyOf(new File(TestResourceHandler.TESTRESOURCES, "landcover2000.shp")));
    DataSource d = dsf.getDataSource("big");

    d.open();
    Metadata m = d.getMetadata();
    int fc = m.getFieldCount();
    d.removeField(1);
    d.commit();
    d.close();
    d.open();
    assertEquals(fc - 1, m.getFieldCount());
    d.close();
  }
  private void testIndexRealData(String source, double checkPeriod, DiskRTree tree)
      throws NoSuchTableException, DataSourceCreationException, DriverException, IOException,
          Exception {
    DataSource ds = dsf.getDataSource(source);
    String fieldName = "the_geom";

    ds.open();
    int fieldIndex = ds.getFieldIndexByName(fieldName);
    for (int i = 0; i < ds.getRowCount(); i++) {
      if (i / (int) checkPeriod == i / checkPeriod) {
        tree.checkTree();
        tree.close();
        tree.openIndex(indexFile);
        tree.checkTree();
        checkLookUp(tree, ds, fieldIndex);
      }
      Envelope value = ds.getFieldValue(i, fieldIndex).getAsGeometry().getEnvelopeInternal();
      tree.insert(value, i);
    }
    for (int i = 0; i < ds.getRowCount(); i++) {
      if (i / (int) checkPeriod == i / checkPeriod) {
        tree.checkTree();
        tree.save();
        tree.checkTree();
        checkLookUp(tree, ds, fieldIndex);
      }
      Value value = ds.getFieldValue(i, fieldIndex);
      tree.delete(value.getAsGeometry().getEnvelopeInternal(), i);
    }

    ds.close();
  }
 private void failedCommit(DataSource ds, IndexQuery query)
     throws DriverException, NonEditableDataSourceException {
   ds.deleteRow(2);
   ds.setFieldValue(0, 1, ValueFactory.createValue("nouveau"));
   Value[] row = ds.getRow(0);
   row[1] = ValueFactory.createValue("aaaaa");
   ds.insertFilledRow(row);
   Value[][] table = super.getDataSourceContents(ds);
   Iterator<Integer> it = ds.queryIndex(query);
   try {
     ReadDriver.failOnWrite = true;
     ds.commit();
     ds.close();
   } catch (DriverException e) {
     assertTrue(equals(table, super.getDataSourceContents(ds)));
     if (it != null) {
       assertTrue(ds.queryIndex(query) != null);
     } else {
       assertTrue(ds.queryIndex(query) == null);
     }
     ReadDriver.failOnWrite = false;
     ds.commit();
     ds.close();
   }
   ds.open();
   assertTrue(equals(table, super.getDataSourceContents(ds)));
   ds.close();
 }
Example #6
0
  private String getContent(String name) throws Exception {
    DataSource ds = dsf.getDataSource(name);
    ds.open();
    String ret = ds.getAsString();
    ds.close();

    return ret;
  }
  @Test
  public void testGetSetCRS() throws Exception {
    dsf.executeSQL("CREATE TABLE init AS SELECT * FROM ST_RandomGeometry('point', 10);");
    DataSource ds = dsf.getDataSourceFromSQL("SELECT ST_CRS(the_geom) from init;");

    ds.open();
    assertTrue(ds.isNull(0, 0));
    ds.close();

    ds = dsf.getDataSourceFromSQL("SELECT ST_CRS(ST_SetCRS(the_geom, 'EPSG:27572')) from init;");

    ds.open();
    for (int i = 0; i < ds.getRowCount(); i++) {
      assertTrue(ds.getString(i, 0).contains("EPSG:27572"));
    }
    ds.close();
  }
Example #8
0
  @Test
  public void testEditionWithFieldRemoved() throws Exception {
    sm.register("ile", getTempCopyOf(super.getAnyNonSpatialResource()));
    DataSource d = dsf.getDataSource("ile", DataSourceFactory.EDITABLE);
    d.open();
    String fieldName = d.getFieldName(1);
    Value testValue = d.getFieldValue(0, 2);
    d.removeField(1);
    assertTrue(equals(testValue, d.getFieldValue(0, 1)));
    new UndoRedoTest().testAlphanumericEditionUndoRedo(d);
    d.commit();
    d.close();

    d.open();
    assertEquals(d.getFieldIndexByName(fieldName), -1);
    d.close();
  }
Example #9
0
 public void testProducedRasterEnvelope() throws Exception {
   DataSource ds = dsf.getDataSource("raster");
   ds.open();
   SpatialDataSourceDecorator sds = new SpatialDataSourceDecorator(ds);
   Envelope env = sds.getFullExtent();
   assertTrue(env.getWidth() > 0);
   assertTrue(env.getHeight() > 0);
   ds.close();
 }
Example #10
0
 @Test
 public void testEditionWithFieldAdded() throws Exception {
   sm.register("toto", getTempCopyOf(super.getAnyNonSpatialResource()));
   DataSource d = dsf.getDataSource("toto", DataSourceFactory.EDITABLE);
   d.open();
   d.addField("extra", TypeFactory.createType(Type.STRING));
   int fi = d.getFieldIndexByName("extra");
   new UndoRedoTest().testAlphanumericEditionUndoRedo(d);
   Value newValue = ValueFactory.createValue("hi");
   d.setFieldValue(0, fi, newValue);
   d.undo();
   d.redo();
   d.commit();
   d.close();
   d.open();
   assertTrue(equals(d.getFieldValue(0, d.getFieldIndexByName("extra")), newValue));
   d.close();
 }
Example #11
0
  @Test
  public void testSameSourceSameDSInstance() throws Exception {
    sm.register(SOURCE, testFile);

    DataSource ds1 = dsf.getDataSource(SOURCE, DataSourceFactory.NORMAL);
    DataSource ds2 = dsf.getDataSource(SOURCE, DataSourceFactory.NORMAL);
    ds1.open();
    assertTrue(ds2.isOpen());
    ds2.close();
  }
Example #12
0
  private String associateString(Source source, String propertyName) throws Exception {
    DataSource ds = dsf.getDataSource(SOURCE);
    ds.open();
    long rc = ds.getRowCount();
    ds.close();

    String rcStr = Long.toString(rc);
    source.putProperty(propertyName, rcStr);
    return rcStr;
  }
Example #13
0
  @Test
  public void testFieldIndexDataSource() throws Exception {

    DataSource ds = dsf.getDataSource(super.getAnyNonSpatialResource());
    ds.open();
    assertEquals(
        ds.getMetadata().getFieldIndex(ds.getMetadata().getFieldName(1)),
        ds.getFieldIndexByName(ds.getFieldName(1)));
    ds.close();
  }
Example #14
0
  @Test
  public void testTypeInAddField() throws Exception {
    String dsName = "toto";
    sm.register(dsName, getTempCopyOf(super.getAnyNonSpatialResource()));
    DataSource d = dsf.getDataSource(dsName);

    d.open();
    int fc = d.getMetadata().getFieldCount();
    Type type = (d.getDriver()).getTypesDefinitions()[0].createType();
    d.addField("new", type);
    assertEquals(d.getMetadata().getFieldType(fc).getTypeCode(), type.getTypeCode());
    d.commit();
    d.close();

    d = dsf.getDataSource(dsName);
    d.open();
    assertEquals(d.getMetadata().getFieldCount(), fc + 1);
    assertEquals(d.getMetadata().getFieldType(fc).getTypeCode(), type.getTypeCode());
    d.close();
  }
Example #15
0
  @Test
  public void testSaveWithAnOpenHSQLDBDataSource() throws Exception {
    Assume.assumeTrue(hsqlDbAvailable);

    sm.register("db", testDB);
    DataSource ds = dsf.getDataSource("db");
    ds.open();
    sm.saveStatus();
    ds.getFieldValue(0, 0);
    ds.close();
  }
Example #16
0
 private void testOpen(File file) throws Exception {
   GeoRaster gr = GeoRasterFactory.createGeoRaster(file.getAbsolutePath());
   gr.open();
   int rasterType = gr.getType();
   DataSource ds = dsf.getDataSource(file);
   ds.open();
   Metadata metadata = ds.getMetadata();
   Type fieldType = metadata.getFieldType(0);
   assertTrue(fieldType.getIntConstraint(Constraint.RASTER_TYPE) == rasterType);
   ds.getFieldValue(0, 0);
   ds.close();
 }
Example #17
0
 @Test
 public void testFieldDeletionEditionWhileEdition() throws Exception {
   sm.register("toto", super.getAnyNonSpatialResource());
   DataSource d = dsf.getDataSource("toto");
   d.open();
   Value[][] content = super.getDataSourceContents(d);
   d.deleteRow(0);
   d.setFieldValue(0, 2, d.getFieldValue(1, 2));
   d.removeField(1);
   assertTrue(equals(d.getFieldValue(0, 1), content[2][2]));
   d.setFieldValue(0, 0, d.getFieldValue(1, 0));
   assertTrue(equals(d.getFieldValue(0, 0), content[2][0]));
   d.close();
 }
  @Test
  public void testST_Transform() throws Exception {
    dsf.executeSQL(
        "CREATE TABLE init AS SELECT 'POINT(584173.736059813 2594514.82833411)'::GEOMETRY as the_geom;");
    DataSource ds = dsf.getDataSourceFromSQL("SELECT * from init;");
    // EPSG:27572;584173.736059813;2594514.82833411;EPSG:4326;;0.01

    WKTReader wKTReader = new WKTReader();
    Geometry targetGeom = wKTReader.read("POINT(2.114551393 50.345609791)");
    ds.open();
    assertTrue(!ds.isNull(0, 0));
    ds.close();

    ds =
        dsf.getDataSourceFromSQL(
            "SELECT ST_TRANSFORM(the_geom, 'EPSG:27572', 'EPSG:4326') from init;");

    ds.open();

    assertTrue(ds.getGeometry(0).equalsExact(targetGeom, 0.01));

    ds.close();
  }
Example #19
0
  @Test
  public void testFieldInsertionEditionWhileEdition() throws Exception {
    sm.register("toto", getTempCopyOf(super.getAnyNonSpatialResource()));
    DataSource d = dsf.getDataSource("toto");
    d.open();
    String nouveau = "nouveau";
    Value newValue = ValueFactory.createValue(nouveau);
    Value testValue = d.getFieldValue(2, 1);
    int lastField = d.getMetadata().getFieldCount();
    d.deleteRow(0);
    d.setFieldValue(0, 1, d.getFieldValue(1, 1));
    d.addField(nouveau, TypeFactory.createType(Type.STRING));
    d.setFieldValue(0, lastField, newValue);
    assertTrue(equals(d.getFieldValue(0, lastField), newValue));
    d.commit();
    d.close();

    d.open();
    assertEquals(d.getMetadata().getFieldName(lastField).toLowerCase(), nouveau);
    assertTrue(equals(d.getFieldValue(0, lastField), newValue));
    assertTrue(equals(d.getFieldValue(0, 1), testValue));
    d.close();
  }
Example #20
0
 @Test
 public void testRegisterTwice() throws Exception {
   sm.register(SOURCE, getTempFile(".shp"));
   try {
     sm.register(SOURCE, getTempFile(".shp"));
     fail();
   } catch (SourceAlreadyExistsException e) {
     // we check that the failed registration has broken nothing
     sm.remove(SOURCE);
     sm.register(SOURCE, testFile);
     DataSource ds = dsf.getDataSource(SOURCE);
     ds.open();
     ds.close();
   }
 }
Example #21
0
  private void testMetadataEditionListenerTest(String dsName, Type type) throws Exception {
    DataSource d = dsf.getDataSource(dsName);

    d.open();
    ListenerCounter elc = new ListenerCounter();
    d.addMetadataEditionListener(elc);
    d.removeField(1);
    d.addField("nuevo", type);
    d.setFieldName(1, "jjjj");
    assertEquals(elc.fieldDeletions, 1);
    assertEquals(elc.fieldInsertions, 1);
    assertEquals(elc.fieldModifications, 1);
    assertEquals(elc.total, 3);
    d.close();
  }
Example #22
0
  private String associateFile(Source source, String propertyName) throws Exception {
    if (source.hasProperty(propertyName)) {
      source.deleteProperty(propertyName);
    }
    File stats = source.createFileProperty(propertyName);
    DataSource ds = dsf.getDataSource(source.getName());
    ds.open();
    long rc = ds.getRowCount();
    ds.close();

    FileOutputStream fis = new FileOutputStream(stats);
    String rcStr = Long.toString(rc);
    fis.write(rcStr.getBytes());
    fis.close();

    return rcStr;
  }
    @Override
    public void sourceAdded(SourceEvent e) {
      String name = e.getName();
      SourceManager sm = dsf.getSourceManager();
      if (e.isWellKnownName()
          && !sm.getSource(name).isSystemTableSource()
          && !layerMap.containsKey(name)) {
        try {
          Layer layer = new Layer();
          layer.setName(name);
          layer.setTitle(name);

          // Setting the bounding box data
          DataSource ds = dsf.getDataSource(name);
          ds.open();
          Envelope env = ds.getFullExtent();
          CoordinateReferenceSystem crs = ds.getCRS();
          ds.close();
          BoundingBox bBox = getBoundingBox(env, crs);
          if (bBox == null) {
            return;
          }
          layer.getCRS().add(bBox.getCRS());
          layer.getBoundingBox().add(bBox);
          layer.setEXGeographicBoundingBox(getGeographicBoundingBox(env, bBox.getCRS()));
          layer.setQueryable(true);
          if (layerStyles.containsKey(name)) {
            String[] lStyles = layerStyles.get(name);
            for (int i = 0; i < lStyles.length; i++) {
              Style style = new Style();
              String styleName = lStyles[i];
              style.setName(styleName);
              style.setTitle(styleName);
              layer.getStyle().add(style);
            }
          }
          layerMap.put(name, layer);
        } catch (NoSuchTableException ex) {
        } catch (DataSourceCreationException ex) {
        } catch (DriverException ex) {
        }
      }
    }
  @Test
  public void testIndexVisitor() throws Exception {
    DiskRTree tree = new DiskRTree(16, 1024, false);
    tree.newIndex(indexFile);
    DataSource ds = dsf.getDataSource("points");
    String fieldName = "the_geom";

    ds.open();
    int fieldIndex = ds.getFieldIndexByName(fieldName);
    for (int i = 0; i < ds.getRowCount(); i++) {
      Envelope value = ds.getFieldValue(i, fieldIndex).getAsGeometry().getEnvelopeInternal();
      tree.insert(value, i);
    }
    Envelope e = ds.getGeometry((ds.getRowCount() - 1) / 2).getEnvelopeInternal();

    IV iV = new IV(ds);
    tree.query(e, iV);

    assertTrue(iV.fired);
  }
 public void testAlphanumericObjectfailedCommit() throws Exception {
   DataSource ds = dsf.getDataSource("object");
   ds.open();
   failedCommit(ds, new FooQuery());
 }
 public void testAlphanumericDBFailOnWrite() throws Exception {
   DataSource ds = dsf.getDataSource("executeDB");
   ds.open();
   ReadDriver.setCurrentDataSource(ds);
   failedCommit(ds, new FooQuery());
 }
 public void testAlphanumericFileFailOnWrite() throws Exception {
   DataSource ds = dsf.getDataSource("writeFile");
   ds.open();
   failedCommit(ds, new FooQuery());
 }
  public ModifyFrame(String configPath, String choice)
      throws DataSourceCreationException, DriverException {
    super("Urban Sprawl Model - Modify");
    this.configPath = configPath;
    this.oldChoice = choice;

    // parameters reading
    Map<String, Double> parameters = new HashMap<String, Double>();
    DataSourceFactory dsf = new DataSourceFactory();
    File configFile = new File(configPath);
    DataSource configSource = dsf.getDataSource(configFile);
    configSource.open();
    parameters.put(
        "year",
        configSource.getFieldValue(0, configSource.getFieldIndexByName("year")).getAsDouble());
    parameters.put(
        "numberOfTurns",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("numberOfTurns"))
            .getAsDouble());
    parameters.put(
        "bufferSize",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("bufferSize"))
            .getAsDouble());
    parameters.put(
        "amenitiesWeighting",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("amenitiesWeighting"))
            .getAsDouble());
    parameters.put(
        "constructibilityWeighting",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("constructibilityWeighting"))
            .getAsDouble());
    parameters.put(
        "idealhousingWeighting",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("idealhousingWeighting"))
            .getAsDouble());
    parameters.put(
        "gaussDeviation",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("gaussDeviation"))
            .getAsDouble());
    parameters.put(
        "segregationThreshold",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("segregationThreshold"))
            .getAsDouble());
    parameters.put(
        "segregationTolerance",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("segregationTolerance"))
            .getAsDouble());
    parameters.put(
        "householdMemory",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("householdMemory"))
            .getAsDouble());
    parameters.put(
        "movingThreshold",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("movingThreshold"))
            .getAsDouble());
    parameters.put(
        "immigrantNumber",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("immigrantNumber"))
            .getAsDouble());
    parameters.put(
        "threshold_1",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("threshold_1"))
            .getAsDouble());
    parameters.put(
        "threshold_2",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("threshold_2"))
            .getAsDouble());
    parameters.put(
        "threshold_3",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("threshold_3"))
            .getAsDouble());
    parameters.put(
        "threshold_4",
        configSource
            .getFieldValue(0, configSource.getFieldIndexByName("threshold_4"))
            .getAsDouble());
    configSource.close();

    spp = new SpinnerParameterPanel(parameters);
    add(spp, BorderLayout.NORTH);

    // buttons
    JPanel buttonPanel = new JPanel();
    JButton modify = new JButton("Modify");
    JButton cancel = new JButton("Cancel");
    modify.setActionCommand("modify");
    cancel.setActionCommand("cancel");
    modify.addActionListener(this);
    cancel.addActionListener(this);
    buttonPanel.add(modify);
    buttonPanel.add(cancel);

    add(buttonPanel, BorderLayout.SOUTH);
    pack();
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  }
Example #29
0
 private void testListenCommits(DataSource ds) throws DriverException {
   ds.open();
   ds.close();
 }