📄 testwrite.java
字号:
/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.==================================================================== */package org.apache.poi.hpsf.basic;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.io.StringWriter;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset;import java.util.Date;import java.util.HashMap;import java.util.Map;import junit.framework.Assert;import junit.framework.TestCase;import org.apache.poi.hpsf.ClassID;import org.apache.poi.hpsf.Constants;import org.apache.poi.hpsf.HPSFRuntimeException;import org.apache.poi.hpsf.IllegalPropertySetDataException;import org.apache.poi.hpsf.MutableProperty;import org.apache.poi.hpsf.MutablePropertySet;import org.apache.poi.hpsf.MutableSection;import org.apache.poi.hpsf.NoFormatIDException;import org.apache.poi.hpsf.NoPropertySetStreamException;import org.apache.poi.hpsf.PropertySet;import org.apache.poi.hpsf.PropertySetFactory;import org.apache.poi.hpsf.ReadingNotSupportedException;import org.apache.poi.hpsf.Section;import org.apache.poi.hpsf.SummaryInformation;import org.apache.poi.hpsf.UnsupportedVariantTypeException;import org.apache.poi.hpsf.Variant;import org.apache.poi.hpsf.VariantSupport;import org.apache.poi.hpsf.WritingNotSupportedException;import org.apache.poi.hpsf.wellknown.PropertyIDMap;import org.apache.poi.hpsf.wellknown.SectionIDMap;import org.apache.poi.poifs.eventfilesystem.POIFSReader;import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.util.LittleEndian;import org.apache.poi.util.TempFile;/** * <p>Tests HPSF's writing functionality.</p> * * @author Rainer Klute (klute@rainer-klute.de) * @since 2003-02-07 * @version $Id: TestWrite.java 489730 2006-12-22 19:18:16Z bayard $ */public class TestWrite extends TestCase{ static final String POI_FS = "TestHPSFWritingFunctionality.doc"; static final int BYTE_ORDER = 0xfffe; static final int FORMAT = 0x0000; static final int OS_VERSION = 0x00020A04; static final int[] SECTION_COUNT = {1, 2}; static final boolean[] IS_SUMMARY_INFORMATION = {true, false}; static final boolean[] IS_DOCUMENT_SUMMARY_INFORMATION = {false, true}; final String IMPROPER_DEFAULT_CHARSET_MESSAGE = "Your default character set is " + getDefaultCharsetName() + ". However, this testcase must be run in an environment " + "with a default character set supporting at least " + "8-bit-characters. You can achieve this by setting the " + "LANG environment variable to a proper value, e.g. " + "\"de_DE\"."; POIFile[] poiFiles; /** * <p>Constructor</p> * * @param name the test case's name */ public TestWrite(final String name) { super(name); } /** * @see TestCase#setUp() */ public void setUp() { VariantSupport.setLogUnsupportedTypes(false); } /** * <p>Writes an empty property set to a POIFS and reads it back * in.</p> * * @exception IOException if an I/O exception occurs */ public void testNoFormatID() throws IOException { final String dataDirName = System.getProperty("HPSF.testdata.path"); final File dataDir = new File(dataDirName); final File filename = new File(dataDir, POI_FS); filename.deleteOnExit(); /* Create a mutable property set with a section that does not have the * formatID set: */ final OutputStream out = new FileOutputStream(filename); final POIFSFileSystem poiFs = new POIFSFileSystem(); final MutablePropertySet ps = new MutablePropertySet(); ps.clearSections(); ps.addSection(new MutableSection()); /* Write it to a POIFS and the latter to disk: */ try { final ByteArrayOutputStream psStream = new ByteArrayOutputStream(); ps.write(psStream); psStream.close(); final byte[] streamData = psStream.toByteArray(); poiFs.createDocument(new ByteArrayInputStream(streamData), SummaryInformation.DEFAULT_STREAM_NAME); poiFs.writeFilesystem(out); out.close(); Assert.fail("Should have thrown a NoFormatIDException."); } catch (Exception ex) { Assert.assertTrue(ex instanceof NoFormatIDException); } finally { out.close(); } } /** * <p>Writes an empty property set to a POIFS and reads it back * in.</p> * * @exception IOException if an I/O exception occurs * @exception UnsupportedVariantTypeException if HPSF does not yet support * a variant type to be written */ public void testWriteEmptyPropertySet() throws IOException, UnsupportedVariantTypeException { final File dataDir = new File(System.getProperty("HPSF.testdata.path")); final File filename = new File(dataDir, POI_FS); filename.deleteOnExit(); /* Create a mutable property set and write it to a POIFS: */ final OutputStream out = new FileOutputStream(filename); final POIFSFileSystem poiFs = new POIFSFileSystem(); final MutablePropertySet ps = new MutablePropertySet(); final MutableSection s = (MutableSection) ps.getSections().get(0); s.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID); final ByteArrayOutputStream psStream = new ByteArrayOutputStream(); ps.write(psStream); psStream.close(); final byte[] streamData = psStream.toByteArray(); poiFs.createDocument(new ByteArrayInputStream(streamData), SummaryInformation.DEFAULT_STREAM_NAME); poiFs.writeFilesystem(out); out.close(); /* Read the POIFS: */ final POIFSReader r = new POIFSReader(); r.registerListener(new MyPOIFSReaderListener(), SummaryInformation.DEFAULT_STREAM_NAME); r.read(new FileInputStream(filename)); } /** * <p>Writes a simple property set with a SummaryInformation section to a * POIFS and reads it back in.</p> * * @exception IOException if an I/O exception occurs * @exception UnsupportedVariantTypeException if HPSF does not yet support * a variant type to be written */ public void testWriteSimplePropertySet() throws IOException, UnsupportedVariantTypeException { final String AUTHOR = "Rainer Klute"; final String TITLE = "Test Document"; final File dataDir = new File(System.getProperty("HPSF.testdata.path")); final File filename = new File(dataDir, POI_FS); filename.deleteOnExit(); final OutputStream out = new FileOutputStream(filename); final POIFSFileSystem poiFs = new POIFSFileSystem(); final MutablePropertySet ps = new MutablePropertySet(); final MutableSection si = new MutableSection(); si.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID); ps.getSections().set(0, si); final MutableProperty p = new MutableProperty(); p.setID(PropertyIDMap.PID_AUTHOR); p.setType(Variant.VT_LPWSTR); p.setValue(AUTHOR); si.setProperty(p); si.setProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPSTR, TITLE); poiFs.createDocument(ps.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME); poiFs.writeFilesystem(out); out.close(); /* Read the POIFS: */ final PropertySet[] psa = new PropertySet[1]; final POIFSReader r = new POIFSReader(); r.registerListener(new POIFSReaderListener() { public void processPOIFSReaderEvent (final POIFSReaderEvent event) { try { psa[0] = PropertySetFactory.create(event.getStream()); } catch (Exception ex) { fail(org.apache.poi.hpsf.Util.toString(ex)); } } }, SummaryInformation.DEFAULT_STREAM_NAME); r.read(new FileInputStream(filename)); Assert.assertNotNull(psa[0]); Assert.assertTrue(psa[0].isSummaryInformation()); final Section s = (Section) (psa[0].getSections().get(0)); Object p1 = s.getProperty(PropertyIDMap.PID_AUTHOR); Object p2 = s.getProperty(PropertyIDMap.PID_TITLE); Assert.assertEquals(AUTHOR, p1); Assert.assertEquals(TITLE, p2); } /** * <p>Writes a simple property set with two sections to a POIFS and reads it * back in.</p> * * @exception IOException if an I/O exception occurs * @exception WritingNotSupportedException if HPSF does not yet support * a variant type to be written */ public void testWriteTwoSections() throws WritingNotSupportedException, IOException { final String STREAM_NAME = "PropertySetStream"; final String SECTION1 = "Section 1"; final String SECTION2 = "Section 2"; final File dataDir = new File(System.getProperty("HPSF.testdata.path")); final File filename = new File(dataDir, POI_FS); filename.deleteOnExit(); final OutputStream out = new FileOutputStream(filename); final POIFSFileSystem poiFs = new POIFSFileSystem(); final MutablePropertySet ps = new MutablePropertySet(); ps.clearSections(); final ClassID formatID = new ClassID(); formatID.setBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); final MutableSection s1 = new MutableSection(); s1.setFormatID(formatID); s1.setProperty(2, SECTION1); ps.addSection(s1); final MutableSection s2 = new MutableSection(); s2.setFormatID(formatID); s2.setProperty(2, SECTION2); ps.addSection(s2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -