ReaderJunits

From RifidiWiki

Jump to: navigation, search

Purpose of Junits

Junits are automated tests that programatically test functionality. The advantage of them is that you can run the tests to make sure that as new functionality is added, old functionality doesn't break.

Creating a new Junit in eclipse

Currently our testing packages are stored internally to Pramari because they have dependencies to non-free libraries provided by RFID reader companies. We are currently working on opening up our tests. However, you can ignore the first step and add the test to a testing package contained in your reader's source code.

  1. Checkout org.rifidi.tests and org.rifidi.internal.dependencies from the internal repository
  2. Create a new package for your tests
  3. Add a new Junit in eclipse
    1. New->Other->Junit Test Case
    2. Fill in the information in the wizard. You need a Junit 4 test case with the SetUpBeforeClass() and TearDownAfterClass() methods generated.Newjunit.png


Designing a Junit to Test a Virtual Reader

The test that you are designing depends on which version of emulator you are running. To figure this out, open up the plugin.xml file for org.rifidi.emulator, and look at the plugin version

Junit Guidelines

  • When creating a Junit, it is important to add comments to almost everything you do, because your test might not fail for months after you've written it, and it will be difficult to go in and figure out why it failed if there are no comments
  • It is important to think through exactly what you want to test and make sure that you are testing it well. For example, The following test could be designed better, because it will not fail if more than one tag comes back from the alien reader for some reason after the getTaglist command is sent
  • It is also important that one test not depend on the state of previously run test. All test methods should run independently and should only assume that SetUpBeforeClass() has finished without error. For example, if I added another test method to run after the getTaglistTest method, it should not assume that a tag is on the reader. Also this test method should have removed the tag after it was done.

Junits for org.rifidi.emulator 1.x

/**
 * 
 */
package org.rifidi.tests.reader.alien;

import gnu.cajo.utils.extra.TransparentItemProxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.rifidi.emulator.reader.module.GeneralReaderPropertyHolder;
import org.rifidi.emulator.rmi.server.ReaderModuleManagerInterface;
import org.rifidi.emulator.rmi.server.RifidiManager;
import org.rifidi.emulator.rmi.server.RifidiManagerInterface;
import org.rifidi.emulator.tags.impl.C1G2Tag;
import org.rifidi.emulator.tags.impl.RifidiTag;
import org.rifidi.utilities.formatting.ByteAndHexConvertingUtility;

/**
 * 
 * This Junit is designed to demonstrate how to write Junits for Rifidi Virtual
 * Readers
 * 
 * @author Kyle Neumeier - kyle@pramari.com
 * 
 */
public class AlienExample {

	/**
	 * The logger for this class. Allows you to keep track of debug statements
	 * instead of using
	 */
	private static final Log logger = LogFactory.getLog(AlienExample.class);

	public static final String READER_IP_ADDRESS = "127.0.0.1";
	public static final int READER_PORT = 20000;
	public static final String READER_NAME = "virtualAlienReader";

	public static final String RMI_SERVER_IP_ADDRESS = "127.0.0.1";
	public static final int RMI_SERVER_PORT = 1099;

	/**
	 * The RMI Client Interface for the Alien Reader. It allows you to turn the
	 * reader on and off as well as add and remove tags to the antenna
	 */
	private static ReaderModuleManagerInterface alienManager;

	/**
	 * The Interface for the RMI server
	 */
	private static RifidiManagerInterface RMIManager;

	/**
	 * The client connection to the Alien reader
	 */
	private static Socket connection = null;
	private static PrintWriter out = null;
	private static BufferedReader in = null;

	/**
	 * This method is run before any of the tests in this file
	 * 
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		// set up RMI Server, Client, and Readers
		RifidiManager.startManager(RMI_SERVER_IP_ADDRESS, RMI_SERVER_PORT);
		RMIManager = RifidiManager.getManager();

		/*
		 * The General Reader Property Holder contains information needed to
		 * instantiate the virutal reader. Note that the GRPH has a property map
		 * in it that is different for every reader. To find out which
		 * properties you should have in your reader, look at the required
		 * properties in the emulator.xml file in the reader project.
		 */
		GeneralReaderPropertyHolder alienGRPH = new GeneralReaderPropertyHolder();
		alienGRPH.setNumAntennas(2);
		alienGRPH.setNumGPIs(2);
		alienGRPH.setNumGPOs(2);
		alienGRPH.setReaderName(READER_NAME);
		alienGRPH.setReaderClassName("org.rifidi.emulator.reader.alien.module.AlienReaderModule");
		alienGRPH.setProperty("inet_address", READER_IP_ADDRESS + ":"+ READER_PORT);
		alienGRPH.setProperty("heartbeat_address", READER_IP_ADDRESS + ":54321");

		// create the reader
		RMIManager.createReader(alienGRPH);

		// get the reader manager from the RMI server
		alienManager = (ReaderModuleManagerInterface) TransparentItemProxy
				.getItem("//" + RMI_SERVER_IP_ADDRESS + ":" + RMI_SERVER_PORT + "/" + READER_NAME,
				         new Class[] { ReaderModuleManagerInterface.class });

		// turn on the reader
		alienManager.turnReaderOn();

		// wait for reader to turn on
		Thread.sleep(500);

		// create a new client connection
		connection = new Socket(READER_IP_ADDRESS, READER_PORT);

		in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		out = new PrintWriter(connection.getOutputStream());

		try {
			// read welcome message
			System.out.println(readFromReader(in));
			// send user name
			out.write("alien\n");
			out.flush();
			// read resoponse
			System.out.println(readFromReader(in));
			Thread.sleep(500);
			// send password
			out.write("password\n");
			out.flush();
			System.out.println(readFromReader(in));
		} catch (Exception e) {
			Assert.fail(e.getMessage());
		}
	}

	/**
	 * This class is run after all the tests in this class have been run. It
	 * should shut down the reader and tear down the RMI server
	 * 
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		out.write("q");
		connection.close();
		alienManager.turnReaderOff();
		RMIManager.removeReader(READER_NAME);
		RMIManager.cleanup();
	}

	/**
	 * This is a Junit Test. Use the "@Test" annotation above the class so that
	 * it will be run as a Junit.
	 * 
	 * This Junit adds a tag to the reader, then sends a get taglist command to
	 * the reader. It then compares the response with the sent tag information
	 * and will fail if the information is not the same.
	 * 
	 * @throws Exception
	 */
	@Test
	public void getTaglistTest() throws Exception {
		// make sure information comes back in the right format
		String command1 = "\1set TagListFormat = TEXT\n";
		out.write(command1);
		out.flush();
		String returnVal1 = readFromReader(in);
		Assert.assertTrue(returnVal1.toLowerCase().contains("TagListFormat = TEXT".toLowerCase()));

		// create a new tag
		byte[] epcID = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C };
		byte[] pass = { 0x00, 0x00, 0x00, 0x00 };
		C1G2Tag t = new C1G2Tag(epcID, pass, pass.clone());
		RifidiTag tag = new RifidiTag(t);
		ArrayList<RifidiTag> tags = new ArrayList<RifidiTag>();
		tags.add(tag);

		// add tag to antenna 0 on the reader
		alienManager.addTags(0, tags);

		Thread.sleep(500);

		// send a get taglist command
		out.write("\1getTaglist\n");
		out.flush();
		String taglist = readFromReader(in);
		logger.debug(taglist);

		// process command and compare byte ids.
		String[] info = taglist.split(",");
		String id = info[0].split(":")[1];
		byte[] byteID = ByteAndHexConvertingUtility.fromHexString(id.trim());
		Assert.assertArrayEquals(epcID, byteID);

	}

	public static String readFromReader(BufferedReader inBuf)
			throws IOException {
		StringBuffer buf = new StringBuffer();
		int ch = inBuf.read();
		while ((char) ch != '\0') {
			buf.append((char) ch);
			ch = inBuf.read();
		}
		return buf.toString();
	}

}

Junits for org.rifidi.emulator 2.x

Creating Junits for emulator 2.x is similar to that. The main difference is how tags are created. An example is coming soon.

Running the Junit Test Case

  1. Right click the Junit in the tree on the left inside of eclipse
  2. Select RunAs -> Junit Plugin Test
Personal tools