<commit_before>package at.jku.pervasive.ecg;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Semaphore;

import javax.bluetooth.ServiceRecord;

import junit.framework.TestCase;

public class HeartManDiscoveryTest extends TestCase {

  private HeartManDiscovery heartManDiscovery;
  private HeartManSimulator heartManSimulator;

  public void testDiscoverHeartManDevices() throws Exception {
    heartManSimulator.createDevice();

    List<HeartManDevice> devices = heartManDiscovery.discoverHeartManDevices();
    assertNotNull(devices);
    assertEquals(1, devices.size());

    HeartManDevice device = devices.get(0);
    assertNotNull(device);
  }

  public void testGetService() throws Exception {
    String address = heartManSimulator.createDevice();

    heartManDiscovery.discoverHeartManDevices();

    List<ServiceRecord> services = heartManDiscovery.searchServices(address);
    assertNotNull(services);
    assertEquals(1, services.size());
  }

  public void testListeningOnMoreDevices() throws Exception {
    final String first = heartManSimulator
        .createFileDevice(getFile("/recording20s_sleep5ms_1.dat"));
    final String second = heartManSimulator
        .createFileDevice(getFile("/recording20s_sleep5ms_2.dat"));

    heartManDiscovery.discoverHeartManDevices();

    final Semaphore s1 = new Semaphore(0);
    final Semaphore s2 = new Semaphore(0);
    TestHeartManListener l = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        System.out.printf("%1$s received %2$f\n", address, value);
        if (address.equals(first)) {
          s1.release();
        } else if (address.equals(second)) {
          s2.release();
        }
      }
    };

    heartManDiscovery.startListening(first, l);
    heartManDiscovery.startListening(second, l);

    s1.acquire();
    s2.acquire();
  }

  public void testRecording() throws Exception {
    String address = heartManSimulator.createDevice();
    heartManDiscovery.discoverHeartManDevices();
    heartManDiscovery.startRecording(address);

    final Semaphore s = new Semaphore(0);
    TestHeartManListener listener = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        s.release();
      }
    };
    heartManDiscovery.startListening(address, listener);
    heartManSimulator.sendValue(address, 1.0);
    heartManSimulator.sendValue(address, 2.0);
    heartManSimulator.sendValue(address, 3.0);
    heartManSimulator.sendValue(address, 4.0);
    s.acquire(4);

    List<Double> recordings = heartManDiscovery.stopRecording(address);
    assertNotNull(recordings);
    assertEquals(4, recordings.size());
    assertEquals(1.0, recordings.get(0), 0.1D);
    assertEquals(2.0, recordings.get(1), 0.1D);
    assertEquals(3.0, recordings.get(2), 0.1D);
    assertEquals(4.0, recordings.get(3), 0.1D);
  }

  public void testStartListening() throws Exception {
    String address = heartManSimulator.createDevice();

    heartManDiscovery.discoverHeartManDevices();

    final Semaphore s = new Semaphore(0);
    TestHeartManListener listener = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        super.dataReceived(address, timestamp, value);
        s.release();
      }
    };
    heartManDiscovery.startListening(address, listener);
    heartManSimulator.sendValue(address, 6.0D);
    s.acquireUninterruptibly();

    assertEquals(6.0D, listener.receivedValue, 0.01D);
    System.out.println("finished");
  }

  public void testStartListeningForInvalidAdress() throws Exception {
    try {
      heartManDiscovery.startListening((String) null,
          new TestHeartManListener());
      fail("should throw an exception when trying to start listening for a device which is not available");
    } catch (Exception e) {
    }
  }

  public void testStartListeningForMoreDevices() throws Exception {
    String first = heartManSimulator.createDevice();
    String second = heartManSimulator.createDevice();

    final Semaphore s = new Semaphore(0);
    TestHeartManListener listener = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        super.dataReceived(address, timestamp, value);
        s.release(1);
      }
    };

    List<HeartManDevice> devices = heartManDiscovery.discoverHeartManDevices();
    assertNotNull(devices);
    assertEquals(2, devices.size());

    heartManDiscovery.startListening(first, listener);
    heartManDiscovery.startListening(second, listener);

    heartManSimulator.sendValue(first, 1.0D);
    s.acquire();

    assertTrue(listener.invoked);
    assertEquals(first, listener.address);
    assertEquals(1.0D, listener.receivedValue, 0.1D);
    assertEquals(0, s.availablePermits());

    listener.reset();
    heartManSimulator.sendValue(second, 2.0D);
    s.acquire();

    assertTrue(listener.invoked);
    assertEquals(second, listener.address);
    assertEquals(2.0D, listener.receivedValue, 0.1D);
    assertEquals(0, s.availablePermits());
  }

  public void testStartListeningWithMoreListeners() throws Exception {
    String address = heartManSimulator.createDevice();

    final Semaphore s = new Semaphore(0);
    TestHeartManListener l1 = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        super.dataReceived(address, timestamp, value);
        s.release(1);
      }
    };
    TestHeartManListener l2 = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        super.dataReceived(address, timestamp, value);
        s.release(1);
      }
    };

    heartManDiscovery.discoverHeartManDevices();

    heartManDiscovery.startListening(address, l1);
    heartManSimulator.sendValue(address, 1.0D);
    // wait until listener got invoked
    s.acquire(1);

    // l1 invoked - l2 not
    assertTrue(l1.invoked);
    assertEquals(1.0D, l1.receivedValue, 0.1D);
    assertFalse(l2.invoked);

    // reset
    l1.reset();
    l2.reset();

    // add second listener
    heartManDiscovery.startListening(address, l2);
    heartManSimulator.sendValue(address, 5.0D);
    // wait until both are invoked
    s.acquire(2);

    // both listeners invoked
    assertTrue(l1.invoked);
    assertEquals(5.0D, l1.receivedValue, 0.1D);
    assertTrue(l2.invoked);
    assertEquals(5.0D, l2.receivedValue, 0.1D);
  }

  public void testStartListeringWithInvalidListener() throws Exception {
    String address = heartManSimulator.createDevice();

    List<HeartManDevice> devices = heartManDiscovery.discoverHeartManDevices();
    assertNotNull(devices);
    assertEquals(1, devices.size());

    try {
      heartManDiscovery.startListening(address, null);
    } catch (Exception e) {
      fail("should not throw an exception");
    }
  }

  public void testStopListening() throws Exception {
    String address = heartManSimulator.createDevice();
    heartManDiscovery.discoverHeartManDevices();

    final Semaphore s = new Semaphore(0);
    TestHeartManListener listener = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        super.dataReceived(address, timestamp, value);
        s.release();
      }
    };

    heartManDiscovery.startListening(address, listener);
    heartManSimulator.sendValue(address, 42);
    s.acquire();

    assertTrue(listener.invoked);
    listener.reset();

    heartManDiscovery.stopListening(address);

    heartManSimulator.sendValue(address, 69);

    assertFalse(listener.invoked);
  }

  public void testStopListeningForNullAddress() throws Exception {
    try {
      heartManDiscovery.stopListening(null);
    } catch (Exception e) {
      fail("should not thrown an exception");
    }
  }

  public void testStopListeningWhenNotListening() throws Exception {
    String address = heartManSimulator.createDevice();

    try {
      heartManDiscovery.stopListening(address);
    } catch (Exception e) {
      fail("should not thrown an exception");
    }
  }

  public void testTwoHeartManDiscoveryInstances() throws Exception {
    heartManSimulator.createDevice();

    List<HeartManDevice> firstList = heartManDiscovery
        .discoverHeartManDevices();
    assertNotNull(firstList);
    assertEquals(1, firstList.size());

    HeartManDiscovery secondDiscovery = new HeartManDiscovery();
    List<HeartManDevice> secondList = secondDiscovery.discoverHeartManDevices();
    assertNotNull(secondList);
    assertEquals(1, secondList.size());
  }

  protected File getFile(String path) throws URISyntaxException {
    URL url = ECGMonitorMain.class.getResource(path);
    return new File(url.toURI());
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();

    heartManSimulator = new HeartManSimulator();

    heartManDiscovery = new HeartManDiscovery();
    assertTrue(heartManDiscovery.isBluetoothEnabled());
  }

  @Override
  protected void tearDown() throws Exception {
    super.tearDown();

    heartManDiscovery = null;

    heartManSimulator.stopServer();
    heartManSimulator = null;
  }

}
<commit_msg>update test so bluetooth devices are started correctly<commit_after>package at.jku.pervasive.ecg;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Semaphore;

import javax.bluetooth.ServiceRecord;

import junit.framework.TestCase;

public class HeartManDiscoveryTest extends TestCase {

  private HeartManDiscovery heartManDiscovery;
  private HeartManSimulator heartManSimulator;

  public void testDiscoverHeartManDevices() throws Exception {
    heartManSimulator.createDevice();

    List<HeartManDevice> devices = heartManDiscovery.discoverHeartManDevices();
    assertNotNull(devices);
    assertEquals(1, devices.size());

    HeartManDevice device = devices.get(0);
    assertNotNull(device);
  }

  public void testGetService() throws Exception {
    String address = heartManSimulator.createDevice();

    heartManDiscovery.discoverHeartManDevices();

    List<ServiceRecord> services = heartManDiscovery.searchServices(address);
    assertNotNull(services);
    assertEquals(1, services.size());
  }

  public void testListeningOnMoreDevices() throws Exception {
    final String first = heartManSimulator
        .createFileDevice(getFile("/recording20s_sleep5ms_1.dat"));
    final String second = heartManSimulator
        .createFileDevice(getFile("/recording20s_sleep5ms_2.dat"));

    heartManDiscovery.discoverHeartManDevices();

    List<ServiceRecord> firstServices = heartManDiscovery.searchServices(first);
    assertNotNull(firstServices);
    assertTrue(firstServices.size() > 0);
    ServiceRecord firstServiceRecord = firstServices.get(0);

    List<ServiceRecord> secondServices = heartManDiscovery
        .searchServices(second);
    assertNotNull(secondServices);
    assertTrue(secondServices.size() > 0);
    ServiceRecord secondServiceRecord = secondServices.get(0);

    final Semaphore s1 = new Semaphore(0);
    final Semaphore s2 = new Semaphore(0);
    TestHeartManListener l = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        System.out.printf("%1$s received %2$f\n", address, value);
        if (address.equals(first)) {
          s1.release();
        } else if (address.equals(second)) {
          s2.release();
        }
      }
    };

    heartManDiscovery.startListening(first, l, firstServiceRecord);
    heartManDiscovery.startListening(second, l, secondServiceRecord);

    s1.acquire();
    s2.acquire();
  }

  public void testRecording() throws Exception {
    String address = heartManSimulator.createDevice();
    heartManDiscovery.discoverHeartManDevices();
    heartManDiscovery.startRecording(address);

    final Semaphore s = new Semaphore(0);
    TestHeartManListener listener = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        s.release();
      }
    };
    heartManDiscovery.startListening(address, listener);
    heartManSimulator.sendValue(address, 1.0);
    heartManSimulator.sendValue(address, 2.0);
    heartManSimulator.sendValue(address, 3.0);
    heartManSimulator.sendValue(address, 4.0);
    s.acquire(4);

    List<Double> recordings = heartManDiscovery.stopRecording(address);
    assertNotNull(recordings);
    assertEquals(4, recordings.size());
    assertEquals(1.0, recordings.get(0), 0.1D);
    assertEquals(2.0, recordings.get(1), 0.1D);
    assertEquals(3.0, recordings.get(2), 0.1D);
    assertEquals(4.0, recordings.get(3), 0.1D);
  }

  public void testStartListening() throws Exception {
    String address = heartManSimulator.createDevice();

    heartManDiscovery.discoverHeartManDevices();

    final Semaphore s = new Semaphore(0);
    TestHeartManListener listener = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        super.dataReceived(address, timestamp, value);
        s.release();
      }
    };
    heartManDiscovery.startListening(address, listener);
    heartManSimulator.sendValue(address, 6.0D);
    s.acquireUninterruptibly();

    assertEquals(6.0D, listener.receivedValue, 0.01D);
    System.out.println("finished");
  }

  public void testStartListeningForInvalidAdress() throws Exception {
    try {
      heartManDiscovery.startListening((String) null,
          new TestHeartManListener());
      fail("should throw an exception when trying to start listening for a device which is not available");
    } catch (Exception e) {
    }
  }

  public void testStartListeningForMoreDevices() throws Exception {
    String first = heartManSimulator.createDevice();
    String second = heartManSimulator.createDevice();

    final Semaphore s = new Semaphore(0);
    TestHeartManListener listener = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        super.dataReceived(address, timestamp, value);
        s.release(1);
      }
    };

    List<HeartManDevice> devices = heartManDiscovery.discoverHeartManDevices();
    assertNotNull(devices);
    assertEquals(2, devices.size());

    heartManDiscovery.startListening(first, listener);
    heartManDiscovery.startListening(second, listener);

    heartManSimulator.sendValue(first, 1.0D);
    s.acquire();

    assertTrue(listener.invoked);
    assertEquals(first, listener.address);
    assertEquals(1.0D, listener.receivedValue, 0.1D);
    assertEquals(0, s.availablePermits());

    listener.reset();
    heartManSimulator.sendValue(second, 2.0D);
    s.acquire();

    assertTrue(listener.invoked);
    assertEquals(second, listener.address);
    assertEquals(2.0D, listener.receivedValue, 0.1D);
    assertEquals(0, s.availablePermits());
  }

  public void testStartListeningWithMoreListeners() throws Exception {
    String address = heartManSimulator.createDevice();

    final Semaphore s = new Semaphore(0);
    TestHeartManListener l1 = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        super.dataReceived(address, timestamp, value);
        s.release(1);
      }
    };
    TestHeartManListener l2 = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        super.dataReceived(address, timestamp, value);
        s.release(1);
      }
    };

    heartManDiscovery.discoverHeartManDevices();

    heartManDiscovery.startListening(address, l1);
    heartManSimulator.sendValue(address, 1.0D);
    // wait until listener got invoked
    s.acquire(1);

    // l1 invoked - l2 not
    assertTrue(l1.invoked);
    assertEquals(1.0D, l1.receivedValue, 0.1D);
    assertFalse(l2.invoked);

    // reset
    l1.reset();
    l2.reset();

    // add second listener
    heartManDiscovery.startListening(address, l2);
    heartManSimulator.sendValue(address, 5.0D);
    // wait until both are invoked
    s.acquire(2);

    // both listeners invoked
    assertTrue(l1.invoked);
    assertEquals(5.0D, l1.receivedValue, 0.1D);
    assertTrue(l2.invoked);
    assertEquals(5.0D, l2.receivedValue, 0.1D);
  }

  public void testStartListeringWithInvalidListener() throws Exception {
    String address = heartManSimulator.createDevice();

    List<HeartManDevice> devices = heartManDiscovery.discoverHeartManDevices();
    assertNotNull(devices);
    assertEquals(1, devices.size());

    try {
      heartManDiscovery.startListening(address, null);
    } catch (Exception e) {
      fail("should not throw an exception");
    }
  }

  public void testStopListening() throws Exception {
    String address = heartManSimulator.createDevice();
    heartManDiscovery.discoverHeartManDevices();

    final Semaphore s = new Semaphore(0);
    TestHeartManListener listener = new TestHeartManListener() {
      @Override
      public void dataReceived(String address, long timestamp, double value) {
        super.dataReceived(address, timestamp, value);
        s.release();
      }
    };

    heartManDiscovery.startListening(address, listener);
    heartManSimulator.sendValue(address, 42);
    s.acquire();

    assertTrue(listener.invoked);
    listener.reset();

    heartManDiscovery.stopListening(address);

    heartManSimulator.sendValue(address, 69);

    assertFalse(listener.invoked);
  }

  public void testStopListeningForNullAddress() throws Exception {
    try {
      heartManDiscovery.stopListening(null);
    } catch (Exception e) {
      fail("should not thrown an exception");
    }
  }

  public void testStopListeningWhenNotListening() throws Exception {
    String address = heartManSimulator.createDevice();

    try {
      heartManDiscovery.stopListening(address);
    } catch (Exception e) {
      fail("should not thrown an exception");
    }
  }

  public void testTwoHeartManDiscoveryInstances() throws Exception {
    heartManSimulator.createDevice();

    List<HeartManDevice> firstList = heartManDiscovery
        .discoverHeartManDevices();
    assertNotNull(firstList);
    assertEquals(1, firstList.size());

    HeartManDiscovery secondDiscovery = new HeartManDiscovery();
    List<HeartManDevice> secondList = secondDiscovery.discoverHeartManDevices();
    assertNotNull(secondList);
    assertEquals(1, secondList.size());
  }

  protected File getFile(String path) throws URISyntaxException {
    URL url = ECGMonitorMain.class.getResource(path);
    return new File(url.toURI());
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();

    heartManSimulator = new HeartManSimulator();

    heartManDiscovery = new HeartManDiscovery();
    assertTrue(heartManDiscovery.isBluetoothEnabled());
  }

  @Override
  protected void tearDown() throws Exception {
    super.tearDown();

    heartManDiscovery = null;

    heartManSimulator.stopServer();
    heartManSimulator = null;
  }

}

{'fraction_non_alphanumeric': 0.10338956817830057, 'fraction_numerical': 0.012691533818294382, 'mean_word_length': 3.8399500624219725, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5999025', 'n_tokens_mistral': 5755, 'n_tokens_neox': 5502, 'n_words': 1154}