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.CompositeName;
026: import javax.naming.LinkRef;
027: import javax.naming.MalformedLinkException;
028: import javax.naming.Name;
029: import javax.naming.NamingException;
030: import javax.naming.StringRefAddr;
031:
032: import junit.framework.TestCase;
033:
034: public class LinkRefTest extends TestCase {
035:
036: public void testConstructor_ByName() throws NamingException {
037: Name name = new CompositeName("www.apache.org/index.html");
038: LinkRef linkRef = new LinkRef(name);
039: assertEquals(1, linkRef.size());
040: assertEquals(name.toString(), linkRef.getLinkName());
041: }
042:
043: public void testConstrcutor_ByNameNull() {
044: Name name = null;
045: try {
046: new LinkRef(name);
047: fail("It should throw NullPointerException.");
048: } catch (NullPointerException e) {
049: }
050: }
051:
052: public void testConstructor_ByString() {
053: String name = "www.apache.org/index.html";
054: LinkRef linkRef = new LinkRef(name);
055: assertEquals(1, linkRef.size());
056: }
057:
058: public void testConstrcutor_ByStringNull() {
059: String name = null;
060: LinkRef linkRef = new LinkRef(name);
061: assertNull(linkRef.get("LinkAddress").getContent());
062: }
063:
064: public void testGetLinkName_Simple() throws NamingException {
065: String name = "www.apache.org/index.html";
066: LinkRef linkRef = new LinkRef(name);
067:
068: assertEquals(name, linkRef.getLinkName());
069: }
070:
071: public void testGetLinkName_MalformedLinkException() {
072: /*
073: * log.setMethod("testGetLinkName_MalformedLinkException");
074: *
075: * String name = "www.apache.org/index.html"; LinkRef linkRef = new
076: * LinkRef(name); linkRef.className = "illegal class name";
077: * log.log(linkRef.toString()); try { String link =
078: * linkRef.getLinkName(); fail( "It should throw
079: * MalformedLinkException."); } catch (Throwable e) {
080: * log.log(e.getClass().getName()); log.log(e.getMessage()); }
081: */
082: }
083:
084: public void testGetLinkName_NamingException()
085: throws NamingException {
086: String name = "www.apache.org/index.html";
087: LinkRef linkRef = new LinkRef(name);
088: linkRef.clear();
089: try {
090: linkRef.getLinkName();
091: fail("It should throw a MalformedLinkException");
092: } catch (MalformedLinkException e1) {
093: }
094: }
095:
096: public void testGetLinkName_InvalidClassName()
097: throws NamingException {
098: String name = "www.apache.org/index.html";
099: MyLinkRef linkRef = new MyLinkRef(name);
100: linkRef.setClassName("Invalid Class name");
101: try {
102: linkRef.getLinkName();
103: fail("Should throw MalformedLinkException");
104: } catch (MalformedLinkException e) {
105: }
106: }
107:
108: public void testGetClassName() {
109: String name = "www.apache.org/index.html";
110: LinkRef linkRef = new LinkRef(name);
111: assertEquals(LinkRef.class.getName(), linkRef.getClassName());
112: }
113:
114: public void testGetFactoryClassName() {
115: String name = "www.apache.org/index.html";
116: LinkRef linkRef = new LinkRef(name);
117: assertNull(linkRef.getFactoryClassName());
118: }
119:
120: public void testGetFactoryClassLocation() {
121: String name = "www.apache.org/index.html";
122: LinkRef linkRef = new LinkRef(name);
123: assertNull(linkRef.getFactoryClassLocation());
124: }
125:
126: public void testEquals_Simple() {
127: String name = "www.apache.org/index.html";
128: LinkRef linkRef0 = new LinkRef(name);
129: LinkRef linkRef1 = new LinkRef(name);
130:
131: assertTrue(linkRef0.equals(linkRef1));
132: assertTrue(linkRef0.equals(linkRef0));
133: assertTrue(linkRef1.equals(linkRef0));
134: assertFalse(linkRef0.equals(null));
135: }
136:
137: public void testHashcode_Simple() {
138: String name = "www.apache.org/index.html";
139: StringRefAddr addr = new StringRefAddr("LinkAddress", name);
140: String className = LinkRef.class.getName();
141: LinkRef linkRef = new LinkRef(name);
142:
143: assertEquals(className.hashCode() + addr.hashCode(), linkRef
144: .hashCode());
145: }
146:
147: public void testToString_Simple() {
148: String name = "www.apache.org/index.html";
149: LinkRef linkRef = new LinkRef(name);
150: StringRefAddr addr1 = new StringRefAddr("LinkAddress",
151: "www.apache.org");
152: linkRef.add(addr1);
153:
154: /*
155: * assertEquals( "Reference class name: " + LinkRef.class.getName() +
156: * "\nReference addresses:\n\t" + addr0.toString() + "\n\t" +
157: * addr1.toString() + "\n", linkRef.toString());
158: */
159: assertNotNull(linkRef.toString());
160: }
161:
162: public void testSerializable_Simple()
163: throws ClassNotFoundException, IOException {
164: String name = "www.apache.org/index.html";
165: LinkRef linkRef = new LinkRef(name);
166: StringRefAddr addr = new StringRefAddr("StringRefAddr",
167: "This is a String RefAddr.");
168: linkRef.add(addr);
169:
170: // write to byte array
171: ByteArrayOutputStream baos = new ByteArrayOutputStream();
172: ObjectOutputStream oos = new ObjectOutputStream(baos);
173: oos.writeObject(linkRef);
174: byte[] buffer = baos.toByteArray();
175: oos.close();
176: baos.close();
177:
178: // read from byte array
179: ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
180: ObjectInputStream ois = new ObjectInputStream(bais);
181: LinkRef linkRef2 = (LinkRef) ois.readObject();
182: ois.close();
183: bais.close();
184:
185: assertEquals(linkRef, linkRef2);
186: }
187:
188: public void testSerializable_compatibility()
189: throws ClassNotFoundException, IOException {
190: ObjectInputStream ois = new ObjectInputStream(getClass()
191: .getClassLoader().getResourceAsStream(
192: "/serialization/javax/naming/LinkRef.ser"));
193: LinkRef linkRef2 = (LinkRef) ois.readObject();
194: ois.close();
195:
196: String name = "www.eclipse.org/org/index.html";
197: LinkRef linkRef = new LinkRef(name);
198: StringRefAddr addr = new StringRefAddr("StringRefAddr",
199: "This is a String RefAddr.");
200: linkRef.add(addr);
201:
202: assertEquals(linkRef, linkRef2);
203: }
204:
205: class MyLinkRef extends LinkRef {
206: private static final long serialVersionUID = 1L;
207:
208: public MyLinkRef(String s) {
209: super (s);
210: }
211:
212: public void setClassName(String className) {
213: this.className = className;
214: }
215: }
216: }
|