001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.jndi.tests.javax.naming;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024:
025: import javax.naming.StringRefAddr;
026:
027: import junit.framework.TestCase;
028:
029: public class StringRefAddrTest extends TestCase {
030:
031: public void testConstructor_Simple() {
032: String type = "StringAddr";
033: String address = "/home/neuser";
034: StringRefAddr addr = new StringRefAddr(type, address);
035: assertEquals(type, addr.getType());
036: assertEquals(address, addr.getContent());
037: }
038:
039: public void testConstructor_AddressNull() {
040: String type = "StringAddr";
041: StringRefAddr addr = new StringRefAddr(type, null);
042: assertEquals(type, addr.getType());
043: assertNull(addr.getContent());
044: }
045:
046: public void testGetType_Normal() {
047: StringRefAddr addr = new StringRefAddr("type", "content");
048: assertEquals("type", addr.getType());
049: }
050:
051: public void testGetType_Null() {
052: StringRefAddr addr = new StringRefAddr(null, "content");
053: assertNull(addr.getType());
054: }
055:
056: public void testGetContent_Normal() {
057: StringRefAddr addr = new StringRefAddr("type", "content");
058: assertEquals("content", addr.getContent());
059: }
060:
061: public void testGetContent_Null() {
062: StringRefAddr addr = new StringRefAddr("type", null);
063: assertNull(addr.getContent());
064: }
065:
066: public void testConstructor_Null() {
067: StringRefAddr addr = new StringRefAddr(null, null);
068: assertNull(addr.getType());
069: assertNull(addr.getContent());
070: }
071:
072: public void testEquals_Simple() {
073: String type = "String address";
074: String address = "this is a simple object";
075: StringRefAddr addr0 = new StringRefAddr(type, address);
076: StringRefAddr addr1 = new StringRefAddr(type, address);
077: assertTrue(addr0.equals(addr1));
078: }
079:
080: public void testEquals_NotEquals() {
081: String type = "String address";
082: String address0 = "this is a simple object";
083: String address1 = "this is another simple object";
084: StringRefAddr addr0 = new StringRefAddr(type, address0);
085: StringRefAddr addr1 = new StringRefAddr(type, address1);
086: assertFalse(addr0.equals(addr1));
087: }
088:
089: public void testEquals_AddressNull() {
090: String type = "null";
091: StringRefAddr addr0 = new StringRefAddr(type, null);
092: StringRefAddr addr1 = new StringRefAddr(type, null);
093: assertTrue(addr0.equals(addr0));
094: assertFalse(addr0.equals(null));
095: assertTrue(addr0.equals(addr1));
096: assertTrue(addr1.equals(addr0));
097: }
098:
099: public void testEquals_ObjNull() {
100: String type = "String address";
101: String address = "this is a simple object";
102: StringRefAddr addr0 = new StringRefAddr(type, address);
103: assertFalse(addr0.equals(null));
104: }
105:
106: public void testEquals_TypeNull() {
107: String address = "this is a simple object";
108: StringRefAddr addr0 = new StringRefAddr(null, address);
109: StringRefAddr addr1 = new StringRefAddr(null, address);
110: try {
111: addr0.equals(addr1);
112: fail("Should throw NullPointerException.");
113: } catch (NullPointerException e) {
114: }
115: }
116:
117: public void testHashcode_Simple() {
118: String type = "String address";
119: String address = "this is a simple object";
120: StringRefAddr addr0 = new StringRefAddr(type, address);
121: assertEquals(type.hashCode() + address.hashCode(), addr0
122: .hashCode());
123: }
124:
125: public void testHashcode_TypeNull() {
126: String content = "null";
127: StringRefAddr addr0 = new StringRefAddr(null, content);
128: try {
129: addr0.hashCode();
130: fail("Should throw NullPointerException.");
131: } catch (NullPointerException e) {
132: }
133:
134: }
135:
136: public void testHashcode_AddressNull() {
137: String type = "null";
138: StringRefAddr addr0 = new StringRefAddr(type, null);
139: assertEquals(type.hashCode(), addr0.hashCode());
140: }
141:
142: public void testToString_Simple() {
143: String type = "address type";
144: String address = "this is a simple object";
145: StringRefAddr addr0 = new StringRefAddr(type, address);
146: /*
147: * assertEquals( "The type of the address is: " + type + "\nThe content
148: * of the address is: " + address + "\n", addr0.toString());
149: */
150: assertNotNull(addr0.toString());
151: }
152:
153: public void testToString_AddressNull() {
154: String type = "null";
155: StringRefAddr addr0 = new StringRefAddr(type, null);
156: // System.out.println();
157: /*
158: * assertEquals( "The type of the address is: " + type + "\nThe content
159: * of the address is: null\n", addr0.toString());
160: */
161: assertNotNull(addr0.toString());
162: }
163:
164: public void testToString_typeNull() {
165: String address = "this is a simple object with null type";
166: StringRefAddr stringRefAddr = new StringRefAddr(null, address);
167: // assertEquals(str, stringRefAddr.toString());
168: assertNotNull(stringRefAddr.toString());
169: }
170:
171: public void testSerializable_Simple()
172: throws ClassNotFoundException, IOException {
173: String type = "String address";
174: String address = "this is a simple object";
175: StringRefAddr addr = new StringRefAddr(type, address);
176:
177: // write to byte array
178: ByteArrayOutputStream baos = new ByteArrayOutputStream();
179: ObjectOutputStream oos = new ObjectOutputStream(baos);
180: oos.writeObject(addr);
181: byte[] buffer = baos.toByteArray();
182: oos.close();
183: baos.close();
184:
185: // read from byte array
186: ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
187: ObjectInputStream ois = new ObjectInputStream(bais);
188: StringRefAddr addr2 = (StringRefAddr) ois.readObject();
189: ois.close();
190: bais.close();
191:
192: assertEquals(addr, addr2);
193: }
194:
195: public void testSerializable_compatibility()
196: throws ClassNotFoundException, IOException {
197: ObjectInputStream ois = new ObjectInputStream(
198: getClass()
199: .getClassLoader()
200: .getResourceAsStream(
201: "/serialization/javax/naming/StringRefAddr.ser"));
202: StringRefAddr addr = (StringRefAddr) ois.readObject();
203: ois.close();
204:
205: String type = "StringAddr";
206: String address = "/home/anyuser";
207: StringRefAddr addr2 = new StringRefAddr(type, address);
208:
209: assertEquals(addr, addr2);
210: }
211: }
|