import java.io.IOException;
import java.net.URI;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
import java.util.Map.Entry;
+import java.util.Set;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
* @param alreadySaved context set containing already visited nodes
*/
protected final void onSave(Set<PackagePart> alreadySaved) throws IOException{
+ // this usually clears out previous content in the part...
+ prepareForCommit();
+
commit();
alreadySaved.add(this.getPackagePart());
for(POIXMLDocumentPart p : relations.values()){
}
}
+ /**
+ * Ensure that a memory based package part does not have lingering data from previous
+ * commit() calls.
+ *
+ * Note: This is overwritten for some objects, as *PictureData seem to store the actual content
+ * in the part directly without keeping a copy like all others therefore we need to handle them differently.
+ */
+ protected void prepareForCommit() {
+ PackagePart part = this.getPackagePart();
+ if(part != null) {
+ part.clear();
+ }
+ }
+
/**
* Create a new child POIXMLDocumentPart
*
// Create a memory part
PackagePart part = _container.createPart(this._partName,
this._contentType.toString(), false);
- part._relationships = this._relationships;
if (part == null) {
- throw new InvalidOperationException(
- "Can't create a temporary part !");
+ throw new InvalidOperationException(
+ "Can't create a temporary part !");
}
+ part._relationships = this._relationships;
outStream = part.getOutputStreamImpl();
} else {
outStream = this.getOutputStreamImpl();
* respective buffer.
*/
public abstract void flush();
+
+ /**
+ * Allows sub-classes to clean up before new data is added.
+ */
+ public void clear() {
+ }
}
return data == null ? 0 : data.length;
}
- public void clear() {
+ @Override
+ public void clear() {
data = null;
}
==================================================================== */
package org.apache.poi.xslf.usermodel;
+import java.awt.Dimension;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLException;
import org.apache.poi.POIXMLRelation;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
-import org.apache.poi.openxml4j.opc.*;
-import org.apache.poi.util.*;
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackagePartName;
+import org.apache.poi.openxml4j.opc.TargetMode;
+import org.apache.poi.util.Beta;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
+import org.apache.poi.util.PackageHelper;
+import org.apache.poi.util.Units;
import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideSize;
import org.openxmlformats.schemas.presentationml.x2006.main.PresentationDocument;
-import java.awt.Dimension;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.regex.Pattern;
-
/**
* High level representation of a ooxml slideshow.
* This is the first object most users will construct whether
/**
* Get the document's embedded files.
*/
+ @Override
public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
return Collections.unmodifiableList(
getPackage().getPartsByName(Pattern.compile("/ppt/embeddings/.*?"))
package org.apache.poi.xslf.usermodel;
+import java.io.IOException;
+
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLException;
import org.apache.poi.POIXMLRelation;
import org.apache.poi.util.Beta;
import org.apache.poi.util.IOUtils;
-import java.io.IOException;
-
/**
* Instantiates sub-classes of POIXMLDocumentPart depending on their relationship type
*
}
return checksum;
}
+
+ /**
+ * *PictureData objects store the actual content in the part directly without keeping a
+ * copy like all others therefore we need to handle them differently.
+ */
+ @Override
+ protected void prepareForCommit() {
+ // do not clear the part here
+ }
}
\ No newline at end of file
public String getMimeType() {
return getPackagePart().getContentType();
}
+
+ /**
+ * *PictureData objects store the actual content in the part directly without keeping a
+ * copy like all others therefore we need to handle them differently.
+ */
+ @Override
+ protected void prepareForCommit() {
+ // do not clear the part here
+ }
}
public int hashCode() {
return getChecksum().hashCode();
}
+
+ /**
+ * *PictureData objects store the actual content in the part directly without keeping a
+ * copy like all others therefore we need to handle them differently.
+ */
+ @Override
+ protected void prepareForCommit() {
+ // do not clear the part here
+ }
}
==================================================================== */\r
package org.apache.poi.xslf.usermodel;\r
\r
-import junit.framework.TestCase;\r
-import org.apache.poi.xslf.XSLFTestDataSamples;\r
-import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;\r
+import static org.junit.Assert.assertArrayEquals;\r
\r
-import java.util.Arrays;\r
import java.util.HashMap;\r
import java.util.List;\r
import java.util.Map;\r
\r
+import junit.framework.TestCase;\r
+\r
+import org.apache.poi.xslf.XSLFTestDataSamples;\r
+import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;\r
+\r
/**\r
* @author Yegor Kozlov\r
*/\r
XMLSlideShow ppt = new XMLSlideShow();\r
assertEquals(0, ppt.getAllPictures().size());\r
byte[] data1 = new byte[100];\r
+ for(int i = 0;i < 100;i++) { data1[i] = (byte)i; }\r
int idx1 = ppt.addPicture(data1, XSLFPictureData.PICTURE_TYPE_JPEG);\r
assertEquals(0, idx1);\r
assertEquals(1, ppt.getAllPictures().size());\r
XSLFSlide slide = ppt.createSlide();\r
XSLFPictureShape shape1 = slide.createPicture(idx1);\r
assertNotNull(shape1.getPictureData());\r
- assertTrue(Arrays.equals(data1, shape1.getPictureData().getData()));\r
+ assertArrayEquals(data1, shape1.getPictureData().getData());\r
\r
byte[] data2 = new byte[200];\r
+ for(int i = 0;i < 200;i++) { data2[i] = (byte)i; }\r
int idx2 = ppt.addPicture(data2, XSLFPictureData.PICTURE_TYPE_PNG);\r
XSLFPictureShape shape2 = slide.createPicture(idx2);\r
assertNotNull(shape2.getPictureData());\r
assertEquals(1, idx2);\r
assertEquals(2, ppt.getAllPictures().size());\r
- assertTrue(Arrays.equals(data2, shape2.getPictureData().getData()));\r
+ assertArrayEquals(data2, shape2.getPictureData().getData());\r
\r
ppt = XSLFTestDataSamples.writeOutAndReadBack(ppt);\r
List<XSLFPictureData> pics = ppt.getAllPictures();\r
assertEquals(2, pics.size());\r
- assertTrue(Arrays.equals(data1, pics.get(0).getData()));\r
- assertTrue(Arrays.equals(data2, pics.get(1).getData()));\r
+ assertArrayEquals(data1, pics.get(0).getData());\r
+ assertArrayEquals(data2, pics.get(1).getData());\r
\r
XSLFShape[] shapes = ppt.getSlides()[0].getShapes();\r
- assertTrue(Arrays.equals(data1, ((XSLFPictureShape) shapes[0]).getPictureData().getData()));\r
- assertTrue(Arrays.equals(data2, ((XSLFPictureShape) shapes[1]).getPictureData().getData()));\r
+ assertArrayEquals(data1, ((XSLFPictureShape) shapes[0]).getPictureData().getData());\r
+ assertArrayEquals(data2, ((XSLFPictureShape) shapes[1]).getPictureData().getData());\r
}\r
\r
public void testCreateMultiplePictures() {\r
// POI saves images as image1.png, image2.png, etc.\r
String fileName = "image" + (elementIndex + 1) + ".png";\r
assertEquals(fileName, picture.getPictureData().getFileName());\r
- assertTrue(Arrays.equals(data, picture.getPictureData().getData()));\r
+ assertArrayEquals(data, picture.getPictureData().getData());\r
}\r
\r
// and then add next 20 images to a group\r
assertEquals(pictureIndex, elementIndex); // added images have indexes 0,1,2....19\r
String fileName = "image" + (pictureIndex + 1) + ".png";\r
assertEquals(fileName, picture.getPictureData().getFileName());\r
- assertTrue(Arrays.equals(data, picture.getPictureData().getData()));\r
+ assertArrayEquals(data, picture.getPictureData().getData());\r
}\r
\r
// serialize, read back and check that all images are there\r
XSLFPictureData data = pics.get(fileName);\r
assertNotNull(data);\r
assertEquals(fileName, data.getFileName());\r
- assertTrue(Arrays.equals(data1, data.getData()));\r
+ assertArrayEquals(data1, data.getData());\r
}\r
}\r
\r
assertEquals(1, ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG));\r
\r
XSLFSlide slide1 = ppt.createSlide();\r
+ assertNotNull(slide1);\r
XSLFSlide slide2 = ppt.createSlide();\r
+ assertNotNull(slide2);\r
\r
}\r
\r
XSLFSlide slide2 = ppt2.createSlide().importContent(slide1);\r
XSLFPictureShape shape2 = (XSLFPictureShape)slide2.getShapes()[0];\r
\r
- assertTrue(Arrays.equals(data1, shape2.getPictureData().getData()));\r
+ assertArrayEquals(data1, shape2.getPictureData().getData());\r
\r
CTPicture ctPic2 = (CTPicture)shape2.getXmlObject();\r
assertFalse(ctPic2.getNvPicPr().getNvPr().isSetCustDataLst());\r
package org.apache.poi.xssf.model;
-import java.util.List;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
import java.util.ArrayList;
-import java.io.*;
+import java.util.List;
import junit.framework.TestCase;
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.POIXMLException;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.POIDataSamples;
-import org.apache.poi.POIXMLException;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
CTRst st2 = items2.get(i);
assertEquals(st1.toString(), st2.toString());
}
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
String val = s.getRow(i++).getCell(0).getStringCellValue();
assertEquals(str, val);
}
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(w));
}
private List<String> readStrings(String filename) throws IOException {
package org.apache.poi.xssf.model;
+import junit.framework.TestCase;
+
+import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.apache.poi.xssf.XSSFTestDataSamples;
-
-import junit.framework.TestCase;
public final class TestStylesTable extends TestCase {
private String testFile = "Formatting.xlsx";
assertEquals(1, st._getXfsSize());
assertEquals(1, st._getStyleXfsSize());
assertEquals(0, st._getNumberFormatSize());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
public void testLoadExisting() {
StylesTable st = workbook.getStylesSource();
doTestExisting(st);
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
}
+
public void testLoadSaveLoad() {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile);
assertNotNull(workbook.getStylesSource());
st = XSSFTestDataSamples.writeOutAndReadBack(workbook).getStylesSource();
doTestExisting(st);
}
+
public void doTestExisting(StylesTable st) {
// Check contents
assertNotNull(st.getCTStylesheet());
assertEquals("yyyy-mm-dd", st.getNumberFormatAt(nf1));
assertEquals(nf1, st.putNumberFormat("yyyy-mm-dd"));
assertEquals(nf2, st.putNumberFormat("yyyy-mm-DD"));
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
public void testPopulateExisting() {
assertEquals("YYYY-mm-dd", st.getNumberFormatAt(nf1));
assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
assertEquals(nf2, st.putNumberFormat("YYYY-mm-DD"));
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
}
}
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
public class TestXSSFCellStyle extends TestCase {
private StylesTable stylesTable;
assertEquals(IndexedColors.AUTOMATIC.getIndex(), style1.getFillBackgroundColor());
assertNull(style1.getFillBackgroundXSSFColor());
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb1));
+
//compatibility with HSSF
HSSFWorkbook wb2 = new HSSFWorkbook();
HSSFCellStyle style2 = wb2.createCellStyle();
public void testGetFillForegroundColor() {
-
XSSFWorkbook wb = new XSSFWorkbook();
StylesTable styles = wb.getStylesSource();
assertEquals(1, wb.getNumCellStyles());
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), style.getFillForegroundColor());
assertEquals(4, styles.getFills().size());
}
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
public void testGetFillPattern() {
assertTrue(fnt == clone.getFont());
assertTrue(18 == clone.getDataFormat());
assertEquals(2, wb.getNumberOfFonts());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
+
/**
* Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
*/
assertEquals("TestingFont", reload.getFont().getFontName());
assertEquals(fmtClone.getFormat("Test##"), reload.getDataFormat());
assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wbOrig));
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wbClone));
}
/**
StylesTable st = workbook.getStylesSource();
assertEquals(0, st._getStyleXfsSize());
-
XSSFCellStyle style = workbook.createCellStyle(); // no exception at this point
assertNull(style.getStyleXf());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
}
/**
// no exception at this point
XSSFCellStyle style = workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle();
assertNull(style.getStyleXf());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
}
public void testShrinkToFit() {
r = s.getRow(0);
assertEquals(false, r.getCell(0).getCellStyle().getShrinkToFit());
assertEquals(true, r.getCell(1).getCellStyle().getShrinkToFit());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wbOrig));
}
}
assertEquals(0, s1.getRelations().size());
assertEquals(1, s2.getRelations().size());
assertEquals(1, s3.getRelations().size());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
public void testGetCharts() throws Exception {
chart = s3.createDrawingPatriarch().getCharts().get(0);
assertEquals("Sheet 3 Chart with Title", chart.getTitle().getString());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
public void testAddChartsToNewWorkbook() throws Exception {
XSSFClientAnchor a2 = new XSSFClientAnchor(0, 0, 0, 0, 1, 11, 10, 60);
XSSFChart c2 = d1.createChart(a2);
+ assertNotNull(c2);
assertEquals(2, d1.getCharts().size());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
}
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.XSSFITestDataProvider;
+import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
assertEquals(8.5, rPr.getSzArray(0).getVal());
assertEquals(IndexedColors.BLUE_GREY.getIndex(), rPr.getColorArray(0).getIndexed());
assertEquals("Tahoma", rPr.getRFontArray(0).getVal());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
public void testAuthor() {
==================================================================== */
package org.apache.poi.xssf.usermodel;
-import java.awt.*;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
+import java.awt.Color;
import java.util.Arrays;
import java.util.List;
for(XSSFShape sh : shapes) assertNotNull(sh.getAnchor());
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
public void testNew() throws Exception {
c1.setLineStyle(1);
XSSFShapeGroup c2 = drawing.createGroup(new XSSFClientAnchor(0,0,0,0,0,0,5,5));
+ assertNotNull(c2);
XSSFSimpleShape c3 = drawing.createSimpleShape(new XSSFClientAnchor(0,0,0,0,2,2,3,4));
c3.setText(new XSSFRichTextString("Test String"));
String xml = ctDrawing.toString();
assertTrue(xml.contains("xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\""));
assertTrue(xml.contains("xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\""));
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
public void testMultipleDrawings(){
for (int i = 0; i < 3; i++) {
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
+ assertNotNull(drawing);
}
OPCPackage pkg = wb.getPackage();
assertEquals(3, pkg.getPartsByContentType(XSSFRelation.DRAWINGS.getContentType()).size());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
public void testClone() throws Exception{
assertEquals(shapes1.size(), shapes2.size());
for(int i = 0; i < shapes1.size(); i++){
- XSSFShape sh1 = (XSSFShape)shapes1.get(i);
- XSSFShape sh2 = (XSSFShape)shapes2.get(i);
+ XSSFShape sh1 = shapes1.get(i);
+ XSSFShape sh2 = shapes2.get(i);
assertTrue(sh1.getClass() == sh2.getClass());
assertEquals(sh1.getShapeProperties().toString(), sh2.getShapeProperties().toString());
}
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
assertTrue(Arrays.equals(
new byte[]{0, (byte)128, (byte)128} ,
rPr.getSolidFill().getSrgbClr().getVal()));
-
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4);
XSSFShape shape1 = drawing.createTextbox(anchor1);
+ assertNotNull(shape1);
XSSFClientAnchor anchor2 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 5);
XSSFShape shape2 = drawing.createTextbox(anchor2);
+ assertNotNull(shape2);
int pictureIndex= wb.addPicture(new byte[]{}, XSSFWorkbook.PICTURE_TYPE_PNG);
XSSFClientAnchor anchor3 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 6);
XSSFShape shape3 = drawing.createPicture(anchor3, pictureIndex);
+ assertNotNull(shape3);
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
assertEquals(shapes.get(0).getAnchor(), anchor1);
assertEquals(shapes.get(1).getAnchor(), anchor2);
assertEquals(shapes.get(2).getAnchor(), anchor3);
-
-
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
assertTrue(Arrays.equals(
new byte[]{0, (byte)128, (byte)128} ,
rPr.getSolidFill().getSrgbClr().getVal()));
-
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
* Test setText single paragraph to ensure backwards compatibility
*/
public void testSetTextSingleParagraph() {
-
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
assertTrue(Arrays.equals(
new int[] { 0, 255, 255 } ,
new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() }));
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
* Test addNewTextParagraph
*/
public void testAddNewTextParagraph() {
-
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
List<XSSFTextRun> runs = para.getTextRuns();
assertEquals(1, runs.size());
assertEquals("Line 1", runs.get(0).getText());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
* Test addNewTextParagraph using RichTextString
*/
public void testAddNewTextParagraphWithRTS() {
-
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
assertTrue(Arrays.equals(
new int[] { 0, 255, 255 } ,
new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() }));
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
* Test add multiple paragraphs and retrieve text
*/
public void testAddMultipleParagraphs() {
-
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
List<XSSFTextParagraph> paras = shape.getTextParagraphs();
assertEquals(4, paras.size()); // this should be 4 as XSSFSimpleShape creates a default paragraph (no text), and then we added 3 paragraphs
assertEquals("Line 1\nLine 2\nLine 3", shape.getText());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
* Test setting the text, then adding multiple paragraphs and retrieve text
*/
public void testSetAddMultipleParagraphs() {
-
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
List<XSSFTextParagraph> paras = shape.getTextParagraphs();
assertEquals(3, paras.size()); // this should be 3 as we overwrote the default paragraph with setText, then added 2 new paragraphs
assertEquals("Line 1\nLine 2\nLine 3", shape.getText());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(4);
assertEquals("Sheet with various pictures\n(jpeg, png, wmf, emf and pict)", textbox.getText());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
assertTrue(Arrays.equals(
new int[] { 0, 0, 255 } ,
new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() }));
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
+
/**
* Test adding and reading back paragraphs as bullet points
*/
builder.append(paraString10);
assertEquals(builder.toString(), sshape.getText());
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
/**
sb.append("\t\n\t\n\t\n\t");
assertEquals(sb.toString(), extracted);
+
+ assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
}
package org.apache.poi.xssf.usermodel;
-import java.util.Arrays;
+import static org.junit.Assert.assertArrayEquals;
+
import java.util.List;
import junit.framework.TestCase;
assertEquals(pictures.size() - 1, idx);
XSSFPictureData pict = pictures.get(idx);
assertEquals("jpeg", pict.suggestFileExtension());
- assertTrue(Arrays.equals(pictureData, pict.getData()));
+ assertArrayEquals(pictureData, pict.getData());
}
public void testNew(){
int jpegIdx = wb.addPicture(jpegData, XSSFWorkbook.PICTURE_TYPE_JPEG);
assertEquals(1, pictures.size());
assertEquals("jpeg", pictures.get(jpegIdx).suggestFileExtension());
- assertTrue(Arrays.equals(jpegData, pictures.get(jpegIdx).getData()));
+ assertArrayEquals(jpegData, pictures.get(jpegIdx).getData());
int wmfIdx = wb.addPicture(wmfData, XSSFWorkbook.PICTURE_TYPE_WMF);
assertEquals(2, pictures.size());
assertEquals("wmf", pictures.get(wmfIdx).suggestFileExtension());
- assertTrue(Arrays.equals(wmfData, pictures.get(wmfIdx).getData()));
+ assertArrayEquals(wmfData, pictures.get(wmfIdx).getData());
int pngIdx = wb.addPicture(pngData, XSSFWorkbook.PICTURE_TYPE_PNG);
assertEquals(3, pictures.size());
assertEquals("png", pictures.get(pngIdx).suggestFileExtension());
- assertTrue(Arrays.equals(pngData, pictures.get(pngIdx).getData()));
+ assertArrayEquals(pngData, pictures.get(pngIdx).getData());
//TODO finish usermodel API for XSSFPicture
XSSFPicture p1 = drawing.createPicture(new XSSFClientAnchor(), jpegIdx);
+ assertNotNull(p1);
XSSFPicture p2 = drawing.createPicture(new XSSFClientAnchor(), wmfIdx);
+ assertNotNull(p2);
XSSFPicture p3 = drawing.createPicture(new XSSFClientAnchor(), pngIdx);
+ assertNotNull(p3);
//check that the added pictures are accessible after write
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
assertEquals(3, pictures2.size());
assertEquals("jpeg", pictures2.get(jpegIdx).suggestFileExtension());
- assertTrue(Arrays.equals(jpegData, pictures2.get(jpegIdx).getData()));
+ assertArrayEquals(jpegData, pictures2.get(jpegIdx).getData());
assertEquals("wmf", pictures2.get(wmfIdx).suggestFileExtension());
- assertTrue(Arrays.equals(wmfData, pictures2.get(wmfIdx).getData()));
+ assertArrayEquals(wmfData, pictures2.get(wmfIdx).getData());
assertEquals("png", pictures2.get(pngIdx).suggestFileExtension());
- assertTrue(Arrays.equals(pngData, pictures2.get(pngIdx).getData()));
+ assertArrayEquals(pngData, pictures2.get(pngIdx).getData());
}
public void test53568(){
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53568.xlsx");
List<XSSFPictureData> pictures = wb.getAllPictures();
+ assertNotNull(pictures);
+ assertEquals(4, pictures.size());
XSSFSheet sheet1 = wb.getSheetAt(0);
List<XSSFShape> shapes1 = sheet1.createDrawingPatriarch().getShapes();
-
+ assertNotNull(shapes1);
+ assertEquals(5, shapes1.size());
+
for(int i = 0; i < wb.getNumberOfSheets(); i++){
XSSFSheet sheet = wb.getSheetAt(i);
XSSFDrawing drawing = sheet.createDrawingPatriarch();
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.CRC32;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
import org.apache.poi.ss.usermodel.BaseTestWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
Workbook read = XSSFTestDataSamples.writeOutAndReadBack(workbook);
assertSheetOrder(read, "Sheet2", "Sheet0", "Sheet1");
}
+
+ public void testBug51158() throws IOException {
+ // create a workbook
+ final XSSFWorkbook workbook = new XSSFWorkbook();
+ XSSFSheet sheet = workbook.createSheet("Test Sheet");
+ XSSFRow row = sheet.createRow(2);
+ XSSFCell cell = row.createCell(3);
+ cell.setCellValue("test1");
+
+ //XSSFCreationHelper helper = workbook.getCreationHelper();
+ //cell.setHyperlink(helper.createHyperlink(0));
+
+ XSSFComment comment = sheet.createDrawingPatriarch().createCellComment(new XSSFClientAnchor());
+ assertNotNull(comment);
+ comment.setString("some comment");
+
+// CellStyle cs = workbook.createCellStyle();
+// cs.setShrinkToFit(false);
+// row.createCell(0).setCellStyle(cs);
+
+ // write the first excel file
+ XSSFWorkbook readBack = XSSFTestDataSamples.writeOutAndReadBack(workbook);
+ assertNotNull(readBack);
+ assertEquals("test1", readBack.getSheetAt(0).getRow(2).getCell(3).getStringCellValue());
+ assertNull(readBack.getSheetAt(0).getRow(2).getCell(4));
+
+ // add a new cell to the sheet
+ cell = row.createCell(4);
+ cell.setCellValue("test2");
+
+ // write the second excel file
+ readBack = XSSFTestDataSamples.writeOutAndReadBack(workbook);
+ assertNotNull(readBack);
+ assertEquals("test1", readBack.getSheetAt(0).getRow(2).getCell(3).getStringCellValue());
+ assertEquals("test2", readBack.getSheetAt(0).getRow(2).getCell(4).getStringCellValue());
+ }
+
+ public void testBug51158a() throws IOException {
+ // create a workbook
+ final XSSFWorkbook workbook = new XSSFWorkbook();
+ workbook.createSheet("Test Sheet");
+
+ XSSFSheet sheetBack = workbook.getSheetAt(0);
+
+ // committing twice did add the XML twice without clearing the part in between
+ sheetBack.commit();
+
+ // ensure that a memory based package part does not have lingering data from previous commit() calls
+ if(sheetBack.getPackagePart() instanceof MemoryPackagePart) {
+ ((MemoryPackagePart)sheetBack.getPackagePart()).clear();
+ }
+
+ sheetBack.commit();
+
+ String str = new String(IOUtils.toByteArray(sheetBack.getPackagePart().getInputStream()));
+ System.out.println(str);
+
+ assertEquals(1, countMatches(str, "<worksheet"));
+ }
+
+ private static final int INDEX_NOT_FOUND = -1;
+
+ private static boolean isEmpty(CharSequence cs) {
+ return cs == null || cs.length() == 0;
+ }
+
+ private static int countMatches(CharSequence str, CharSequence sub) {
+ if (isEmpty(str) || isEmpty(sub)) {
+ return 0;
+ }
+ int count = 0;
+ int idx = 0;
+ while ((idx = indexOf(str, sub, idx)) != INDEX_NOT_FOUND) {
+ count++;
+ idx += sub.length();
+ }
+ return count;
+ }
+
+ private static int indexOf(CharSequence cs, CharSequence searchChar, int start) {
+ return cs.toString().indexOf(searchChar.toString(), start);
+ }
}
package org.apache.poi.xwpf.usermodel;
+import static org.junit.Assert.assertArrayEquals;
+
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public void testPictureInHeader() throws IOException
{
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
+ verifyOneHeaderPicture(sampleDoc);
+
+ XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
+ verifyOneHeaderPicture(readBack);
+ }
+
+ private void verifyOneHeaderPicture(XWPFDocument sampleDoc) {
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
XWPFHeader header = policy.getDefaultHeader();
{
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx");
byte[] jpegData = XWPFTestDataSamples.getImage("nature1.jpg");
+ assertNotNull(jpegData);
byte[] gifData = XWPFTestDataSamples.getImage("nature1.gif");
+ assertNotNull(gifData);
byte[] pngData = XWPFTestDataSamples.getImage("nature1.png");
+ assertNotNull(pngData);
List<XWPFPictureData> pictures = doc.getAllPictures();
assertEquals(0,pictures.size());
assertEquals("/word/media/image1.jpeg",jpegRel.getTargetURI().getPath());
XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
- byte[] newJPEGData = pictureDataByID.getData();
- assertEquals(newJPEGData.length,jpegData.length);
- for (int i = 0 ; i < newJPEGData.length ; i++)
- {
- assertEquals(newJPEGData[i],jpegData[i]);
- }
+ assertArrayEquals(jpegData, pictureDataByID.getData());
// Save an re-load, check it appears
doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
assertEquals(1,doc.getAllPictures().size());
assertEquals(1,doc.getAllPackagePictures().size());
- }
-
- public void testGetChecksum() {
+
+ // verify the picture that we read back in
+ pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
+ assertArrayEquals(jpegData, pictureDataByID.getData());
}
}
}
-
- private void process(XWPFParagraph paragraph){
-
- }
}