01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17:
18: package org.apache.poi.hslf;
19:
20: import junit.framework.TestCase;
21: import java.io.*;
22: import java.util.*;
23: import org.apache.poi.hslf.record.*;
24: import org.apache.poi.poifs.filesystem.*;
25:
26: /**
27: * Tests that HSLFSlideShow writes the powerpoint bit of data back out
28: * in a sane manner - i.e. records end up in the right place
29: *
30: * @author Nick Burch (nick at torchbox dot com)
31: */
32: public class TestReWriteSanity extends TestCase {
33: // HSLFSlideShow primed on the test data
34: private HSLFSlideShow ss;
35: // POIFS primed on the test data
36: private POIFSFileSystem pfs;
37:
38: public TestReWriteSanity() throws Exception {
39: String dirname = System.getProperty("HSLF.testdata.path");
40: String filename = dirname + "/basic_test_ppt_file.ppt";
41: FileInputStream fis = new FileInputStream(filename);
42: pfs = new POIFSFileSystem(fis);
43: ss = new HSLFSlideShow(pfs);
44: }
45:
46: public void testUserEditAtomsRight() throws Exception {
47: // Write out to a byte array
48: ByteArrayOutputStream baos = new ByteArrayOutputStream();
49: ss.write(baos);
50:
51: // Build an input stream of it
52: ByteArrayInputStream bais = new ByteArrayInputStream(baos
53: .toByteArray());
54:
55: // Create a new one from that
56: HSLFSlideShow wss = new HSLFSlideShow(bais);
57:
58: // Find the location of the PersistPtrIncrementalBlocks and
59: // UserEditAtoms
60: Record[] r = wss.getRecords();
61: Hashtable pp = new Hashtable();
62: Hashtable ue = new Hashtable();
63: ue.put(new Integer(0), new Integer(0)); // Will show 0 if first
64: int pos = 0;
65: int lastUEPos = -1;
66:
67: for (int i = 0; i < r.length; i++) {
68: if (r[i] instanceof PersistPtrHolder) {
69: pp.put(new Integer(pos), r[i]);
70: }
71: if (r[i] instanceof UserEditAtom) {
72: ue.put(new Integer(pos), r[i]);
73: lastUEPos = pos;
74: }
75:
76: ByteArrayOutputStream bc = new ByteArrayOutputStream();
77: r[i].writeOut(bc);
78: pos += bc.size();
79: }
80:
81: // Check that the UserEditAtom's point to right stuff
82: for (int i = 0; i < r.length; i++) {
83: if (r[i] instanceof UserEditAtom) {
84: UserEditAtom uea = (UserEditAtom) r[i];
85: int luPos = uea.getLastUserEditAtomOffset();
86: int ppPos = uea.getPersistPointersOffset();
87:
88: assertTrue(pp.containsKey(new Integer(ppPos)));
89: assertTrue(ue.containsKey(new Integer(luPos)));
90: }
91: }
92:
93: // Check that the CurrentUserAtom points to the right UserEditAtom
94: CurrentUserAtom cua = wss.getCurrentUserAtom();
95: int listedUEPos = (int) cua.getCurrentEditOffset();
96: assertEquals(lastUEPos, listedUEPos);
97: }
98: }
|