Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

4.1 KB
Raw Blame
package opensuite.ui;

import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.Map;

/** A form's state is taken from one form and applied to another of the same shape. */
public final class FormStateTest {

    /** A form whose second field only exists once the combo is on its second item. */
    private static final class Form extends JPanel {
        final JCheckBox box = new JCheckBox("box");
        final JRadioButton one = new JRadioButton("one", true), two = new JRadioButton("two");
        final JTextField text = new JTextField("start");
        final JComboBox<String> kind = new JComboBox<>(new String[] { "plain", "extra" });
        final JPanel dynamic = new JPanel();
        final DefaultTableModel rows = new DefaultTableModel(new Object[] { "On", "Name" }, 0);
        final JTextArea note = new JTextArea("a fixed note");
        int clicks;

        Form() {
            ButtonGroup g = new ButtonGroup();
            g.add(one);
            g.add(two);
            two.addActionListener(e -> clicks++);
            note.setEditable(false);
            kind.addActionListener(e -> {
                dynamic.removeAll();
                if (kind.getSelectedIndex() == 1) dynamic.add(new JTextField("made by the combo"));
            });
            for (Component c : new Component[] { box, one, two, text, kind, dynamic, new JTable(rows), note }) add(c);
        }
    }

    public void run() {
        Harness.suite("form state");

        Harness.test("every editable control is copied, and controls a choice creates are filled too", () -> {
            Form from = new Form();
            from.box.setSelected(true);
            from.two.setSelected(true);
            from.text.setText("changed");
            from.kind.setSelectedIndex(1);
            ((JTextField) from.dynamic.getComponent(0)).setText("typed later");
            from.rows.addRow(new Object[] { Boolean.TRUE, "first" });
            from.rows.addRow(new Object[] { Boolean.FALSE, "second" });

            Form to = new Form();
            FormState.apply(to, FormState.capture(from));

            Expect.isTrue(to.box.isSelected(), "the tick box");
            Expect.isTrue(to.two.isSelected(), "the radio");
            Expect.isTrue(to.clicks == 1, "the radio's listener heard it once, got " + to.clicks);
            Expect.isTrue("changed".equals(to.text.getText()), "the text field");
            Expect.isTrue(to.kind.getSelectedIndex() == 1, "the combo");
            Expect.isTrue("typed later".equals(((JTextField) to.dynamic.getComponent(0)).getText()), "the field the combo made");
            Expect.isTrue(to.rows.getRowCount() == 2, "the table rows");
            Expect.isTrue(Boolean.TRUE.equals(to.rows.getValueAt(0, 0)) && "second".equals(to.rows.getValueAt(1, 1)), "cell types and values");
            Expect.isTrue("a fixed note".equals(to.note.getText()), "a note that cannot be edited is left alone");
        });

        Harness.test("the state survives a trip through a properties file", () -> {
            Form from = new Form();
            from.text.setText("a # b = c ! d");
            from.rows.addRow(new Object[] { Boolean.TRUE, "a=b" });
            Map<String, String> state = FormState.capture(from);
            java.util.Properties p = FormState.toProperties(state);
            java.io.StringWriter w = new java.io.StringWriter();
            try { p.store(w, null); } catch (java.io.IOException e) { throw new RuntimeException(e); }
            java.util.Properties back = new java.util.Properties();
            try { back.load(new java.io.StringReader(w.toString())); } catch (java.io.IOException e) { throw new RuntimeException(e); }
            Form to = new Form();
            FormState.apply(to, FormState.fromProperties(back));
            Expect.isTrue("a # b = c ! d".equals(to.text.getText()), "text with the characters a properties file treats specially");
            Expect.isTrue(to.rows.getRowCount() == 1 && "a=b".equals(to.rows.getValueAt(0, 1)), "the table row");
        });
    }
}