Improve error message slightly, add tests to verify things failing in Gump-build currently

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1692836 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2015-07-27 08:44:04 +00:00
parent b2e18cf5ed
commit 4728df5b6a
5 changed files with 108 additions and 21 deletions

View File

@ -73,7 +73,7 @@ public class PresetGeometries extends LinkedHashMap<String, CustomGeometry> {
cntElem++;
if(containsKey(name)) {
LOG.log(POILogger.WARN, "Duplicate definoition of " + name);
LOG.log(POILogger.WARN, "Duplicate definition of " + name);
}
put(name, new CustomGeometry(cus));
}

View File

@ -577,7 +577,7 @@ public abstract class XSLFSimpleShape extends XSLFShape implements SimpleShape {
String name = spPr.getPrstGeom().getPrst().toString();
geom = dict.get(name);
if(geom == null) {
throw new IllegalStateException("Unknown shape geometry: " + name);
throw new IllegalStateException("Unknown shape geometry: " + name + ", available geometries are: " + dict.keySet());
}
} else if (spPr.isSetCustGeom()){
XMLStreamReader staxReader = spPr.getCustGeom().newXMLStreamReader();

View File

@ -22,12 +22,9 @@ package org.apache.poi.xslf.usermodel;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.poi.sl.draw.Drawable;
import org.apache.poi.util.JvmBugs;
import org.apache.poi.xslf.XSLFTestDataSamples;
@ -43,16 +40,20 @@ public class TestPPTX2PNG {
public void render() throws Exception {
String[] testFiles = {"backgrounds.pptx","layouts.pptx", "sample.pptx", "shapes.pptx", "themes.pptx",};
for(String sampleFile : testFiles){
XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument(sampleFile);
Dimension pg = pptx.getPageSize();
int slideNo=1;
for(XSLFSlide slide : pptx.getSlides()){
BufferedImage img = new BufferedImage(pg.width, pg.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = img.createGraphics();
fixFonts(graphics);
slide.draw(graphics);
// ImageIO.write(img, "PNG", new File("build/tmp/"+sampleFile.replaceFirst(".pptx?", "-")+slideNo+".png"));
slideNo++;
try {
XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument(sampleFile);
Dimension pg = pptx.getPageSize();
//int slideNo=1;
for(XSLFSlide slide : pptx.getSlides()){
BufferedImage img = new BufferedImage(pg.width, pg.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = img.createGraphics();
fixFonts(graphics);
slide.draw(graphics);
// ImageIO.write(img, "PNG", new File("build/tmp/"+sampleFile.replaceFirst(".pptx?", "-")+slideNo+".png"));
//slideNo++;
}
} catch (IllegalStateException e) {
throw new IllegalStateException("While reading file " + sampleFile, e);
}
}
}

View File

@ -19,14 +19,25 @@ package org.apache.poi.xslf.usermodel;
import static org.junit.Assert.*;
import java.awt.Color;
import java.io.IOException;
import java.util.List;
import org.apache.poi.sl.draw.geom.TestPresetGeometries;
import org.apache.poi.sl.usermodel.StrokeStyle.LineCap;
import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
import org.apache.poi.util.Units;
import org.apache.poi.xslf.XSLFTestDataSamples;
import org.junit.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import org.openxmlformats.schemas.drawingml.x2006.main.CTEffectStyleItem;
import org.openxmlformats.schemas.drawingml.x2006.main.CTEffectStyleList;
import org.openxmlformats.schemas.drawingml.x2006.main.CTOuterShadowEffect;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTStyleMatrix;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineCap;
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
/**
* @author Yegor Kozlov
@ -34,7 +45,7 @@ import org.openxmlformats.schemas.drawingml.x2006.main.*;
public class TestXSLFSimpleShape {
@Test
public void testLineStyles() {
public void testLineStyles() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
@ -121,10 +132,12 @@ public class TestXSLFSimpleShape {
assertEquals(null, ln3.getLineDash());
ln3.setLineCap(null);
assertEquals(null, ln3.getLineDash());
ppt.close();
}
@Test
public void testFill() {
public void testFill() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
@ -146,6 +159,7 @@ public class TestXSLFSimpleShape {
shape.setFillColor(null);
assertNull(shape.getFillColor());
assertFalse(shape.getSpPr().isSetSolidFill());
ppt.close();
}
@Test
@ -252,7 +266,7 @@ public class TestXSLFSimpleShape {
@SuppressWarnings({ "deprecation", "unused" })
@Test
public void testShadowEffects(){
public void testShadowEffects() throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
CTStyleMatrix styleMatrix = slide.getTheme().getXmlObject().getThemeElements().getFmtScheme();
@ -261,5 +275,65 @@ public class TestXSLFSimpleShape {
for(CTEffectStyleItem ef : lst.getEffectStyleArray()){
CTOuterShadowEffect obj = ef.getEffectLst().getOuterShdw();
}
ppt.close();
}
@Test
public void testValidGeometry() throws Exception {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFSimpleShape shape = slide.createAutoShape();
CTShapeProperties spPr = shape.getSpPr();
CTPresetGeometry2D prstGeom = CTPresetGeometry2D.Factory.newInstance();
prstGeom.setPrst(STShapeType.Enum.forInt(1));
assertNotNull(prstGeom.getPrst());
assertNotNull(prstGeom.getPrst().toString());
assertNotNull(spPr.getPrstGeom());
spPr.setPrstGeom(prstGeom);
assertNotNull(spPr.getPrstGeom().getPrst());
assertNotNull(spPr.getPrstGeom().getPrst().toString());
assertNotNull(shape.getGeometry());
ppt.close();
}
@Test
public void testInvalidGeometry() throws Exception {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFSimpleShape shape = slide.createAutoShape();
CTShapeProperties spPr = shape.getSpPr();
CTPresetGeometry2D prstGeom = CTPresetGeometry2D.Factory.newInstance();
prstGeom.setPrst(STShapeType.Enum.forInt(1));
assertNotNull(prstGeom.getPrst());
assertNotNull(prstGeom.getPrst().toString());
assertNotNull(spPr.getPrstGeom());
spPr.setPrstGeom(prstGeom);
assertNotNull(spPr.getPrstGeom().getPrst());
assertNotNull(spPr.getPrstGeom().getPrst().toString());
try {
// cause the geometries to be not found
TestPresetGeometries.clearPreset();
try {
shape.getGeometry();
fail("Should fail without the geometry");
} catch (IllegalStateException e) {
assertTrue(e.getMessage(), e.getMessage().contains("line"));
}
} finally {
// reset to not affect other tests
TestPresetGeometries.resetPreset();
}
ppt.close();
}
}

View File

@ -16,7 +16,7 @@
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.xslf.geom;
package org.apache.poi.sl.draw.geom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -25,7 +25,6 @@ import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.util.Map;
import org.apache.poi.sl.draw.geom.*;
import org.junit.Test;
/**
@ -54,4 +53,17 @@ public class TestPresetGeometries {
}
}
}
// helper methods to adjust list of presets for other tests
public static void clearPreset() {
// ensure that we are initialized
assertNotNull(PresetGeometries.getInstance());
// test handling if some presets are not found
PresetGeometries._inst.clear();
}
public static void resetPreset() {
PresetGeometries._inst = null;
}
}