Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better unit tests #37

Open
tresf opened this issue Apr 14, 2019 · 4 comments
Open

Better unit tests #37

tresf opened this issue Apr 14, 2019 · 4 comments
Assignees
Labels
clean-up component:maven Changes to the java build process good first issue Good for newcomers help wanted Extra attention is needed language:java Needs changes to java sources.
Milestone

Comments

@tresf
Copy link

tresf commented Apr 14, 2019

A placeholder to port over some of @vogt31337's unit tests from the 2.8.1-experimental branch.

    public void testSerialPortListing() {
        String[] out = SerialPortList.getPortNames();
        System.out.println(Arrays.toString(out));

        // if we reached this point, loading the dynamic lib worked.
        assertTrue(true);
    }

    private String findASerialPort() {
        String[] out = SerialPortList.getPortNames();

        if (out.length > 0) {
            return out[0];
        } else {
            return null;
        }
    }

    public void testSerialPortRead() {
        String port = findASerialPort(); // could also be "COM1" under windows, or /dev/ttyS0 under linux

        if (port != null) {
            SerialPort serialPort = new SerialPort(port);
            try {
                serialPort.openPort();//Open serial port
                serialPort.setParams(9600, 8, 1, 0);//Set params.
                byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
                serialPort.closePort();//Close serial port
            } catch (SerialPortException ex) {
                System.out.println(ex);
            }
        } else {
            Assert.fail("No comport found. Maybe running on a laptop? Or cables aren't attached?");
        }
        assertTrue(true);

    }

    private String findASerialPort() {
        String[] out = SerialPortList.getPortNames();

        if (out.length > 0) {
            return out[0];
        } else {
            return null;
        }
    }

    public void testSerialPortWrite() {
        String port = findASerialPort(); // could also be "COM1" under windows, or /dev/ttyS0 under linux

        if (port != null) {
            SerialPort serialPort = new SerialPort(port);
            try {
                serialPort.openPort();//Open serial port
                serialPort.setParams(SerialPort.BAUDRATE_9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
                serialPort.writeBytes("This is a test string".getBytes());//Write data to port
                serialPort.closePort();//Close serial port
            } catch (SerialPortException ex) {
                System.out.println(ex);
            }
        } else {
            Assert.fail("No comport found. Maybe running on a laptop? Or cables aren't attached?");
        }
        assertTrue(true);
    }
@tresf tresf added clean-up language:java Needs changes to java sources. component:maven Changes to the java build process help wanted Extra attention is needed good first issue Good for newcomers labels Apr 14, 2019
@bmarwell
Copy link

To get a stable unit test, we might want to create a virtual serial port using a @Before directive.

The setup might differ a little per Operating System. Another good option would be to use a junit @Rule. For Linux, this woulduse socat, other methods for other OSes.

@bmarwell bmarwell self-assigned this Apr 14, 2019
@bmarwell bmarwell added this to the Release 3.0.0 milestone Apr 14, 2019
@tresf
Copy link
Author

tresf commented Apr 16, 2019

The setup might differ a little per Operating System.

I vote we skip virtual com ports for Windows. They generally require signed drivers and get into a bit of a mess. If a CI service such as AppVeyor has support for them, this would allow us to do CI testing, but local testing on a PC would still need to make some assumptions.

@bmarwell
Copy link

The setup might differ a little per Operating System.

I vote we skip virtual com ports for Windows. They generally require signed drivers and get into a bit of a mess. If a CI service such as AppVeyor has support for them, this would allow us to do CI testing, but local testing on a PC would still need to make some assumptions.

That's okay for me! :)
I will start with linux for now.

tresf added a commit that referenced this issue Nov 8, 2023
@tresf
Copy link
Author

tresf commented Nov 10, 2023

Partially implemented via #47, #151.

#151 was particularly interesting to figure out, mostly due to some test running against old binaries. This should all be sorted now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clean-up component:maven Changes to the java build process good first issue Good for newcomers help wanted Extra attention is needed language:java Needs changes to java sources.
Projects
None yet
Development

No branches or pull requests

2 participants