]> source.dussan.org Git - poi.git/commitdiff
Tests to ensure we create empty StyleTextPropAtoms correctly
authorNick Burch <nick@apache.org>
Sun, 19 Mar 2006 14:52:41 +0000 (14:52 +0000)
committerNick Burch <nick@apache.org>
Sun, 19 Mar 2006 14:52:41 +0000 (14:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@386976 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestRichTextRun.java

index 2d27eb1207a59c61750bbcd9301fe3e156c21b7c..69453243df3af875e0420cbb96176a148a305914 100644 (file)
@@ -31,20 +31,24 @@ import java.util.LinkedList;
  * @author Nick Burch (nick at torchbox dot com)
  */
 public class TestStyleTextPropAtom extends TestCase {
-       // From a real file: a paragraph with 4 different styles:
-       // ??? (21)
-       // ??? (17)
-       // ??? (16)
-       private byte[] data_a = new byte[] { 0, 0, 0xA1-256, 0x0F, 0x2A, 0, 0, 0,
-      0x36, 00, 00, 00, 00, 00, 00, 00, 
-         00, 00, 0x15, 00, 00, 00, 00, 00,
-      00, 00, 0x11, 00, 00, 00, 00, 00,
-         0x04, 00, 00, 00, 00, 0x05, 0x10, 00,
-      00, 00, 00, 00, 0x04, 00, 0xFF-256, 0x33, 00, 0xFE-256
+       // From a real file: a paragraph with 4 different styles
+       private byte[] data_a = new byte[] { 
+         0, 0, 0xA1-256, 0x0F, 0x2A, 0, 0, 0,
+      0x36, 00, 00, 00, // paragraph is 54 long 
+      00, 00,           // (paragraph reserved field)
+      00, 00, 00, 00,   // it doesn't have any styles
+      0x15, 00, 00, 00, // first char run is 21 long
+      00, 00, 00, 00,   // it doesn't have any styles
+      0x11, 00, 00, 00, // second char run is 17 long
+      00, 00, 0x04, 00, // font.color only
+      00, 00, 00, 0x05, // blue
+      0x10, 00, 00, 00, // third char run is 16 long
+      00, 00, 0x04, 00, // font.color only
+      0xFF-256, 0x33, 00, 0xFE-256 // red
        };
        private int data_a_text_len = 54;
 
-       // From a real file: a paragraph with text in 4 different styles:
+       // From a real file: 4 paragraphs with text in 4 different styles:
        // left aligned+bold (30)
        // centre aligned+italic+blue (28)
        // right aligned+red (25)
@@ -365,6 +369,46 @@ public class TestStyleTextPropAtom extends TestCase {
                        // Good, as expected
                }
        }
+       
+       /**
+        * Try to recreate an existing StyleTextPropAtom from the empty
+        *  constructor, and setting the required properties
+        */
+       public void testCreateFromScatch() throws Exception {
+               // Start with an empty one
+               StyleTextPropAtom stpa = new StyleTextPropAtom(54);
+               
+               // Don't need to touch the paragraph styles
+               // Add two more character styles
+               LinkedList cs = stpa.getCharacterStyles();
+               
+               // First char style is boring, and 21 long
+               TextPropCollection tpca = (TextPropCollection)cs.get(0);
+               tpca.updateTextSize(21);
+               
+               // Second char style is coloured, 00 00 00 05, and 17 long
+               TextPropCollection tpcb = new TextPropCollection(17);
+               TextProp tpb = tpcb.addWithName("font.color");
+               tpb.setValue(0x05000000);
+               cs.add(tpcb);
+               
+               // Third char style is coloured, FF 33 00 FE, and 16 long
+               TextPropCollection tpcc = new TextPropCollection(16);
+               TextProp tpc = tpcc.addWithName("font.color");
+               tpc.setValue(0xFE0033FF);
+               cs.add(tpcc);
+               
+               // Should now be the same as data_a
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               stpa.writeOut(baos);
+               byte[] b = baos.toByteArray();
+
+               assertEquals(data_a.length, b.length);
+               for(int i=0; i<data_a.length; i++) {
+                       System.out.println(i + "\t" + b[i] + "\t" + data_a[i]);
+                       assertEquals(data_a[i],b[i]);
+               }
+       }
 
 
        public void testWrite() throws Exception {
index c2dbe5731dabfe2ac82cc546718da344179c7bf8..76da976312dda6891f039e1f2551bec68333773c 100644 (file)
@@ -1,5 +1,8 @@
 package org.apache.poi.hslf.usermodel;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
 import org.apache.poi.hslf.HSLFSlideShow;
 import org.apache.poi.hslf.model.Slide;
 import org.apache.poi.hslf.model.TextRun;
@@ -152,4 +155,43 @@ public class TestRichTextRun extends TestCase {
                assertNotNull(rtr._getRawParagraphStyle());
                assertEquals(2, ss.getFontCollection().getChildRecords().length);
        }
+       
+       public void testChangeWriteRead() throws Exception {
+               HSLFSlideShow[] h = new HSLFSlideShow[] { hss, hssRichA, hssRichB };
+               Slide[] s = new Slide[] { ss.getSlides()[0], ssRichA.getSlides()[0], ssRichB.getSlides()[0] };
+               
+               for(int i=0; i<h.length; i++) {
+                       // Change
+                       Slide slideOne = s[i];
+                       TextRun[] textRuns = slideOne.getTextRuns();
+                       RichTextRun rtr = textRuns[0].getRichTextRuns()[0];
+                       
+                       rtr.setBold(true);
+                       rtr.setFontSize(18);
+                       rtr.setFontName("Courier");
+                       
+                       // Write out and back in
+                       ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                       h[i].write(baos);
+                       ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+                       
+                       HSLFSlideShow readHSLF = new HSLFSlideShow(bais);
+                       SlideShow readS = new SlideShow(readHSLF);
+                       
+                       // Tweak existing one again, to ensure really worked
+                       rtr.setBold(false);
+                       rtr.setFontSize(17);
+                       rtr.setFontName("CourierZZ");
+                       
+                       
+                       // Check written out/back ing one contains modifications
+                       Slide slideOneRR = readS.getSlides()[0];
+                       TextRun[] textRunsRR = slideOneRR.getTextRuns();
+                       RichTextRun rtrRRa = textRunsRR[0].getRichTextRuns()[0];
+                       
+                       assertEquals(true, rtrRRa.isBold());
+                       assertEquals(18, rtrRRa.getFontSize());
+                       assertEquals("Courier", rtrRRa.getFontName());
+               }
+       }
 }