@Test public void testAutoDetectDsnIfNotProvided( @Injectable final Raven mockRaven, @SuppressWarnings("unused") @Mocked final Dsn mockDsn) throws Exception { final String dsn = "protocol://*****:*****@host:port/3"; RavenFactory.registerFactory(ravenFactory); new NonStrictExpectations() { { Dsn.dsnLookup(); result = dsn; ravenFactory.createRavenInstance((Dsn) any); result = mockRaven; } }; Raven raven = RavenFactory.ravenInstance(); assertThat(raven, is(mockRaven)); new Verifications() { { new Dsn(dsn); } }; }
@Test(expectedExceptions = IllegalStateException.class) public void testRavenInstanceForFactoryNameFailsIfNoFactoryFound( @Injectable final Raven mockRaven, @Injectable final Dsn mockDsn) throws Exception { String factoryName = "invalidName"; RavenFactory.registerFactory(ravenFactory); new NonStrictExpectations() { { ravenFactory.createRavenInstance(mockDsn); result = mockRaven; } }; RavenFactory.ravenInstance(mockDsn, factoryName); }
@Test public void testGetFactoriesManuallyAdded( @Injectable final Raven mockRaven, @Injectable final Dsn mockDsn) throws Exception { RavenFactory.registerFactory(ravenFactory); new NonStrictExpectations() { { ravenFactory.createRavenInstance(mockDsn); result = mockRaven; } }; Raven raven = RavenFactory.ravenInstance(mockDsn); assertThat(raven, is(mockRaven)); }
@Test public void testRavenInstanceForFactoryNameSucceedsIfFactoryFound( @Injectable final Raven mockRaven, @Injectable final Dsn mockDsn) throws Exception { String factoryName = ravenFactory.getClass().getName(); RavenFactory.registerFactory(ravenFactory); new NonStrictExpectations() { { ravenFactory.createRavenInstance(mockDsn); result = mockRaven; } }; Raven raven = RavenFactory.ravenInstance(mockDsn, factoryName); assertThat(raven, is(mockRaven)); }
@Test public void testRavenInstantiationFailureCaught(@Injectable final Dsn mockDsn) throws Exception { RavenFactory.registerFactory(ravenFactory); Exception exception = null; new NonStrictExpectations() { { ravenFactory.createRavenInstance(mockDsn); result = new RuntimeException(); } }; try { RavenFactory.ravenInstance(mockDsn); } catch (IllegalStateException e) { exception = e; } assertThat(exception, notNullValue()); }
@Test public void testGetFactoriesFromServiceLoader( @Injectable final Raven mockRaven, @Injectable final Dsn mockDsn) throws Exception { new NonStrictExpectations() { { mockServiceLoader.iterator(); result = new Delegate<Iterator<RavenFactory>>() { @SuppressWarnings("unused") public Iterator<RavenFactory> iterator() { return Collections.singletonList(ravenFactory).iterator(); } }; ravenFactory.createRavenInstance(mockDsn); result = mockRaven; } }; Raven raven = RavenFactory.ravenInstance(mockDsn); assertThat(raven, is(mockRaven)); }