1

I want to test my program which sends DatagramSocket data between a sender and a receiver class. How should I go about testing that data is being sent between the classes?

Here is my Sender code...

public class CheckIn {

    private String patientName;
    private String pTime;
    private int portnumber;

    public CheckIn(String pname, String time, int port){
        patientName = pname;
        pTime = time;
        portnumber = port;
        process();
    }

    public void process() {
        try {
            DatagramSocket is = new DatagramSocket(5800,InetAddress.getLocalHost());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            String n = "Your "+"\n" + pTime + "\n" + patientName + " has arrived";
            oos.writeObject(n);
            byte[] buffer = baos.toByteArray();
            baos.close();
            oos.close();
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length,InetAddress.getLocalHost(), portnumber);
            is.send(packet);

            System.out.println( "patient name " + patientName );
            System.out.println( "Check In");
        }
        catch (Exception e){
            System.out.println("ERROR Cashier: " + e.getMessage() );
            e.printStackTrace();
        }
    }
}

And this is my Receiver class...

class Reciever2 extends Thread {
    public Reciever2(){
        this.start();   //starts the thread
    }

    public void run(){
        try {
            DatagramSocket socket = new DatagramSocket(5700,InetAddress.getLocalHost());
            System.out.println( "Recieving" );   //prints when receiving a message

            while (true){
                byte[] data = new byte[1024];    //array of bytes
                DatagramPacket packet = new DatagramPacket(data,data.length);  
                socket.receive(packet);      //receives the packet
                ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData()); 
                ObjectInputStream ois =  new ObjectInputStream(bais);
                String s = (String) ois.readObject();   //reading the data
                bais.close();   //closes input stream
                ois.close();   //closes input stream

                System.out.println(s);
                theText.setText( s);   //displays message
            }
        }
        catch (Exception e){
            System.out.println("ERROR Cook: " + e.getMessage() );
            }
        }
    }
wattostudios
  • 8,666
  • 13
  • 43
  • 57
isaiahbn
  • 61
  • 1
  • 2
  • 7
  • Wouldn't the easiest be just to write a receiver? – Erik Apr 16 '12 at 13:47
  • @Erik - yes, that's what I would do, adding the code into the CheckIn class. Then replace the magic '5800' port number with a ctor parameter and a add a port to receive from. Then make two of them with complementary ports, then pass them to a couple pairs of threads, then fire in one datagram and watch as it circulates round. – Martin James Apr 16 '12 at 13:54
  • Hmm. only need two threads - each waits for datagrams and, on receipt, sends them out again. – Martin James Apr 16 '12 at 13:57

2 Answers2

1

You would probably need to write a receiver class for testing against, or have access to the destination server so you can observe the change. The UDP protocol is pretty much a one-way protocol.

This guide might be useful to understand more about DatagramSockets - it also has a section on writing a sender and receiver. http://docs.oracle.com/javase/tutorial/networking/datagrams/index.html

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • Well, isn't the reciver class working? If it is, just print out the values in the packages as you recive them – John Snow Apr 16 '12 at 13:58
  • Well, start the Receiver program, then start your Sender program - you should be able to see whether they are talking to each other, especially if you're using an IDE like Eclipse and can set a debugger breakpoint in the Receiver. – wattostudios Apr 16 '12 at 13:59
  • yes the reciever class works.. just wanted like a jUnit orJmock test to show it works but the reciever class recieves it an displays on a GUI – isaiahbn Apr 16 '12 at 14:00
0

You could use a technique called Mocking. See What's the best mock framework for Java? for more information.

Community
  • 1
  • 1
Jeff
  • 1,871
  • 1
  • 17
  • 28