001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hpsf.basic;
019:
020: import java.io.File;
021: import java.io.FileFilter;
022: import java.io.FileInputStream;
023: import java.io.FileNotFoundException;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026: import java.io.OutputStream;
027: import java.util.Calendar;
028: import java.util.Date;
029: import java.util.GregorianCalendar;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.Map.Entry;
034:
035: import junit.framework.TestCase;
036:
037: import org.apache.poi.hpsf.CustomProperties;
038: import org.apache.poi.hpsf.CustomProperty;
039: import org.apache.poi.hpsf.DocumentSummaryInformation;
040: import org.apache.poi.hpsf.MarkUnsupportedException;
041: import org.apache.poi.hpsf.MutableProperty;
042: import org.apache.poi.hpsf.MutableSection;
043: import org.apache.poi.hpsf.NoPropertySetStreamException;
044: import org.apache.poi.hpsf.PropertySet;
045: import org.apache.poi.hpsf.PropertySetFactory;
046: import org.apache.poi.hpsf.SummaryInformation;
047: import org.apache.poi.hpsf.UnexpectedPropertySetTypeException;
048: import org.apache.poi.hpsf.Variant;
049: import org.apache.poi.hpsf.VariantSupport;
050: import org.apache.poi.hpsf.WritingNotSupportedException;
051: import org.apache.poi.hpsf.wellknown.SectionIDMap;
052: import org.apache.poi.poifs.filesystem.DirectoryEntry;
053: import org.apache.poi.poifs.filesystem.DocumentEntry;
054: import org.apache.poi.poifs.filesystem.DocumentInputStream;
055: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
056:
057: /**
058: * <p>Tests HPSF's high-level writing functionality for the well-known property
059: * set "SummaryInformation" and "DocumentSummaryInformation".</p>
060: *
061: * @author Rainer Klute
062: * <a href="mailto:klute@rainer-klute.de">klute@rainer-klute.de</a>
063: * @since 2006-02-01
064: * @version $Id: TestWriteWellKnown.java 489730 2006-12-22 19:18:16Z bayard $
065: */
066: public class TestWriteWellKnown extends TestCase {
067:
068: private static final String POI_FS = "TestWriteWellKnown.doc";
069:
070: /**
071: * <p>Constructor</p>
072: *
073: * @param name the test case's name
074: */
075: public TestWriteWellKnown(final String name) {
076: super (name);
077: }
078:
079: /**
080: * @see TestCase#setUp()
081: */
082: public void setUp() {
083: VariantSupport.setLogUnsupportedTypes(false);
084: }
085:
086: /**
087: * <p>This test method checks whether DocumentSummary information streams
088: * can be read. This is done by opening all "Test*" files in the directrory
089: * pointed to by the "HPSF.testdata.path" system property, trying to extract
090: * the document summary information stream in the root directory and calling
091: * its get... methods.</p>
092: * @throws IOException
093: * @throws FileNotFoundException
094: * @throws MarkUnsupportedException
095: * @throws NoPropertySetStreamException
096: * @throws UnexpectedPropertySetTypeException
097: */
098: public void testReadDocumentSummaryInformation()
099: throws FileNotFoundException, IOException,
100: NoPropertySetStreamException, MarkUnsupportedException,
101: UnexpectedPropertySetTypeException {
102: final String dataDirName = System
103: .getProperty("HPSF.testdata.path");
104: final File dataDir = new File(dataDirName);
105: final File[] docs = dataDir.listFiles(new FileFilter() {
106: public boolean accept(final File file) {
107: return file.isFile()
108: && file.getName().startsWith("Test");
109: }
110: });
111: for (int i = 0; i < docs.length; i++) {
112: final File doc = docs[i];
113: System.out.println("Reading file " + doc);
114:
115: /* Read a test document <em>doc</em> into a POI filesystem. */
116: final POIFSFileSystem poifs = new POIFSFileSystem(
117: new FileInputStream(doc));
118: final DirectoryEntry dir = poifs.getRoot();
119: DocumentEntry dsiEntry = null;
120: try {
121: dsiEntry = (DocumentEntry) dir
122: .getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
123: } catch (FileNotFoundException ex) {
124: /*
125: * A missing document summary information stream is not an error
126: * and therefore silently ignored here.
127: */
128: }
129:
130: /*
131: * If there is a document summry information stream, read it from
132: * the POI filesystem.
133: */
134: if (dsiEntry != null) {
135: final DocumentInputStream dis = new DocumentInputStream(
136: dsiEntry);
137: final PropertySet ps = new PropertySet(dis);
138: final DocumentSummaryInformation dsi = new DocumentSummaryInformation(
139: ps);
140:
141: /* Execute the get... methods. */
142: dsi.getByteCount();
143: dsi.getByteOrder();
144: dsi.getCategory();
145: dsi.getCompany();
146: dsi.getCustomProperties();
147: // FIXME dsi.getDocparts();
148: // FIXME dsi.getHeadingPair();
149: dsi.getHiddenCount();
150: dsi.getLineCount();
151: dsi.getLinksDirty();
152: dsi.getManager();
153: dsi.getMMClipCount();
154: dsi.getNoteCount();
155: dsi.getParCount();
156: dsi.getPresentationFormat();
157: dsi.getScale();
158: dsi.getSlideCount();
159: }
160: }
161: }
162:
163: /**
164: * <p>This test method test the writing of properties in the well-known
165: * property set streams "SummaryInformation" and
166: * "DocumentSummaryInformation" by performing the following steps:</p>
167: *
168: * <ol>
169: *
170: * <li><p>Read a test document <em>doc1</em> into a POI filesystem.</p></li>
171: *
172: * <li><p>Read the summary information stream and the document summary
173: * information stream from the POI filesystem.</p></li>
174: *
175: * <li><p>Write all properties supported by HPSF to the summary
176: * information (e.g. author, edit date, application name) and to the
177: * document summary information (e.g. company, manager).</p></li>
178: *
179: * <li><p>Write the summary information stream and the document summary
180: * information stream to the POI filesystem.</p></li>
181: *
182: * <li><p>Write the POI filesystem to a (temporary) file <em>doc2</em>
183: * and close the latter.</p></li>
184: *
185: * <li><p>Open <em>doc2</em> for reading and check summary information
186: * and document summary information. All properties written before must be
187: * found in the property streams of <em>doc2</em> and have the correct
188: * values.</p></li>
189: *
190: * <li><p>Remove all properties supported by HPSF from the summary
191: * information (e.g. author, edit date, application name) and from the
192: * document summary information (e.g. company, manager).</p></li>
193: *
194: * <li><p>Write the summary information stream and the document summary
195: * information stream to the POI filesystem.</p></li>
196: *
197: * <li><p>Write the POI filesystem to a (temporary) file <em>doc3</em>
198: * and close the latter.</p></li>
199: *
200: * <li><p>Open <em>doc3</em> for reading and check summary information
201: * and document summary information. All properties removed before must not
202: * be found in the property streams of <em>doc3</em>.</p></li> </ol>
203: *
204: * @throws IOException if some I/O error occurred.
205: * @throws MarkUnsupportedException
206: * @throws NoPropertySetStreamException
207: * @throws UnexpectedPropertySetTypeException
208: * @throws WritingNotSupportedException
209: */
210: public void testWriteWellKnown() throws IOException,
211: NoPropertySetStreamException, MarkUnsupportedException,
212: UnexpectedPropertySetTypeException,
213: WritingNotSupportedException {
214: final String dataDirName = System
215: .getProperty("HPSF.testdata.path");
216: final File dataDir = new File(dataDirName);
217: final File doc1 = new File(dataDir, POI_FS);
218:
219: /* Read a test document <em>doc1</em> into a POI filesystem. */
220: POIFSFileSystem poifs = new POIFSFileSystem(
221: new FileInputStream(doc1));
222: DirectoryEntry dir = poifs.getRoot();
223: DocumentEntry siEntry = (DocumentEntry) dir
224: .getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
225: DocumentEntry dsiEntry = (DocumentEntry) dir
226: .getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
227:
228: /*
229: * Read the summary information stream and the document summary
230: * information stream from the POI filesystem.
231: *
232: * Please note that the result consists of SummaryInformation and
233: * DocumentSummaryInformation instances which are in memory only. To
234: * make them permanent they have to be written to a POI filesystem
235: * explicitly (overwriting the former contents). Then the POI filesystem
236: * should be saved to a file.
237: */
238: DocumentInputStream dis = new DocumentInputStream(siEntry);
239: PropertySet ps = new PropertySet(dis);
240: SummaryInformation si = new SummaryInformation(ps);
241: dis = new DocumentInputStream(dsiEntry);
242: ps = new PropertySet(dis);
243: DocumentSummaryInformation dsi = new DocumentSummaryInformation(
244: ps);
245:
246: /*
247: * Write all properties supported by HPSF to the summary information
248: * (e.g. author, edit date, application name) and to the document
249: * summary information (e.g. company, manager).
250: */
251: Calendar cal = new GregorianCalendar();
252: cal.set(2000, 6, 6, 6, 6, 6);
253: final long time1 = cal.getTimeInMillis();
254: cal.set(2001, 7, 7, 7, 7, 7);
255: final long time2 = cal.getTimeInMillis();
256: cal.set(2002, 8, 8, 8, 8, 8);
257: final long time3 = cal.getTimeInMillis();
258:
259: int nr = 4711;
260: final String P_APPLICATION_NAME = "ApplicationName";
261: final String P_AUTHOR = "Author";
262: final int P_CHAR_COUNT = ++nr;
263: final String P_COMMENTS = "Comments";
264: final Date P_CREATE_DATE_TIME = new Date(time1);
265: final long P_EDIT_TIME = ++nr * 1000 * 10;
266: final String P_KEYWORDS = "Keywords";
267: final String P_LAST_AUTHOR = "LastAuthor";
268: final Date P_LAST_PRINTED = new Date(time2);
269: final Date P_LAST_SAVE_DATE_TIME = new Date(time3);
270: final int P_PAGE_COUNT = ++nr;
271: final String P_REV_NUMBER = "RevNumber";
272: final int P_SECURITY = 1;
273: final String P_SUBJECT = "Subject";
274: final String P_TEMPLATE = "Template";
275: // FIXME (byte array properties not yet implemented): final byte[] P_THUMBNAIL = new byte[123];
276: final String P_TITLE = "Title";
277: final int P_WORD_COUNT = ++nr;
278:
279: final int P_BYTE_COUNT = ++nr;
280: final String P_CATEGORY = "Category";
281: final String P_COMPANY = "Company";
282: // FIXME (byte array properties not yet implemented): final byte[] P_DOCPARTS = new byte[123];
283: // FIXME (byte array properties not yet implemented): final byte[] P_HEADING_PAIR = new byte[123];
284: final int P_HIDDEN_COUNT = ++nr;
285: final int P_LINE_COUNT = ++nr;
286: final boolean P_LINKS_DIRTY = true;
287: final String P_MANAGER = "Manager";
288: final int P_MM_CLIP_COUNT = ++nr;
289: final int P_NOTE_COUNT = ++nr;
290: final int P_PAR_COUNT = ++nr;
291: final String P_PRESENTATION_FORMAT = "PresentationFormat";
292: final boolean P_SCALE = false;
293: final int P_SLIDE_COUNT = ++nr;
294: final Date now = new Date();
295:
296: final Integer POSITIVE_INTEGER = new Integer(2222);
297: final Long POSITIVE_LONG = new Long(3333);
298: final Double POSITIVE_DOUBLE = new Double(4444);
299: final Integer NEGATIVE_INTEGER = new Integer(2222);
300: final Long NEGATIVE_LONG = new Long(3333);
301: final Double NEGATIVE_DOUBLE = new Double(4444);
302:
303: final Integer MAX_INTEGER = new Integer(Integer.MAX_VALUE);
304: final Integer MIN_INTEGER = new Integer(Integer.MIN_VALUE);
305: final Long MAX_LONG = new Long(Long.MAX_VALUE);
306: final Long MIN_LONG = new Long(Long.MIN_VALUE);
307: final Double MAX_DOUBLE = new Double(Double.MAX_VALUE);
308: final Double MIN_DOUBLE = new Double(Double.MIN_VALUE);
309:
310: si.setApplicationName(P_APPLICATION_NAME);
311: si.setAuthor(P_AUTHOR);
312: si.setCharCount(P_CHAR_COUNT);
313: si.setComments(P_COMMENTS);
314: si.setCreateDateTime(P_CREATE_DATE_TIME);
315: si.setEditTime(P_EDIT_TIME);
316: si.setKeywords(P_KEYWORDS);
317: si.setLastAuthor(P_LAST_AUTHOR);
318: si.setLastPrinted(P_LAST_PRINTED);
319: si.setLastSaveDateTime(P_LAST_SAVE_DATE_TIME);
320: si.setPageCount(P_PAGE_COUNT);
321: si.setRevNumber(P_REV_NUMBER);
322: si.setSecurity(P_SECURITY);
323: si.setSubject(P_SUBJECT);
324: si.setTemplate(P_TEMPLATE);
325: // FIXME (byte array properties not yet implemented): si.setThumbnail(P_THUMBNAIL);
326: si.setTitle(P_TITLE);
327: si.setWordCount(P_WORD_COUNT);
328:
329: dsi.setByteCount(P_BYTE_COUNT);
330: dsi.setCategory(P_CATEGORY);
331: dsi.setCompany(P_COMPANY);
332: // FIXME (byte array properties not yet implemented): dsi.setDocparts(P_DOCPARTS);
333: // FIXME (byte array properties not yet implemented): dsi.setHeadingPair(P_HEADING_PAIR);
334: dsi.setHiddenCount(P_HIDDEN_COUNT);
335: dsi.setLineCount(P_LINE_COUNT);
336: dsi.setLinksDirty(P_LINKS_DIRTY);
337: dsi.setManager(P_MANAGER);
338: dsi.setMMClipCount(P_MM_CLIP_COUNT);
339: dsi.setNoteCount(P_NOTE_COUNT);
340: dsi.setParCount(P_PAR_COUNT);
341: dsi.setPresentationFormat(P_PRESENTATION_FORMAT);
342: dsi.setScale(P_SCALE);
343: dsi.setSlideCount(P_SLIDE_COUNT);
344:
345: CustomProperties customProperties = dsi.getCustomProperties();
346: if (customProperties == null)
347: customProperties = new CustomProperties();
348: customProperties.put("Schlüssel ä", "Wert ä");
349: customProperties.put("Schlüssel äö", "Wert äö");
350: customProperties.put("Schlüssel äöü", "Wert äöü");
351: customProperties.put("Schlüssel äöüß", "Wert äöüß");
352: customProperties.put("positive_Integer", POSITIVE_INTEGER);
353: customProperties.put("positive_Long", POSITIVE_LONG);
354: customProperties.put("positive_Double", POSITIVE_DOUBLE);
355: customProperties.put("negative_Integer", NEGATIVE_INTEGER);
356: customProperties.put("negative_Long", NEGATIVE_LONG);
357: customProperties.put("negative_Double", NEGATIVE_DOUBLE);
358: customProperties.put("Boolean", new Boolean(true));
359: customProperties.put("Date", now);
360: customProperties.put("max_Integer", MAX_INTEGER);
361: customProperties.put("min_Integer", MIN_INTEGER);
362: customProperties.put("max_Long", MAX_LONG);
363: customProperties.put("min_Long", MIN_LONG);
364: customProperties.put("max_Double", MAX_DOUBLE);
365: customProperties.put("min_Double", MIN_DOUBLE);
366: dsi.setCustomProperties(customProperties);
367:
368: /* Write the summary information stream and the document summary
369: * information stream to the POI filesystem. */
370: si.write(dir, siEntry.getName());
371: dsi.write(dir, dsiEntry.getName());
372:
373: /* Write the POI filesystem to a (temporary) file <em>doc2</em>
374: * and close the latter. */
375: final File doc2 = File.createTempFile("POI_HPSF_Test.", ".tmp");
376: doc2.deleteOnExit();
377: OutputStream out = new FileOutputStream(doc2);
378: poifs.writeFilesystem(out);
379: out.close();
380:
381: /*
382: * Open <em>doc2</em> for reading and check summary information and
383: * document summary information. All properties written before must be
384: * found in the property streams of <em>doc2</em> and have the correct
385: * values.
386: */
387: poifs = new POIFSFileSystem(new FileInputStream(doc2));
388: dir = poifs.getRoot();
389: siEntry = (DocumentEntry) dir
390: .getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
391: dsiEntry = (DocumentEntry) dir
392: .getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
393:
394: dis = new DocumentInputStream(siEntry);
395: ps = new PropertySet(dis);
396: si = new SummaryInformation(ps);
397: dis = new DocumentInputStream(dsiEntry);
398: ps = new PropertySet(dis);
399: dsi = new DocumentSummaryInformation(ps);
400:
401: assertEquals(P_APPLICATION_NAME, si.getApplicationName());
402: assertEquals(P_AUTHOR, si.getAuthor());
403: assertEquals(P_CHAR_COUNT, si.getCharCount());
404: assertEquals(P_COMMENTS, si.getComments());
405: assertEquals(P_CREATE_DATE_TIME, si.getCreateDateTime());
406: assertEquals(P_EDIT_TIME, si.getEditTime());
407: assertEquals(P_KEYWORDS, si.getKeywords());
408: assertEquals(P_LAST_AUTHOR, si.getLastAuthor());
409: assertEquals(P_LAST_PRINTED, si.getLastPrinted());
410: assertEquals(P_LAST_SAVE_DATE_TIME, si.getLastSaveDateTime());
411: assertEquals(P_PAGE_COUNT, si.getPageCount());
412: assertEquals(P_REV_NUMBER, si.getRevNumber());
413: assertEquals(P_SECURITY, si.getSecurity());
414: assertEquals(P_SUBJECT, si.getSubject());
415: assertEquals(P_TEMPLATE, si.getTemplate());
416: // FIXME (byte array properties not yet implemented): assertEquals(P_THUMBNAIL, si.getThumbnail());
417: assertEquals(P_TITLE, si.getTitle());
418: assertEquals(P_WORD_COUNT, si.getWordCount());
419:
420: assertEquals(P_BYTE_COUNT, dsi.getByteCount());
421: assertEquals(P_CATEGORY, dsi.getCategory());
422: assertEquals(P_COMPANY, dsi.getCompany());
423: // FIXME (byte array properties not yet implemented): assertEquals(P_, dsi.getDocparts());
424: // FIXME (byte array properties not yet implemented): assertEquals(P_, dsi.getHeadingPair());
425: assertEquals(P_HIDDEN_COUNT, dsi.getHiddenCount());
426: assertEquals(P_LINE_COUNT, dsi.getLineCount());
427: assertEquals(P_LINKS_DIRTY, dsi.getLinksDirty());
428: assertEquals(P_MANAGER, dsi.getManager());
429: assertEquals(P_MM_CLIP_COUNT, dsi.getMMClipCount());
430: assertEquals(P_NOTE_COUNT, dsi.getNoteCount());
431: assertEquals(P_PAR_COUNT, dsi.getParCount());
432: assertEquals(P_PRESENTATION_FORMAT, dsi.getPresentationFormat());
433: assertEquals(P_SCALE, dsi.getScale());
434: assertEquals(P_SLIDE_COUNT, dsi.getSlideCount());
435:
436: final CustomProperties cps = dsi.getCustomProperties();
437: assertEquals(customProperties, cps);
438: assertNull(cps.get("No value available"));
439: assertEquals("Wert ä", cps.get("Schlüssel ä"));
440: assertEquals("Wert äö", cps.get("Schlüssel äö"));
441: assertEquals("Wert äöü", cps.get("Schlüssel äöü"));
442: assertEquals("Wert äöüß", cps.get("Schlüssel äöüß"));
443: assertEquals(POSITIVE_INTEGER, cps.get("positive_Integer"));
444: assertEquals(POSITIVE_LONG, cps.get("positive_Long"));
445: assertEquals(POSITIVE_DOUBLE, cps.get("positive_Double"));
446: assertEquals(NEGATIVE_INTEGER, cps.get("negative_Integer"));
447: assertEquals(NEGATIVE_LONG, cps.get("negative_Long"));
448: assertEquals(NEGATIVE_DOUBLE, cps.get("negative_Double"));
449: assertEquals(new Boolean(true), cps.get("Boolean"));
450: assertEquals(now, cps.get("Date"));
451: assertEquals(MAX_INTEGER, cps.get("max_Integer"));
452: assertEquals(MIN_INTEGER, cps.get("min_Integer"));
453: assertEquals(MAX_LONG, cps.get("max_Long"));
454: assertEquals(MIN_LONG, cps.get("min_Long"));
455: assertEquals(MAX_DOUBLE, cps.get("max_Double"));
456: assertEquals(MIN_DOUBLE, cps.get("min_Double"));
457:
458: /* Remove all properties supported by HPSF from the summary
459: * information (e.g. author, edit date, application name) and from the
460: * document summary information (e.g. company, manager). */
461: si.removeApplicationName();
462: si.removeAuthor();
463: si.removeCharCount();
464: si.removeComments();
465: si.removeCreateDateTime();
466: si.removeEditTime();
467: si.removeKeywords();
468: si.removeLastAuthor();
469: si.removeLastPrinted();
470: si.removeLastSaveDateTime();
471: si.removePageCount();
472: si.removeRevNumber();
473: si.removeSecurity();
474: si.removeSubject();
475: si.removeTemplate();
476: si.removeThumbnail();
477: si.removeTitle();
478: si.removeWordCount();
479:
480: dsi.removeByteCount();
481: dsi.removeCategory();
482: dsi.removeCompany();
483: dsi.removeCustomProperties();
484: dsi.removeDocparts();
485: dsi.removeHeadingPair();
486: dsi.removeHiddenCount();
487: dsi.removeLineCount();
488: dsi.removeLinksDirty();
489: dsi.removeManager();
490: dsi.removeMMClipCount();
491: dsi.removeNoteCount();
492: dsi.removeParCount();
493: dsi.removePresentationFormat();
494: dsi.removeScale();
495: dsi.removeSlideCount();
496:
497: /*
498: * <li><p>Write the summary information stream and the document summary
499: * information stream to the POI filesystem. */
500: si.write(dir, siEntry.getName());
501: dsi.write(dir, dsiEntry.getName());
502:
503: /*
504: * <li><p>Write the POI filesystem to a (temporary) file <em>doc3</em>
505: * and close the latter. */
506: final File doc3 = File.createTempFile("POI_HPSF_Test.", ".tmp");
507: doc3.deleteOnExit();
508: out = new FileOutputStream(doc3);
509: poifs.writeFilesystem(out);
510: out.close();
511:
512: /*
513: * Open <em>doc3</em> for reading and check summary information
514: * and document summary information. All properties removed before must not
515: * be found in the property streams of <em>doc3</em>.
516: */
517: poifs = new POIFSFileSystem(new FileInputStream(doc3));
518: dir = poifs.getRoot();
519: siEntry = (DocumentEntry) dir
520: .getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
521: dsiEntry = (DocumentEntry) dir
522: .getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
523:
524: dis = new DocumentInputStream(siEntry);
525: ps = new PropertySet(dis);
526: si = new SummaryInformation(ps);
527: dis = new DocumentInputStream(dsiEntry);
528: ps = new PropertySet(dis);
529: dsi = new DocumentSummaryInformation(ps);
530:
531: assertEquals(null, si.getApplicationName());
532: assertEquals(null, si.getAuthor());
533: assertEquals(0, si.getCharCount());
534: assertTrue(si.wasNull());
535: assertEquals(null, si.getComments());
536: assertEquals(null, si.getCreateDateTime());
537: assertEquals(0, si.getEditTime());
538: assertTrue(si.wasNull());
539: assertEquals(null, si.getKeywords());
540: assertEquals(null, si.getLastAuthor());
541: assertEquals(null, si.getLastPrinted());
542: assertEquals(null, si.getLastSaveDateTime());
543: assertEquals(0, si.getPageCount());
544: assertTrue(si.wasNull());
545: assertEquals(null, si.getRevNumber());
546: assertEquals(0, si.getSecurity());
547: assertTrue(si.wasNull());
548: assertEquals(null, si.getSubject());
549: assertEquals(null, si.getTemplate());
550: assertEquals(null, si.getThumbnail());
551: assertEquals(null, si.getTitle());
552: assertEquals(0, si.getWordCount());
553: assertTrue(si.wasNull());
554:
555: assertEquals(0, dsi.getByteCount());
556: assertTrue(dsi.wasNull());
557: assertEquals(null, dsi.getCategory());
558: assertEquals(null, dsi.getCustomProperties());
559: // FIXME (byte array properties not yet implemented): assertEquals(null, dsi.getDocparts());
560: // FIXME (byte array properties not yet implemented): assertEquals(null, dsi.getHeadingPair());
561: assertEquals(0, dsi.getHiddenCount());
562: assertTrue(dsi.wasNull());
563: assertEquals(0, dsi.getLineCount());
564: assertTrue(dsi.wasNull());
565: assertEquals(false, dsi.getLinksDirty());
566: assertTrue(dsi.wasNull());
567: assertEquals(null, dsi.getManager());
568: assertEquals(0, dsi.getMMClipCount());
569: assertTrue(dsi.wasNull());
570: assertEquals(0, dsi.getNoteCount());
571: assertTrue(dsi.wasNull());
572: assertEquals(0, dsi.getParCount());
573: assertTrue(dsi.wasNull());
574: assertEquals(null, dsi.getPresentationFormat());
575: assertEquals(false, dsi.getScale());
576: assertTrue(dsi.wasNull());
577: assertEquals(0, dsi.getSlideCount());
578: assertTrue(dsi.wasNull());
579: }
580:
581: /**
582: * <p>Tests the simplified custom properties by reading them from the
583: * available test files.</p>
584: *
585: * @throws Throwable if anything goes wrong.
586: */
587: public void testReadCustomPropertiesFromFiles() throws Throwable {
588: final AllDataFilesTester.TestTask task = new AllDataFilesTester.TestTask() {
589: public void runTest(final File file)
590: throws FileNotFoundException, IOException,
591: NoPropertySetStreamException,
592: MarkUnsupportedException,
593: UnexpectedPropertySetTypeException {
594: /* Read a test document <em>doc</em> into a POI filesystem. */
595: final POIFSFileSystem poifs = new POIFSFileSystem(
596: new FileInputStream(file));
597: final DirectoryEntry dir = poifs.getRoot();
598: DocumentEntry dsiEntry = null;
599: try {
600: dsiEntry = (DocumentEntry) dir
601: .getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
602: } catch (FileNotFoundException ex) {
603: /*
604: * A missing document summary information stream is not an error
605: * and therefore silently ignored here.
606: */
607: }
608:
609: /*
610: * If there is a document summry information stream, read it from
611: * the POI filesystem, else create a new one.
612: */
613: DocumentSummaryInformation dsi;
614: if (dsiEntry != null) {
615: final DocumentInputStream dis = new DocumentInputStream(
616: dsiEntry);
617: final PropertySet ps = new PropertySet(dis);
618: dsi = new DocumentSummaryInformation(ps);
619: } else
620: dsi = PropertySetFactory
621: .newDocumentSummaryInformation();
622: final CustomProperties cps = dsi.getCustomProperties();
623:
624: if (cps == null)
625: /* The document does not have custom properties. */
626: return;
627:
628: for (final Iterator i = cps.entrySet().iterator(); i
629: .hasNext();) {
630: final Map.Entry e = (Entry) i.next();
631: final CustomProperty cp = (CustomProperty) e
632: .getValue();
633: cp.getName();
634: cp.getValue();
635: }
636: }
637: };
638:
639: final String dataDirName = System
640: .getProperty("HPSF.testdata.path");
641: final File dataDir = new File(dataDirName);
642: final File[] docs = dataDir.listFiles(new FileFilter() {
643: public boolean accept(final File file) {
644: return file.isFile()
645: && file.getName().startsWith("Test");
646: }
647: });
648:
649: for (int i = 0; i < docs.length; i++) {
650: task.runTest(docs[i]);
651: }
652: }
653:
654: /**
655: * <p>Tests basic custom property features.</p>
656: */
657: public void testCustomerProperties() {
658: final String KEY = "Schlüssel ä";
659: final String VALUE_1 = "Wert 1";
660: final String VALUE_2 = "Wert 2";
661:
662: CustomProperty cp;
663: CustomProperties cps = new CustomProperties();
664: assertEquals(0, cps.size());
665:
666: /* After adding a custom property the size must be 1 and it must be
667: * possible to extract the custom property from the map. */
668: cps.put(KEY, VALUE_1);
669: assertEquals(1, cps.size());
670: Object v1 = cps.get(KEY);
671: assertEquals(VALUE_1, v1);
672:
673: /* After adding a custom property with the same name the size must still
674: * be one. */
675: cps.put(KEY, VALUE_2);
676: assertEquals(1, cps.size());
677: Object v2 = cps.get(KEY);
678: assertEquals(VALUE_2, v2);
679:
680: /* Removing the custom property must return the remove property and
681: * reduce the size to 0. */
682: cp = (CustomProperty) cps.remove(KEY);
683: assertEquals(KEY, cp.getName());
684: assertEquals(VALUE_2, cp.getValue());
685: assertEquals(0, cps.size());
686: }
687:
688: /**
689: * <p>Tests reading custom properties from a section including reading
690: * custom properties which are not pure.</p>
691: */
692: public void testGetCustomerProperties() {
693: final int ID_1 = 2;
694: final int ID_2 = 3;
695: final String NAME_1 = "Schlüssel ä";
696: final String VALUE_1 = "Wert 1";
697: final Map dictionary = new HashMap();
698:
699: DocumentSummaryInformation dsi = PropertySetFactory
700: .newDocumentSummaryInformation();
701: CustomProperties cps;
702: MutableSection s;
703:
704: /* A document summary information set stream by default does have custom properties. */
705: cps = dsi.getCustomProperties();
706: assertEquals(null, cps);
707:
708: /* Test an empty custom properties set. */
709: s = new MutableSection();
710: s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[1]);
711: // s.setCodepage(Constants.CP_UNICODE);
712: dsi.addSection(s);
713: cps = dsi.getCustomProperties();
714: assertEquals(0, cps.size());
715:
716: /* Add a custom property. */
717: MutableProperty p = new MutableProperty();
718: p.setID(ID_1);
719: p.setType(Variant.VT_LPWSTR);
720: p.setValue(VALUE_1);
721: s.setProperty(p);
722: dictionary.put(new Long(ID_1), NAME_1);
723: s.setDictionary(dictionary);
724: cps = dsi.getCustomProperties();
725: assertEquals(1, cps.size());
726: assertTrue(cps.isPure());
727:
728: /* Add another custom property. */
729: s.setProperty(ID_2, Variant.VT_LPWSTR, VALUE_1);
730: dictionary.put(new Long(ID_2), NAME_1);
731: s.setDictionary(dictionary);
732: cps = dsi.getCustomProperties();
733: assertEquals(1, cps.size());
734: assertFalse(cps.isPure());
735: }
736:
737: /**
738: * <p>Runs the test cases stand-alone.</p>
739: *
740: * @param args The command-line parameters.
741: * @throws Throwable if anything goes wrong.
742: */
743: public static void main(final String[] args) throws Throwable {
744: System.setProperty("HPSF.testdata.path",
745: "./src/testcases/org/apache/poi/hpsf/data");
746: junit.textui.TestRunner.run(TestWriteWellKnown.class);
747: }
748:
749: }
|