⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sampleadaptertest.java

📁 SUnRDIS网络应用平台,可以在该平台上开发RFID应用,系统.
💻 JAVA
字号:
/* * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. * */package com.mycompany.adapter.sample;import java.io.IOException;import java.util.Collection;import java.util.Iterator;import java.util.Properties;import java.net.URI;import junit.framework.*;import com.sun.autoid.ems.EMSEventListener;import com.sun.autoid.identity.*;import com.sun.autoid.event.IdentifierListEvent;import com.sun.autoid.event.Event;public class SampleAdapterTest extends TestCase {        // TODO provide basic properties    public static final String HOST = "localhost";    public static final String PORT = "9011";    public static final String READEREPC = "urn:epc:tag:gid-96:1.255.1";    public static final String LOGLEVEL_TEST = "FINEST";    public static final String FORMATTED_EPC = "urn:epc:";        protected SampleAdapter adapter = null;    protected Properties props = null;    private Identifier readerIdentifier = null;        public SampleAdapterTest(java.lang.String testName) {        super(testName);    }        public static Test suite() {        TestSuite suite = new TestSuite(SampleAdapterTest.class);        return suite;    }        /**     * Sets up the test fixture.     *     * Called before every test case method.     */    protected void setUp() {    }        /**     * Tears down the test fixture.     *     * Called after every test case method.     */    protected void tearDown() {        if ( adapter != null && adapter.isStarted()) {            adapter.shutdown();            pause(500);        }        props    = null;        adapter = null;    }        /**     * Set properties     */    protected void createProperties(String autoRead) {     // TODO put your read's properties below, for example:     props = new Properties();     props.put("LogLevel", LOGLEVEL_TEST);     props.put("autoread", autoRead);          props.put("hostname", HOST);     props.put("port", PORT);     String readerEpc = READEREPC;     props.put("readerepc", readerEpc);     setReaderIdentifier(readerEpc);    }        protected void setReaderIdentifier(String readerEpc) {        try {            readerIdentifier = IdentifierFactory.createIdentifier(new URI(readerEpc), 96);        } catch (Exception ex) {            fail("\t---> could not set ReaderIdentifier for: " + adapter.getName());            ex.printStackTrace();        }    }        /**     * Verify properties     */    protected void verifyProps() {     // TODO put you code here to verify the properties, for example:     System.out.println("Hostname: " + adapter.getHostname());     assertEquals(props.getProperty("hostname"),adapter.getHostname());    }        /**     * Create adapter     */    protected void createAdapter() {     try {         adapter = new SampleAdapter(props);     } catch (Exception ex) {         fail("Creation of  SampleAdapter failed in createAdapter()");         ex.printStackTrace();     }    }        /**     * more commands can be process here     */    protected void processAdditionalCmds() {        // TODO put you code here for additional commands    }        /**     * Below methods are used for testing, ususally you don't need to modify them     */    protected void listenerCallback(Event event){        if(event instanceof IdentifierListEvent) {            IdentifierListEvent eventList = (IdentifierListEvent)event;            verifyEPCTagList(eventList);        }    }        public void testAutoRead() {        System.out.println("\nTEST: testAutoRead");                try {            init("true");              if ( adapter.isConnected() ) {                          System.out.println("\t---> Reader is Connected: State = " + adapter.getStatus());                                boolean autoMode = Boolean.valueOf(props.getProperty("autoread")).booleanValue();                boolean autoread = adapter.isAutoRead();                assertEquals(autoread, autoMode);                if ( autoread ) {                    assertEquals("Connected", adapter.getStatus());                                        TestListener listener = new TestListener();                    adapter.addEMSEventListener(listener);                                        // Pause while we process auto read events from the reader.                    // Events will be caught by TestListener.                    pause(8000);                    adapter.removeEMSEventListener(listener);                } else {                    fail(adapter.getName() + ": testAutoRead - autoread mode should be true");                }            }// Reader is disconnected.            else {                fail("Reader DISCONNECTED: AutoRead testing NOT performed");            }        } catch (Exception ex) {            System.out.println("\t---> Failed to start adapter");            ex.printStackTrace();        }    }        public void testGetTaglist() {        System.out.println("\nTEST: testGetTaglist");                try {            init("false");            verifyProps();                        if ( adapter.isConnected() ) {                System.out.println("\t---> Reader is Connected: State = " + adapter.getStatus());                                boolean autoMode = Boolean.valueOf(props.getProperty("autoread")).booleanValue();                boolean autoread = adapter.isAutoRead();                assertEquals(autoread, autoMode);                                if ( !autoread ) {                    assertEquals("Connected", adapter.getStatus());                    Collection c = adapter.getTagList(100);                    verifyReaderTagList(c);                    long tagsRead = adapter.getTagsRead();                                    } else {                    fail(adapter.getName() + ": testGetTagList - autoread mode should be false");                }                // Reader is disconnected.            } else {                fail("Reader DISCONNECTED: GetTagList testing NOT performed");            }                    } catch (Exception ex) {            System.out.println("\t---> Failed to start adapter");            ex.printStackTrace();        }    }        public void testFirmwareVersion() {        System.out.println("\nTEST: testFirmwareVersion");                try {            init("false");            if ( adapter.isConnected() ) {                String firmwareVer = adapter.getFirmwareVersion();                assertNotNull(firmwareVer);            } else {                fail("Reader DISCONNECTED: testFirmwareVersion testing NOT performed");            }        } catch (Exception ex) {            System.out.println("\t---> Failed to start adapter");            ex.printStackTrace();        }    }        public void testSupportedProtocols() {        System.out.println("\nTEST: testSupportedProtocols");                try {            init("false");            if ( adapter.isConnected() ) {                String[] protocols = adapter.getSupportedProtocols();                assertTrue(protocols.length > 0);                assertNotNull(protocols[0]);            } else {                fail("Reader DISCONNECTED: testSupportedProtocols testing NOT performed");            }        } catch (Exception ex) {            System.out.println("\t---> Failed to start adapter");            ex.printStackTrace();        }    }        public void testAdditionalCmds() {        System.out.println("\nTEST: testAdditionalCmds");                try {            init("false");            if ( adapter.isConnected() ) {                boolean autoMode = Boolean.valueOf(props.getProperty("autoread")).booleanValue();                boolean autoread = adapter.isAutoRead();                assertEquals(autoread, autoMode);                                if ( !autoread ) {                    assertEquals("Connected", adapter.getStatus());                    processAdditionalCmds();                } else {                    fail(adapter.getName() + ": testAdditionalCmds - autoread mode should be false");                }            } else {                fail("Reader DISCONNECTED: AdditionalCmds testing NOT performed");            }        } catch (Exception ex) {            System.out.println("\t---> Failed to start adapter");            ex.printStackTrace();        }    }        /**     * Test updateReaderProperties.     */    public void testUpdateReaderProperties() {        System.out.println("\nTEST: testUpdateReaderProperties");        try {            init("false");        } catch (Exception ex) {            System.out.println("\t---> Failed to start adapter");            ex.printStackTrace();        }        Properties testProps = new Properties();        testProps.put("LogLevel", "INFO");        testProps.put("hostname", "1.1.2.52");        String readerEpc = "urn:epc:tag:gid-96:1.1.2";        testProps.put("readerepc", readerEpc);        setReaderIdentifier(readerEpc);        testProps.put("autoread", "true");                //Update adapter properties with new values        adapter.updateReaderProperties(testProps);                //Make sure adapter properties have been set to new values.        Properties newProps = adapter.getProperties();        assertEquals("INFO", newProps.getProperty("LogLevel"));        assertEquals("1.1.2.52", newProps.getProperty("hostname"));        assertEquals(readerEpc, newProps.getProperty("readerepc"));        assertEquals("true", newProps.getProperty("autoread"));                testProps = null;        newProps = null;    }        protected void init(String autoread) throws Exception {        createProperties(autoread);        createAdapter();        adapter.startup();        pause(2000);        assertTrue(adapter.isStarted());    }        protected void pause(long delay) {        try {            Thread.sleep(delay);        } catch (InterruptedException ignored) {}    }        protected void verifyReaderTagList(Collection c) {        Iterator i = c.iterator();        while (i.hasNext()) {            // Get an IdentifierListEvent at a time            IdentifierListEvent eventList = (IdentifierListEvent) i.next();            verifyEPCTagList(eventList);        }    }        protected void verifyEPCTagList(IdentifierListEvent eventList) {        try {            Properties adapterProps = adapter.getProperties();            System.out.println("Source= " + eventList.getSource());            System.out.println("readerepc=" + adapterProps.getProperty("readerepc"));            Identifier sourceIdentifier = IdentifierFactory.createIdentifier(new URI(eventList.getSource()), 96);            assertEquals(sourceIdentifier.getURI(), readerIdentifier.getURI());            Iterator j = eventList.getTagList().iterator();            while (j.hasNext()) {                // Get an EPC at a time                EPC epc = (EPC)j.next();                System.out.println("tag= " + epc.getURI());                assertTrue(epc.toString().startsWith(FORMATTED_EPC));            }            System.out.println();        } catch (Exception ex) {            ex.printStackTrace();            fail("\t---> could not set ReaderIdentifier for: " + adapter.getName());        }    }            class TestListener implements EMSEventListener {        public TestListener() {        }                public void postEvent(Event event) {            System.out.println("=====> TestListener got event: " + event.getClass());            listenerCallback(event);        }            }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -