Search in sources :

Example 36 with XMLEncoder

use of java.beans.XMLEncoder 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<>();
    ExceptionListener listener = exceptions::addLast;
    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()) {
        // Can only chain one of the exceptions. Take the first one.
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
    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()) {
        // Can only chain one of the exceptions. Take the first one.
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PatternSyntaxException(java.util.regex.PatternSyntaxException) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 37 with XMLEncoder

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

the class ProjectTest method testEncodeDecode.

/**
 * Test that a {@code Project} instance can be encoded and decoded without
 * errors. Bug #3077.
 */
@Test
public 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);
    Project p1 = new Project("foo");
    enc.writeObject(p1);
    enc.close();
    // verify that the write didn't 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());
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    XMLDecoder dec = new XMLDecoder(in, null, listener);
    Project p2 = (Project) dec.readObject();
    assertNotNull(p2);
    dec.close();
    // verify that the read didn't 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());
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 38 with XMLEncoder

use of java.beans.XMLEncoder in project kie-wb-common by kiegroup.

the class XMLEncoderDiagramMetadataMarshaller method marshall.

@Override
public String marshall(final Metadata metadata) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(os);
    encoder.writeObject(metadata);
    encoder.close();
    String raw = os.toString(CHARSET);
    return raw;
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 39 with XMLEncoder

use of java.beans.XMLEncoder in project smscgateway by RestComm.

the class SmppSimulatorForm method initialize.

private void initialize() {
    frmSmppSimulator = new JFrame();
    frmSmppSimulator.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent arg0) {
            if (frmSmppSimulator.getDefaultCloseOperation() == JDialog.DO_NOTHING_ON_CLOSE) {
                JOptionPane.showMessageDialog(getJFrame(), "Before exiting you must close a test window form");
            } else {
            // if (hostImpl != null) {
            // hostImpl.quit();
            // }
            }
        }
    });
    frmSmppSimulator.setResizable(false);
    frmSmppSimulator.setTitle("SMPP Simulator");
    frmSmppSimulator.setBounds(100, 100, 510, 299);
    frmSmppSimulator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    frmSmppSimulator.getContentPane().add(panel, BorderLayout.CENTER);
    panel.setLayout(null);
    btnConfigure = new JButton("Configure");
    btnConfigure.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            SmppParametersForm frame = new SmppParametersForm(getJFrame());
            frame.setData(par);
            frame.setVisible(true);
            SmppSimulatorParameters newPar = frame.getData();
            if (newPar != null) {
                par = newPar;
                try {
                    BufferedOutputStream bis = new BufferedOutputStream(new FileOutputStream("SmppSimulatorParameters.xml"));
                    XMLEncoder d = new XMLEncoder(bis);
                    d.writeObject(newPar);
                    d.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Failed when saving the parameter file SmppSimulatorParameters.xml: " + ee.getMessage());
                }
            }
        }
    });
    btnConfigure.setBounds(10, 25, 183, 23);
    panel.add(btnConfigure);
    btnRun = new JButton("Run test");
    btnRun.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            runTest();
        }
    });
    btnRun.setBounds(10, 59, 183, 23);
    panel.add(btnRun);
}
Also used : JPanel(javax.swing.JPanel) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) WindowAdapter(java.awt.event.WindowAdapter) XMLEncoder(java.beans.XMLEncoder) ActionListener(java.awt.event.ActionListener) JFrame(javax.swing.JFrame) WindowEvent(java.awt.event.WindowEvent) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 40 with XMLEncoder

use of java.beans.XMLEncoder in project cache2k by cache2k.

the class CacheTypeTest method copyObjectViaXmlEncoder.

private <T> T copyObjectViaXmlEncoder(T o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLEncoder enc = new XMLEncoder(bos);
    enc.writeObject(o);
    enc.close();
    ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
    XMLDecoder dec = new XMLDecoder(bin);
    Object o2 = dec.readObject();
    dec.close();
    assertTrue("no reference identity", o2 != o);
    assertEquals("same class", o.getClass(), o2.getClass());
    return (T) o2;
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

XMLEncoder (java.beans.XMLEncoder)109 ByteArrayOutputStream (java.io.ByteArrayOutputStream)48 BufferedOutputStream (java.io.BufferedOutputStream)37 FileOutputStream (java.io.FileOutputStream)36 IOException (java.io.IOException)30 File (java.io.File)24 XMLDecoder (java.beans.XMLDecoder)22 ByteArrayInputStream (java.io.ByteArrayInputStream)22 ExceptionListener (java.beans.ExceptionListener)16 LinkedList (java.util.LinkedList)16 Test (org.junit.jupiter.api.Test)12 FileInputStream (java.io.FileInputStream)9 Test (org.junit.Test)8 Encoder (java.beans.Encoder)6 BufferedInputStream (java.io.BufferedInputStream)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 DefaultPersistenceDelegate (java.beans.DefaultPersistenceDelegate)5 PersistenceDelegate (java.beans.PersistenceDelegate)5 OutputStream (java.io.OutputStream)5 Expression (java.beans.Expression)4