View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
4    * 
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: TestResponderClientRFCOMM.java 2607 2008-12-17 23:51:33Z skarzhevskyy $
24   */
25  package net.sf.bluecove;
26  
27  import java.io.DataInputStream;
28  import java.io.IOException;
29  
30  import javax.microedition.io.Connection;
31  import javax.microedition.io.StreamConnection;
32  
33  import org.bluecove.tester.log.Logger;
34  
35  import junit.framework.Assert;
36  
37  /**
38   * 
39   */
40  public class TestResponderClientRFCOMM extends TestResponderClientConnection {
41  
42  	private ConnectionHolderStream c;
43  
44  	static void connectAndTest(TestResponderClient client, String serverURL) {
45  		client.connectAndTest(serverURL, "", Configuration.TEST_CASE_FIRST, Configuration.TEST_CASE_LAST,
46  				new TestResponderClientRFCOMM());
47  	}
48  
49  	public String protocolID() {
50  		return "RF";
51  	}
52  
53  	public ConnectionHolder connected(Connection conn) throws IOException {
54  		c = new ConnectionHolderStream((StreamConnection) conn);
55  		c.os = c.conn.openOutputStream();
56  
57  		return c;
58  	}
59  
60  	public void executeTest(int testType, TestStatus testStatus) throws IOException {
61  		c.os.write(Consts.SEND_TEST_START);
62  		c.os.write(testType);
63  		c.os.flush();
64  
65  		c.is = c.conn.openInputStream();
66  		c.active();
67  
68  		CommunicationTester.runTest(testType, false, c, testStatus);
69  		c.active();
70  
71  	}
72  
73  	public void replySuccess(String logPrefix, int testType, TestStatus testStatus) throws IOException {
74  		c.os.flush();
75  		Logger.debug(logPrefix + "read server status");
76  		int ok = c.is.read();
77  		if (ok != Consts.SEND_TEST_REPLY_OK_MESSAGE) {
78  			Assert.assertEquals("Server reply OK", Consts.SEND_TEST_REPLY_OK, ok);
79  		}
80  		int conformTestType = c.is.read();
81  		Assert.assertEquals("Test reply conform#", testType, conformTestType);
82  		if (ok == Consts.SEND_TEST_REPLY_OK_MESSAGE) {
83  			DataInputStream dis = new DataInputStream(c.is);
84  			String message = dis.readUTF();
85  			if (message == null) {
86  				Assert.fail("Server message expected");
87  			}
88  			Logger.info(logPrefix + "Server message\n[" + message + "]");
89  		}
90  	}
91  
92  	public void sendStopServerCmd(String serverURL) {
93  		// StreamConnection conn = null;
94  		// InputStream is = null;
95  		// OutputStream os = null;
96  		// try {
97  		// Logger.debug("Send stopServer command");
98  		// conn = (StreamConnection) Connector.open(serverURL);
99  		// os = conn.openOutputStream();
100 		//
101 		// os.write(Consts.TEST_SERVER_TERMINATE);
102 		// os.flush();
103 		// try {
104 		// Thread.sleep(1000);
105 		// } catch (Exception e) {
106 		// }
107 		// } catch (Throwable e) {
108 		// Logger.error("stopServer error", e);
109 		// }
110 		// IOUtils.closeQuietly(os);
111 		// IOUtils.closeQuietly(is);
112 		// IOUtils.closeQuietly(conn);
113 	}
114 }