📄 messagetest.java
字号:
/* * $Id: MessageTest.java,v 1.1 2006/05/09 01:32:14 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 * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. ******************** */package net.jxta.endpoint;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.FilterInputStream;import java.io.InputStream;import java.io.SequenceInputStream;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.ListIterator;import java.util.Vector;import java.util.List;import java.util.Set;import java.io.IOException;import java.util.ConcurrentModificationException;import junit.framework.*;import net.jxta.document.MimeMediaType;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.StringMessageElement;/** * * @author mike */public class MessageTest extends TestCase { public MessageTest(java.lang.String testName) { super(testName); } public static void main(java.lang.String[] args) { junit.textui.TestRunner.run(suite()); System.err.flush(); System.out.flush(); } public static Test suite() { TestSuite suite = new TestSuite(MessageTest.class); return suite; } /** * Tests Default Namespace **/ public void testMessageDefaultNamespaces() { Message msg1 = new Message(); Message msg2 = new Message( "default" ); assertTrue( !msg1.getDefaultNamespace().equals( msg2.getDefaultNamespace() ) ); MessageElement elm1 = new StringMessageElement( "element1", "test1", null ); MessageElement elm2 = new StringMessageElement( "element1", "test2", null ); msg1.addMessageElement( elm1 ); msg2.addMessageElement( elm1 ); msg1.addMessageElement( "not-default", elm2 ); msg2.addMessageElement( "not-default", elm2 ); assertTrue( "Messages should not be equal", !msg1.equals( msg2 ) ); } /** * Tests Simple AddElement **/ public void testMessageAddGetElement() { Message msg1 = new Message(); List list = new ArrayList(); MessageElement elm1 = new StringMessageElement( "element1", "test1", null ); MessageElement elm2 = new StringMessageElement( "element1", "test2", null ); int initialModCount = msg1.getMessageModCount(); msg1.addMessageElement( elm1 ); int firstElementModCount = msg1.getMessageModCount(); assertTrue( "element was added, modcounts should differ", initialModCount != firstElementModCount ); msg1.addMessageElement( "not-default", elm2 ); int secondElementModCount = msg1.getMessageModCount(); assertTrue( "element was added, modcounts should differ", firstElementModCount != secondElementModCount ); assertTrue( "message should have had an element1 in default namespace", elm1 == msg1.getMessageElement( null, "element1" ) ); assertTrue( "message should have had an element1 in non-default namespace", elm2 == msg1.getMessageElement( "not-default", "element1" ) ); assertTrue( "message should not have had an element bogus", null == msg1.getMessageElement( "bogus" ) ); assertTrue( "message should not have had an element bogus", null == msg1.getMessageElement( null, "bogus" ) ); assertTrue( "message should not have had an element bogus", null == msg1.getMessageElement( "not-default", "bogus" ) ); } /** * tests the clear method **/ public void testMessageClear() { Message msg1 = new Message(); MessageElement elm1 = new StringMessageElement( "element1", "test1", null ); int initialModCount = msg1.getMessageModCount(); msg1.addMessageElement( elm1 ); int afterAddModCount = msg1.getMessageModCount(); assertTrue( "element was added, modcounts should differ", initialModCount != afterAddModCount ); Iterator elements = msg1.getMessageElements(); assertTrue( "message should have had an element", elements.hasNext() ); msg1.clear(); int afterClearModCount = msg1.getMessageModCount(); assertTrue( "cleared, modcounts should differ", afterAddModCount != afterClearModCount ); elements = msg1.getMessageElements(); assertTrue( "message should not have had an element", !elements.hasNext() ); } /** * tests the getMessageElements iterator **/ public void testMessageGetMessageElements() { Message msg1 = new Message(); List list = new ArrayList(); List namespaces = new ArrayList(); MessageElement elm1 = new StringMessageElement( "element1", "test1", null ); MessageElement elm2 = new StringMessageElement( "element2", "test2", null ); msg1.addMessageElement( elm1 ); list.add( elm1 ); namespaces.add( "" ); msg1.addMessageElement( "not-default", elm1 ); list.add( elm1 ); namespaces.add( "not-default" ); msg1.addMessageElement( "not-default", elm2 ); list.add( elm2 ); namespaces.add( "not-default" ); msg1.addMessageElement( "not-default", elm2 ); list.add( elm2 ); namespaces.add( "not-default" ); // check if all of the elements are returned Message.ElementIterator eachElement = msg1.getMessageElements(); Iterator eachListElement = list.iterator(); Iterator eachNamesElement = namespaces.iterator(); while( eachElement.hasNext() ) { MessageElement aElement = (MessageElement) eachElement.next(); MessageElement aListElement = (MessageElement) eachListElement.next(); assertTrue( "namespaces should have matched", eachElement.getNamespace().equals( eachNamesElement.next() ) ); assertTrue( "should be the same element", aElement == aListElement ); assertTrue( "elements should be equal", aElement.equals( aListElement ) ); } assertTrue( "iterators should have ended at the same time", !eachListElement.hasNext() ); } /** * tests the getMessageElements( String ) iterator **/ public void testMessageGetMessageElementsNamed() { Message msg1 = new Message(); List list = new ArrayList(); List namespaces = new ArrayList(); MessageElement elm1 = new StringMessageElement( "element1", "test1", null ); MessageElement elm2 = new StringMessageElement( "element2", "test2", null ); msg1.addMessageElement( elm1 ); list.add( elm1 ); namespaces.add( "" ); msg1.addMessageElement( "not-default", elm1 ); list.add( elm1 ); namespaces.add( "not-default" ); // check if all of the elements are returned Message.ElementIterator eachElement = msg1.getMessageElements( "element1" ); Iterator eachListElement = list.iterator(); Iterator eachNamesElement = namespaces.iterator(); while( eachElement.hasNext() ) { MessageElement aElement = (MessageElement) eachElement.next(); MessageElement aListElement = (MessageElement) eachListElement.next(); assertTrue( "elements should have the same name", "element1".equals( aElement.getElementName() ) ); assertTrue( "elements should be in the same namespace", eachElement.getNamespace().equals( eachNamesElement.next() ) ); assertTrue( "should be the same element", aElement == aListElement ); assertTrue( "elements should be equal", aElement.equals( aListElement ) ); } assertTrue( "iterators should have ended at the same time", !eachListElement.hasNext() ); } /** * tests the getMessageElementsOfNamespace iterator **/ public void testMessagegeGetMessageElementsOfNamespace() { Message msg1 = new Message(); List list = new ArrayList(); MessageElement elm1 = new StringMessageElement( "element1", "test1", null ); MessageElement elm2 = new StringMessageElement( "element2", "test2", null ); msg1.addMessageElement( elm1 ); msg1.addMessageElement( "not-default", elm1 ); list.add( elm1 ); msg1.addMessageElement( "not-default", elm2 ); list.add( elm2 ); msg1.addMessageElement( "not-default", elm2 ); list.add( elm2 ); // check if all of the elements of the namespace "not-default" are returned. Message.ElementIterator eachElement = msg1.getMessageElementsOfNamespace( "not-default" ); Iterator eachListElement = list.iterator(); while( eachElement.hasNext() ) { MessageElement aElement = (MessageElement) eachElement.next(); MessageElement aListElement = (MessageElement) eachListElement.next(); assertTrue( "elements should be in the same namespace", eachElement.getNamespace().equals( "not-default" ) ); assertTrue( "should be the same element", aElement == aListElement ); assertTrue( "elements should be equal", aElement.equals( aListElement ) ); } assertTrue( "iterators should have ended at the same time", !eachListElement.hasNext() ); } /** * tests the getMessageElements Mimetype iterator **/ public void testMessagegeGetMessageElementsMimeType() { Message msg1 = new Message(); List list = new ArrayList(); MimeMediaType foo = MimeMediaType.valueOf( "text/foo"); MimeMediaType bar = MimeMediaType.valueOf( "text/bar"); MessageElement elm1 = new ByteArrayMessageElement( "element1", foo, "test1".getBytes(), null ); MessageElement elm2 = new ByteArrayMessageElement( "element2", bar, "test2".getBytes(), null ); msg1.addMessageElement( elm1 ); list.add( elm1 ); msg1.addMessageElement( "not-default", elm1 ); list.add( elm1 ); msg1.addMessageElement( "not-default", elm2 ); msg1.addMessageElement( "not-default", elm2 ); // check if all of the elements matching type "foo" are returned. Message.ElementIterator eachElement = msg1.getMessageElements( foo ); Iterator eachListElement = list.iterator(); while( eachElement.hasNext() ) { MessageElement aElement = (MessageElement) eachElement.next(); assertTrue( "Should still be contents in the test vector.", eachListElement.hasNext() ); MessageElement aListElement = (MessageElement) eachListElement.next(); assertTrue( "Should be the correct mime type", aElement.getMimeType().equals(foo) ); assertTrue( "Should be the same element", aElement == aListElement ); assertTrue( "Elements should be equal", aElement.equals( aListElement ) ); } assertTrue( "iterators should have ended at the same time", !eachListElement.hasNext() ); } /** * tests the getMessageElements Mimetype iterator **/ public void testMessagegeGetMessageElementsNamespaceMimeType() { Message msg1 = new Message(); List list = new ArrayList(); MimeMediaType foo = MimeMediaType.valueOf( "text/foo"); MimeMediaType bar = MimeMediaType.valueOf( "text/bar"); MessageElement elm1 = new ByteArrayMessageElement( "element1", foo, "test1".getBytes(), null ); MessageElement elm2 = new ByteArrayMessageElement( "element2", bar, "test2".getBytes(), null ); msg1.addMessageElement( elm1 ); msg1.addMessageElement( "not-default", elm1 ); list.add( elm1 ); msg1.addMessageElement( "not-default", elm1 ); list.add( elm1 ); msg1.addMessageElement( "not-default", elm2 ); msg1.addMessageElement( "not-default", elm2 ); // check if all of the elements matching type "foo" are returned.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -