Search in sources :

Example 11 with ExceptionListener

use of java.beans.ExceptionListener in project whole by wholeplatform.

the class BeansPersistenceKit method doReadModel.

protected IEntity doReadModel(IPersistenceProvider pp) throws Exception {
    // TODO cannot use getEncoding()
    XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(pp.getInputStream()) {

        @Override
        public void close() throws IOException {
        }
    }, null, new ExceptionListener() {

        public void exceptionThrown(Exception e) {
        // do nothing
        }
    }, ReflectionFactory.getPlatformClassLoader());
    IEntity model = (IEntity) decoder.readObject();
    decoder.close();
    return model;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) IEntity(org.whole.lang.model.IEntity) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) IOException(java.io.IOException) IOException(java.io.IOException)

Example 12 with ExceptionListener

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

the class ConfigurationTest method testEncodeDecode.

@Test
void testEncodeDecode() {
    // Create an exception listener to detect errors while encoding and
    // decoding
    final LinkedList<Exception> exceptions = new LinkedList<>();
    ExceptionListener listener = exceptions::addLast;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder enc = new XMLEncoder(out);
    enc.setExceptionListener(listener);
    Configuration configuration1 = new Configuration();
    configuration1.setInterval(500);
    configuration1.setSearchTimeout(1000);
    configuration1.setConnectTimeout(42);
    configuration1.setCountLimit(10);
    configuration1.setServers(new ArrayList<>(List.of(new LdapServer("http://server.com"))));
    WebHooks webHooks = new WebHooks();
    WebHook hook = new WebHook();
    hook.setContent("foo");
    hook.setURI("http://localhost:8080/source/api/v1/messages");
    webHooks.setFail(hook);
    configuration1.setWebHooks(webHooks);
    enc.writeObject(configuration1);
    enc.close();
    // verify that the write didn't fail
    if (!exceptions.isEmpty()) {
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    XMLDecoder dec = new XMLDecoder(in, null, listener);
    Configuration configuration2 = (Configuration) dec.readObject();
    assertNotNull(configuration2);
    assertEquals(configuration1.getXMLRepresentationAsString(), configuration2.getXMLRepresentationAsString());
    dec.close();
    // verify that the read didn't fail
    if (!exceptions.isEmpty()) {
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
}
Also used : WebHook(opengrok.auth.plugin.util.WebHook) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) LinkedList(java.util.LinkedList) LdapServer(opengrok.auth.plugin.ldap.LdapServer) XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) WebHooks(opengrok.auth.plugin.util.WebHooks) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 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.
 */
@Test
void testEncodeDecode() throws 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<>();
    ExceptionListener listener = exceptions::addLast;
    // Actually create the file and directory for much better test coverage.
    File tmpdir = Files.createTempDirectory("ignoredNames").toFile();
    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()) {
        // Can only chain one of the exceptions. Take the first one.
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
    // 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.
    IOUtils.removeRecursive(tmpdir.toPath());
}
Also used : XMLEncoder(java.beans.XMLEncoder) FileOutputStream(java.io.FileOutputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) Test(org.junit.jupiter.api.Test) CAnalyzerFactoryTest(org.opengrok.indexer.analysis.c.CAnalyzerFactoryTest)

Example 14 with ExceptionListener

use of java.beans.ExceptionListener in project omegat by omegat-org.

the class MappingRulesModel method fireException.

public void fireException(Exception e) {
    for (int i = listeners.size() - 1; i >= 0; i--) {
        ExceptionListener l = listeners.get(i);
        l.exceptionThrown(e);
    }
}
Also used : ExceptionListener(java.beans.ExceptionListener)

Example 15 with ExceptionListener

use of java.beans.ExceptionListener in project omegat by omegat-org.

the class SegmentationRulesModel method fireException.

public void fireException(Exception e) {
    for (int i = listeners.size() - 1; i >= 0; i--) {
        ExceptionListener l = listeners.get(i);
        l.exceptionThrown(e);
    }
}
Also used : ExceptionListener(java.beans.ExceptionListener)

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