Skip to content

Commit

Permalink
test showing that translation from sbgn 0.2 does not use proper encod…
Browse files Browse the repository at this point in the history
…ing on machines with encoding different than utf-8 (for instance typical Windows machine)
  • Loading branch information
piotr-gawron committed Apr 4, 2020
1 parent 3c0607f commit 09c8a73
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
52 changes: 52 additions & 0 deletions org.sbgn/test/org/sbgn/SbgnUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.sbgn;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import javax.xml.bind.JAXBException;

import org.junit.Test;
import org.sbgn.bindings.Sbgn;

public class SbgnUtilTest {

@Test
public void testUtfEncodingWithDifferentSystemFileEncoding() throws JAXBException {
try {
// modify the encoding that was taken from file.encoding system property
// this is done at JVM startup therefore instead of changing system property we
// must modify the cached value to simulate running it on the machine with
// ISO-8859-1 encoding
Field field = null;
for (Field f : Charset.class.getDeclaredFields()) {
if (f.getName().equals("defaultCharset")) {
field = f;
}
}
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

field.set(null, StandardCharsets.ISO_8859_1);

try {
Sbgn sbgn = SbgnUtil.readFromFile(new File("test-files/PD/utf8_sbgn.0.2.sbgn"));
assertEquals("β", sbgn.getMap().get(0).getGlyph().get(0).getLabel().getText());
} finally {
//restore default encoding to UTF-8
field.set(null, StandardCharsets.UTF_8);
}

} catch (Exception e) {
e.printStackTrace();
}

}

}
9 changes: 9 additions & 0 deletions test-files/PD/utf8_sbgn.0.2.sbgn
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sbgn xmlns="http://sbgn.org/libsbgn/0.2">
<map language="process description">
<glyph class="simple chemical" id="glyph_n3">
<label text="β"/>
<bbox w="140.0" h="32.0" x="75.0" y="272.53503"/>
</glyph>
</map>
</sbgn>

0 comments on commit 09c8a73

Please sign in to comment.