My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2008-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package net.sf.bluecove;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;

import junit.framework.Assert;
import junit.framework.TestCase;

import com.intel.bluetooth.BlueCoveConfigProperties;
import com.intel.bluetooth.BlueCoveImpl;
import com.intel.bluetooth.EmulatorTestsHelper;

/**
*
*/
public abstract class BaseEmulatorTestCase extends TestCase {

// Use this to debug tests
protected boolean debug = false;

protected boolean debugOnInEclipse = true;

protected Thread testServerThread;

protected ThreadGroup testServerThreadGroup;

protected LongRunningTestMonitor monitor;

/**
* Default 30 seconds
*
* @return the length of time for test in milliseconds.
*/
protected int gracePeriod() {
return 30 * 1000;
}

@Override
protected void setUp() throws Exception {
super.setUp();
boolean eclipse = isEclipse();
if (eclipse && debugOnInEclipse) {
debug = true;
}
if (!eclipse && (gracePeriod() > 0)) {
monitor = new LongRunningTestMonitor(gracePeriod(), this.getClass().getName() + "." + this.getName());
monitor.start();
}
// Use this to debug tests
if (debug) {
BlueCoveImpl.setConfigProperty(BlueCoveConfigProperties.PROPERTY_DEBUG, "true");
BlueCoveImpl.setConfigProperty(BlueCoveConfigProperties.PROPERTY_DEBUG_STDOUT, "false");
} else {
BlueCoveImpl.setConfigProperty(BlueCoveConfigProperties.PROPERTY_DEBUG_LOG4J, "false");
}
EmulatorTestsHelper.startInProcessServer();
Runnable r = createTestServer();
if (r != null) {
testServerThread = EmulatorTestsHelper.runNewEmulatorStack(r);
testServerThread.setName(this.getClass().getSimpleName() + "-ServerThread");
testServerThreadGroup = testServerThread.getThreadGroup();
if (monitor != null) {
monitor.setSecondaryTestThreadGroup(testServerThreadGroup);
}
}
EmulatorTestsHelper.useThreadLocalEmulator();
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
if (testServerThreadGroup != null) {
testServerThreadGroup.interrupt();
int count = testServerThreadGroup.activeCount();
Thread[] active = new Thread[count];
count = testServerThreadGroup.enumerate(active);
for (int i = 0; i < count; i++) {
active[i].join(5000);
}
}
EmulatorTestsHelper.stopInProcessServer();
if (monitor != null) {
monitor.finish();
monitor = null;
}
}

/**
* Override if test needs a second Thread with server
*
* @return
*/
protected Runnable createTestServer() {
return null;
}

static public void assertEquals(String message, byte[] expected, byte[] actual) {
Assert.assertNotNull(message + " value is null", actual);
Assert.assertEquals(message + " length", expected.length, actual.length);
for (int i = 0; i < expected.length; i++) {
Assert.assertEquals(message + " byte [" + i + "]", expected[i], actual[i]);
}
}

static public void assertEquals(String message, int length, byte[] expected, byte[] actual) {
Assert.assertTrue(message + " expected.length", expected.length >= length);
Assert.assertTrue(message + " actual.length", actual.length >= length);
for (int i = 0; i < length; i++) {
Assert.assertEquals(message + " byte [" + i + "]", expected[i], actual[i]);
}
}

protected String selectService(String uuid) throws BluetoothStateException {
return selectService(new UUID(uuid, false));
}

protected String selectService(UUID uuid) throws BluetoothStateException {
DiscoveryAgent discoveryAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();

// Find service
String serverURL = null;
int retry = 0;
while ((serverURL == null) && ((retry++) < 3)) {
serverURL = discoveryAgent.selectService(uuid, ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
}

Assert.assertNotNull("service not found", serverURL);

return serverURL;
}

boolean isEclipse() {
StackTraceElement[] ste = new Throwable().getStackTrace();
for (StackTraceElement s : ste) {
if (s.getClassName().startsWith("org.eclipse.jdt")) {
return true;
}
}
return false;
}

}

Change log

r2996 by skarzhevskyy on Jun 23, 2009   Diff
stack trace for secondary Threads
Go to: 
Project members, sign in to write a code review

Older revisions

r2917 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
r2471 by skarzhevskyy on Nov 30, 2008   Diff
Change license to Apache License,
Version 2.0, Update headers
r2430 by skarzhevskyy on Oct 16, 2008   Diff
gracePeriod 30 seconds
All revisions of this file

File info

Size: 5746 bytes, 169 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting