Search in sources :

Example 6 with XMLDecoder

use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.

the class MessageTest method testEncodeDecode.

/**
     * Test that a {@code Message} instance can be encoded and decoded without
     * errors.
     */
@Test
public void testEncodeDecode() {
    // Create an exception listener to detect errors while encoding and
    // decoding
    final LinkedList<Exception> exceptions = new LinkedList<Exception>();
    ExceptionListener listener = new ExceptionListener() {

        public void exceptionThrown(Exception e) {
            exceptions.addLast(e);
        }
    };
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder enc = new XMLEncoder(out);
    enc.setExceptionListener(listener);
    Message m1 = new NormalMessage();
    enc.writeObject(m1);
    enc.close();
    // verify that the write didn't fail
    if (!exceptions.isEmpty()) {
        AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
        // Can only chain one of the exceptions. Take the first one.
        afe.initCause(exceptions.getFirst());
        throw afe;
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    XMLDecoder dec = new XMLDecoder(in, null, listener);
    Message m2 = (Message) dec.readObject();
    assertNotNull(m2);
    dec.close();
    // verify that the read didn't fail
    if (!exceptions.isEmpty()) {
        AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
        // Can only chain one of the exceptions. Take the first one.
        afe.initCause(exceptions.getFirst());
        throw afe;
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssertionFailedError(junit.framework.AssertionFailedError) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 7 with XMLDecoder

use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.

the class GroupTest method testEncodeDecode.

/**
     * Test that a {@code Group} instance can be encoded and decoded without
     * errors.
     */
@Test
public void testEncodeDecode() {
    // Create an exception listener to detect errors while encoding and
    // decoding
    final LinkedList<Exception> exceptions = new LinkedList<Exception>();
    ExceptionListener listener = new ExceptionListener() {

        @Override
        public void exceptionThrown(Exception e) {
            exceptions.addLast(e);
        }
    };
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder enc = new XMLEncoder(out);
    enc.setExceptionListener(listener);
    Group g1 = new Group();
    enc.writeObject(g1);
    enc.close();
    // verify that the write didn'abcd fail
    if (!exceptions.isEmpty()) {
        AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
        // Can only chain one of the exceptions. Take the first one.
        afe.initCause(exceptions.getFirst());
        throw afe;
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    XMLDecoder dec = new XMLDecoder(in, null, listener);
    Group g2 = (Group) dec.readObject();
    assertNotNull(g2);
    dec.close();
    // verify that the read didn'abcd fail
    if (!exceptions.isEmpty()) {
        AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
        // Can only chain one of the exceptions. Take the first one.
        afe.initCause(exceptions.getFirst());
        throw afe;
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssertionFailedError(junit.framework.AssertionFailedError) LinkedList(java.util.LinkedList) PatternSyntaxException(java.util.regex.PatternSyntaxException) Test(org.junit.Test)

Example 8 with XMLDecoder

use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.

the class IgnoredNamesTest method testEncodeDecode.

/**
     * Make sure that encoding and decoding IgnoredNames object is 1:1 operation.
     * @throws FileNotFoundException
     * @throws IOException 
     */
@Test
public void testEncodeDecode() throws FileNotFoundException, IOException {
    IgnoredNames in = new IgnoredNames();
    // Add file and directory to list of ignored items.
    in.add("f:foo.txt");
    in.add("d:bar");
    // Create an exception listener to detect errors while encoding and decoding
    final LinkedList<Exception> exceptions = new LinkedList<Exception>();
    ExceptionListener listener = new ExceptionListener() {

        @Override
        public void exceptionThrown(Exception e) {
            exceptions.addLast(e);
        }
    };
    // Actually create the file and directory for much better test coverage.
    File tmpdir = FileUtilities.createTemporaryDirectory("ignoredNames");
    File foo = new File(tmpdir, "foo.txt");
    foo.createNewFile();
    assertTrue(foo.isFile());
    File bar = new File(tmpdir, "bar");
    bar.mkdir();
    assertTrue(bar.isDirectory());
    // Store the IgnoredNames object as XML file.
    File testXML = new File(tmpdir, "Test.xml");
    XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(testXML)));
    e.setExceptionListener(listener);
    e.writeObject(in);
    e.close();
    // Restore the IgnoredNames object from XML file.
    XMLDecoder d = new XMLDecoder(new FileInputStream(testXML));
    IgnoredNames in2 = (IgnoredNames) d.readObject();
    d.close();
    // Verify that the XML encoding/decoding did not fail.
    if (!exceptions.isEmpty()) {
        AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
        // Can only chain one of the exceptions. Take the first one.
        afe.initCause(exceptions.getFirst());
        throw afe;
    }
    // Make sure the complete list of items is equal after decoding.
    // This will is a simple casual test that cannot verify that sub-classes
    // are intact. For that there are the following tests.
    assertTrue(in.getItems().containsAll(in2.getItems()));
    // Use the restored object to test the matching of file and directory.
    assertTrue(in2.ignore("foo.txt"));
    assertTrue(in2.ignore("bar"));
    assertTrue(in2.ignore(foo));
    assertTrue(in2.ignore(bar));
    // Cleanup.
    FileUtilities.removeDirs(tmpdir);
}
Also used : XMLEncoder(java.beans.XMLEncoder) FileOutputStream(java.io.FileOutputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) AssertionFailedError(junit.framework.AssertionFailedError) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) LinkedList(java.util.LinkedList) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) CAnalyzerFactoryTest(org.opensolaris.opengrok.analysis.c.CAnalyzerFactoryTest) Test(org.junit.Test)

Example 9 with XMLDecoder

use of java.beans.XMLDecoder in project jdk8u_jdk by JetBrains.

the class Test4625418 method test.

private void test(String string) {
    try {
        File file = new File("4625418." + this.encoding + ".xml");
        FileOutputStream output = new FileOutputStream(file);
        XMLEncoder encoder = new XMLEncoder(output, this.encoding, true, 0);
        encoder.setExceptionListener(this);
        encoder.writeObject(string);
        encoder.close();
        FileInputStream input = new FileInputStream(file);
        XMLDecoder decoder = new XMLDecoder(input);
        decoder.setExceptionListener(this);
        Object object = decoder.readObject();
        decoder.close();
        if (!string.equals(object))
            throw new Error(this.encoding + " - can't read properly");
        file.delete();
    } catch (FileNotFoundException exception) {
        throw new Error(this.encoding + " - file not found", exception);
    } catch (IllegalCharsetNameException exception) {
        throw new Error(this.encoding + " - illegal charset name", exception);
    } catch (UnsupportedCharsetException exception) {
        throw new Error(this.encoding + " - unsupported charset", exception);
    } catch (UnsupportedOperationException exception) {
        throw new Error(this.encoding + " - unsupported encoder", exception);
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) XMLEncoder(java.beans.XMLEncoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) FileOutputStream(java.io.FileOutputStream) XMLDecoder(java.beans.XMLDecoder) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 10 with XMLDecoder

use of java.beans.XMLDecoder in project jdk8u_jdk by JetBrains.

the class Test6341798 method test.

private static void test(Locale locale, byte[] data) {
    Locale.setDefault(locale);
    System.out.println("locale = " + locale);
    XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(data));
    System.out.println("object = " + decoder.readObject());
    decoder.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder)

Aggregations

XMLDecoder (java.beans.XMLDecoder)24 ByteArrayInputStream (java.io.ByteArrayInputStream)15 XMLEncoder (java.beans.XMLEncoder)7 IOException (java.io.IOException)7 ExceptionListener (java.beans.ExceptionListener)6 LinkedList (java.util.LinkedList)6 BufferedInputStream (java.io.BufferedInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)5 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 AssertionFailedError (junit.framework.AssertionFailedError)4 File (java.io.File)3 BufferedOutputStream (java.io.BufferedOutputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 MapNode (aimax.osm.data.entities.MapNode)1 MapViewEvent (aimax.osm.viewer.MapViewEvent)1