479N/A/*
479N/A * CDDL HEADER START
479N/A *
479N/A * The contents of this file are subject to the terms of the
479N/A * Common Development and Distribution License (the "License").
479N/A * You may not use this file except in compliance with the License.
479N/A *
479N/A * See LICENSE.txt included in this distribution for the specific
479N/A * language governing permissions and limitations under the License.
479N/A *
479N/A * When distributing Covered Code, include this CDDL HEADER in each
479N/A * file and include the License file at LICENSE.txt.
479N/A * If applicable, add the following below this CDDL HEADER, with the
479N/A * fields enclosed by brackets "[]" replaced with your own identifying
479N/A * information: Portions Copyright [yyyy] [name of copyright owner]
479N/A *
479N/A * CDDL HEADER END
479N/A */
479N/A
479N/A/*
479N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
479N/A * Use is subject to license terms.
479N/A */
479N/A
479N/Apackage org.opensolaris.opengrok.configuration;
479N/A
479N/Aimport java.beans.ExceptionListener;
479N/Aimport java.beans.XMLDecoder;
479N/Aimport java.beans.XMLEncoder;
479N/Aimport java.io.ByteArrayInputStream;
479N/Aimport java.io.ByteArrayOutputStream;
479N/Aimport java.util.LinkedList;
479N/Aimport junit.framework.AssertionFailedError;
479N/Aimport org.junit.Test;
479N/Aimport static org.junit.Assert.*;
479N/A
479N/Apublic class ProjectTest {
479N/A /**
479N/A * Test that a {@code Project} instance can be encoded and decoded
479N/A * without errors. Bug #3077.
479N/A */
479N/A @Test
479N/A public void testEncodeDecode() {
479N/A // Create an exception listener to detect errors while encoding and
479N/A // decoding
479N/A final LinkedList<Exception> exceptions = new LinkedList<Exception>();
479N/A ExceptionListener listener = new ExceptionListener() {
479N/A public void exceptionThrown(Exception e) {
479N/A exceptions.addLast(e);
479N/A }
479N/A };
479N/A
479N/A ByteArrayOutputStream out = new ByteArrayOutputStream();
479N/A XMLEncoder enc = new XMLEncoder(out);
479N/A enc.setExceptionListener(listener);
479N/A Project p1 = new Project();
479N/A enc.writeObject(p1);
479N/A enc.close();
479N/A
479N/A // verify that the write didn't fail
479N/A if (!exceptions.isEmpty()) {
479N/A AssertionFailedError afe = new AssertionFailedError(
479N/A "Got " + exceptions.size() + " exception(s)");
479N/A // Can only chain one of the exceptions. Take the first one.
479N/A afe.initCause(exceptions.getFirst());
479N/A throw afe;
479N/A }
479N/A
479N/A ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
479N/A XMLDecoder dec = new XMLDecoder(in, null, listener);
479N/A Project p2 = (Project) dec.readObject();
479N/A assertNotNull(p2);
479N/A dec.close();
479N/A
479N/A // verify that the read didn't fail
479N/A if (!exceptions.isEmpty()) {
479N/A AssertionFailedError afe = new AssertionFailedError(
479N/A "Got " + exceptions.size() + " exception(s)");
479N/A // Can only chain one of the exceptions. Take the first one.
479N/A afe.initCause(exceptions.getFirst());
479N/A throw afe;
479N/A }
479N/A }
479N/A}