Skip to content

Commit

Permalink
Set the charset to the writer as well as is done in the reader
Browse files Browse the repository at this point in the history
Update test containing text with special characters

Decode the encoded text containing special characters
  • Loading branch information
emanuelaepure10 committed Jul 6, 2023
1 parent 6817550 commit cf74501
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import java.time.Instant
import java.time.LocalDate
import java.util.function.Consumer

import org.geotools.data.shapefile.ShapefileDataStore
import org.junit.Test
import org.locationtech.jts.geom.Coordinate
import org.locationtech.jts.geom.Geometry
Expand Down Expand Up @@ -184,9 +185,9 @@ class ShapefileInstanceWriterTest {
try {
println "Temporary file is $tmpFile"
writeInstances(tmpFile.toFile(), schema, instances, configurator)
handler.accept(tmpFile.toFile())

testCpgFileAgainstRelatedShapefile(tmpFile.toFile())

handler.accept(tmpFile.toFile())
} finally {
tmpDir.deleteDir()
}
Expand All @@ -213,9 +214,9 @@ class ShapefileInstanceWriterTest {
try {
println "Temporary file is $tmpFile"
writeInstancesWithReporterErrors(tmpFile.toFile(), schema, instances, configurator)
handler.accept(tmpFile.toFile())

testCpgFileAgainstRelatedShapefile(tmpFile.toFile())

handler.accept(tmpFile.toFile())
} finally {
tmpDir.deleteDir()
}
Expand Down Expand Up @@ -294,6 +295,7 @@ class ShapefileInstanceWriterTest {
// load instances again and test

def loaded = loadInstances(file)
ShapefileDataStore shapeFileDataStore = new ShapefileDataStore(file.toURL())

int num = 0
loaded.iterator().withCloseable {
Expand All @@ -311,16 +313,18 @@ class ShapefileInstanceWriterTest {
def jts = the_geom.geometry
assert jts instanceof Point
def name = inst.p.name.value()
assert name
switch (name) {

String dencodedString = new String(name.getBytes(shapeFileDataStore.getCharset()));
assert dencodedString
switch (dencodedString) {
case 'Darmstadt':
assert inst.p.population.value() == 158254
break
case 'München':
assert inst.p.population.value() == 1471508
break
default:
throw new IllegalStateException("Unexpected type $typeName")
throw new IllegalStateException("Unexpected type $dencodedString")
}
}
}
Expand Down Expand Up @@ -367,6 +371,7 @@ class ShapefileInstanceWriterTest {
// load instances again and test

def loaded = loadInstances(file)
ShapefileDataStore shapeFileDataStore = new ShapefileDataStore(file.toURL())

int num = 0
loaded.iterator().withCloseable {
Expand All @@ -386,8 +391,9 @@ class ShapefileInstanceWriterTest {

assert jts instanceof MultiPolygon
def name = inst.p.name.value()
assert name
switch (name) {
String dencodedString = new String(name.getBytes(shapeFileDataStore.getCharset()));
assert dencodedString
switch (dencodedString) {
case 'Darmstadt':
assert inst.p.population.value() == 158254
break
Expand Down Expand Up @@ -440,6 +446,7 @@ class ShapefileInstanceWriterTest {
int num = 0
for (geom in geomNames) {
def loaded = loadInstances(file, geom)
ShapefileDataStore shapeFileDataStore = new ShapefileDataStore(file.toURL())

loaded.iterator().withCloseable {
while (it.hasNext()) {
Expand All @@ -457,8 +464,9 @@ class ShapefileInstanceWriterTest {
def jts = the_geom.geometry
assert jts
def name = inst.p.name.value()
assert name
switch (name) {
String dencodedString = new String(name.getBytes(shapeFileDataStore.getCharset()));
assert dencodedString
switch (dencodedString) {
case 'Darmstadt':
assert inst.p.population.value() == 158254
break
Expand Down Expand Up @@ -1176,6 +1184,7 @@ class ShapefileInstanceWriterTest {
withNewShapefile(schema, instances) { file ->
// load instances again and test
def loaded = loadInstances(file)
ShapefileDataStore shapeFileDataStore = new ShapefileDataStore(file.toURL())

int num = 0
loaded.iterator().withCloseable {
Expand All @@ -1196,8 +1205,9 @@ class ShapefileInstanceWriterTest {
def jts = geom.geometry
assert jts instanceof Point
def name = inst.p.name.value()
assert name
switch (name) {
String dencodedString = new String(name.getBytes(shapeFileDataStore.getCharset()));
assert dencodedString
switch (dencodedString) {
case 'Darmstadt':
assert inst.p.population.value() == 158254
break
Expand Down Expand Up @@ -1357,6 +1367,7 @@ class ShapefileInstanceWriterTest {
// load instances again and test

def loaded = loadInstances(file)
ShapefileDataStore shapeFileDataStore = new ShapefileDataStore(file.toURL())

int num = 0
loaded.iterator().withCloseable {
Expand All @@ -1376,8 +1387,9 @@ class ShapefileInstanceWriterTest {

assert jts instanceof MultiPolygon
def name = inst.p.name.value()
assert name
switch (name) {
String dencodedString = new String(name.getBytes(shapeFileDataStore.getCharset()));
assert dencodedString
switch (dencodedString) {
case 'Darmstadt':
assert inst.p.po12.value() == 158254
break
Expand Down Expand Up @@ -1431,6 +1443,7 @@ class ShapefileInstanceWriterTest {
// load instances again and test

def loaded = loadInstances(file)
ShapefileDataStore shapeFileDataStore = new ShapefileDataStore(file.toURL())

int num = 0
loaded.iterator().withCloseable {
Expand All @@ -1450,8 +1463,9 @@ class ShapefileInstanceWriterTest {

assert jts instanceof MultiPolygon
def name = inst.p.name.value()
assert name
switch (name) {
String dencodedString = new String(name.getBytes(shapeFileDataStore.getCharset()));
assert dencodedString
switch (dencodedString) {
case 'München':
assert inst.p.po12.value() == 1471508
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ private Map<String, Map<String, ShapefileDataStore>> createSchema(URI location,
ShapefileDataStore newDataStore;

newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
newDataStore.setCharset(getCharset());
newDataStore.createSchema(geometryEntry.getValue());
schemaDataStoreMap
.computeIfAbsent(schemaEntry.getKey(),
Expand Down

0 comments on commit cf74501

Please sign in to comment.