View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gwes.uiproxy;
7   
8   import junit.framework.TestCase;
9   
10  import java.util.*;
11  
12  /**
13     Tests for the UIProxy
14   */
15  public class TestUIProxy extends TestCase {
16      protected class DummyBuffer implements Buffer {
17  	int _size;
18  	List _content;
19  	String _id;
20  	boolean _overflow;
21  
22  	public DummyBuffer(String id, int size) {
23  	    _id = id;
24  	    _size = size;
25  	    _content = new ArrayList(_size);
26  	    _overflow = false;
27  	}
28  
29  	public String getID() {
30  	    return _id;
31  	}
32  
33  	public void setProperties(Properties props) {
34  	    return;
35  	}
36  
37  	public void setProperty(String key, String value) {
38  	    return;
39  	}
40  
41  	public synchronized void handle(Object message) {
42  	    if (_content.size()==_size) _overflow = true;
43  	    else _content.add(message);
44  	}
45  
46  	public synchronized Object[] read() throws BufferException {
47  	    if (_overflow) throw new BufferOverflowException(this);
48  	    Object[] messages = _content.toArray();
49  	    _content.clear();
50  	    return messages;
51  	}
52      }
53  
54      protected BufferFactory _bfact = new BufferFactory() {
55  	    public Buffer createBuffer(String type, Properties props) {
56  		return new DummyBuffer("buffer", 5);
57  	    }
58  	};
59  
60      protected SubscriptionHandler _shandler = new SubscriptionHandler() {
61  	    public String subscribe(Consumer consumer, String subscription) {
62  		return "1";
63  	    }
64  
65  	    public void cancel(String subscriptionid) {
66  		return;
67  	    }
68  	};
69  
70      protected MessageCodec _codec = new MessageCodec() {
71  	    public String marshal(Object message) {
72  		return message.toString();
73  	    }
74  
75  	    public Object unmarshal(String spec) {
76  		return spec;
77  	    }
78  	};
79  
80      public void testSubscribe() throws SubscriptionFailed, BufferException, UnknownBufferException, UnknownClientException, CodecException {
81  	UIProxy proxy = new UIProxyImpl(_bfact, _codec, _shandler);
82  	String bufferid = proxy.subscribe("client", "subscription", "buffertype", new String[] { }, new String[] { });	
83  	proxy.poll("client", bufferid);
84      }
85  
86      public void testPoll() throws SubscriptionFailed, BufferException, UnknownBufferException, UnknownClientException, CodecException {
87  	UIProxyImpl proxy = new UIProxyImpl(_bfact, _codec, _shandler);
88  	String bufferid = proxy.subscribe("client", new String(), "buffertype", new String[] { }, new String[] { });
89  	Buffer buffer = proxy.getBuffer("client", bufferid);
90  	for (int i=0; i<5; i++) {
91  	    buffer.handle(""+i);
92  	}
93  	String[] messages = proxy.poll("client", bufferid);
94  	for (int i=0; i<5; i++) {
95  	    assertEquals("Messages corrupt after polling.", ""+i, messages[i]);
96  	}
97  	messages = proxy.poll("client", bufferid);
98  	assertTrue("Buffer should be empty.", messages.length==0);
99      }
100 
101     public void testUnknown() throws SubscriptionFailed {
102 	UIProxy proxy = new UIProxyImpl(_bfact, _codec, _shandler);
103 	String bufferid = proxy.subscribe("client", new String(), "buffertype", new String[] { }, new String[] { });
104 	try {
105 	    proxy.poll("clientx", "buffery");
106 	    fail("UnknownClientException expected.");
107 	} catch (UnknownClientException e) {
108 	    // fine
109 	} catch (UnknownBufferException e) {
110 	    fail("UnknownClientException expected.");
111 	} catch (BufferException e) {
112 	    fail("UnknownClientException expected.");
113 	} catch (CodecException e) {
114 	    fail("UnknownClientException expected.");
115 	}
116 	try {
117 	    proxy.poll("client", "buffery");
118 	    fail("UnknownBufferException expected.");
119 	} catch (UnknownBufferException e) {
120 	    // fine
121 	} catch (UnknownClientException e) {
122 	    fail("UnknownBufferException expected.");
123 	} catch (BufferException e) {
124 	    fail("UnknownBufferException expected.");
125 	} catch (CodecException e) {
126 	    fail("UnknownBufferException expected.");
127 	}
128     }
129 
130     public void testOverflow() throws SubscriptionFailed {
131 	UIProxyImpl proxy = new UIProxyImpl(_bfact, _codec, _shandler);
132 	String bufferid = proxy.subscribe("client", new String(), "buffertype", new String[] { }, new String[] { });
133 	Buffer buffer = proxy.getBuffer("client", bufferid);
134 	for (int i=0; i<6; i++) {
135 	    buffer.handle(""+i);
136 	}
137 	try {
138 	    String[] messages = proxy.poll("client", bufferid);
139 	    fail("BufferOverflowException expected.");
140 	} catch (BufferException e) {
141 	    // fine
142 	} catch (UnknownClientException e) {
143 	    fail("BufferOverflowException expected.");
144 	} catch (UnknownBufferException e) {
145 	    fail("BufferOverflowException expected.");
146 	} catch (CodecException e) {
147 	    fail("BufferOverflowException expected.");
148 	} 
149 	try {
150 	    String[] messages = proxy.poll("client", bufferid);
151 	    fail("UnknownBufferException expected.");
152 	} catch (UnknownBufferException e) {
153 	    // fine
154 	} catch (UnknownClientException e) {
155 	    fail("UnknownBufferException expected.");
156 	} catch (BufferException e) {
157 	    fail("UnknownBufferException expected.");
158 	} catch (CodecException e) {
159 	    fail("UnknownBufferException expected.");
160 	}
161     }
162 
163     public synchronized void testTimeout() throws SubscriptionFailed, InterruptedException {
164 	UIProxyImpl proxy = new UIProxyImpl(_bfact, _codec, _shandler);
165 	proxy.setLeaseTime(1000);
166 	String bufferid = proxy.subscribe("client", new String(), "buffertype", new String[] { }, new String[] { });
167 	wait(1500);
168 	try {
169 	    proxy.poll("client", bufferid);
170 	    fail("UnknownClientException expected.");
171 	} catch (UnknownClientException e) {
172 	    // fine
173 	} catch (UnknownBufferException e) {
174 	    fail("UnknownClientException expected.");
175 	} catch (BufferException e) {
176 	    fail("UnknownClientException expected.");
177 	} catch (CodecException e) {
178 	    fail("UnknownClientException expected.");
179 	}	
180     }
181 }