Search in sources :

Example 1 with ConnectorService

use of org.osgi.service.io.ConnectorService in project felix by apache.

the class ConnectorServiceTest method testMultipleScheme.

/**
 * Tests ConnectionFactory service which support 3 different schemes.
 *
 * @throws Exception
 */
public void testMultipleScheme() throws Exception {
    ConnectionFactoryMock connFactory = new ConnectionFactoryMock();
    Dictionary props = new Hashtable();
    props.put(ConnectionFactory.IO_SCHEME, new String[] { "file", "http", "sms" });
    ServiceRegistration registration = registerConnectionFactory(connFactory, props);
    ConnectorService service = getConnectorService();
    Connection connection = service.open("file://test.txt");
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ_WRITE, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned connection", connection);
    connection = service.open("http://test.txt", ConnectorService.READ);
    assertEquals("http://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned connection", connection);
    connection = service.open("sms://test.txt", ConnectorService.READ);
    assertEquals("sms://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned connection", connection);
    try {
        connection = service.open("ftp://test.txt", ConnectorService.READ);
        fail("Connection shouldn't be created");
    } catch (ConnectionNotFoundException e) {
    // "expected"
    }
    registration.unregister();
}
Also used : Dictionary(java.util.Dictionary) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) Hashtable(java.util.Hashtable) Connection(javax.microedition.io.Connection) ConnectorService(org.osgi.service.io.ConnectorService) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with ConnectorService

use of org.osgi.service.io.ConnectorService in project felix by apache.

the class ConnectorServiceTest method testLowestServiceId.

/**
 * Registers two ConnectionFactory services with same IO_SCHEME. Connector Service should pickup service with lowest
 * service.id.
 *
 * @throws Exception
 */
public void testLowestServiceId() throws Exception {
    ConnectionFactoryMock connFactory = new ConnectionFactoryMock();
    Dictionary props = new Hashtable();
    props.put(ConnectionFactory.IO_SCHEME, "file");
    ServiceRegistration registration = registerConnectionFactory(connFactory, props);
    ConnectionFactoryMock connFactory2 = new ConnectionFactoryMock();
    ServiceRegistration registration2 = registerConnectionFactory(connFactory2, props);
    ConnectorService service = getConnectorService();
    Connection connection = service.open("file://test.txt");
    assertEquals("uri checks for highest service.id", null, connFactory2.getName());
    assertEquals("uri checks for lowest service.id", "file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ_WRITE, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned Connection", connection);
    Long serviceId1 = (Long) registration.getReference().getProperty(Constants.SERVICE_ID);
    Long serviceId2 = (Long) registration2.getReference().getProperty(Constants.SERVICE_ID);
    assertTrue(serviceId1.longValue() < serviceId2.longValue());
    registration.unregister();
    registration2.unregister();
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) Connection(javax.microedition.io.Connection) ConnectorService(org.osgi.service.io.ConnectorService) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 3 with ConnectorService

use of org.osgi.service.io.ConnectorService in project felix by apache.

the class ConnectorServiceTest method testHighestRanking.

/**
 * Registers two ConnectionFactory services with same IO_SCHEME. One with higher service.ranking Connector Service
 * should pickup service with highest service.ranking.
 *
 * @throws Exception
 */
public void testHighestRanking() throws Exception {
    ConnectionFactoryMock connFactory = new ConnectionFactoryMock();
    Dictionary props = new Hashtable();
    props.put(ConnectionFactory.IO_SCHEME, "file");
    registerConnectionFactory(connFactory, props);
    ConnectionFactoryMock connFactory2 = new ConnectionFactoryMock();
    props.put(Constants.SERVICE_RANKING, new Integer(Integer.MAX_VALUE));
    registerConnectionFactory(connFactory2, props);
    ConnectorService service = getConnectorService();
    Connection connection = service.open("file://test.txt");
    assertEquals("uri checks for lowest ranking service", null, connFactory.getName());
    assertEquals("file://test.txt", connFactory2.getName());
    assertEquals(ConnectorService.READ_WRITE, connFactory2.getMode());
    assertEquals(false, connFactory2.isTimeout());
    assertNotNull("Connection should be created", connection);
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) Connection(javax.microedition.io.Connection) ConnectorService(org.osgi.service.io.ConnectorService)

Example 4 with ConnectorService

use of org.osgi.service.io.ConnectorService in project felix by apache.

the class ConnectorServiceTest method testOpen.

/**
 * Tests all methods provided by {@link ConnectorService}.
 *
 * @throws Exception
 */
public void testOpen() throws Exception {
    ConnectionFactoryMock connFactory = new ConnectionFactoryMock();
    Dictionary props = new Hashtable();
    props.put(ConnectionFactory.IO_SCHEME, "file");
    ServiceRegistration registration = registerConnectionFactory(connFactory, props);
    ConnectorService service = getConnectorService();
    Connection connection = service.open("file://test.txt");
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ_WRITE, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned Connection", connection);
    connection = service.open("file://test.txt", ConnectorService.READ);
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned Connection", connection);
    connection = service.open("file://test.txt", ConnectorService.WRITE);
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.WRITE, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned Connection", connection);
    connection = service.open("file://test.txt", ConnectorService.READ, true);
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ, connFactory.getMode());
    assertEquals(true, connFactory.isTimeout());
    assertNotNull("checks returned Connection", connection);
    try {
        connection = service.open("http://test.txt", ConnectorService.READ);
        fail("Connection shouldn't be created");
    } catch (ConnectionNotFoundException e) {
    // "expected"
    }
    try {
        service.open("file.txt");
        fail("Illegal format of uri");
    } catch (IllegalArgumentException e) {
    // expected
    }
    DataInputStream dataInStream = service.openDataInputStream("file://test.txt");
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned DataInputStream", dataInStream);
    DataOutputStream dataOutStream = service.openDataOutputStream("file://test.txt");
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.WRITE, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned DataOutputStream", dataOutStream);
    InputStream inStream = service.openInputStream("file://test.txt");
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned InputStream", inStream);
    OutputStream outStream = service.openOutputStream("file://test.txt");
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.WRITE, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned OutputStream", outStream);
    registration.unregister();
}
Also used : Dictionary(java.util.Dictionary) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) Hashtable(java.util.Hashtable) DataOutputStream(java.io.DataOutputStream) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) DataOutputStream(java.io.DataOutputStream) Connection(javax.microedition.io.Connection) DataInputStream(java.io.DataInputStream) ConnectorService(org.osgi.service.io.ConnectorService) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

Dictionary (java.util.Dictionary)4 Hashtable (java.util.Hashtable)4 Connection (javax.microedition.io.Connection)4 ConnectorService (org.osgi.service.io.ConnectorService)4 ServiceRegistration (org.osgi.framework.ServiceRegistration)3 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)2 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1