EnOceanConnectionManager { public synchronized EnOceanConnection getConnection(EspPortConfiguration configuration, RadioTelegramListener listener) throws ConfigurationException, ConnectionException { if(configuration == null) { throw new IllegalArgumentException("null configuration"); } if(connection != null) { return connection; } if(configuration.getSerialProtocol() == EspPortConfiguration.SerialProtocol.ESP2) { connection = createEsp2Connection(configuration, listener); } else if(configuration.getSerialProtocol() == EspPortConfiguration.SerialProtocol.ESP3) { connection = createEsp3Connection(configuration, listener); } else { throw new ConfigurationException( "Invalid EnOcean serial protocol configuration " + "(see enocean.serialProtocol config property)." ); } if(connection != null) { connection.connect(); } return connection; } EnOceanConnectionManager(); synchronized EnOceanConnection getConnection(EspPortConfiguration configuration, RadioTelegramListener listener); synchronized void disconnect();  }
@Test public void testGetConnection() throws Exception { TestConnectionManager mgr = new TestConnectionManager(); EspPortConfiguration config = new EspPortConfiguration(); config.setComPort("COM1"); config.setCommLayer(EspPortConfiguration.CommLayer.PAD); config.setSerialProtocol(EspPortConfiguration.SerialProtocol.ESP3); RadioTelegramListener listener = new TestListener(); EnOceanConnection conn1 = mgr.getConnection(config, listener); EnOceanConnection conn2 = mgr.getConnection(config, listener); Assert.assertNotNull(conn1); Assert.assertNotNull(conn2); Assert.assertTrue(conn1 instanceof TestEsp3Connection); Assert.assertEquals(conn1, conn2); Assert.assertEquals(1, ((TestEsp3Connection) conn1).connectCount); mgr = new TestConnectionManager(); config = new EspPortConfiguration(); config.setComPort("COM2"); config.setCommLayer(EspPortConfiguration.CommLayer.PAD); config.setSerialProtocol(EspPortConfiguration.SerialProtocol.ESP2); EnOceanConnection conn3 = mgr.getConnection(config, listener); Assert.assertNotNull(conn3); Assert.assertTrue(conn3 instanceof TestEsp2Connection); Assert.assertEquals(1, ((TestEsp2Connection) conn3).connectCount); }
@Test (expected = IllegalArgumentException.class) public void testNullConfiguration() throws Exception { RadioTelegramListener listener = new TestListener(); EnOceanConnectionManager mgr = new EnOceanConnectionManager(); EnOceanConnection conn1 = mgr.getConnection(null, listener); }
@Test (expected = ConfigurationException.class) public void testMissingComPort() throws Exception { RadioTelegramListener listener = new TestListener(); EnOceanConnectionManager mgr = new EnOceanConnectionManager(); EspPortConfiguration config = new EspPortConfiguration(); EnOceanConnection conn1 = mgr.getConnection(config, listener); }