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

📄 documenttest_java.htm

📁 jxtx开发实例
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0125)http://platform.jxta.org/source/browse/*checkout*/platform/binding/java/test/src/net/jxta/document/DocumentTest.java?rev=1.22 -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.3059" name=GENERATOR></HEAD>
<BODY><PRE>/*
 * $Id: DocumentTest.java,v 1.22 2007/01/26 02:13:53 bondolo Exp $
 ********************
 *
 * Copyright (c) 2001 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *       Sun Microsystems, Inc. for Project JXTA."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact Project JXTA at http://www.jxta.org.
 *
 * 5. Products derived from this software may not be called "JXTA",
 *    nor may "JXTA" appear in their name, without prior written
 *    permission of Sun.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Project JXTA.  For more
 * information on Project JXTA, please see
 * &lt;http://www.jxta.org/&gt;.
 *
 * This license is based on the BSD license adopted by the Apache Foundation.
 ********************
 */

package net.jxta.document;

import java.io.*;
import java.util.Enumeration;
import java.util.Collections;
import java.util.List;
import java.security.ProviderException;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import net.jxta.impl.document.LiteXMLDocument;
import net.jxta.impl.document.LiteXMLElement;
import net.jxta.impl.document.PlainTextDocument;
import net.jxta.impl.document.PlainTextElement;

public final class DocumentTest extends TestCase {
       
    final static String badlittleimpl = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n" +
    "&lt;!DOCTYPE jxta:MIA&gt;\n" +
    "&lt;jxta:MIA xmlns:jxta=\"http://jxta.org\"&gt;\n" +
    "  	&lt;MSID/&gt;\n" +
    " 	&lt;Parm&gt;\n" +
    " 		&lt;Svc&gt;\n" +
    " 			&lt;jxta:MIA xmlns:jxta=\"http://jxta.org\"&gt;\n" +
    " 				&lt;MSID/&gt;\n" +
    " 			&lt;/jxta:MIA&gt;\n" +
    " 		&lt;/Svc&gt;\n" +
    " 		&lt;Proto&gt;\n" +
    " 			&lt;jxta:MIA xmlns:jxta=\"http://jxta.org\"&gt;\n" +
    " 				&lt;MSID/&gt;\n" +
    " 			&lt;/jxta:MIA&gt;\n" +
    " 		&lt;/Proto&gt;\n" +
    " 	&lt;/Parm&gt;\n" +
    " &lt;/jxta:MIA&gt;\n";
    
     public static class LiteXMLBug {
        public LiteXMLBug( String xml ) {
            MimeMediaType mimeMediaType = MimeMediaType.XMLUTF8;
            StringReader reader = new StringReader( xml );
            try {
                LiteXMLDocument document = (LiteXMLDocument)
                LiteXMLDocument.INSTANTIATOR.newInstance( mimeMediaType, reader );
                spew( document );
            } catch( IOException caught ) {
                fail( "should not have thrown an exception" );
            } finally {
                reader.close();
            }
        }
        
        public void spew( LiteXMLElement element ) {
            System.out.println( element.getValue() );
            
            Enumeration children = element.getChildren();
            while ( children.hasMoreElements() ) {
                LiteXMLElement child = (LiteXMLElement) children.nextElement();
                spew( child );
            }
        }
        
    }
    
    /** Creates new DocTest */
    public DocumentTest(String name) {
        super(name);
    }
    
    public static Test suite() {
        TestSuite suite = new TestSuite(DocumentTest.class);
        return suite;
    }
    
    private void _test( StructuredDocumentFactory.Instantiator instantiator, MimeMediaType type ) {
        try {
            final String useDocType = "Test";
            StructuredTextDocument doc = null;
            try {
                doc = (StructuredTextDocument) instantiator.newInstance( type, useDocType );
            } catch( Throwable thrown ) {
                fail("exception thrown during construction!" + thrown.toString() );
            }
            
            assertTrue("could not construct object for type : " + type, doc != null);
            
            String itsType = doc.getName();
            
            assertTrue( "returned doctype does not equal type document was created with!",
            useDocType.equals( itsType ) );
            
            assertTrue(
            "returned doc name does not equal name of document element",
            doc.getName().equals( itsType ) );
            
            TextElement testElement = doc.createElement( "element" );
            
            doc.appendChild( testElement );
            
            try {
                Element firstchild = doc.getChildren().nextElement();
                assertTrue(
                "added a single element, but something else was returned",
                testElement.equals( firstchild ) );
            } catch( Exception e ) {
                e.printStackTrace();
                fail( "added a single element, but it was not returned" );
            }
            
            final String useName = "element2";
            final String useValue = "value&amp;&lt;!";
            
            TextElement testElement2 = doc.createElement( useName, useValue );
            
            testElement.appendChild( testElement2 );
            
            String itsName = testElement2.getName();
            assertTrue("name of element was not correct after creation",
            useName.equals(itsName) );
            
            String itsValue = testElement2.getTextValue();
            assertTrue(
            "value of element was not correct after creation. was '" + itsValue + "' should be '" + useValue + "'",
            useValue.equals(itsValue) );
            
            testElement2 = doc.createElement( "element3", useValue );
            
            testElement.appendChild( testElement2 );
            
            testElement2 = doc.createElement( "element4", "1" );
            
            testElement.appendChild( testElement2 );
            
            itsValue = testElement2.getTextValue();
            assertTrue("value of element was not correct after creation (length 1)",
            "1".equals(itsValue) );
            
            if( type.getSubtype().equalsIgnoreCase( "XML" ) ) {
                try {
                    
                    TextElement testElement5 = doc.createElement( "really wrong and long", "1" );
                    
                    fail( "Tag names with spaces should be disallowed" );
                } catch ( Exception failed ) {
                    // that's ok
                }
            }
            
            int count = 0;
            for( Enumeration eachChild = doc.getChildren(); eachChild.hasMoreElements(); count++, eachChild.nextElement() )
                ;
            
            assertTrue("Doc didnt have one child", 1 == count );
            
            count = 0;
            for( Enumeration eachChild = doc.getChildren( "element" ); eachChild.hasMoreElements(); count++, eachChild.nextElement() )
                ;
            
            assertTrue("Doc didnt have one child named 'element'", 1 == count );
            
            count = 0;
            for( Enumeration eachChild = doc.getChildren( "bogus" ); eachChild.hasMoreElements(); count++, eachChild.nextElement() )
                ;
            
            assertTrue(" Doc shouldnt have had a child named 'bogus'", 0 == count );
            
            count = 0;
            for( Enumeration eachChild = testElement.getChildren( ); eachChild.hasMoreElements(); count++, eachChild.nextElement() )
                ;
            
            assertTrue("element didnt have expected number of children" , 3 == count );
            

⌨️ 快捷键说明

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