Skip to content

Commit

Permalink
Fix a few known problems related to Android
Browse files Browse the repository at this point in the history
* Different fields order in JSON serialization
* Ignore veraPdf validator
* Different behavior of DeflaterOutputStream
* Ignore test with OutOfMemory exception
* Fix different behavior of ZipFileWriter\Reader

DEVSIX-7066
  • Loading branch information
introfog committed Feb 16, 2023
1 parent 940875c commit 3b87cf8
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 11 deletions.
48 changes: 40 additions & 8 deletions commons/src/test/java/com/itextpdf/commons/utils/JsonUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public class JsonUtilTest extends ExtendedITextTest {

private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/commons/utils/JsonUtilTest/";

private static boolean isRunOnJava = false;

// Android-Excise-Start
static {
isRunOnJava = true;
}
// Android-Excise-End

@Test
public void utf8CharsetStringTest() {
Assert.assertEquals("\"©\"", JsonUtil.serializeToString("©"));
Expand All @@ -77,9 +85,15 @@ public void serializeInstanceWithEnumStringTest() throws IOException {

@Test
public void serializeInstanceWithEnumStreamTest() throws IOException {
String path = SOURCE_FOLDER + "classWithEnum.json";
String cmp;
if (isRunOnJava) {
cmp = SOURCE_FOLDER + "classWithEnum.json";
} else {
// Test is run on Android, so field order will be different from Java.
cmp = SOURCE_FOLDER + "classWithEnumAndroid.json";
}

try (InputStream inputStream = FileUtil.getInputStreamForFile(path);
try (InputStream inputStream = FileUtil.getInputStreamForFile(cmp);
ByteArrayOutputStream baos = convertInputStreamToOutput(inputStream);
ByteArrayOutputStream serializationResult = new ByteArrayOutputStream()) {
JsonUtil.serializeToStream(serializationResult, createClassWithEnumObject());
Expand All @@ -102,9 +116,15 @@ public void serializeToMinimalInstanceWithEnumStringTest() throws IOException {

@Test
public void serializeToMinimalInstanceWithEnumStreamTest() throws IOException {
String path = SOURCE_FOLDER + "minimalClassWithEnum.json";
String cmp;
if (isRunOnJava) {
cmp = SOURCE_FOLDER + "minimalClassWithEnum.json";
} else {
// Test is run on Android, so field order will be different from Java.
cmp = SOURCE_FOLDER + "minimalClassWithEnumAndroid.json";
}

try (InputStream inputStream = FileUtil.getInputStreamForFile(path);
try (InputStream inputStream = FileUtil.getInputStreamForFile(cmp);
ByteArrayOutputStream baos = convertInputStreamToOutput(inputStream);
ByteArrayOutputStream serializationResult = new ByteArrayOutputStream()) {
JsonUtil.serializeToMinimalStream(serializationResult, createClassWithEnumObject());
Expand Down Expand Up @@ -175,9 +195,15 @@ public void serializeComplexStructureStringTest() throws IOException {

@Test
public void serializeComplexStructureStreamTest() throws IOException {
String path = SOURCE_FOLDER + "complexStructure.json";
String cmp;
if (isRunOnJava) {
cmp = SOURCE_FOLDER + "complexStructure.json";
} else {
// Test is run on Android, so field order will be different from Java.
cmp = SOURCE_FOLDER + "complexStructureAndroid.json";
}

try (InputStream inputStream = FileUtil.getInputStreamForFile(path);
try (InputStream inputStream = FileUtil.getInputStreamForFile(cmp);
ByteArrayOutputStream baos = convertInputStreamToOutput(inputStream);
ByteArrayOutputStream serializationResult = new ByteArrayOutputStream()) {
JsonUtil.serializeToStream(serializationResult, createComplexStructureObject());
Expand All @@ -200,9 +226,15 @@ public void serializeToMinimalComplexStructureStringTest() throws IOException {

@Test
public void serializeToMinimalComplexStructureStreamTest() throws IOException {
String path = SOURCE_FOLDER + "minimalComplexStructure.json";
String cmp;
if (isRunOnJava) {
cmp = SOURCE_FOLDER + "minimalComplexStructure.json";
} else {
// Test is run on Android, so field order will be different from Java.
cmp = SOURCE_FOLDER + "minimalComplexStructureAndroid.json";
}

try (InputStream inputStream = FileUtil.getInputStreamForFile(path);
try (InputStream inputStream = FileUtil.getInputStreamForFile(cmp);
ByteArrayOutputStream baos = convertInputStreamToOutput(inputStream);
ByteArrayOutputStream serializationResult = new ByteArrayOutputStream()) {
JsonUtil.serializeToMinimalStream(serializationResult, createComplexStructureObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void constructorWithNonZipPathTest() {
}

@Test
// Android-Ignore (TODO DEVSIX-6906 fix different behavior of ZipFileWriter\Reader)
public void getFileNamesFromEmptyZipTest() throws IOException {
try (ZipFileReader fileReader = new ZipFileReader(SOURCE_FOLDER + "emptyZip.zip")) {
Set<String> nameSet = fileReader.getFileNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void constructorWithDirectoryPathTest() throws IOException {
}

@Test
// Android-Ignore (TODO DEVSIX-6906 fix different behavior of ZipFileWriter\Reader)
public void emptyZipCreationTest() throws IOException {
final String pathToFile = DESTINATION_FOLDER + "emptyZipCreation.zip";

Expand All @@ -113,6 +114,7 @@ public void emptyZipCreationTest() throws IOException {
}

@Test
// Android-Ignore (TODO DEVSIX-6906 fix different behavior of ZipFileWriter\Reader)
public void addNullFileEntryTest() throws IOException {
final String pathToFile = DESTINATION_FOLDER + "addNullFileEntry.zip";

Expand All @@ -124,6 +126,7 @@ public void addNullFileEntryTest() throws IOException {
}

@Test
// Android-Ignore (TODO DEVSIX-6906 fix different behavior of ZipFileWriter\Reader)
public void addEntryWithNotExistingFileTest() throws IOException {
try (ZipFileWriter writer = new ZipFileWriter(
DESTINATION_FOLDER + "addEntryWithNotExistingFile.zip")) {
Expand All @@ -133,6 +136,7 @@ public void addEntryWithNotExistingFileTest() throws IOException {
}

@Test
// Android-Ignore (TODO DEVSIX-6906 fix different behavior of ZipFileWriter\Reader)
public void addNullStreamEntryTest() throws IOException {
final String pathToFile = DESTINATION_FOLDER + "addNullStreamEntry.zip";

Expand All @@ -144,6 +148,7 @@ public void addNullStreamEntryTest() throws IOException {
}

@Test
// Android-Ignore (TODO DEVSIX-6906 fix different behavior of ZipFileWriter\Reader)
public void addNullJsonEntryTest() throws IOException {
final String pathToFile = DESTINATION_FOLDER + "addNullJsonEntry.zip";

Expand All @@ -155,6 +160,7 @@ public void addNullJsonEntryTest() throws IOException {
}

@Test
// Android-Ignore (TODO DEVSIX-6906 fix different behavior of ZipFileWriter\Reader)
public void addEntryWhenWriterIsClosedTest() throws IOException {
final String pathToFile = DESTINATION_FOLDER + "addEntryWhenWriterIsClosed.zip";

Expand Down Expand Up @@ -276,6 +282,7 @@ public void addSeveralEntriesToZipTest() throws IOException {
}

@Test
// Android-Ignore (TODO DEVSIX-6906 fix different behavior of ZipFileWriter\Reader)
public void addEntryWithNullFileNameTest() throws IOException {
final String pathToFile = DESTINATION_FOLDER + "addEntryWithNullFileName.zip";
final String firstTextFilePath = SOURCE_FOLDER + "testFile.txt";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"enumArray": [
"FIRST_VALUE",
"FIRST_VALUE",
"SECOND_VALUE"
],
"firstValue": "SECOND_VALUE"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"childsMap": {
"ChildMapkey": {
"arrayStr": [
"someStr1",
"someStr2"
],
"grandsons": [
{
"integer": 13,
"name": "someName"
},
{
"integer": 0,
"name": ""
}
]
},
"ChildMapKey2": {
"arrayStr": [
""
],
"grandsons": [
{
"integer": 0,
"name": ""
}
]
}
},
"map": {
"FirstMapKey": 15,
"SecondMapKey": 8
},
"str": "StringFieldValue"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"enumArray":["FIRST_VALUE","FIRST_VALUE","SECOND_VALUE"],"firstValue":"SECOND_VALUE"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"childsMap":{"ChildMapkey":{"arrayStr":["someStr1","someStr2"],"grandsons":[{"integer":13,"name":"someName"},{"integer":0,"name":""}]},"ChildMapKey2":{"arrayStr":[""],"grandsons":[{"integer":0,"name":""}]}},"map":{"FirstMapKey":15,"SecondMapKey":8},"str":"StringFieldValue"}
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,14 @@ public void testJPXDecode() throws Exception {

@Test
// TODO: DEVSIX-3538 (update test after fix)
// Android-Ignore (TODO DEVSIX-7079 increase memory available for virtual machine while test running)
public void testSeparationCSWithICCBasedAsAlternative() throws Exception {
testFile("separationCSWithICCBasedAsAlternative.pdf", "Im1", "png");
}

@Test
// TODO: DEVSIX-3538 (update test after fix)
// Android-Ignore (TODO DEVSIX-6445 fix different DeflaterOutputStream behavior)
public void testSeparationCSWithDeviceCMYKAsAlternative() throws Exception {
Assert.assertThrows(UnsupportedOperationException.class, () ->
{
Expand All @@ -185,12 +187,14 @@ public void testGrayScalePng() throws Exception {

@Test
// TODO: DEVSIX-3538 (update test after fix)
// Android-Ignore (TODO DEVSIX-6445 fix different DeflaterOutputStream behavior)
public void testSeparationCSWithDeviceRGBAsAlternative() throws Exception {
testFile("separationCSWithDeviceRgbAsAlternative.pdf", "Im1", "png");
}

@Test
// TODO: DEVSIX-3538 (update test after fix)
// Android-Ignore (TODO DEVSIX-6445 fix different DeflaterOutputStream behavior)
public void testSeparationCSWithDeviceRGBAsAlternative2() throws Exception {
testFile("spotColorImagesSmall.pdf", "Im1", "png");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ This file is part of the iText (R) project.
package com.itextpdf.test;

import com.itextpdf.test.annotations.type.UnitTest;
import com.itextpdf.test.pdfa.VeraPdfValidator;
import com.itextpdf.test.pdfa.VeraPdfValidator; // Android-Skip
import com.itextpdf.test.utils.FileUtil;

import java.io.IOException;
Expand Down Expand Up @@ -76,9 +76,9 @@ public void checkValidatorLogsTest() throws IOException {
+ "WARNING: Invalid embedded cff font. Charset range exceeds number of glyphs\n"
+ "WARNING: Missing OutputConditionIdentifier in an output intent dictionary\n"
+ "WARNING: The Top DICT does not begin with ROS operator";
Assert.assertEquals(expectedWarningsForFileWithWarnings, new VeraPdfValidator().validate(DESTINATION_FOLDER + fileNameWithWarnings));
Assert.assertEquals(expectedWarningsForFileWithWarnings, new VeraPdfValidator().validate(DESTINATION_FOLDER + fileNameWithWarnings)); // Android-Skip

//We check that the logs are empty after the first check
Assert.assertNull(new VeraPdfValidator().validate(DESTINATION_FOLDER + fileNameWithoutWarnings));
Assert.assertNull(new VeraPdfValidator().validate(DESTINATION_FOLDER + fileNameWithoutWarnings)); // Android-Skip
}
}

0 comments on commit 3b87cf8

Please sign in to comment.