View Javadoc

1   /*
2    * Copyright (c) 2004 by Cosylab
3    *
4    * The full license specifying the redistribution, modification, usage and other
5    * rights and obligations is included with the distribution of this project in
6    * the file "LICENSE-CAJ". If the license is not included visit Cosylab web site,
7    * <http://www.cosylab.com>.
8    *
9    * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE
10   * IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES
11   * _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION,
12   * OR REDISTRIBUTION OF THIS SOFTWARE.
13   */
14  
15  package com.cosylab.epics.caj.impl.handlers.test;
16  
17  import gov.aps.jca.CAException;
18  import gov.aps.jca.CAStatus;
19  import gov.aps.jca.Channel;
20  import gov.aps.jca.TimeoutException;
21  import gov.aps.jca.dbr.DBR;
22  import gov.aps.jca.event.GetEvent;
23  import gov.aps.jca.event.GetListener;
24  
25  import com.cosylab.epics.caj.CAJContext;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * Exception response test.
31   * @author <a href="mailto:matej.sekoranjaATcosylab.com">Matej Sekoranja</a>
32   * @version $id$
33   */
34  public class ExceptionResponseTest extends TestCase {
35  
36  	private class GetListenerImpl implements GetListener
37  	{
38  	    public DBR value = null;
39  	    public CAStatus status = null;
40  	    
41  		/**
42  		 * @see gov.aps.jca.event.GetListener#getCompleted(gov.aps.jca.event#GetEvent)
43  		 */
44  		public synchronized void getCompleted(GetEvent ev) {
45  		    status = ev.getStatus();
46  		    value = ev.getDBR();
47  		    this.notifyAll();
48  		}
49  	}
50  
51  	/**
52  	 * Context to be tested.
53  	 */
54  	private CAJContext context;
55  	
56      /**
57  	 * Channel to be tested.
58  	 */
59      private Channel channel;
60      
61  	/**
62  	 * Constructor for ExceptionResponseTest.
63  	 * @param methodName
64  	 */
65  	public ExceptionResponseTest(String methodName) {
66  		super(methodName);
67  	}
68  
69  	/**
70  	 * Exception test.
71  	 */
72  	public void testExceptionResponse() throws CAException, TimeoutException, InterruptedException {
73  		GetListenerImpl listener = new GetListenerImpl();
74  		channel.get(0xFFFF, listener);
75  		synchronized (listener)
76  		{
77  			context.flushIO();
78  			listener.wait(3000);
79  		}
80  		assertEquals(CAStatus.GETFAIL, listener.status);
81  	}
82  
83  	/*
84  	 * @see TestCase#setUp()
85  	 */
86  	protected void setUp() throws Exception {
87  		context = new CAJContext();
88  	    channel = context.createChannel("record1");
89  	    context.pendIO(5.0);
90  	    assertEquals(Channel.CONNECTED, channel.getConnectionState());
91  	}
92  
93  	/*
94  	 * @see TestCase#tearDown()
95  	 */
96  	protected void tearDown() throws Exception {
97  		if (!context.isDestroyed())
98  			context.destroy();
99  		context = null;
100 	}
101 
102 	/**
103 	 * Java main entry point.
104 	 * @param args	arguments.
105 	 */
106 	public static void main(String[] args) {
107 		junit.textui.TestRunner.run(ExceptionResponseTest.class);
108 	}
109 	
110 }