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

📄 testmetadataipi.java

📁 java 读写word excel ppt
💻 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.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.Random;import junit.framework.TestCase;import org.apache.poi.hpsf.CustomProperties;import org.apache.poi.hpsf.DocumentSummaryInformation;import org.apache.poi.hpsf.MarkUnsupportedException;import org.apache.poi.hpsf.NoPropertySetStreamException;import org.apache.poi.hpsf.PropertySet;import org.apache.poi.hpsf.PropertySetFactory;import org.apache.poi.hpsf.SummaryInformation;import org.apache.poi.hpsf.UnexpectedPropertySetTypeException;import org.apache.poi.hpsf.WritingNotSupportedException;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.DocumentInputStream;import org.apache.poi.poifs.filesystem.POIFSFileSystem;/** * Basing on: src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java * This class tests reading and writing of meta data. No actual document is created. All information * is stored in a virtal document in a ByteArrayOutputStream * @author Matthias G黱ter * @since 2006-03-03 * @version $Id: TestEmptyProperties.java 353563 2004-06-22 16:16:33Z klute $ */public class TestMetaDataIPI extends TestCase{ private ByteArrayOutputStream bout= null; //our store private POIFSFileSystem poifs=null; DirectoryEntry dir = null; DocumentSummaryInformation dsi=null; SummaryInformation si=null;  /**  * Standard constructor  * @param s  */ public TestMetaDataIPI(String s ){	 super(s); } /**  * Setup is used to get the document ready. Gets the DocumentSummaryInformation and the   * SummaryInformation to reasonable values  */ public void setUp(){	 bout=new ByteArrayOutputStream();	 poifs= new POIFSFileSystem();     dir = poifs.getRoot();     dsi=null;     try     {         DocumentEntry dsiEntry = (DocumentEntry)             dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);         DocumentInputStream dis = new DocumentInputStream(dsiEntry);         PropertySet ps = new PropertySet(dis);         dis.close();         dsi = new DocumentSummaryInformation(ps);                     }     catch (FileNotFoundException ex)     {         /* There is no document summary information yet. We have to create a          * new one. */         dsi = PropertySetFactory.newDocumentSummaryInformation();         assertNotNull(dsi);     } catch (IOException e) {		e.printStackTrace();		fail();	} catch (NoPropertySetStreamException e) {		e.printStackTrace();		fail();	} catch (MarkUnsupportedException e) {		e.printStackTrace();		fail();	} catch (UnexpectedPropertySetTypeException e) {		e.printStackTrace();		fail();	}    assertNotNull(dsi);    try    {        DocumentEntry dsiEntry = (DocumentEntry)            dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);        DocumentInputStream dis = new DocumentInputStream(dsiEntry);        PropertySet ps = new PropertySet(dis);        dis.close();        si = new SummaryInformation(ps);                  }    catch (FileNotFoundException ex)    {        /* There is no document summary information yet. We have to create a         * new one. */        si = PropertySetFactory.newSummaryInformation();        assertNotNull(si);    } catch (IOException e) {		e.printStackTrace();		fail();	} catch (NoPropertySetStreamException e) {		e.printStackTrace();		fail();	} catch (MarkUnsupportedException e) {		e.printStackTrace();		fail();	} catch (UnexpectedPropertySetTypeException e) {		e.printStackTrace();		fail();	}	assertNotNull(dsi);         }  /**  * Setting a lot of things to null.  */ public void tearDown(){	bout=null;	poifs=null;	dir=null;	dsi=null;	  }   /**  * Closes the ByteArrayOutputStream and reads it into a ByteArrayInputStream.  * When finished writing information this method is used in the tests to  * start reading from the created document and then the see if the results match.  *  */  public void closeAndReOpen(){	     	     try {			dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);			si.write(dir,SummaryInformation.DEFAULT_STREAM_NAME);		} catch (WritingNotSupportedException e) {			e.printStackTrace();			fail();		} catch (IOException e) {			e.printStackTrace();			fail();		}        si=null;        dsi=null;		try {						poifs.writeFilesystem(bout);			bout.flush();					} catch (IOException e) {			e.printStackTrace();			fail();		}	     	     InputStream is=new ByteArrayInputStream(bout.toByteArray());	   assertNotNull(is);	   POIFSFileSystem poifs=null;		try {			poifs = new POIFSFileSystem(is);		} catch (IOException e) {			e.printStackTrace();			fail();		}	     try {			is.close();		} catch (IOException e) {			e.printStackTrace();			fail();		}	    assertNotNull(poifs);	     /* Read the document summary information. */	     DirectoryEntry dir = poifs.getRoot();	     	     try	     {	         DocumentEntry dsiEntry = (DocumentEntry)	             dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);	         DocumentInputStream dis = new DocumentInputStream(dsiEntry);	         PropertySet ps = new PropertySet(dis);	         dis.close();	         dsi = new DocumentSummaryInformation(ps);	     }	     catch (FileNotFoundException ex)	     {	         fail();	     } catch (IOException e) {			e.printStackTrace();			fail();		} catch (NoPropertySetStreamException e) {			e.printStackTrace();			fail();		} catch (MarkUnsupportedException e) {			e.printStackTrace();			fail();		} catch (UnexpectedPropertySetTypeException e) {			e.printStackTrace();			fail();		}		  try		    {		        DocumentEntry dsiEntry = (DocumentEntry)		            dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);		        DocumentInputStream dis = new DocumentInputStream(dsiEntry);		        PropertySet ps = new PropertySet(dis);		        dis.close();		        si = new SummaryInformation(ps);		      		        		    }		    catch (FileNotFoundException ex)		    {		        /* There is no document summary information yet. We have to create a		         * new one. */		        si = PropertySetFactory.newSummaryInformation();		        assertNotNull(si);		    } catch (IOException e) {				e.printStackTrace();				fail();			} catch (NoPropertySetStreamException e) {				e.printStackTrace();				fail();			} catch (MarkUnsupportedException e) {				e.printStackTrace();				fail();			} catch (UnexpectedPropertySetTypeException e) {				e.printStackTrace();				fail();			}  }    /**   * Sets the most important information in DocumentSummaryInformation and Summary Information and rereads it   *   */ public void testOne(){	 	 //DocumentSummaryInformation	 dsi.setCompany("xxxCompanyxxx");	 dsi.setManager("xxxManagerxxx");	 dsi.setCategory("xxxCategoryxxx");	 	 //SummaryInformation	 si.setTitle("xxxTitlexxx");	 si.setAuthor("xxxAuthorxxx");	 si.setComments("xxxCommentsxxx");	 si.setKeywords("xxxKeyWordsxxx");	 si.setSubject("xxxSubjectxxx");	 	 //Custom Properties (in DocumentSummaryInformation     CustomProperties customProperties = dsi.getCustomProperties();     if (customProperties == null){         customProperties = new CustomProperties();     }          /* Insert some custom properties into the container. */     customProperties.put("Key1", "Value1");     customProperties.put("Schl黶sel2", "Wert2");     customProperties.put("Sample Integer", new Integer(12345));     customProperties.put("Sample Boolean", new Boolean(true));     Date date=new Date();     customProperties.put("Sample Date", date);     customProperties.put("Sample Double", new Double(-1.0001));     customProperties.put("Sample Negative Integer", new Integer(-100000));     dsi.setCustomProperties(customProperties);     //start reading	 closeAndReOpen();     	 //testing     assertNotNull(dsi);     assertNotNull(si);          assertEquals("Category","xxxCategoryxxx",dsi.getCategory());     assertEquals("Company","xxxCompanyxxx",dsi.getCompany());     assertEquals("Manager","xxxManagerxxx",dsi.getManager());          assertEquals("","xxxAuthorxxx",si.getAuthor());     assertEquals("","xxxTitlexxx",si.getTitle());     assertEquals("","xxxCommentsxxx",si.getComments());     assertEquals("","xxxKeyWordsxxx",si.getKeywords());     assertEquals("","xxxSubjectxxx",si.getSubject());          /* Read the custom properties. If there are no custom properties yet,      * the application has to create a new CustomProperties object. It will      * serve as a container for custom properties. */     customProperties = dsi.getCustomProperties();     if (customProperties == null){    	 fail();         }          /* Insert some custom properties into the container. */     String a1=(String) customProperties.get("Key1");     assertEquals("Key1","Value1",a1);     String a2=(String) customProperties.get("Schl黶sel2");     assertEquals("Schl黶sel2","Wert2",a2);     Integer a3=(Integer) customProperties.get("Sample Integer");     assertEquals("Sample Number",new Integer(12345),a3);     Boolean a4=(Boolean) customProperties.get("Sample Boolean");     assertEquals("Sample Boolean",new Boolean(true),a4);     Date a5=(Date) customProperties.get("Sample Date");     assertEquals("Custom Date:",date,a5);          Double a6=(Double) customProperties.get("Sample Double");     assertEquals("Custom Float",new Double(-1.0001),a6);          Integer a7=(Integer) customProperties.get("Sample Negative Integer");     assertEquals("Neg", new Integer(-100000),a7); }      /**  * multiplies a string  * @param s Input String  * @return  the multiplied String  */ public String elongate(String s){	 StringBuffer sb=new StringBuffer();	 for (int i=0;i<10000;i++){		 sb.append(s);		 sb.append(" ");	 }	 return sb.toString(); }    /**  * Test very long input in each of the fields (approx 30-60KB each)  *  */public void testTwo(){	 	String company=elongate("company");	String manager=elongate("manager");	String category=elongate("category");	String title=elongate("title");	String author=elongate("author");	String comments=elongate("comments");	String keywords=elongate("keywords");	String subject=elongate("subject");	String p1=elongate("p1");	String p2=elongate("p2");	String k1=elongate("k1");	String k2=elongate("k2");		 dsi.setCompany(company);	 dsi.setManager(manager);	 dsi.setCategory(category);	 	 si.setTitle(title);	 si.setAuthor(author);	 si.setComments(comments);	 si.setKeywords(keywords);	 si.setSubject(subject);    CustomProperties customProperties = dsi.getCustomProperties();    if (customProperties == null){        customProperties = new CustomProperties();    }        /* Insert some custom properties into the container. */    customProperties.put(k1, p1);    customProperties.put(k2, p2);    customProperties.put("Sample Number", new Integer(12345));    customProperties.put("Sample Boolean", new Boolean(true));    Date date=new Date();    customProperties.put("Sample Date", date);    dsi.setCustomProperties(customProperties);	 closeAndReOpen();     assertNotNull(dsi);   assertNotNull(si);    /* Change the category to "POI example". Any former category value will     * be lost. If there has been no category yet, it will be created. */    assertEquals("Category",category,dsi.getCategory());    assertEquals("Company",company,dsi.getCompany());    assertEquals("Manager",manager,dsi.getManager());        assertEquals("",author,si.getAuthor());    assertEquals("",title,si.getTitle());    assertEquals("",comments,si.getComments());    assertEquals("",keywords,si.getKeywords());    assertEquals("",subject,si.getSubject());            /* Read the custom properties. If there are no custom properties     * yet, the application has to create a new CustomProperties object.     * It will serve as a container for custom properties. */    customProperties = dsi.getCustomProperties();    if (customProperties == null){   	 fail();        }        /* Insert some custom properties into the container. */    String a1=(String) customProperties.get(k1);    assertEquals("Key1",p1,a1);    String a2=(String) customProperties.get(k2);    assertEquals("Schl黶sel2",p2,a2);    Integer a3=(Integer) customProperties.get("Sample Number");    assertEquals("Sample Number",new Integer(12345),a3);    Boolean a4=(Boolean) customProperties.get("Sample Boolean");    assertEquals("Sample Boolean",new Boolean(true),a4);    Date a5=(Date) customProperties.get("Sample Date");    assertEquals("Custom Date:",date,a5);	}    /** * adds strange characters to the string * @param s Input String * @return  the multiplied String */public String strangize(String s){	 StringBuffer sb=new StringBuffer();	 String[] umlaute= {"

⌨️ 快捷键说明

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