📄 messagetest.java
字号:
/************************************************************************
*
* $Id: MessageTest.java,v 1.1 2002/02/18 16:06:30 echtcherbina 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.test.endpoint;
import junit.framework.*;
import java.io.Writer;
import java.io.StringWriter;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Random;
import net.jxta.endpoint.*;
import net.jxta.impl.endpoint.*;
import net.jxta.document.MimeMediaType;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.DataInputStream;
import net.jxta.util.StringEnumeration;
/**
*
* @author chgenly
* @version
*/
public class MessageTest extends TestCase {
MimeMediaType[] testTypes = {
new MimeMediaType("text", "plain"),
new MimeMediaType("text", "xml"),
new MimeMediaType("pig-in-a", "poke")
};
/** Creates new DocTest */
public MessageTest(String name)
{
super(name);
}
public void testCreateElement()
{
try {
Message m = new MessageImpl();
assertTrue(m != null);
MessageElement element1 = createElement(m, 1, "ding");
m.addElement(element1);
assertTrue(element1.getName().equals("ding:elem1"));
checkElement(element1);
} catch(Exception ex) {
fail("Threw "+ex);
}
}
/**
* Same as ByteArrayInputStream, but we can ask if the stream
* has been closed.
*/
static class BAIS extends ByteArrayInputStream {
boolean closed = false;
public BAIS(byte[] bytes)
{
super(bytes);
}
public void close() throws IOException
{
closed = true;
super.close();
}
public boolean isClosed()
{
return closed;
}
}
public void testCreateElementFromStream()
{
try {
Message m = new MessageImpl();
byte[] bytes = {1, 2, 3, 4};
BAIS bais = new BAIS(bytes);
MessageElement el = m.newMessageElement("hi", null, bais);
assertTrue(el != null);
// We expect the input stream to be closed
assertTrue(bais.read() == -1);
assertTrue(bais.isClosed());
for(int i=0; i<bytes.length; ++i)
assertTrue(el.getBytesOffset()[i] == bytes[i]);
} catch(Exception ex) {
ex.printStackTrace();
fail(ex.toString());
}
}
public void testGetNames()
{
Message m = new MessageImpl();
for(int i=0; i<40; ++i) {
m.setBytes("el"+i, new byte[0]);
StringEnumeration se = m.getNames();
for(int j=i; j >= 0; --j) {
String s = se.nextString();
assertEquals("el"+j, s);
}
}
}
public void testWireFormatXML()
{
try {
//Create the message
Message m = new MessageImpl();
byte[] bytes = {1, 2, 3, 4};
BAIS bais = new BAIS(bytes);
MessageElement el1 = m.newMessageElement("hi", new MimeMediaType("application/octet-stream"),
bais);
assertTrue(el1 != null);
m.addElement(el1);
String text = "This is <text> & & <>";
MessageElement el2 = m.newMessageElement("a string", new MimeMediaType("text/plain"),
text.getBytes());
assertTrue(el2 != null);
m.addElement(el2);
// Write the message
ByteArrayOutputStream baos = new ByteArrayOutputStream();
getXMLMessageFormat().writeMessage(baos, m);
// Read the message
byte[] mbytes = baos.toByteArray();
//String s = new String(mbytes);
//System.out.println("s="+s);
//printMessage("original message", m);
ByteArrayInputStream is = new ByteArrayInputStream(mbytes);
Message m2 = new MessageImpl();
getXMLMessageFormat().readMessage(is, m2);
//printMessage("read message", m2);
// Verify the read message
assertTrue(m.equals(m2));
} catch(Exception ex) {
ex.printStackTrace();
fail(ex.toString());
}
}
public void testCreateElementFromStreamWithLength()
{
try {
Message m = new MessageImpl();
byte[] bytes = {1, 2, 3, 4};
BAIS bais = new BAIS(bytes);
MessageElement el = m.newMessageElement("hi", null, bais, bytes.length-1);
assertTrue(el != null);
// We expect the input stream to remain open
assertTrue(bais.read() == 4);
assertTrue(!bais.isClosed());
for(int i=0; i<bytes.length-1; ++i)
assertTrue(el.getBytesOffset()[i] == bytes[i]);
} catch(Exception ex) {
ex.printStackTrace();
fail(ex.toString());
}
}
public void testPush()
{
byte[] bytes = {5,6,7,8,9,10,44};
try {
for(int t=0; t<2; ++t) {
Message m = new MessageImpl();
assertTrue(m != null);
if (t == 0)
m.setBytes("n:abc", bytes);
else {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
MessageElement el = m.newMessageElement ("n:abc",
new MimeMediaType ("text/xml"),
bais);
}
MessageElement el = m.getElement("n:abc");
assertTrue(el.getName().equals("n:abc"));
assertTrue(el.getType() == null);
assertTrue(el.getOffset() == 0);
assertTrue(el.getLength() == bytes.length);
byte[] bs = el.getBytesOffset();
assertTrue(bs.length == bytes.length);
for(int i=0; i<bytes.length; ++i) {
assertTrue(bytes[i] == bs[i]);
}
}
} catch(Exception ex) {
fail(ex.toString());
}
}
public void testPop()
{
byte[] bytes = {1,2,3,4,5,6,45};
try {
for(int t=0; t<2; ++t) {
Message m = new MessageImpl();
MimeMediaType type = new MimeMediaType("this", "that");
MessageElement el = m.newMessageElement("myspace:yourplace", type, bytes);
m.addElement(el);
byte[] bs;
if (t == 0) {
bs = m.getBytes("myspace:yourplace");
} else {
InputStream is =
m.getElement("myspace:yourplace").getStream ();
DataInputStream dis = new DataInputStream(is);
bs = new byte[is.available()];
dis.readFully(bs);
}
assertTrue(bs.length == bytes.length);
for(int i=0; i<bytes.length; ++i) {
assertTrue(bytes[i] == bs[i]);
}
}
} catch(Exception ex) {
fail(ex.toString());
}
}
public void testMessageGetElements()
{
Vector v = new Vector();
Message m = new MessageImpl();
assertTrue(m != null);
for(int i=0; i<30; ++i) {
MessageElement element = createElement(m, i);
m.addElement(element);
v.addElement(element);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -