Fix inconsistent line endings and some eclipse warnings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1695757 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-08-13 19:20:58 +00:00
parent b21dd0241e
commit 0588fdec98
14 changed files with 1217 additions and 1226 deletions

View File

@ -1,71 +1,68 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership. this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0 The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.junit.Test; import org.junit.Test;
public class TestChangeTracking { public class TestChangeTracking {
@Test
@Test public void detection() throws Exception {
public void detection() throws Exception { XWPFDocument documentWithoutChangeTracking = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_off.docx");
assertFalse(documentWithoutChangeTracking.isTrackRevisions());
XWPFDocument documentWithoutChangeTracking = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_off.docx");
assertFalse(documentWithoutChangeTracking.isTrackRevisions()); XWPFDocument documentWithChangeTracking = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_on.docx");
assertTrue(documentWithChangeTracking.isTrackRevisions());
XWPFDocument documentWithChangeTracking = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_on.docx"); }
assertTrue(documentWithChangeTracking.isTrackRevisions());
@Test
} public void activateChangeTracking() throws Exception {
XWPFDocument document = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_off.docx");
@Test assertFalse(document.isTrackRevisions());
public void activateChangeTracking() throws Exception {
XWPFDocument document = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_off.docx"); document.setTrackRevisions(true);
assertFalse(document.isTrackRevisions());
assertTrue(document.isTrackRevisions());
document.setTrackRevisions(true); }
assertTrue(document.isTrackRevisions()); @Test
} @SuppressWarnings("resource")
public void integration() throws Exception {
@Test XWPFDocument doc = new XWPFDocument();
public void integration() throws Exception {
XWPFDocument doc = new XWPFDocument(); XWPFParagraph p1 = doc.createParagraph();
XWPFParagraph p1 = doc.createParagraph(); XWPFRun r1 = p1.createRun();
r1.setText("Lorem ipsum dolor sit amet.");
XWPFRun r1 = p1.createRun(); doc.setTrackRevisions(true);
r1.setText("Lorem ipsum dolor sit amet.");
doc.setTrackRevisions(true); ByteArrayOutputStream out = new ByteArrayOutputStream();
doc.write(out);
ByteArrayOutputStream out = new ByteArrayOutputStream();
doc.write(out); ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
XWPFDocument document = new XWPFDocument(inputStream);
ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray()); inputStream.close();
XWPFDocument document = new XWPFDocument(inputStream);
inputStream.close(); assertTrue(document.isTrackRevisions());
}
assertTrue(document.isTrackRevisions()); }
}
}

View File

@ -1,104 +1,104 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership. this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0 The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFRun.FontCharRange; import org.apache.poi.xwpf.usermodel.XWPFRun.FontCharRange;
import org.junit.Test; import org.junit.Test;
public class TestXWPFBugs { public class TestXWPFBugs {
@Test @Test
public void bug55802() throws Exception { public void bug55802() throws Exception {
String blabla = String blabla =
"Bir, iki, \u00fc\u00e7, d\u00f6rt, be\u015f,\n" + "Bir, iki, \u00fc\u00e7, d\u00f6rt, be\u015f,\n" +
"\nalt\u0131, yedi, sekiz, dokuz, on.\n" + "\nalt\u0131, yedi, sekiz, dokuz, on.\n" +
"\nK\u0131rm\u0131z\u0131 don,\n" + "\nK\u0131rm\u0131z\u0131 don,\n" +
"\ngel bizim bah\u00e7eye kon,\n" + "\ngel bizim bah\u00e7eye kon,\n" +
"\nsar\u0131 limon"; "\nsar\u0131 limon";
XWPFDocument doc = new XWPFDocument(); XWPFDocument doc = new XWPFDocument();
XWPFRun run = doc.createParagraph().createRun(); XWPFRun run = doc.createParagraph().createRun();
for (String str : blabla.split("\n")) { for (String str : blabla.split("\n")) {
run.setText(str); run.setText(str);
run.addBreak(); run.addBreak();
} }
run.setFontFamily("Times New Roman"); run.setFontFamily("Times New Roman");
run.setFontSize(20); run.setFontSize(20);
assertEquals(run.getFontFamily(), "Times New Roman"); assertEquals(run.getFontFamily(), "Times New Roman");
assertEquals(run.getFontFamily(FontCharRange.cs), "Times New Roman"); assertEquals(run.getFontFamily(FontCharRange.cs), "Times New Roman");
assertEquals(run.getFontFamily(FontCharRange.eastAsia), "Times New Roman"); assertEquals(run.getFontFamily(FontCharRange.eastAsia), "Times New Roman");
assertEquals(run.getFontFamily(FontCharRange.hAnsi), "Times New Roman"); assertEquals(run.getFontFamily(FontCharRange.hAnsi), "Times New Roman");
run.setFontFamily("Arial", FontCharRange.hAnsi); run.setFontFamily("Arial", FontCharRange.hAnsi);
assertEquals(run.getFontFamily(FontCharRange.hAnsi), "Arial"); assertEquals(run.getFontFamily(FontCharRange.hAnsi), "Arial");
} }
@Test @Test
public void bug57312_NullPointException() throws IOException { public void bug57312_NullPointException() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("57312.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("57312.docx");
assertNotNull(doc); assertNotNull(doc);
for (IBodyElement bodyElement : doc.getBodyElements()) { for (IBodyElement bodyElement : doc.getBodyElements()) {
BodyElementType elementType = bodyElement.getElementType(); BodyElementType elementType = bodyElement.getElementType();
if (elementType == BodyElementType.PARAGRAPH) { if (elementType == BodyElementType.PARAGRAPH) {
XWPFParagraph paragraph = (XWPFParagraph) bodyElement; XWPFParagraph paragraph = (XWPFParagraph) bodyElement;
for (IRunElement iRunElem : paragraph.getIRuns()) { for (IRunElement iRunElem : paragraph.getIRuns()) {
if (iRunElem instanceof XWPFRun) { if (iRunElem instanceof XWPFRun) {
XWPFRun runElement = (XWPFRun) iRunElem; XWPFRun runElement = (XWPFRun) iRunElem;
UnderlinePatterns underline = runElement.getUnderline(); UnderlinePatterns underline = runElement.getUnderline();
assertNotNull(underline); assertNotNull(underline);
//System.out.println("Found: " + underline + ": " + runElement.getText(0)); //System.out.println("Found: " + underline + ": " + runElement.getText(0));
} }
} }
} }
} }
} }
@Test @Test
public void test56392() throws IOException, OpenXML4JException { public void test56392() throws IOException, OpenXML4JException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("56392.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("56392.docx");
assertNotNull(doc); assertNotNull(doc);
} }
/** /**
* Removing a run needs to remove it from both Runs and IRuns * Removing a run needs to remove it from both Runs and IRuns
*/ */
@Test @Test
public void test57829() throws Exception { public void test57829() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
assertNotNull(doc); assertNotNull(doc);
assertEquals(3, doc.getParagraphs().size()); assertEquals(3, doc.getParagraphs().size());
for (XWPFParagraph paragraph : doc.getParagraphs()) { for (XWPFParagraph paragraph : doc.getParagraphs()) {
paragraph.removeRun(0); paragraph.removeRun(0);
assertNotNull(paragraph.getText()); assertNotNull(paragraph.getText());
} }
} }
} }

View File

@ -19,50 +19,50 @@ package org.apache.poi.xwpf.usermodel;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.List; import java.util.List;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFtnEdn; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFtnEdn;
public class TestXWPFFootnotes extends TestCase { public class TestXWPFFootnotes extends TestCase {
public void testAddFootnotesToDocument() throws IOException { public void testAddFootnotesToDocument() throws IOException {
XWPFDocument docOut = new XWPFDocument(); XWPFDocument docOut = new XWPFDocument();
BigInteger noteId = BigInteger.valueOf(1); BigInteger noteId = BigInteger.valueOf(1);
XWPFFootnotes footnotes = docOut.createFootnotes(); XWPFFootnotes footnotes = docOut.createFootnotes();
CTFtnEdn ctNote = CTFtnEdn.Factory.newInstance(); CTFtnEdn ctNote = CTFtnEdn.Factory.newInstance();
ctNote.setId(noteId); ctNote.setId(noteId);
ctNote.setType(STFtnEdn.NORMAL); ctNote.setType(STFtnEdn.NORMAL);
footnotes.addFootnote(ctNote); footnotes.addFootnote(ctNote);
XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut); XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
XWPFFootnote note = docIn.getFootnoteByID(noteId.intValue()); XWPFFootnote note = docIn.getFootnoteByID(noteId.intValue());
assertEquals(note.getCTFtnEdn().getType(), STFtnEdn.NORMAL); assertEquals(note.getCTFtnEdn().getType(), STFtnEdn.NORMAL);
} }
/** /**
* Bug 55066 - avoid double loading the footnotes * Bug 55066 - avoid double loading the footnotes
*/ */
public void testLoadFootnotesOnce() throws IOException { public void testLoadFootnotesOnce() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx");
List<XWPFFootnote> footnotes = doc.getFootnotes(); List<XWPFFootnote> footnotes = doc.getFootnotes();
int hits = 0; int hits = 0;
for (XWPFFootnote fn : footnotes) { for (XWPFFootnote fn : footnotes) {
for (IBodyElement e : fn.getBodyElements()) { for (IBodyElement e : fn.getBodyElements()) {
if (e instanceof XWPFParagraph) { if (e instanceof XWPFParagraph) {
String txt = ((XWPFParagraph) e).getText(); String txt = ((XWPFParagraph) e).getText();
if (txt.indexOf("Footnote_sdt") > -1) { if (txt.indexOf("Footnote_sdt") > -1) {
hits++; hits++;
} }
} }
} }
} }
assertEquals("Load footnotes once", 1, hits); assertEquals("Load footnotes once", 1, hits);
} }
} }

View File

@ -17,12 +17,12 @@
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import java.io.IOException; import java.io.IOException;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
@ -158,25 +158,25 @@ public final class TestXWPFHeader extends TestCase {
assertEquals("First paragraph for the footer", paras[0].getText()); assertEquals("First paragraph for the footer", paras[0].getText());
assertEquals("Second paragraph for the footer", paras[1].getText()); assertEquals("Second paragraph for the footer", paras[1].getText());
} }
public void testSetWatermark() throws IOException { public void testSetWatermark() throws IOException {
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx"); XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
// No header is set (yet) // No header is set (yet)
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
assertNull(policy.getDefaultHeader()); assertNull(policy.getDefaultHeader());
assertNull(policy.getFirstPageHeader()); assertNull(policy.getFirstPageHeader());
assertNull(policy.getDefaultFooter()); assertNull(policy.getDefaultFooter());
policy.createWatermark("DRAFT"); policy.createWatermark("DRAFT");
assertNotNull(policy.getDefaultHeader()); assertNotNull(policy.getDefaultHeader());
assertNotNull(policy.getFirstPageHeader()); assertNotNull(policy.getFirstPageHeader());
assertNotNull(policy.getEvenPageHeader()); assertNotNull(policy.getEvenPageHeader());
// Re-open, and check // Re-open, and check
XWPFDocument reopened = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc); XWPFDocument reopened = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
policy = reopened.getHeaderFooterPolicy(); policy = reopened.getHeaderFooterPolicy();
assertNotNull(policy.getDefaultHeader()); assertNotNull(policy.getDefaultHeader());
assertNotNull(policy.getFirstPageHeader()); assertNotNull(policy.getFirstPageHeader());

View File

@ -16,38 +16,38 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import java.io.IOException; import java.io.IOException;
import junit.framework.TestCase;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
/** import junit.framework.TestCase;
* @author Paolo Mottadelli import org.apache.poi.xwpf.XWPFTestDataSamples;
*/ import org.apache.xmlbeans.XmlException;
public final class TestXWPFHeadings extends TestCase { import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
private static final String HEADING1 = "Heading1";
/**
public void testSetParagraphStyle() throws IOException, XmlException { * @author Paolo Mottadelli
//new clean instance of paragraph */
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("heading123.docx"); public final class TestXWPFHeadings extends TestCase {
XWPFParagraph p = doc.createParagraph(); private static final String HEADING1 = "Heading1";
XWPFRun run = p.createRun();
run.setText("Heading 1"); public void testSetParagraphStyle() throws IOException, XmlException {
//new clean instance of paragraph
CTSdtBlock block = doc.getDocument().getBody().addNewSdt(); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("heading123.docx");
XWPFParagraph p = doc.createParagraph();
assertNull(p.getStyle()); XWPFRun run = p.createRun();
p.setStyle(HEADING1); run.setText("Heading 1");
assertEquals(HEADING1, p.getCTP().getPPr().getPStyle().getVal());
CTSdtBlock block = doc.getDocument().getBody().addNewSdt();
doc.createTOC();
/* assertNull(p.getStyle());
// TODO - finish this test p.setStyle(HEADING1);
if (false) { assertEquals(HEADING1, p.getCTP().getPPr().getPStyle().getVal());
CTStyles styles = doc.getStyle();
CTStyle style = styles.addNewStyle(); doc.createTOC();
/*
// TODO - finish this test
if (false) {
CTStyles styles = doc.getStyle();
CTStyle style = styles.addNewStyle();
style.setType(STStyleType.PARAGRAPH); style.setType(STStyleType.PARAGRAPH);
style.setStyleId("Heading1"); style.setStyleId("Heading1");
} }

View File

@ -18,85 +18,85 @@
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNum; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNum;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumLvl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumLvl;
public class TestXWPFNumbering extends TestCase { public class TestXWPFNumbering extends TestCase {
public void testCompareAbstractNum() throws IOException { public void testCompareAbstractNum() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx");
XWPFNumbering numbering = doc.getNumbering(); XWPFNumbering numbering = doc.getNumbering();
BigInteger numId = BigInteger.valueOf(1); BigInteger numId = BigInteger.valueOf(1);
assertTrue(numbering.numExist(numId)); assertTrue(numbering.numExist(numId));
XWPFNum num = numbering.getNum(numId); XWPFNum num = numbering.getNum(numId);
BigInteger abstrNumId = num.getCTNum().getAbstractNumId().getVal(); BigInteger abstrNumId = num.getCTNum().getAbstractNumId().getVal();
XWPFAbstractNum abstractNum = numbering.getAbstractNum(abstrNumId); XWPFAbstractNum abstractNum = numbering.getAbstractNum(abstrNumId);
BigInteger compareAbstractNum = numbering.getIdOfAbstractNum(abstractNum); BigInteger compareAbstractNum = numbering.getIdOfAbstractNum(abstractNum);
assertEquals(abstrNumId, compareAbstractNum); assertEquals(abstrNumId, compareAbstractNum);
} }
public void testAddNumberingToDoc() throws IOException { public void testAddNumberingToDoc() throws IOException {
BigInteger abstractNumId = BigInteger.valueOf(1); BigInteger abstractNumId = BigInteger.valueOf(1);
BigInteger numId = BigInteger.valueOf(1); BigInteger numId = BigInteger.valueOf(1);
XWPFDocument docOut = new XWPFDocument(); XWPFDocument docOut = new XWPFDocument();
XWPFNumbering numbering = docOut.createNumbering(); XWPFNumbering numbering = docOut.createNumbering();
numId = numbering.addNum(abstractNumId); numId = numbering.addNum(abstractNumId);
XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut); XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
numbering = docIn.getNumbering(); numbering = docIn.getNumbering();
assertTrue(numbering.numExist(numId)); assertTrue(numbering.numExist(numId));
XWPFNum num = numbering.getNum(numId); XWPFNum num = numbering.getNum(numId);
BigInteger compareAbstractNum = num.getCTNum().getAbstractNumId().getVal(); BigInteger compareAbstractNum = num.getCTNum().getAbstractNumId().getVal();
assertEquals(abstractNumId, compareAbstractNum); assertEquals(abstractNumId, compareAbstractNum);
} }
public void testGetNumIlvl() throws IOException { public void testGetNumIlvl() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx");
BigInteger numIlvl = BigInteger.valueOf(0); BigInteger numIlvl = BigInteger.valueOf(0);
assertEquals(numIlvl, doc.getParagraphs().get(0).getNumIlvl()); assertEquals(numIlvl, doc.getParagraphs().get(0).getNumIlvl());
numIlvl = BigInteger.valueOf(1); numIlvl = BigInteger.valueOf(1);
assertEquals(numIlvl, doc.getParagraphs().get(5).getNumIlvl()); assertEquals(numIlvl, doc.getParagraphs().get(5).getNumIlvl());
} }
public void testGetNumFmt() throws IOException { public void testGetNumFmt() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx");
assertEquals("bullet", doc.getParagraphs().get(0).getNumFmt()); assertEquals("bullet", doc.getParagraphs().get(0).getNumFmt());
assertEquals("bullet", doc.getParagraphs().get(1).getNumFmt()); assertEquals("bullet", doc.getParagraphs().get(1).getNumFmt());
assertEquals("bullet", doc.getParagraphs().get(2).getNumFmt()); assertEquals("bullet", doc.getParagraphs().get(2).getNumFmt());
assertEquals("bullet", doc.getParagraphs().get(3).getNumFmt()); assertEquals("bullet", doc.getParagraphs().get(3).getNumFmt());
assertEquals("decimal", doc.getParagraphs().get(4).getNumFmt()); assertEquals("decimal", doc.getParagraphs().get(4).getNumFmt());
assertEquals("lowerLetter", doc.getParagraphs().get(5).getNumFmt()); assertEquals("lowerLetter", doc.getParagraphs().get(5).getNumFmt());
assertEquals("lowerRoman", doc.getParagraphs().get(6).getNumFmt()); assertEquals("lowerRoman", doc.getParagraphs().get(6).getNumFmt());
} }
public void testLvlText() throws IOException { public void testLvlText() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx");
assertEquals("%1.%2.%3.", doc.getParagraphs().get(12).getNumLevelText()); assertEquals("%1.%2.%3.", doc.getParagraphs().get(12).getNumLevelText());
assertEquals("NEW-%1-FORMAT", doc.getParagraphs().get(14).getNumLevelText()); assertEquals("NEW-%1-FORMAT", doc.getParagraphs().get(14).getNumLevelText());
XWPFParagraph p = doc.getParagraphs().get(18); XWPFParagraph p = doc.getParagraphs().get(18);
assertEquals("%1.", p.getNumLevelText()); assertEquals("%1.", p.getNumLevelText());
//test that null doesn't throw NPE //test that null doesn't throw NPE
assertNull(p.getNumFmt()); assertNull(p.getNumFmt());
} }
public void testOverrideList() throws IOException { public void testOverrideList() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("NumberingWOverrides.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("NumberingWOverrides.docx");
XWPFParagraph p = doc.getParagraphs().get(4); XWPFParagraph p = doc.getParagraphs().get(4);
XWPFNumbering numbering = doc.getNumbering(); XWPFNumbering numbering = doc.getNumbering();
CTNum ctNum = numbering.getNum(p.getNumID()).getCTNum(); CTNum ctNum = numbering.getNum(p.getNumID()).getCTNum();
assertEquals(9, ctNum.sizeOfLvlOverrideArray()); assertEquals(9, ctNum.sizeOfLvlOverrideArray());
CTNumLvl ctNumLvl = ctNum.getLvlOverrideArray(0); CTNumLvl ctNumLvl = ctNum.getLvlOverrideArray(0);
assertEquals("upperLetter", ctNumLvl.getLvl().getNumFmt().getVal().toString()); assertEquals("upperLetter", ctNumLvl.getLvl().getNumFmt().getVal().toString());
} }
} }

View File

@ -20,129 +20,129 @@ package org.apache.poi.xwpf.usermodel;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.xssf.usermodel.XSSFRelation; import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
public class TestXWPFPictureData extends TestCase { public class TestXWPFPictureData extends TestCase {
public void testRead() throws InvalidFormatException, IOException { public void testRead() throws InvalidFormatException, IOException {
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx"); XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx");
List<XWPFPictureData> pictures = sampleDoc.getAllPictures(); List<XWPFPictureData> pictures = sampleDoc.getAllPictures();
assertEquals(5, pictures.size()); assertEquals(5, pictures.size());
String[] ext = {"wmf", "png", "emf", "emf", "jpeg"}; String[] ext = {"wmf", "png", "emf", "emf", "jpeg"};
for (int i = 0; i < pictures.size(); i++) { for (int i = 0; i < pictures.size(); i++) {
assertEquals(ext[i], pictures.get(i).suggestFileExtension()); assertEquals(ext[i], pictures.get(i).suggestFileExtension());
} }
int num = pictures.size(); int num = pictures.size();
byte[] pictureData = XWPFTestDataSamples.getImage("nature1.jpg"); byte[] pictureData = XWPFTestDataSamples.getImage("nature1.jpg");
String relationId = sampleDoc.addPictureData(pictureData, XWPFDocument.PICTURE_TYPE_JPEG); String relationId = sampleDoc.addPictureData(pictureData, XWPFDocument.PICTURE_TYPE_JPEG);
// picture list was updated // picture list was updated
assertEquals(num + 1, pictures.size()); assertEquals(num + 1, pictures.size());
XWPFPictureData pict = (XWPFPictureData) sampleDoc.getRelationById(relationId); XWPFPictureData pict = (XWPFPictureData) sampleDoc.getRelationById(relationId);
assertEquals("jpeg", pict.suggestFileExtension()); assertEquals("jpeg", pict.suggestFileExtension());
assertArrayEquals(pictureData, pict.getData()); assertArrayEquals(pictureData, pict.getData());
} }
public void testPictureInHeader() throws IOException { public void testPictureInHeader() throws IOException {
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx"); XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
verifyOneHeaderPicture(sampleDoc); verifyOneHeaderPicture(sampleDoc);
XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc); XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
verifyOneHeaderPicture(readBack); verifyOneHeaderPicture(readBack);
} }
private void verifyOneHeaderPicture(XWPFDocument sampleDoc) { private void verifyOneHeaderPicture(XWPFDocument sampleDoc) {
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
XWPFHeader header = policy.getDefaultHeader(); XWPFHeader header = policy.getDefaultHeader();
List<XWPFPictureData> pictures = header.getAllPictures(); List<XWPFPictureData> pictures = header.getAllPictures();
assertEquals(1, pictures.size()); assertEquals(1, pictures.size());
} }
public void testNew() throws InvalidFormatException, IOException { public void testNew() throws InvalidFormatException, IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx");
byte[] jpegData = XWPFTestDataSamples.getImage("nature1.jpg"); byte[] jpegData = XWPFTestDataSamples.getImage("nature1.jpg");
assertNotNull(jpegData); assertNotNull(jpegData);
byte[] gifData = XWPFTestDataSamples.getImage("nature1.gif"); byte[] gifData = XWPFTestDataSamples.getImage("nature1.gif");
assertNotNull(gifData); assertNotNull(gifData);
byte[] pngData = XWPFTestDataSamples.getImage("nature1.png"); byte[] pngData = XWPFTestDataSamples.getImage("nature1.png");
assertNotNull(pngData); assertNotNull(pngData);
List<XWPFPictureData> pictures = doc.getAllPictures(); List<XWPFPictureData> pictures = doc.getAllPictures();
assertEquals(0, pictures.size()); assertEquals(0, pictures.size());
// Document shouldn't have any image relationships // Document shouldn't have any image relationships
assertEquals(13, doc.getPackagePart().getRelationships().size()); assertEquals(13, doc.getPackagePart().getRelationships().size());
for (PackageRelationship rel : doc.getPackagePart().getRelationships()) { for (PackageRelationship rel : doc.getPackagePart().getRelationships()) {
if (rel.getRelationshipType().equals(XSSFRelation.IMAGE_JPEG.getRelation())) { if (rel.getRelationshipType().equals(XSSFRelation.IMAGE_JPEG.getRelation())) {
fail("Shouldn't have JPEG yet"); fail("Shouldn't have JPEG yet");
} }
} }
// Add the image // Add the image
String relationId = doc.addPictureData(jpegData, XWPFDocument.PICTURE_TYPE_JPEG); String relationId = doc.addPictureData(jpegData, XWPFDocument.PICTURE_TYPE_JPEG);
assertEquals(1, pictures.size()); assertEquals(1, pictures.size());
XWPFPictureData jpgPicData = (XWPFPictureData) doc.getRelationById(relationId); XWPFPictureData jpgPicData = (XWPFPictureData) doc.getRelationById(relationId);
assertEquals("jpeg", jpgPicData.suggestFileExtension()); assertEquals("jpeg", jpgPicData.suggestFileExtension());
assertArrayEquals(jpegData, jpgPicData.getData()); assertArrayEquals(jpegData, jpgPicData.getData());
// Ensure it now has one // Ensure it now has one
assertEquals(14, doc.getPackagePart().getRelationships().size()); assertEquals(14, doc.getPackagePart().getRelationships().size());
PackageRelationship jpegRel = null; PackageRelationship jpegRel = null;
for (PackageRelationship rel : doc.getPackagePart().getRelationships()) { for (PackageRelationship rel : doc.getPackagePart().getRelationships()) {
if (rel.getRelationshipType().equals(XWPFRelation.IMAGE_JPEG.getRelation())) { if (rel.getRelationshipType().equals(XWPFRelation.IMAGE_JPEG.getRelation())) {
if (jpegRel != null) if (jpegRel != null)
fail("Found 2 jpegs!"); fail("Found 2 jpegs!");
jpegRel = rel; jpegRel = rel;
} }
} }
assertNotNull("JPEG Relationship not found", jpegRel); assertNotNull("JPEG Relationship not found", jpegRel);
// Check the details // Check the details
assertNotNull(jpegRel); assertNotNull(jpegRel);
assertEquals(XWPFRelation.IMAGE_JPEG.getRelation(), jpegRel.getRelationshipType()); assertEquals(XWPFRelation.IMAGE_JPEG.getRelation(), jpegRel.getRelationshipType());
assertEquals("/word/document.xml", jpegRel.getSource().getPartName().toString()); assertEquals("/word/document.xml", jpegRel.getSource().getPartName().toString());
assertEquals("/word/media/image1.jpeg", jpegRel.getTargetURI().getPath()); assertEquals("/word/media/image1.jpeg", jpegRel.getTargetURI().getPath());
XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId()); XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
assertArrayEquals(jpegData, pictureDataByID.getData()); assertArrayEquals(jpegData, pictureDataByID.getData());
// Save an re-load, check it appears // Save an re-load, check it appears
doc = XWPFTestDataSamples.writeOutAndReadBack(doc); doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
assertEquals(1, doc.getAllPictures().size()); assertEquals(1, doc.getAllPictures().size());
assertEquals(1, doc.getAllPackagePictures().size()); assertEquals(1, doc.getAllPackagePictures().size());
// verify the picture that we read back in // verify the picture that we read back in
pictureDataByID = doc.getPictureDataByID(jpegRel.getId()); pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
assertArrayEquals(jpegData, pictureDataByID.getData()); assertArrayEquals(jpegData, pictureDataByID.getData());
} }
public void testBug51770() throws InvalidFormatException, IOException { public void testBug51770() throws InvalidFormatException, IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug51170.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug51170.docx");
XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy(); XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
XWPFHeader header = policy.getDefaultHeader(); XWPFHeader header = policy.getDefaultHeader();
for (XWPFParagraph paragraph : header.getParagraphs()) { for (XWPFParagraph paragraph : header.getParagraphs()) {
for (XWPFRun run : paragraph.getRuns()) { for (XWPFRun run : paragraph.getRuns()) {
for (XWPFPicture picture : run.getEmbeddedPictures()) { for (XWPFPicture picture : run.getEmbeddedPictures()) {
if (paragraph.getDocument() != null) { if (paragraph.getDocument() != null) {
//System.out.println(picture.getCTPicture()); //System.out.println(picture.getCTPicture());
XWPFPictureData data = picture.getPictureData(); XWPFPictureData data = picture.getPictureData();
if (data != null) System.out.println(data.getFileName()); if (data != null) System.out.println(data.getFileName());
} }
} }
} }
} }
} }

View File

@ -20,12 +20,12 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear;
@ -45,28 +45,28 @@ public class TestXWPFRun extends TestCase {
p = doc.createParagraph(); p = doc.createParagraph();
this.ctRun = CTR.Factory.newInstance(); this.ctRun = CTR.Factory.newInstance();
} }
public void testSetGetText() { public void testSetGetText() {
ctRun.addNewT().setStringValue("TEST STRING"); ctRun.addNewT().setStringValue("TEST STRING");
ctRun.addNewT().setStringValue("TEST2 STRING"); ctRun.addNewT().setStringValue("TEST2 STRING");
ctRun.addNewT().setStringValue("TEST3 STRING"); ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(3, ctRun.sizeOfTArray()); assertEquals(3, ctRun.sizeOfTArray());
XWPFRun run = new XWPFRun(ctRun, p); XWPFRun run = new XWPFRun(ctRun, p);
assertEquals("TEST2 STRING", run.getText(1)); assertEquals("TEST2 STRING", run.getText(1));
run.setText("NEW STRING", 0); run.setText("NEW STRING", 0);
assertEquals("NEW STRING", run.getText(0)); assertEquals("NEW STRING", run.getText(0));
//run.setText("xxx",14); //run.setText("xxx",14);
//fail("Position wrong"); //fail("Position wrong");
} }
public void testSetGetBold() { public void testSetGetBold() {
CTRPr rpr = ctRun.addNewRPr(); CTRPr rpr = ctRun.addNewRPr();
rpr.addNewB().setVal(STOnOff.TRUE); rpr.addNewB().setVal(STOnOff.TRUE);
XWPFRun run = new XWPFRun(ctRun, p); XWPFRun run = new XWPFRun(ctRun, p);
assertEquals(true, run.isBold()); assertEquals(true, run.isBold());
@ -177,16 +177,16 @@ public class TestXWPFRun extends TestCase {
run.setText("T1"); run.setText("T1");
run.addCarriageReturn(); run.addCarriageReturn();
run.addCarriageReturn(); run.addCarriageReturn();
run.setText("T2"); run.setText("T2");
run.addCarriageReturn(); run.addCarriageReturn();
assertEquals(3, run.getCTR().sizeOfCrArray()); assertEquals(3, run.getCTR().sizeOfCrArray());
assertEquals("T1\n\nT2\n", run.toString()); assertEquals("T1\n\nT2\n", run.toString());
} }
public void testAddTabsAndLineBreaks() { public void testAddTabsAndLineBreaks() {
ctRun.addNewT().setStringValue("TEST STRING"); ctRun.addNewT().setStringValue("TEST STRING");
ctRun.addNewCr(); ctRun.addNewCr();
ctRun.addNewT().setStringValue("TEST2 STRING"); ctRun.addNewT().setStringValue("TEST2 STRING");
ctRun.addNewTab(); ctRun.addNewTab();
ctRun.addNewT().setStringValue("TEST3 STRING"); ctRun.addNewT().setStringValue("TEST3 STRING");
@ -198,21 +198,21 @@ public class TestXWPFRun extends TestCase {
run.addCarriageReturn(); run.addCarriageReturn();
run.setText("T2"); run.setText("T2");
run.addTab(); run.addTab();
run.setText("T3"); run.setText("T3");
assertEquals(1, run.getCTR().sizeOfCrArray()); assertEquals(1, run.getCTR().sizeOfCrArray());
assertEquals(1, run.getCTR().sizeOfTabArray()); assertEquals(1, run.getCTR().sizeOfTabArray());
assertEquals("T1\nT2\tT3", run.toString()); assertEquals("T1\nT2\tT3", run.toString());
} }
public void testAddPageBreak() { public void testAddPageBreak() {
ctRun.addNewT().setStringValue("TEST STRING"); ctRun.addNewT().setStringValue("TEST STRING");
ctRun.addNewBr(); ctRun.addNewBr();
ctRun.addNewT().setStringValue("TEST2 STRING"); ctRun.addNewT().setStringValue("TEST2 STRING");
CTBr breac = ctRun.addNewBr(); CTBr breac = ctRun.addNewBr();
breac.setClear(STBrClear.LEFT); breac.setClear(STBrClear.LEFT);
ctRun.addNewT().setStringValue("TEST3 STRING"); ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(2, ctRun.sizeOfBrArray()); assertEquals(2, ctRun.sizeOfBrArray());
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), p); XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), p);
run.setText("TEXT1"); run.setText("TEXT1");
@ -221,145 +221,145 @@ public class TestXWPFRun extends TestCase {
run.addBreak(BreakType.TEXT_WRAPPING); run.addBreak(BreakType.TEXT_WRAPPING);
assertEquals(2, run.getCTR().sizeOfBrArray()); assertEquals(2, run.getCTR().sizeOfBrArray());
} }
/** /**
* Test that on an existing document, we do the * Test that on an existing document, we do the
* right thing with it * right thing with it
* *
* @throws IOException * @throws IOException
*/ */
public void testExisting() throws IOException { public void testExisting() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
XWPFParagraph p; XWPFParagraph p;
XWPFRun run; XWPFRun run;
// First paragraph is simple // First paragraph is simple
p = doc.getParagraphArray(0); p = doc.getParagraphArray(0);
assertEquals("This is a test document.", p.getText()); assertEquals("This is a test document.", p.getText());
assertEquals(2, p.getRuns().size()); assertEquals(2, p.getRuns().size());
run = p.getRuns().get(0); run = p.getRuns().get(0);
assertEquals("This is a test document", run.toString()); assertEquals("This is a test document", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
assertEquals(null, run.getCTR().getRPr()); assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(1); run = p.getRuns().get(1);
assertEquals(".", run.toString()); assertEquals(".", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
assertEquals(null, run.getCTR().getRPr()); assertEquals(null, run.getCTR().getRPr());
// Next paragraph is all in one style, but a different one // Next paragraph is all in one style, but a different one
p = doc.getParagraphArray(1); p = doc.getParagraphArray(1);
assertEquals("This bit is in bold and italic", p.getText()); assertEquals("This bit is in bold and italic", p.getText());
assertEquals(1, p.getRuns().size()); assertEquals(1, p.getRuns().size());
run = p.getRuns().get(0); run = p.getRuns().get(0);
assertEquals("This bit is in bold and italic", run.toString()); assertEquals("This bit is in bold and italic", run.toString());
assertEquals(true, run.isBold()); assertEquals(true, run.isBold());
assertEquals(true, run.isItalic()); assertEquals(true, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
assertEquals(true, run.getCTR().getRPr().isSetB()); assertEquals(true, run.getCTR().getRPr().isSetB());
assertEquals(false, run.getCTR().getRPr().getB().isSetVal()); assertEquals(false, run.getCTR().getRPr().getB().isSetVal());
// Back to normal // Back to normal
p = doc.getParagraphArray(2); p = doc.getParagraphArray(2);
assertEquals("Back to normal", p.getText()); assertEquals("Back to normal", p.getText());
assertEquals(1, p.getRuns().size()); assertEquals(1, p.getRuns().size());
run = p.getRuns().get(0); run = p.getRuns().get(0);
assertEquals("Back to normal", run.toString()); assertEquals("Back to normal", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
assertEquals(null, run.getCTR().getRPr()); assertEquals(null, run.getCTR().getRPr());
// Different styles in one paragraph // Different styles in one paragraph
p = doc.getParagraphArray(3); p = doc.getParagraphArray(3);
assertEquals("This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.", p.getText()); assertEquals("This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.", p.getText());
assertEquals(11, p.getRuns().size()); assertEquals(11, p.getRuns().size());
run = p.getRuns().get(0); run = p.getRuns().get(0);
assertEquals("This contains ", run.toString()); assertEquals("This contains ", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
assertEquals(null, run.getCTR().getRPr()); assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(1); run = p.getRuns().get(1);
assertEquals("BOLD", run.toString()); assertEquals("BOLD", run.toString());
assertEquals(true, run.isBold()); assertEquals(true, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
run = p.getRuns().get(2); run = p.getRuns().get(2);
assertEquals(", ", run.toString()); assertEquals(", ", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
assertEquals(null, run.getCTR().getRPr()); assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(3); run = p.getRuns().get(3);
assertEquals("ITALIC", run.toString()); assertEquals("ITALIC", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(true, run.isItalic()); assertEquals(true, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
run = p.getRuns().get(4); run = p.getRuns().get(4);
assertEquals(" and ", run.toString()); assertEquals(" and ", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
assertEquals(null, run.getCTR().getRPr()); assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(5); run = p.getRuns().get(5);
assertEquals("BOTH", run.toString()); assertEquals("BOTH", run.toString());
assertEquals(true, run.isBold()); assertEquals(true, run.isBold());
assertEquals(true, run.isItalic()); assertEquals(true, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
run = p.getRuns().get(6); run = p.getRuns().get(6);
assertEquals(", as well as ", run.toString()); assertEquals(", as well as ", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
assertEquals(null, run.getCTR().getRPr()); assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(7); run = p.getRuns().get(7);
assertEquals("RED", run.toString()); assertEquals("RED", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
run = p.getRuns().get(8); run = p.getRuns().get(8);
assertEquals(" and ", run.toString()); assertEquals(" and ", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
assertEquals(null, run.getCTR().getRPr()); assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(9); run = p.getRuns().get(9);
assertEquals("YELLOW", run.toString()); assertEquals("YELLOW", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
run = p.getRuns().get(10); run = p.getRuns().get(10);
assertEquals(" text.", run.toString()); assertEquals(" text.", run.toString());
assertEquals(false, run.isBold()); assertEquals(false, run.isBold());
assertEquals(false, run.isItalic()); assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike()); assertEquals(false, run.isStrike());
assertEquals(null, run.getCTR().getRPr()); assertEquals(null, run.getCTR().getRPr());
} }
public void testPictureInHeader() throws IOException { public void testPictureInHeader() throws IOException {
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx"); XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
@ -373,47 +373,47 @@ public class TestXWPFRun extends TestCase {
for (XWPFPicture pic : pictures) { for (XWPFPicture pic : pictures) {
assertNotNull(pic.getPictureData()); assertNotNull(pic.getPictureData());
assertEquals("DOZOR", pic.getDescription()); assertEquals("DOZOR", pic.getDescription());
} }
count += pictures.size(); count += pictures.size();
} }
} }
assertEquals(1, count); assertEquals(1, count);
} }
public void testAddPicture() throws Exception { public void testAddPicture() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
XWPFParagraph p = doc.getParagraphArray(2); XWPFParagraph p = doc.getParagraphArray(2);
XWPFRun r = p.getRuns().get(0); XWPFRun r = p.getRuns().get(0);
assertEquals(0, doc.getAllPictures().size()); assertEquals(0, doc.getAllPictures().size());
assertEquals(0, r.getEmbeddedPictures().size()); assertEquals(0, r.getEmbeddedPictures().size());
r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32); r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
assertEquals(1, doc.getAllPictures().size()); assertEquals(1, doc.getAllPictures().size());
assertEquals(1, r.getEmbeddedPictures().size()); assertEquals(1, r.getEmbeddedPictures().size());
} }
/** /**
* Bugzilla #52288 - setting the font family on the * Bugzilla #52288 - setting the font family on the
* run mustn't NPE * run mustn't NPE
*/ */
public void testSetFontFamily_52288() throws Exception { public void testSetFontFamily_52288() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52288.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52288.docx");
final Iterator<XWPFParagraph> paragraphs = doc.getParagraphsIterator(); final Iterator<XWPFParagraph> paragraphs = doc.getParagraphsIterator();
while (paragraphs.hasNext()) { while (paragraphs.hasNext()) {
final XWPFParagraph paragraph = paragraphs.next(); final XWPFParagraph paragraph = paragraphs.next();
for (final XWPFRun run : paragraph.getRuns()) { for (final XWPFRun run : paragraph.getRuns()) {
if (run != null) { if (run != null) {
final String text = run.getText(0); final String text = run.getText(0);
if (text != null) { if (text != null) {
run.setFontFamily("Times New Roman"); run.setFontFamily("Times New Roman");
} }
} }
} }
} }
} }
} }

View File

@ -15,46 +15,46 @@
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
public final class TestXWPFSDT extends TestCase { public final class TestXWPFSDT extends TestCase {
/** /**
* Test simple tag and title extraction from SDT * Test simple tag and title extraction from SDT
* *
* @throws Exception * @throws Exception
*/ */
public void testTagTitle() throws Exception { public void testTagTitle() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx");
String tag = null; String tag = null;
String title = null; String title = null;
List<AbstractXWPFSDT> sdts = extractAllSDTs(doc); List<AbstractXWPFSDT> sdts = extractAllSDTs(doc);
for (AbstractXWPFSDT sdt : sdts) { for (AbstractXWPFSDT sdt : sdts) {
if (sdt.getContent().toString().equals("Rich_text")) { if (sdt.getContent().toString().equals("Rich_text")) {
tag = "MyTag"; tag = "MyTag";
title = "MyTitle"; title = "MyTitle";
break; break;
} }
} }
assertEquals("controls size", 13, sdts.size()); assertEquals("controls size", 13, sdts.size());
assertEquals("tag", "MyTag", tag); assertEquals("tag", "MyTag", tag);
assertEquals("title", "MyTitle", title); assertEquals("title", "MyTitle", title);
} }
public void testGetSDTs() throws Exception { public void testGetSDTs() throws Exception {
String[] contents = new String[]{ String[] contents = new String[]{
"header_rich_text", "header_rich_text",
"Rich_text", "Rich_text",
"Rich_text_pre_table\nRich_text_cell1\t\t\t\n\t\t\t\n\t\t\t\n\nRich_text_post_table", "Rich_text_pre_table\nRich_text_cell1\t\t\t\n\t\t\t\n\t\t\t\n\nRich_text_post_table",
"Plain_text_no_newlines", "Plain_text_no_newlines",
"Plain_text_with_newlines1\nplain_text_with_newlines2", "Plain_text_with_newlines1\nplain_text_with_newlines2",
@ -70,126 +70,126 @@ public final class TestXWPFSDT extends TestCase {
}; };
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx");
List<AbstractXWPFSDT> sdts = extractAllSDTs(doc); List<AbstractXWPFSDT> sdts = extractAllSDTs(doc);
assertEquals("number of sdts", contents.length, sdts.size()); assertEquals("number of sdts", contents.length, sdts.size());
for (int i = 0; i < contents.length; i++) { for (int i = 0; i < contents.length; i++) {
AbstractXWPFSDT sdt = sdts.get(i); AbstractXWPFSDT sdt = sdts.get(i);
assertEquals(i + ": " + contents[i], contents[i], sdt.getContent().toString()); assertEquals(i + ": " + contents[i], contents[i], sdt.getContent().toString());
} }
} }
/** /**
* POI-54771 and TIKA-1317 * POI-54771 and TIKA-1317
*/ */
public void testSDTAsCell() throws Exception { public void testSDTAsCell() throws Exception {
//Bug54771a.docx and Bug54771b.docx test slightly //Bug54771a.docx and Bug54771b.docx test slightly
//different recursion patterns. Keep both! //different recursion patterns. Keep both!
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54771a.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54771a.docx");
List<AbstractXWPFSDT> sdts = extractAllSDTs(doc); List<AbstractXWPFSDT> sdts = extractAllSDTs(doc);
String text = sdts.get(0).getContent().getText(); String text = sdts.get(0).getContent().getText();
assertEquals(2, sdts.size()); assertEquals(2, sdts.size());
assertTrue(text.indexOf("Test") > -1); assertTrue(text.indexOf("Test") > -1);
text = sdts.get(1).getContent().getText(); text = sdts.get(1).getContent().getText();
assertTrue(text.indexOf("Test Subtitle") > -1); assertTrue(text.indexOf("Test Subtitle") > -1);
assertTrue(text.indexOf("Test User") > -1); assertTrue(text.indexOf("Test User") > -1);
assertTrue(text.indexOf("Test") < text.indexOf("Test Subtitle")); assertTrue(text.indexOf("Test") < text.indexOf("Test Subtitle"));
doc = XWPFTestDataSamples.openSampleDocument("Bug54771b.docx"); doc = XWPFTestDataSamples.openSampleDocument("Bug54771b.docx");
sdts = extractAllSDTs(doc); sdts = extractAllSDTs(doc);
assertEquals(3, sdts.size()); assertEquals(3, sdts.size());
assertTrue(sdts.get(0).getContent().getText().indexOf("Test") > -1); assertTrue(sdts.get(0).getContent().getText().indexOf("Test") > -1);
assertTrue(sdts.get(1).getContent().getText().indexOf("Test Subtitle") > -1); assertTrue(sdts.get(1).getContent().getText().indexOf("Test Subtitle") > -1);
assertTrue(sdts.get(2).getContent().getText().indexOf("Test User") > -1); assertTrue(sdts.get(2).getContent().getText().indexOf("Test User") > -1);
} }
/** /**
* POI-55142 and Tika 1130 * POI-55142 and Tika 1130
*/ */
public void testNewLinesBetweenRuns() throws Exception { public void testNewLinesBetweenRuns() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug55142.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug55142.docx");
List<AbstractXWPFSDT> sdts = extractAllSDTs(doc); List<AbstractXWPFSDT> sdts = extractAllSDTs(doc);
List<String> targs = new ArrayList<String>(); List<String> targs = new ArrayList<String>();
//these test newlines and tabs in paragraphs/body elements //these test newlines and tabs in paragraphs/body elements
targs.add("Rich-text1 abcdefghi"); targs.add("Rich-text1 abcdefghi");
targs.add("Rich-text2 abcd\t\tefgh"); targs.add("Rich-text2 abcd\t\tefgh");
targs.add("Rich-text3 abcd\nefg"); targs.add("Rich-text3 abcd\nefg");
targs.add("Rich-text4 abcdefg"); targs.add("Rich-text4 abcdefg");
targs.add("Rich-text5 abcdefg\nhijk"); targs.add("Rich-text5 abcdefg\nhijk");
targs.add("Plain-text1 abcdefg"); targs.add("Plain-text1 abcdefg");
targs.add("Plain-text2 abcdefg\nhijk\nlmnop"); targs.add("Plain-text2 abcdefg\nhijk\nlmnop");
//this tests consecutive runs within a cell (not a paragraph) //this tests consecutive runs within a cell (not a paragraph)
//this test case was triggered by Tika-1130 //this test case was triggered by Tika-1130
targs.add("sdt_incell2 abcdefg"); targs.add("sdt_incell2 abcdefg");
for (int i = 0; i < sdts.size(); i++) { for (int i = 0; i < sdts.size(); i++) {
AbstractXWPFSDT sdt = sdts.get(i); AbstractXWPFSDT sdt = sdts.get(i);
assertEquals(targs.get(i), targs.get(i), sdt.getContent().getText()); assertEquals(targs.get(i), targs.get(i), sdt.getContent().getText());
} }
} }
private List<AbstractXWPFSDT> extractAllSDTs(XWPFDocument doc) { private List<AbstractXWPFSDT> extractAllSDTs(XWPFDocument doc) {
List<AbstractXWPFSDT> sdts = new ArrayList<AbstractXWPFSDT>(); List<AbstractXWPFSDT> sdts = new ArrayList<AbstractXWPFSDT>();
List<XWPFHeader> headers = doc.getHeaderList(); List<XWPFHeader> headers = doc.getHeaderList();
for (XWPFHeader header : headers) { for (XWPFHeader header : headers) {
sdts.addAll(extractSDTsFromBodyElements(header.getBodyElements())); sdts.addAll(extractSDTsFromBodyElements(header.getBodyElements()));
} }
sdts.addAll(extractSDTsFromBodyElements(doc.getBodyElements())); sdts.addAll(extractSDTsFromBodyElements(doc.getBodyElements()));
List<XWPFFooter> footers = doc.getFooterList(); List<XWPFFooter> footers = doc.getFooterList();
for (XWPFFooter footer : footers) { for (XWPFFooter footer : footers) {
sdts.addAll(extractSDTsFromBodyElements(footer.getBodyElements())); sdts.addAll(extractSDTsFromBodyElements(footer.getBodyElements()));
} }
for (XWPFFootnote footnote : doc.getFootnotes()) { for (XWPFFootnote footnote : doc.getFootnotes()) {
sdts.addAll(extractSDTsFromBodyElements(footnote.getBodyElements())); sdts.addAll(extractSDTsFromBodyElements(footnote.getBodyElements()));
} }
for (Map.Entry<Integer, XWPFFootnote> e : doc.endnotes.entrySet()) { for (Map.Entry<Integer, XWPFFootnote> e : doc.endnotes.entrySet()) {
sdts.addAll(extractSDTsFromBodyElements(e.getValue().getBodyElements())); sdts.addAll(extractSDTsFromBodyElements(e.getValue().getBodyElements()));
} }
return sdts; return sdts;
} }
private List<AbstractXWPFSDT> extractSDTsFromBodyElements(List<IBodyElement> elements) { private List<AbstractXWPFSDT> extractSDTsFromBodyElements(List<IBodyElement> elements) {
List<AbstractXWPFSDT> sdts = new ArrayList<AbstractXWPFSDT>(); List<AbstractXWPFSDT> sdts = new ArrayList<AbstractXWPFSDT>();
for (IBodyElement e : elements) { for (IBodyElement e : elements) {
if (e instanceof XWPFSDT) { if (e instanceof XWPFSDT) {
XWPFSDT sdt = (XWPFSDT) e; XWPFSDT sdt = (XWPFSDT) e;
sdts.add(sdt); sdts.add(sdt);
} else if (e instanceof XWPFParagraph) { } else if (e instanceof XWPFParagraph) {
XWPFParagraph p = (XWPFParagraph) e; XWPFParagraph p = (XWPFParagraph) e;
for (IRunElement e2 : p.getIRuns()) { for (IRunElement e2 : p.getIRuns()) {
if (e2 instanceof XWPFSDT) { if (e2 instanceof XWPFSDT) {
XWPFSDT sdt = (XWPFSDT) e2; XWPFSDT sdt = (XWPFSDT) e2;
sdts.add(sdt); sdts.add(sdt);
} }
} }
} else if (e instanceof XWPFTable) { } else if (e instanceof XWPFTable) {
XWPFTable table = (XWPFTable) e; XWPFTable table = (XWPFTable) e;
sdts.addAll(extractSDTsFromTable(table)); sdts.addAll(extractSDTsFromTable(table));
} }
} }
return sdts; return sdts;
} }
private List<AbstractXWPFSDT> extractSDTsFromTable(XWPFTable table) { private List<AbstractXWPFSDT> extractSDTsFromTable(XWPFTable table) {
List<AbstractXWPFSDT> sdts = new ArrayList<AbstractXWPFSDT>(); List<AbstractXWPFSDT> sdts = new ArrayList<AbstractXWPFSDT>();
for (XWPFTableRow r : table.getRows()) { for (XWPFTableRow r : table.getRows()) {
for (ICell c : r.getTableICells()) { for (ICell c : r.getTableICells()) {
if (c instanceof XWPFSDTCell) { if (c instanceof XWPFSDTCell) {
sdts.add((XWPFSDTCell) c); sdts.add((XWPFSDTCell) c);
} else if (c instanceof XWPFTableCell) { } else if (c instanceof XWPFTableCell) {
sdts.addAll(extractSDTsFromBodyElements(((XWPFTableCell) c).getBodyElements())); sdts.addAll(extractSDTsFromBodyElements(((XWPFTableCell) c).getBodyElements()));
} }
} }
} }
return sdts; return sdts;
} }
} }

View File

@ -1,38 +1,38 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership. this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0 The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import java.io.IOException; import java.io.IOException;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
/** /**
* Tests for reading SmartTags from Word docx. * Tests for reading SmartTags from Word docx.
* *
* @author Fabian Lange * @author Fabian Lange
*/ */
public final class TestXWPFSmartTag extends TestCase { public final class TestXWPFSmartTag extends TestCase {
public void testSmartTags() throws IOException { public void testSmartTags() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("smarttag-snippet.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("smarttag-snippet.docx");
XWPFParagraph p = doc.getParagraphArray(0); XWPFParagraph p = doc.getParagraphArray(0);
assertTrue(p.getText().contains("Carnegie Mellon University School of Computer Science")); assertTrue(p.getText().contains("Carnegie Mellon University School of Computer Science"));
p = doc.getParagraphArray(2); p = doc.getParagraphArray(2);
assertTrue(p.getText().contains("Alice's Adventures")); assertTrue(p.getText().contains("Alice's Adventures"));
} }
} }

View File

@ -19,82 +19,77 @@ package org.apache.poi.xwpf.usermodel;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLatentStyles; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLatentStyles;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLsdException; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLsdException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType;
public class TestXWPFStyles extends TestCase { public class TestXWPFStyles extends TestCase {
public void testGetUsedStyles() throws IOException {
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("Styles.docx");
List<XWPFStyle> testUsedStyleList = new ArrayList<XWPFStyle>();
XWPFStyles styles = sampleDoc.getStyles();
XWPFStyle style = styles.getStyle("berschrift1");
testUsedStyleList.add(style);
testUsedStyleList.add(styles.getStyle("Standard"));
testUsedStyleList.add(styles.getStyle("berschrift1Zchn"));
testUsedStyleList.add(styles.getStyle("Absatz-Standardschriftart"));
style.hasSameName(style);
// protected void setUp() throws Exception { List<XWPFStyle> usedStyleList = styles.getUsedStyleList(style);
// super.setUp(); assertEquals(usedStyleList, testUsedStyleList);
// } }
public void testGetUsedStyles() throws IOException { public void testAddStylesToDocument() throws IOException {
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("Styles.docx"); XWPFDocument docOut = new XWPFDocument();
List<XWPFStyle> testUsedStyleList = new ArrayList<XWPFStyle>(); XWPFStyles styles = docOut.createStyles();
XWPFStyles styles = sampleDoc.getStyles();
XWPFStyle style = styles.getStyle("berschrift1"); String strStyleId = "headline1";
testUsedStyleList.add(style); CTStyle ctStyle = CTStyle.Factory.newInstance();
testUsedStyleList.add(styles.getStyle("Standard"));
testUsedStyleList.add(styles.getStyle("berschrift1Zchn")); ctStyle.setStyleId(strStyleId);
testUsedStyleList.add(styles.getStyle("Absatz-Standardschriftart")); XWPFStyle s = new XWPFStyle(ctStyle);
style.hasSameName(style); styles.addStyle(s);
List<XWPFStyle> usedStyleList = styles.getUsedStyleList(style); assertTrue(styles.styleExist(strStyleId));
assertEquals(usedStyleList, testUsedStyleList);
XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
} styles = docIn.getStyles();
assertTrue(styles.styleExist(strStyleId));
public void testAddStylesToDocument() throws IOException { }
XWPFDocument docOut = new XWPFDocument();
XWPFStyles styles = docOut.createStyles(); /**
* Bug #52449 - We should be able to write a file containing
String strStyleId = "headline1"; * both regular and glossary styles without error
CTStyle ctStyle = CTStyle.Factory.newInstance(); */
public void test52449() throws Exception {
ctStyle.setStyleId(strStyleId); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52449.docx");
XWPFStyle s = new XWPFStyle(ctStyle); XWPFStyles styles = doc.getStyles();
styles.addStyle(s); assertNotNull(styles);
assertTrue(styles.styleExist(strStyleId)); XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(doc);
styles = docIn.getStyles();
XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut); assertNotNull(styles);
}
styles = docIn.getStyles();
assertTrue(styles.styleExist(strStyleId));
} /**
* YK: tests below don't make much sense,
/** * they exist only to copy xml beans to pi-ooxml-schemas.jar
* Bug #52449 - We should be able to write a file containing */
* both regular and glossary styles without error @SuppressWarnings("resource")
*/ public void testLanguages() {
public void test52449() throws Exception { XWPFDocument docOut = new XWPFDocument();
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52449.docx"); XWPFStyles styles = docOut.createStyles();
XWPFStyles styles = doc.getStyles(); styles.setEastAsia("Chinese");
assertNotNull(styles);
XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(doc);
styles = docIn.getStyles();
assertNotNull(styles);
}
/**
* YK: tests below don't make much sense,
* they exist only to copy xml beans to pi-ooxml-schemas.jar
*/
public void testLanguages() {
XWPFDocument docOut = new XWPFDocument();
XWPFStyles styles = docOut.createStyles();
styles.setEastAsia("Chinese");
styles.setSpellingLanguage("English"); styles.setSpellingLanguage("English");
@ -115,82 +110,82 @@ public class TestXWPFStyles extends TestCase {
CTLsdException ex = latentStyles.addNewLsdException(); CTLsdException ex = latentStyles.addNewLsdException();
ex.setName("ex1"); ex.setName("ex1");
XWPFLatentStyles ls = new XWPFLatentStyles(latentStyles); XWPFLatentStyles ls = new XWPFLatentStyles(latentStyles);
assertEquals(true, ls.isLatentStyle("ex1")); assertEquals(true, ls.isLatentStyle("ex1"));
assertEquals(false, ls.isLatentStyle("notex1")); assertEquals(false, ls.isLatentStyle("notex1"));
} }
public void testSetStyles_Bug57254() throws IOException { public void testSetStyles_Bug57254() throws IOException {
XWPFDocument docOut = new XWPFDocument(); XWPFDocument docOut = new XWPFDocument();
XWPFStyles styles = docOut.createStyles(); XWPFStyles styles = docOut.createStyles();
CTStyles ctStyles = CTStyles.Factory.newInstance(); CTStyles ctStyles = CTStyles.Factory.newInstance();
String strStyleId = "headline1"; String strStyleId = "headline1";
CTStyle ctStyle = ctStyles.addNewStyle(); CTStyle ctStyle = ctStyles.addNewStyle();
ctStyle.setStyleId(strStyleId);
styles.setStyles(ctStyles);
assertTrue(styles.styleExist(strStyleId));
XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
styles = docIn.getStyles(); ctStyle.setStyleId(strStyleId);
assertTrue(styles.styleExist(strStyleId)); styles.setStyles(ctStyles);
}
assertTrue(styles.styleExist(strStyleId));
public void testEasyAccessToStyles() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx"); XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
XWPFStyles styles = doc.getStyles();
assertNotNull(styles); styles = docIn.getStyles();
assertTrue(styles.styleExist(strStyleId));
// Has 3 paragraphs on page one, a break, and 3 on page 2 }
assertEquals(7, doc.getParagraphs().size());
public void testEasyAccessToStyles() throws IOException {
// Check the first three have no run styles, just default paragraph style XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
for (int i = 0; i < 3; i++) { XWPFStyles styles = doc.getStyles();
XWPFParagraph p = doc.getParagraphs().get(i); assertNotNull(styles);
assertEquals(null, p.getStyle());
assertEquals(null, p.getStyleID()); // Has 3 paragraphs on page one, a break, and 3 on page 2
assertEquals(1, p.getRuns().size()); assertEquals(7, doc.getParagraphs().size());
XWPFRun r = p.getRuns().get(0); // Check the first three have no run styles, just default paragraph style
assertEquals(null, r.getColor()); for (int i = 0; i < 3; i++) {
assertEquals(null, r.getFontFamily()); XWPFParagraph p = doc.getParagraphs().get(i);
assertEquals(null, r.getFontName()); assertEquals(null, p.getStyle());
assertEquals(-1, r.getFontSize()); assertEquals(null, p.getStyleID());
} assertEquals(1, p.getRuns().size());
// On page two, has explicit styles, but on runs not on XWPFRun r = p.getRuns().get(0);
// the paragraph itself assertEquals(null, r.getColor());
for (int i = 4; i < 7; i++) { assertEquals(null, r.getFontFamily());
XWPFParagraph p = doc.getParagraphs().get(i); assertEquals(null, r.getFontName());
assertEquals(null, p.getStyle()); assertEquals(-1, r.getFontSize());
assertEquals(null, p.getStyleID()); }
assertEquals(1, p.getRuns().size());
// On page two, has explicit styles, but on runs not on
XWPFRun r = p.getRuns().get(0); // the paragraph itself
assertEquals("Arial Black", r.getFontFamily()); for (int i = 4; i < 7; i++) {
assertEquals("Arial Black", r.getFontName()); XWPFParagraph p = doc.getParagraphs().get(i);
assertEquals(16, r.getFontSize()); assertEquals(null, p.getStyle());
assertEquals("548DD4", r.getColor()); assertEquals(null, p.getStyleID());
} assertEquals(1, p.getRuns().size());
// Check the document styles XWPFRun r = p.getRuns().get(0);
// Should have a style defined for each type assertEquals("Arial Black", r.getFontFamily());
assertEquals(4, styles.getNumberOfStyles()); assertEquals("Arial Black", r.getFontName());
assertEquals(16, r.getFontSize());
assertEquals("548DD4", r.getColor());
}
// Check the document styles
// Should have a style defined for each type
assertEquals(4, styles.getNumberOfStyles());
assertNotNull(styles.getStyle("Normal")); assertNotNull(styles.getStyle("Normal"));
assertNotNull(styles.getStyle("DefaultParagraphFont")); assertNotNull(styles.getStyle("DefaultParagraphFont"));
assertNotNull(styles.getStyle("TableNormal")); assertNotNull(styles.getStyle("TableNormal"));
assertNotNull(styles.getStyle("NoList")); assertNotNull(styles.getStyle("NoList"));
// We can't do much yet with latent styles // We can't do much yet with latent styles
assertEquals(137, styles.getLatentStyles().getNumberOfStyles()); assertEquals(137, styles.getLatentStyles().getNumberOfStyles());
// Check the default styles // Check the default styles
assertNotNull(styles.getDefaultRunStyle()); assertNotNull(styles.getDefaultRunStyle());
assertNotNull(styles.getDefaultParagraphStyle()); assertNotNull(styles.getDefaultParagraphStyle());
assertEquals(11, styles.getDefaultRunStyle().getFontSize()); assertEquals(11, styles.getDefaultRunStyle().getFontSize());
assertEquals(200, styles.getDefaultParagraphStyle().getSpacingAfter()); assertEquals(200, styles.getDefaultParagraphStyle().getSpacingAfter());
} }
} }

View File

@ -17,12 +17,12 @@
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.List; import java.util.List;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType; import org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
@ -34,10 +34,9 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
/** /**
* Tests for XWPF Run * Tests for XWPF Tables
*/ */
public class TestXWPFTable extends TestCase { public class TestXWPFTable extends TestCase {
protected void setUp() { protected void setUp() {
/* /*
XWPFDocument doc = new XWPFDocument(); XWPFDocument doc = new XWPFDocument();
@ -121,13 +120,13 @@ public class TestXWPFTable extends TestCase {
assertEquals(1, xtab.getCTTbl().getTrArray(0).sizeOfTcArray()); assertEquals(1, xtab.getCTTbl().getTrArray(0).sizeOfTcArray());
} }
public void testSetGetWidth() { public void testSetGetWidth() {
XWPFDocument doc = new XWPFDocument(); XWPFDocument doc = new XWPFDocument();
CTTbl table = CTTbl.Factory.newInstance(); CTTbl table = CTTbl.Factory.newInstance();
table.addNewTblPr().addNewTblW().setW(new BigInteger("1000")); table.addNewTblPr().addNewTblW().setW(new BigInteger("1000"));
XWPFTable xtab = new XWPFTable(table, doc); XWPFTable xtab = new XWPFTable(table, doc);
assertEquals(1000, xtab.getWidth()); assertEquals(1000, xtab.getWidth());
@ -145,20 +144,20 @@ public class TestXWPFTable extends TestCase {
XWPFTableRow row = xtab.createRow(); XWPFTableRow row = xtab.createRow();
row.setHeight(20); row.setHeight(20);
assertEquals(20, row.getHeight()); assertEquals(20, row.getHeight());
} }
public void testSetGetMargins() { public void testSetGetMargins() {
// instantiate the following class so it'll get picked up by // instantiate the following class so it'll get picked up by
// the XmlBean process and added to the jar file. it's required // the XmlBean process and added to the jar file. it's required
// for the following XWPFTable methods. // for the following XWPFTable methods.
CTTblCellMar ctm = CTTblCellMar.Factory.newInstance(); CTTblCellMar ctm = CTTblCellMar.Factory.newInstance();
assertNotNull(ctm); assertNotNull(ctm);
// create a table // create a table
XWPFDocument doc = new XWPFDocument(); XWPFDocument doc = new XWPFDocument();
CTTbl ctTable = CTTbl.Factory.newInstance(); CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc); XWPFTable table = new XWPFTable(ctTable, doc);
// set margins // set margins
table.setCellMargins(50, 50, 250, 450); table.setCellMargins(50, 50, 250, 450);
// get margin components // get margin components
int t = table.getCellMarginTop(); int t = table.getCellMarginTop();
assertEquals(50, t); assertEquals(50, t);
@ -168,22 +167,22 @@ public class TestXWPFTable extends TestCase {
assertEquals(250, b); assertEquals(250, b);
int r = table.getCellMarginRight(); int r = table.getCellMarginRight();
assertEquals(450, r); assertEquals(450, r);
} }
public void testSetGetHBorders() { public void testSetGetHBorders() {
// instantiate the following classes so they'll get picked up by // instantiate the following classes so they'll get picked up by
// the XmlBean process and added to the jar file. they are required // the XmlBean process and added to the jar file. they are required
// for the following XWPFTable methods. // for the following XWPFTable methods.
CTTblBorders cttb = CTTblBorders.Factory.newInstance(); CTTblBorders cttb = CTTblBorders.Factory.newInstance();
assertNotNull(cttb); assertNotNull(cttb);
STBorder stb = STBorder.Factory.newInstance(); STBorder stb = STBorder.Factory.newInstance();
assertNotNull(stb); assertNotNull(stb);
// create a table // create a table
XWPFDocument doc = new XWPFDocument(); XWPFDocument doc = new XWPFDocument();
CTTbl ctTable = CTTbl.Factory.newInstance(); CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc); XWPFTable table = new XWPFTable(ctTable, doc);
// set inside horizontal border // set inside horizontal border
table.setInsideHBorder(XWPFBorderType.SINGLE, 4, 0, "FF0000"); table.setInsideHBorder(XWPFBorderType.SINGLE, 4, 0, "FF0000");
// get inside horizontal border components // get inside horizontal border components
int s = table.getInsideHBorderSize(); int s = table.getInsideHBorderSize();
assertEquals(4, s); assertEquals(4, s);
@ -193,26 +192,26 @@ public class TestXWPFTable extends TestCase {
assertEquals("FF0000", clr); assertEquals("FF0000", clr);
XWPFBorderType bt = table.getInsideHBorderType(); XWPFBorderType bt = table.getInsideHBorderType();
assertEquals(XWPFBorderType.SINGLE, bt); assertEquals(XWPFBorderType.SINGLE, bt);
} }
public void testSetGetVBorders() { public void testSetGetVBorders() {
// create a table // create a table
XWPFDocument doc = new XWPFDocument(); XWPFDocument doc = new XWPFDocument();
CTTbl ctTable = CTTbl.Factory.newInstance(); CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc); XWPFTable table = new XWPFTable(ctTable, doc);
// set inside vertical border // set inside vertical border
table.setInsideVBorder(XWPFBorderType.DOUBLE, 4, 0, "00FF00"); table.setInsideVBorder(XWPFBorderType.DOUBLE, 4, 0, "00FF00");
// get inside vertical border components // get inside vertical border components
XWPFBorderType bt = table.getInsideVBorderType(); XWPFBorderType bt = table.getInsideVBorderType();
assertEquals(XWPFBorderType.DOUBLE, bt); assertEquals(XWPFBorderType.DOUBLE, bt);
int sz = table.getInsideVBorderSize(); int sz = table.getInsideVBorderSize();
assertEquals(4, sz); assertEquals(4, sz);
int sp = table.getInsideVBorderSpace(); int sp = table.getInsideVBorderSpace();
assertEquals(0, sp); assertEquals(0, sp);
String clr = table.getInsideVBorderColor(); String clr = table.getInsideVBorderColor();
assertEquals("00FF00", clr); assertEquals("00FF00", clr);
} }
public void testSetGetRowBandSize() { public void testSetGetRowBandSize() {
XWPFDocument doc = new XWPFDocument(); XWPFDocument doc = new XWPFDocument();
CTTbl ctTable = CTTbl.Factory.newInstance(); CTTbl ctTable = CTTbl.Factory.newInstance();
@ -229,30 +228,30 @@ public class TestXWPFTable extends TestCase {
table.setColBandSize(16); table.setColBandSize(16);
int sz = table.getColBandSize(); int sz = table.getColBandSize();
assertEquals(16, sz); assertEquals(16, sz);
} }
public void testCreateTable() throws Exception { public void testCreateTable() throws Exception {
// open an empty document // open an empty document
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
// create a table with 5 rows and 7 columns // create a table with 5 rows and 7 columns
int noRows = 5; int noRows = 5;
int noCols = 7; int noCols = 7;
XWPFTable table = doc.createTable(noRows, noCols); XWPFTable table = doc.createTable(noRows, noCols);
// assert the table is empty // assert the table is empty
List<XWPFTableRow> rows = table.getRows(); List<XWPFTableRow> rows = table.getRows();
assertEquals("Table has less rows than requested.", noRows, rows.size()); assertEquals("Table has less rows than requested.", noRows, rows.size());
for (XWPFTableRow xwpfRow : rows) { for (XWPFTableRow xwpfRow : rows) {
assertNotNull(xwpfRow); assertNotNull(xwpfRow);
for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) {
XWPFTableCell xwpfCell = xwpfRow.getCell(i); XWPFTableCell xwpfCell = xwpfRow.getCell(i);
assertNotNull(xwpfCell); assertNotNull(xwpfCell);
assertEquals("Empty cells should not have one paragraph.", 1, xwpfCell.getParagraphs().size()); assertEquals("Empty cells should not have one paragraph.", 1, xwpfCell.getParagraphs().size());
xwpfCell = xwpfRow.getCell(i); xwpfCell = xwpfRow.getCell(i);
assertEquals("Calling 'getCell' must not modify cells content.", 1, xwpfCell.getParagraphs().size()); assertEquals("Calling 'getCell' must not modify cells content.", 1, xwpfCell.getParagraphs().size());
} }
} }
doc.getPackage().revert(); doc.getPackage().revert();
} }
} }

View File

@ -17,80 +17,81 @@
* ==================================================================== * ====================================================================
*/ */
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge; import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcBorders; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
public class TestXWPFTableCell extends TestCase {
public class TestXWPFTableCell extends TestCase {
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
} }
public void testSetGetVertAlignment() throws Exception { public void testSetGetVertAlignment() throws Exception {
// instantiate the following classes so they'll get picked up by // instantiate the following classes so they'll get picked up by
// the XmlBean process and added to the jar file. they are required // the XmlBean process and added to the jar file. they are required
// for the following XWPFTableCell methods. // for the following XWPFTableCell methods.
CTShd ctShd = CTShd.Factory.newInstance(); CTShd ctShd = CTShd.Factory.newInstance();
assertNotNull(ctShd); assertNotNull(ctShd);
CTVerticalJc ctVjc = CTVerticalJc.Factory.newInstance(); CTVerticalJc ctVjc = CTVerticalJc.Factory.newInstance();
assertNotNull(ctVjc); assertNotNull(ctVjc);
STShd stShd = STShd.Factory.newInstance(); STShd stShd = STShd.Factory.newInstance();
assertNotNull(stShd); assertNotNull(stShd);
STVerticalJc stVjc = STVerticalJc.Factory.newInstance(); STVerticalJc stVjc = STVerticalJc.Factory.newInstance();
assertNotNull(stVjc); assertNotNull(stVjc);
// create a table // create a table
XWPFDocument doc = new XWPFDocument(); XWPFDocument doc = new XWPFDocument();
CTTbl ctTable = CTTbl.Factory.newInstance(); CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc); XWPFTable table = new XWPFTable(ctTable, doc);
// table has a single row by default; grab it // table has a single row by default; grab it
XWPFTableRow tr = table.getRow(0); XWPFTableRow tr = table.getRow(0);
assertNotNull(tr); assertNotNull(tr);
// row has a single cell by default; grab it // row has a single cell by default; grab it
XWPFTableCell cell = tr.getCell(0); XWPFTableCell cell = tr.getCell(0);
cell.setVerticalAlignment(XWPFVertAlign.BOTH); cell.setVerticalAlignment(XWPFVertAlign.BOTH);
XWPFVertAlign al = cell.getVerticalAlignment(); XWPFVertAlign al = cell.getVerticalAlignment();
assertEquals(XWPFVertAlign.BOTH, al); assertEquals(XWPFVertAlign.BOTH, al);
} }
public void testSetGetColor() throws Exception { public void testSetGetColor() throws Exception {
// create a table // create a table
XWPFDocument doc = new XWPFDocument(); XWPFDocument doc = new XWPFDocument();
CTTbl ctTable = CTTbl.Factory.newInstance(); CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc); XWPFTable table = new XWPFTable(ctTable, doc);
// table has a single row by default; grab it // table has a single row by default; grab it
XWPFTableRow tr = table.getRow(0); XWPFTableRow tr = table.getRow(0);
assertNotNull(tr); assertNotNull(tr);
// row has a single cell by default; grab it // row has a single cell by default; grab it
XWPFTableCell cell = tr.getCell(0); XWPFTableCell cell = tr.getCell(0);
cell.setColor("F0000F"); cell.setColor("F0000F");
String clr = cell.getColor(); String clr = cell.getColor();
assertEquals("F0000F", clr); assertEquals("F0000F", clr);
} }
/** /**
* ensure that CTHMerge & CTTcBorders go in poi-ooxml.jar * ensure that CTHMerge & CTTcBorders go in poi-ooxml.jar
*/ */
public void test54099() { @SuppressWarnings("unused")
XWPFDocument doc = new XWPFDocument(); public void test54099() {
CTTbl ctTable = CTTbl.Factory.newInstance(); XWPFDocument doc = new XWPFDocument();
XWPFTable table = new XWPFTable(ctTable, doc); CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc);
XWPFTableRow tr = table.getRow(0); XWPFTableRow tr = table.getRow(0);
XWPFTableCell cell = tr.getCell(0); XWPFTableCell cell = tr.getCell(0);

View File

@ -15,54 +15,53 @@
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
public class TestXWPFTableRow extends TestCase { public class TestXWPFTableRow extends TestCase {
@Override
@Override protected void setUp() throws Exception {
protected void setUp() throws Exception { super.setUp();
super.setUp(); }
}
public void testCreateRow() throws Exception {
public void testCreateRow() throws Exception { CTRow ctRow = CTRow.Factory.newInstance();
CTRow ctRow = CTRow.Factory.newInstance(); assertNotNull(ctRow);
assertNotNull(ctRow); }
}
@Override
@Override protected void tearDown() throws Exception {
protected void tearDown() throws Exception { super.tearDown();
super.tearDown(); }
}
public void testSetGetCantSplitRow() {
public void testSetGetCantSplitRow() { // create a table
// create a table XWPFDocument doc = new XWPFDocument();
XWPFDocument doc = new XWPFDocument(); CTTbl ctTable = CTTbl.Factory.newInstance();
CTTbl ctTable = CTTbl.Factory.newInstance(); XWPFTable table = new XWPFTable(ctTable, doc);
XWPFTable table = new XWPFTable(ctTable, doc); // table has a single row by default; grab it
// table has a single row by default; grab it XWPFTableRow tr = table.getRow(0);
XWPFTableRow tr = table.getRow(0); assertNotNull(tr);
assertNotNull(tr);
tr.setCantSplitRow(true);
tr.setCantSplitRow(true); boolean isCant = tr.isCantSplitRow();
boolean isCant = tr.isCantSplitRow(); assert (isCant);
assert (isCant); }
}
public void testSetGetRepeatHeader() {
public void testSetGetRepeatHeader() { // create a table
// create a table XWPFDocument doc = new XWPFDocument();
XWPFDocument doc = new XWPFDocument(); CTTbl ctTable = CTTbl.Factory.newInstance();
CTTbl ctTable = CTTbl.Factory.newInstance(); XWPFTable table = new XWPFTable(ctTable, doc);
XWPFTable table = new XWPFTable(ctTable, doc); // table has a single row by default; grab it
// table has a single row by default; grab it XWPFTableRow tr = table.getRow(0);
XWPFTableRow tr = table.getRow(0); assertNotNull(tr);
assertNotNull(tr);
tr.setRepeatHeader(true);
tr.setRepeatHeader(true); boolean isRpt = tr.isRepeatHeader();
boolean isRpt = tr.isRepeatHeader(); assert (isRpt);
assert (isRpt); }
} }
}