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