Search in sources :

Example 1 with ExceptionListener

use of java.beans.ExceptionListener 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) PatternSyntaxException(java.util.regex.PatternSyntaxException) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with ExceptionListener

use of java.beans.ExceptionListener 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 3 with ExceptionListener

use of java.beans.ExceptionListener 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) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) CAnalyzerFactoryTest(org.opensolaris.opengrok.analysis.c.CAnalyzerFactoryTest) Test(org.junit.Test)

Example 4 with ExceptionListener

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

the class Configuration method decodeObject.

private static Configuration decodeObject(InputStream in) throws IOException {
    final Object ret;
    final LinkedList<Exception> exceptions = new LinkedList<>();
    ExceptionListener listener = new ExceptionListener() {

        @Override
        public void exceptionThrown(Exception e) {
            exceptions.addLast(e);
        }
    };
    try (XMLDecoder d = new XMLDecoder(new BufferedInputStream(in), null, listener)) {
        ret = d.readObject();
    }
    if (!(ret instanceof Configuration)) {
        throw new IOException("Not a valid config file");
    }
    if (!exceptions.isEmpty()) {
        // see {@code addGroup}
        if (exceptions.getFirst() instanceof IOException) {
            throw (IOException) exceptions.getFirst();
        }
        throw new IOException(exceptions.getFirst());
    }
    Configuration conf = ((Configuration) ret);
    // Removes all non root groups.
    // This ensures that when the configuration is reloaded then the set
    // contains only root groups. Subgroups are discovered again
    // as follows below
    conf.groups.removeIf(new Predicate<Group>() {

        @Override
        public boolean test(Group g) {
            return g.getParent() != null;
        }
    });
    // Traversing subgroups and checking for duplicates,
    // effectively transforms the group tree to a structure (Set)
    // supporting an iterator.
    TreeSet<Group> copy = new TreeSet<>();
    LinkedList<Group> stack = new LinkedList<>(conf.groups);
    while (!stack.isEmpty()) {
        Group group = stack.pollFirst();
        stack.addAll(group.getSubgroups());
        if (!copy.add(group)) {
            throw new IOException(String.format("Duplicate group name '%s' in configuration.", group.getName()));
        }
        // populate groups where the current group in in their subtree
        Group tmp = group.getParent();
        while (tmp != null) {
            tmp.addDescendant(group);
            tmp = tmp.getParent();
        }
    }
    conf.setGroups(copy);
    return conf;
}
Also used : IOException(java.io.IOException) PatternSyntaxException(java.util.regex.PatternSyntaxException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) BufferedInputStream(java.io.BufferedInputStream) TreeSet(java.util.TreeSet) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener)

Example 5 with ExceptionListener

use of java.beans.ExceptionListener in project siddhi by wso2.

the class FaultStreamTestCase method faultStreamTest11.

@Test(dependsOnMethods = "faultStreamTest10")
public void faultStreamTest11() throws Exception {
    log.info("faultStreamTest11-Tests capturing runtime exceptions by registering an exception " + "listener to SiddhiAppRuntime");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "define stream outputStream (symbol string, price float);" + "\n" + "@PrimaryKey('symbol')" + "define table cseStoreTable (symbol string, price float);" + "\n" + "@info(name = 'query1') " + "from cseEventStream " + "select symbol, price " + "insert into cseStoreTable ;" + "\n" + "@info(name = 'query2') " + "from cseEventStream " + "select symbol, price " + "insert into outputStream ;" + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.handleRuntimeExceptionWith(new ExceptionListener() {

        @Override
        public void exceptionThrown(Exception e) {
            failedCaught = true;
            failedCount.incrementAndGet();
        }
    });
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            count.incrementAndGet();
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    try {
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    inputHandler.send(new Object[] { "IBM", 0f, 100L });
                    inputHandler.send(new Object[] { "IBM", 1f, 200L });
                } catch (InterruptedException e) {
                }
            }
        };
        thread.start();
        Thread.sleep(500);
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        siddhiAppRuntime.shutdown();
    }
    Assert.assertTrue(eventArrived);
    Assert.assertTrue(failedCaught);
    Assert.assertEquals(count.get(), 2);
    Assert.assertEquals(failedCount.get(), 1);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ExceptionListener(java.beans.ExceptionListener) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

ExceptionListener (java.beans.ExceptionListener)15 XMLDecoder (java.beans.XMLDecoder)12 LinkedList (java.util.LinkedList)10 XMLEncoder (java.beans.XMLEncoder)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 AssertionFailedError (junit.framework.AssertionFailedError)4 Test (org.junit.Test)4 Test (org.junit.jupiter.api.Test)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedOutputStream (java.io.BufferedOutputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 TreeSet (java.util.TreeSet)2 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1 SiddhiManager (io.siddhi.core.SiddhiManager)1 Event (io.siddhi.core.event.Event)1