⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testwrite.java

📁 java 读写word excel ppt
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            final int diff = diff((byte[]) value, (byte[]) objRead);            if (diff >= 0)                fail("Byte arrays are different. First different byte is at " +                     "index " + diff + ".");        }        else            if (value != null && !value.equals(objRead))            {                fail("Expected: \"" + value + "\" but was: \"" + objRead +                     "\". Codepage: " + codepage +                     (codepage == -1 ?                      " (" + System.getProperty("file.encoding") + ")." : "."));            }            else                assertEquals(value, objRead);    }    /**     * <p>Compares two byte arrays.</p>     *     * @param a The first byte array     * @param b The second byte array     * @return The index of the first byte that is different. If the byte arrays     * are equal, -1 is returned.     */    private int diff(final byte[] a, final byte[] b)    {        final int min = Math.min(a.length, b.length);        for (int i = 0; i < min; i++)            if (a[i] != b[i])                return i;        if (a.length != b.length)            return min;        return -1;    }    /**     * <p>This test method does a write and read back test with all POI     * filesystems in the "data" directory by performing the following     * actions for each file:</p>     *      * <ul>     *      * <li><p>Read its property set streams.</p></li>     *      * <li><p>Create a new POI filesystem containing the origin file's     * property set streams.</p></li>     *      * <li><p>Read the property set streams from the POI filesystem just     * created.</p></li>     *      * <li><p>Compare each property set stream with the corresponding one from     * the origin file and check whether they are equal.</p></li>     *     * </ul>     */    public void testRecreate()    {        final File dataDir =            new File(System.getProperty("HPSF.testdata.path"));        final File[] fileList = dataDir.listFiles(new FileFilter()            {                public boolean accept(final File f)                {                    return f.getName().startsWith("Test");                }            });        for (int i = 0; i < fileList.length; i++)            testRecreate(fileList[i]);    }    /**     * <p>Performs the check described in {@link #testRecreate()} for a single     * POI filesystem.</p>     *     * @param f the POI filesystem to check     */    private void testRecreate(final File f)    {        System.out.println("Recreating file \"" + f + "\"");        try        {            /* Read the POI filesystem's property set streams: */            final POIFile[] psf1 = Util.readPropertySets(f);            /* Create a new POI filesystem containing the origin file's             * property set streams: */            final File copy = TempFile.createTempFile(f.getName(), "");            copy.deleteOnExit();            final OutputStream out = new FileOutputStream(copy);            final POIFSFileSystem poiFs = new POIFSFileSystem();            for (int i = 0; i < psf1.length; i++)            {                final InputStream in =                    new ByteArrayInputStream(psf1[i].getBytes());                final PropertySet psIn = PropertySetFactory.create(in);                final MutablePropertySet psOut = new MutablePropertySet(psIn);                final ByteArrayOutputStream psStream =                    new ByteArrayOutputStream();                psOut.write(psStream);                psStream.close();                final byte[] streamData = psStream.toByteArray();                poiFs.createDocument(new ByteArrayInputStream(streamData),                                     psf1[i].getName());                poiFs.writeFilesystem(out);            }            out.close();            /* Read the property set streams from the POI filesystem just             * created. */            final POIFile[] psf2 = Util.readPropertySets(copy);            for (int i = 0; i < psf2.length; i++)            {                final byte[] bytes1 = psf1[i].getBytes();                final byte[] bytes2 = psf2[i].getBytes();                final InputStream in1 = new ByteArrayInputStream(bytes1);                final InputStream in2 = new ByteArrayInputStream(bytes2);                final PropertySet ps1 = PropertySetFactory.create(in1);                final PropertySet ps2 = PropertySetFactory.create(in2);                            /* Compare the property set stream with the corresponding one                 * from the origin file and check whether they are equal. */                assertEquals("Equality for file " + f.getName(), ps1, ps2);            }        }        catch (Exception ex)        {            handle(ex);        }    }    /**     * <p>Tests writing and reading back a proper dictionary.</p>     */    public void testDictionary()    {        try        {            final File copy = TempFile.createTempFile("Test-HPSF", "ole2");            copy.deleteOnExit();            /* Write: */            final OutputStream out = new FileOutputStream(copy);            final POIFSFileSystem poiFs = new POIFSFileSystem();            final MutablePropertySet ps1 = new MutablePropertySet();            final MutableSection s = (MutableSection) ps1.getSections().get(0);            final Map m = new HashMap(3, 1.0f);            m.put(new Long(1), "String 1");            m.put(new Long(2), "String 2");            m.put(new Long(3), "String 3");            s.setDictionary(m);            s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);            int codepage = Constants.CP_UNICODE;            s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,                          new Integer(codepage));            poiFs.createDocument(ps1.toInputStream(), "Test");            poiFs.writeFilesystem(out);            out.close();            /* Read back: */            final POIFile[] psf = Util.readPropertySets(copy);            Assert.assertEquals(1, psf.length);            final byte[] bytes = psf[0].getBytes();            final InputStream in = new ByteArrayInputStream(bytes);            final PropertySet ps2 = PropertySetFactory.create(in);            /* Check if the result is a DocumentSummaryInformation stream, as             * specified. */            assertTrue(ps2.isDocumentSummaryInformation());            /* Compare the property set stream with the corresponding one             * from the origin file and check whether they are equal. */            assertEquals(ps1, ps2);        }        catch (Exception ex)        {            handle(ex);        }    }    /**     * <p>Tests writing and reading back a proper dictionary with an invalid     * codepage. (HPSF writes Unicode dictionaries only.)</p>     */    public void testDictionaryWithInvalidCodepage()    {        try        {            final File copy = TempFile.createTempFile("Test-HPSF", "ole2");            copy.deleteOnExit();            /* Write: */            final OutputStream out = new FileOutputStream(copy);            final POIFSFileSystem poiFs = new POIFSFileSystem();            final MutablePropertySet ps1 = new MutablePropertySet();            final MutableSection s = (MutableSection) ps1.getSections().get(0);            final Map m = new HashMap(3, 1.0f);            m.put(new Long(1), "String 1");            m.put(new Long(2), "String 2");            m.put(new Long(3), "String 3");            s.setDictionary(m);            s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);            int codepage = 12345;            s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,                          new Integer(codepage));            poiFs.createDocument(ps1.toInputStream(), "Test");            poiFs.writeFilesystem(out);            out.close();            fail("This testcase did not detect the invalid codepage value.");        }        catch (IllegalPropertySetDataException ex)        {            assertTrue(true);        }        catch (Exception ex)        {            handle(ex);        }    }    /**     * <p>Handles unexpected exceptions in testcases.</p>     *     * @param ex The exception that has been thrown.     */    private void handle(final Exception ex)    {        final StringWriter sw = new StringWriter();        final PrintWriter pw = new PrintWriter(sw);        Throwable t = ex;        while (t != null)        {            t.printStackTrace(pw);            if (t instanceof HPSFRuntimeException)                t = ((HPSFRuntimeException) t).getReason();            else                t = null;            if (t != null)                pw.println("Caused by:");        }        pw.close();        try        {            sw.close();        }        catch (IOException ex2)        {            ex.printStackTrace();        }        fail(sw.toString());    }    /**     * <p>Returns the display name of the default character set.</p>     *     * @return the display name of the default character set.     */    private String getDefaultCharsetName()    {        final String charSetName = System.getProperty("file.encoding");        final Charset charSet = Charset.forName(charSetName);        return charSet.displayName();    }    /**     * <p>In order to execute tests with characters beyond US-ASCII, this     * method checks whether the application is runing in an environment     * where the default character set is 16-bit-capable.</p>     *     * @return <code>true</code> if the default character set is 16-bit-capable,     * else <code>false</code>.     */    private boolean hasProperDefaultCharset()    {        final String charSetName = System.getProperty("file.encoding");        final Charset charSet = Charset.forName(charSetName);        return charSet.newEncoder().canEncode('\u00e4');    }    /**     * <p>Runs the test cases stand-alone.</p>     *      * @param args The command-line parameters.     * @throws Throwable if anything goes wrong.     */    public static void main(final String[] args) throws Throwable    {        System.setProperty("HPSF.testdata.path",                           "./src/testcases/org/apache/poi/hpsf/data");        junit.textui.TestRunner.run(TestWrite.class);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -