Adjust for JDK 9 ea152 and newer: certain whitespaces in XML may now be trimmed

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1781821 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-02-05 22:34:09 +00:00
parent be805a6e66
commit 142e52dfba
1 changed files with 55 additions and 59 deletions

View File

@ -16,17 +16,6 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.hwpf.converter; package org.apache.poi.hwpf.converter;
import static org.apache.poi.POITestCase.assertContains;
import static org.junit.Assert.assertFalse;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.PictureType; import org.apache.poi.hwpf.usermodel.PictureType;
@ -34,53 +23,59 @@ import org.apache.poi.util.XMLHelper;
import org.junit.Test; import org.junit.Test;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import static org.apache.poi.POITestCase.assertContains;
import static org.junit.Assert.assertFalse;
/** /**
* Test cases for {@link WordToHtmlConverter} * Test cases for {@link WordToHtmlConverter}
*/ */
public class TestWordToHtmlConverter public class TestWordToHtmlConverter {
{ private static String getHtmlText(final String sampleFileName) throws Exception {
private static String getHtmlText( final String sampleFileName ) return getHtmlText(sampleFileName, false);
throws Exception
{
return getHtmlText( sampleFileName, false);
} }
private static String getHtmlText( final String sampleFileName, private static String getHtmlText(final String sampleFileName,
boolean emulatePictureStorage ) throws Exception boolean emulatePictureStorage) throws Exception {
{ HWPFDocument hwpfDocument = new HWPFDocument(POIDataSamples
HWPFDocument hwpfDocument = new HWPFDocument( POIDataSamples .getDocumentInstance().openResourceAsStream(sampleFileName));
.getDocumentInstance().openResourceAsStream( sampleFileName ));
Document newDocument = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument(); Document newDocument = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument();
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
newDocument); newDocument);
if ( emulatePictureStorage ) if (emulatePictureStorage)
{ {
wordToHtmlConverter.setPicturesManager( new PicturesManager() wordToHtmlConverter.setPicturesManager(new PicturesManager()
{ {
@Override @Override
public String savePicture( byte[] content, public String savePicture(byte[] content,
PictureType pictureType, String suggestedName, PictureType pictureType, String suggestedName,
float widthInches, float heightInches ) float widthInches, float heightInches)
{ {
return suggestedName; return suggestedName;
} }
}); });
} }
wordToHtmlConverter.processDocument( hwpfDocument); wordToHtmlConverter.processDocument(hwpfDocument);
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance() Transformer transformer = TransformerFactory.newInstance()
.newTransformer(); .newTransformer();
transformer.setOutputProperty( OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty( OutputKeys.METHOD, "html"); transformer.setOutputProperty(OutputKeys.METHOD, "html");
transformer.transform( transformer.transform(
new DOMSource( wordToHtmlConverter.getDocument() ), new DOMSource(wordToHtmlConverter.getDocument()),
new StreamResult( stringWriter )); new StreamResult(stringWriter));
return stringWriter.toString(); return stringWriter.toString();
} }
@ -88,14 +83,14 @@ public class TestWordToHtmlConverter
@Test @Test
public void testAIOOBTap() throws Exception public void testAIOOBTap() throws Exception
{ {
String result = getHtmlText( "AIOOB-Tap.doc"); String result = getHtmlText("AIOOB-Tap.doc");
assertContains(result.substring( 0, 6000 ), "<table class=\"t1\">"); assertContains(result.substring(0, 6000), "<table class=\"t1\">");
} }
@Test @Test
public void testBug33519() throws Exception public void testBug33519() throws Exception
{ {
String result = getHtmlText( "Bug33519.doc"); String result = getHtmlText("Bug33519.doc");
assertContains( assertContains(
result, result,
"\u041F\u043B\u0430\u043D\u0438\u043D\u0441\u043A\u0438 \u0442\u0443\u0440\u043E\u0432\u0435"); "\u041F\u043B\u0430\u043D\u0438\u043D\u0441\u043A\u0438 \u0442\u0443\u0440\u043E\u0432\u0435");
@ -106,7 +101,7 @@ public class TestWordToHtmlConverter
@Test @Test
public void testBug46610_2() throws Exception public void testBug46610_2() throws Exception
{ {
String result = getHtmlText( "Bug46610_2.doc"); String result = getHtmlText("Bug46610_2.doc");
assertContains( assertContains(
result, result,
"012345678911234567892123456789312345678941234567890123456789112345678921234567893123456789412345678"); "012345678911234567892123456789312345678941234567890123456789112345678921234567893123456789412345678");
@ -115,7 +110,7 @@ public class TestWordToHtmlConverter
@Test @Test
public void testBug46817() throws Exception public void testBug46817() throws Exception
{ {
String result = getHtmlText( "Bug46817.doc"); String result = getHtmlText("Bug46817.doc");
final String substring = "<table class=\"t1\">"; final String substring = "<table class=\"t1\">";
assertContains(result, substring); assertContains(result, substring);
} }
@ -123,9 +118,9 @@ public class TestWordToHtmlConverter
@Test @Test
public void testBug47286() throws Exception public void testBug47286() throws Exception
{ {
String result = getHtmlText( "Bug47286.doc"); String result = getHtmlText("Bug47286.doc");
assertFalse(result.contains( "FORMTEXT" )); assertFalse(result.contains("FORMTEXT"));
assertContains(result, "color:#4f6228;"); assertContains(result, "color:#4f6228;");
assertContains(result, "Passport No and the date of expire"); assertContains(result, "Passport No and the date of expire");
@ -135,13 +130,13 @@ public class TestWordToHtmlConverter
@Test @Test
public void testBug48075() throws Exception public void testBug48075() throws Exception
{ {
getHtmlText( "Bug48075.doc"); getHtmlText("Bug48075.doc");
} }
@Test @Test
public void testBug52583() throws Exception public void testBug52583() throws Exception
{ {
String result = getHtmlText( "Bug52583.doc"); String result = getHtmlText("Bug52583.doc");
assertContains( assertContains(
result, result,
"<select><option selected>riri</option><option>fifi</option><option>loulou</option></select>"); "<select><option selected>riri</option><option>fifi</option><option>loulou</option></select>");
@ -150,14 +145,14 @@ public class TestWordToHtmlConverter
@Test @Test
public void testBug53182() throws Exception public void testBug53182() throws Exception
{ {
String result = getHtmlText( "Bug53182.doc"); String result = getHtmlText("Bug53182.doc");
assertFalse(result.contains( "italic" )); assertFalse(result.contains("italic"));
} }
@Test @Test
public void testDocumentProperties() throws Exception public void testDocumentProperties() throws Exception
{ {
String result = getHtmlText( "documentProperties.doc"); String result = getHtmlText("documentProperties.doc");
assertContains(result, "<title>This is document title</title>"); assertContains(result, "<title>This is document title</title>");
assertContains(result, assertContains(result,
@ -167,7 +162,7 @@ public class TestWordToHtmlConverter
@Test @Test
public void testEmailhyperlink() throws Exception public void testEmailhyperlink() throws Exception
{ {
String result = getHtmlText( "Bug47286.doc"); String result = getHtmlText("Bug47286.doc");
final String substring = "provisastpet@mfa.gov.cy"; final String substring = "provisastpet@mfa.gov.cy";
assertContains(result, substring); assertContains(result, substring);
} }
@ -175,21 +170,22 @@ public class TestWordToHtmlConverter
@Test @Test
public void testEndnote() throws Exception public void testEndnote() throws Exception
{ {
String result = getHtmlText( "endingnote.doc"); String result = getHtmlText("endingnote.doc");
assertContains( assertContains(
result, result,
"<a class=\"a1 endnoteanchor\" href=\"#endnote_1\" name=\"endnote_back_1\">1</a>"); "<a class=\"a1 endnoteanchor\" href=\"#endnote_1\" name=\"endnote_back_1\">1</a>");
assertContains( assertContains(
result, // starting with JDK 9 such unimportant whitespaces may be trimmed
"<a class=\"a1 endnoteindex\" href=\"#endnote_back_1\" name=\"endnote_1\">1</a> <span"); result.replace("</a> <span", "</a><span"),
"<a class=\"a1 endnoteindex\" href=\"#endnote_back_1\" name=\"endnote_1\">1</a><span");
assertContains(result, "Ending note text"); assertContains(result, "Ending note text");
} }
@Test @Test
public void testEquation() throws Exception public void testEquation() throws Exception
{ {
String result = getHtmlText( "equation.doc"); String result = getHtmlText("equation.doc");
assertContains(result, "<!--Image link to '0.emf' can be here-->"); assertContains(result, "<!--Image link to '0.emf' can be here-->");
} }
@ -197,7 +193,7 @@ public class TestWordToHtmlConverter
@Test @Test
public void testHyperlink() throws Exception public void testHyperlink() throws Exception
{ {
String result = getHtmlText( "hyperlink.doc"); String result = getHtmlText("hyperlink.doc");
assertContains(result, "<span>Before text; </span><a "); assertContains(result, "<span>Before text; </span><a ");
assertContains(result, assertContains(result,
@ -208,13 +204,13 @@ public class TestWordToHtmlConverter
@Test @Test
public void testInnerTable() throws Exception public void testInnerTable() throws Exception
{ {
getHtmlText( "innertable.doc"); getHtmlText("innertable.doc");
} }
@Test @Test
public void testListsMargins() throws Exception public void testListsMargins() throws Exception
{ {
String result = getHtmlText( "lists-margins.doc"); String result = getHtmlText("lists-margins.doc");
assertContains(result, assertContains(result,
".s1{display: inline-block; text-indent: 0; min-width: 0.4861111in;}"); ".s1{display: inline-block; text-indent: 0; min-width: 0.4861111in;}");
@ -231,13 +227,13 @@ public class TestWordToHtmlConverter
@Test @Test
public void testO_kurs_doc() throws Exception public void testO_kurs_doc() throws Exception
{ {
getHtmlText( "o_kurs.doc"); getHtmlText("o_kurs.doc");
} }
@Test @Test
public void testPageref() throws Exception public void testPageref() throws Exception
{ {
String result = getHtmlText( "pageref.doc"); String result = getHtmlText("pageref.doc");
assertContains(result, "<a href=\"#userref\">"); assertContains(result, "<a href=\"#userref\">");
assertContains(result, "<a name=\"userref\">"); assertContains(result, "<a name=\"userref\">");
@ -247,7 +243,7 @@ public class TestWordToHtmlConverter
@Test @Test
public void testPicture() throws Exception public void testPicture() throws Exception
{ {
String result = getHtmlText( "picture.doc", true); String result = getHtmlText("picture.doc", true);
// picture // picture
assertContains(result, "src=\"0.emf\""); assertContains(result, "src=\"0.emf\"");
@ -262,7 +258,7 @@ public class TestWordToHtmlConverter
@Test @Test
public void testPicturesEscher() throws Exception public void testPicturesEscher() throws Exception
{ {
String result = getHtmlText( "pictures_escher.doc", true); String result = getHtmlText("pictures_escher.doc", true);
assertContains(result, "<img src=\"s0.PNG\">"); assertContains(result, "<img src=\"s0.PNG\">");
assertContains(result, "<img src=\"s808.PNG\">"); assertContains(result, "<img src=\"s808.PNG\">");
} }
@ -270,7 +266,7 @@ public class TestWordToHtmlConverter
@Test @Test
public void testTableMerges() throws Exception public void testTableMerges() throws Exception
{ {
String result = getHtmlText( "table-merges.doc"); String result = getHtmlText("table-merges.doc");
assertContains(result, "<td class=\"td1\" colspan=\"3\">"); assertContains(result, "<td class=\"td1\" colspan=\"3\">");
assertContains(result, "<td class=\"td2\" colspan=\"2\">"); assertContains(result, "<td class=\"td2\" colspan=\"2\">");
@ -278,9 +274,9 @@ public class TestWordToHtmlConverter
@Test @Test
public void testBug52420() throws Exception { public void testBug52420() throws Exception {
String result = getHtmlText( "52420.doc"); String result = getHtmlText("52420.doc");
assertFalse(result.contains( "FORMTEXT" )); assertFalse(result.contains("FORMTEXT"));
assertContains(result, "\u0417\u0410\u0414\u0410\u041d\u0418\u0415"); assertContains(result, "\u0417\u0410\u0414\u0410\u041d\u0418\u0415");
assertContains(result, "\u041f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u044c"); assertContains(result, "\u041f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u044c");