messagetests.java.svn-base
来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 182 行
SVN-BASE
182 行
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.j2me.services.messagingService.tests;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import com.sun.tck.j2me.services.messagingService.*;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;/** * Message tests. * */public class MessageTests extends TestCase { private Message msg_NullData; private Message msg_WithData; private Message msg_WithData_Copy; protected void setUp() throws Exception { msg_NullData = new Message(Message.MESSAGE, "From1", "To1", new String[] {"A1", "A11"}); msg_WithData = new Message( Message.GET_MESSAGE, "From2", "To2", new String[] {"B2", "B21"}, new byte[] {(byte)0, (byte)10}); msg_WithData_Copy = new Message( Message.GET_MESSAGE, "From2", "To2", new String[] {"B2", "B21"}, new byte[] {(byte)0, (byte)10}); } protected void tearDown() throws Exception { msg_NullData = null; msg_WithData = null; } public void testNotNull() { assertNotNull("Bundle must not be null", msg_NullData); assertNotNull("Bundle must not be null", msg_WithData); } public void testSetToFrom() { msg_NullData.setTo("NEW_TO"); assertEquals("NEW_TO", msg_NullData.getTo()); msg_WithData.setFrom("New_FROM"); assertEquals("New_FROM", msg_WithData.getFrom()); } public void testArgs() { String[] args1 = new String[] {}; String[] args2 = new String[] {"Test3", "Test4"}; msg_NullData.setArgs(args1); assertEquals(args1, msg_NullData.getArgs()); msg_WithData.setArgs(args2); assertEquals(args2, msg_WithData.getArgs()); } public void testData() { byte[] data1 = new byte[] {}; byte[] data2 = new byte[] {(byte)0, (byte)25}; msg_NullData.setData(data1); assertEquals(data1, msg_NullData.getData()); msg_WithData.setData(data2); assertEquals(data2, msg_WithData.getData()); } public void testToString() { assertEquals("Message[id=1, From=From1, To=To1, Args={A1, A11}, Data=null]", msg_NullData.toString()); msg_NullData.setArgs(null); assertEquals("Message[id=1, From=From1, To=To1, Args=null, Data=null]", msg_NullData.toString()); msg_NullData.setArgs(new String[] {}); assertEquals("Message[id=1, From=From1, To=To1, Args={}, Data=null]", msg_NullData.toString()); assertEquals("Message[id=2, From=From2, To=To2, Args={B2, B21}, Data={0, 10}]", msg_WithData.toString()); msg_WithData.setData(null); assertEquals("Message[id=2, From=From2, To=To2, Args={B2, B21}, Data=null]", msg_WithData.toString()); msg_WithData.setData(new byte[] {}); assertEquals("Message[id=2, From=From2, To=To2, Args={B2, B21}, Data={}]", msg_WithData.toString()); } public void testEqualsToInvalid() { assertFalse(msg_NullData.equals(null)); assertFalse(msg_NullData.equals(new String("Test"))); } public void testEquals() { assertEquals(msg_WithData, msg_WithData_Copy); assertFalse(msg_NullData.equals(msg_WithData)); } public void testHashCode() { // Same Object assertEquals(msg_WithData.hashCode(), msg_WithData.hashCode()); // Equal Objects assertEquals(msg_WithData.hashCode(), msg_WithData_Copy.hashCode()); // Another Equal Objects msg_WithData.setTo("ABCDE"); msg_WithData_Copy.setTo("ABCDE"); assertEquals(msg_WithData.hashCode(), msg_WithData_Copy.hashCode()); } public void testToBytesAndBack() throws IOException{ ByteArrayOutputStream baos; DataOutputStream dos = null; ByteArrayInputStream bais; DataInputStream dis = null; try { baos = new ByteArrayOutputStream(); dos = new DataOutputStream(baos); msg_NullData.write(dos); byte[] array = baos.toByteArray(); bais = new ByteArrayInputStream(array); dis = new DataInputStream(bais); assertEquals(msg_NullData, Message.read(dis)); } finally { if (dos != null) { try { dos.close(); } catch (IOException e) { // ignore } } if (dis != null) { try { dis.close(); } catch (IOException e) { // ignore } } } } public static Test suite() { return new TestSuite(MessageTests.class, "Message Tests"); } public static void main(String[] args) { junit.textui.TestRunner.run(MessageTests.class); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?