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:
018: package org.apache.harmony.jndi.tests.javax.naming;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025:
026: import javax.naming.CompositeName;
027: import javax.naming.InvalidNameException;
028:
029: import junit.framework.TestCase;
030:
031: public class InvalidNameExceptionTest extends TestCase {
032:
033: /**
034: * Test serialize InvalidNameException: write a InvalidNameException object
035: * into a byte array, and read from it. the two object should be equals.
036: */
037: public void testSerializable_Simple()
038: throws ClassNotFoundException, IOException,
039: InvalidNameException {
040:
041: InvalidNameException exception = new InvalidNameException(
042: "Test exception Serializable");
043: exception.setRemainingName(new CompositeName(
044: "www.apache.org/foundation"));
045: exception.setResolvedName(new CompositeName(
046: "http://www.apache.org/index.html"));
047: exception.setResolvedObj("This is a string object.");
048: exception
049: .setRootCause(new NullPointerException("null pointer"));
050:
051: // write to byte array
052: ByteArrayOutputStream baos = new ByteArrayOutputStream();
053: ObjectOutputStream oos = new ObjectOutputStream(baos);
054: oos.writeObject(exception);
055: byte[] buffer = baos.toByteArray();
056: oos.close();
057: baos.close();
058:
059: // read from byte array
060: ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
061: ObjectInputStream ois = new ObjectInputStream(bais);
062: InvalidNameException exception2 = (InvalidNameException) ois
063: .readObject();
064: ois.close();
065: bais.close();
066:
067: assertEquals(exception.getExplanation(), exception2
068: .getExplanation());
069: assertEquals(exception.getResolvedObj(), exception2
070: .getResolvedObj());
071: assertEquals(exception.getRemainingName(), exception2
072: .getRemainingName());
073: assertEquals(exception.getResolvedName(), exception2
074: .getResolvedName());
075: assertEquals(exception.getRootCause().getMessage(), exception2
076: .getRootCause().getMessage());
077: assertEquals(exception.getRootCause().getClass(), exception2
078: .getRootCause().getClass());
079: }
080:
081: /**
082: * Test InvalidNameException serialization compatibility
083: */
084: public void testSerializable_compatibility()
085: throws InvalidNameException, ClassNotFoundException,
086: IOException {
087: ObjectInputStream ois = new ObjectInputStream(
088: getClass()
089: .getClassLoader()
090: .getResourceAsStream(
091: "/serialization/javax/naming/InvalidNameException.ser"));
092: InvalidNameException exception2 = (InvalidNameException) ois
093: .readObject();
094: ois.close();
095:
096: InvalidNameException exception = new InvalidNameException(
097: "Test exception Serializable");
098: exception.setRemainingName(new CompositeName(
099: "www.apache.org/foundation"));
100: exception.setResolvedName(new CompositeName(
101: "http://www.apache.org/index.html"));
102: exception.setResolvedObj("This is a string object.");
103: exception
104: .setRootCause(new NullPointerException("null pointer"));
105:
106: assertEquals(exception.getExplanation(), exception2
107: .getExplanation());
108: assertEquals(exception.getResolvedObj(), exception2
109: .getResolvedObj());
110: assertEquals(exception.getRemainingName(), exception2
111: .getRemainingName());
112: assertEquals(exception.getResolvedName(), exception2
113: .getResolvedName());
114: assertEquals(exception.getRootCause().getMessage(), exception2
115: .getRootCause().getMessage());
116: assertEquals(exception.getRootCause().getClass(), exception2
117: .getRootCause().getClass());
118: }
119: }
|