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