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.

2.9 KB
Raw Blame
package opensuite.ui;

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

import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.table.DefaultTableModel;
import java.util.List;

/**
 * Tests that a history table tells its viewer of a selection only when the
 * selected item changes: not for every row a running scan adds above it.
 */
public final class RowSelectionTest {

    public void run() {
        Harness.suite("Row selection");

        Harness.test("rows added to a newest-first table do not redraw the selected item", () -> {
            DefaultTableModel model = new DefaultTableModel(new Object[] {"#"}, 0) {
                @Override public Class<?> getColumnClass(int c) { return Integer.class; }
            };
            for (int i = 1; i <= 10; i++) model.addRow(new Object[] {i});
            JTable table = new JTable(model);
            table.setAutoCreateRowSorter(true);
            table.getRowSorter().setSortKeys(List.of(new RowSorter.SortKey(0, SortOrder.DESCENDING)));
            int[] shown = {0}, plain = {0};
            Object[] item = {null};
            RowSelection.watch(table, r -> model.getValueAt(r, 0), () -> {
                shown[0]++;
                int row = table.getSelectedRow();
                item[0] = row < 0 ? null : model.getValueAt(table.convertRowIndexToModel(row), 0);
            });
            // What a bare listener hears, to show the table does clear and restore its selection.
            table.getSelectionModel().addListSelectionListener(e -> plain[0]++);

            table.setRowSelectionInterval(0, 0);          // the newest row, number 10
            Expect.equal(shown[0], 1, "selecting an item shows it");
            Expect.equal(item[0], 10, "the item selected");

            for (int i = 11; i <= 510; i++) model.addRow(new Object[] {i});
            Expect.isTrue(plain[0] > 500, "the table changed its selection for every row added: " + plain[0]);
            Expect.equal(shown[0], 1, "and the viewer was not told once");
            Expect.equal(table.getSelectedRow(), 500, "the selection moved down with its item");
            Expect.equal(model.getValueAt(table.convertRowIndexToModel(table.getSelectedRow()), 0), 10, "and is still on it");

            table.setRowSelectionInterval(0, 0);          // now number 510
            Expect.equal(shown[0], 2, "another item is shown");
            Expect.equal(item[0], 510, "the new one");

            table.clearSelection();
            Expect.equal(shown[0], 3, "nothing selected is told too");
            Expect.equal(item[0], null, "so the viewer can go blank");

            table.setRowSelectionInterval(0, 0);
            Expect.equal(shown[0], 4, "and the same item selected again is shown again");

            model.setRowCount(0);
            Expect.equal(shown[0], 5, "a cleared table leaves nothing selected");
        });
    }
}