@Test public void testConfigure(@Mocked final EncryptionServiceImpl asymetricCryptingRSA, @Mocked final KeyStore keyStore, @Mocked final Binder binder, @Mocked final SSLContext sslContext) { final Map<String, KeyStore> keyStores = new HashMap<>(); keyStores.put("k1", keyStore); final Map<Key<EncryptionService>, EncryptionService> rsaServices = new HashMap<>(); rsaServices.put(Key.get(EncryptionService.class, Names.named("k1")), asymetricCryptingRSA); rsaServices.put(Key.get(EncryptionService.class, Names.named("k2")), asymetricCryptingRSA); CryptoModule module = new CryptoModule(rsaServices, keyStores, keyStore, sslContext, new ArrayList<>(), null); module.configure(binder); new Verifications() { { binder.bind(Key.get(EncryptionService.class, Names.named("k1"))).toInstance(asymetricCryptingRSA); times = 1; binder.bind(Key.get(EncryptionService.class, Names.named("k2"))).toInstance(asymetricCryptingRSA); times = 1; binder.bind(Key.get(KeyStore.class, Names.named("k1"))).toInstance(keyStore); times = 1; binder.bind(SSLContext.class).toInstance(sslContext); times = 1; } }; }
@Override protected void configure() { OptionalBinder.newOptionalBinder(binder(), Key.get(KeyStore.class, Names.named(TRUSTSTORE))); if (trustStore != null) { bind(KeyStore.class).annotatedWith(Names.named(TRUSTSTORE)).toInstance(trustStore); } for (Entry<String, KeyStore> keyStoreEntry : this.keyStores.entrySet()) { bind(Key.get(KeyStore.class, Names.named(keyStoreEntry.getKey()))).toInstance(keyStoreEntry.getValue()); } for (Entry<Key<EncryptionService>, EncryptionService> entry : this.encryptionServices.entrySet()) { bind(entry.getKey()).toInstance(entry.getValue()); } bind(HashingService.class).to(PBKDF2HashingService.class); OptionalBinder.newOptionalBinder(binder(), SSLContext.class); if (sslContext != null) { bind(SSLContext.class).toInstance(sslContext); } if (keyManagerClass != null) { bind(X509KeyManager.class).to(keyManagerClass); } else { bind(X509KeyManager.class).toProvider(Providers.of(null)); } keyManagerAdapters.forEach(this::requestInjection); }
CryptoModule extends AbstractModule { @Override protected void configure() { OptionalBinder.newOptionalBinder(binder(), Key.get(KeyStore.class, Names.named(TRUSTSTORE))); if (trustStore != null) { bind(KeyStore.class).annotatedWith(Names.named(TRUSTSTORE)).toInstance(trustStore); } for (Entry<String, KeyStore> keyStoreEntry : this.keyStores.entrySet()) { bind(Key.get(KeyStore.class, Names.named(keyStoreEntry.getKey()))).toInstance(keyStoreEntry.getValue()); } for (Entry<Key<EncryptionService>, EncryptionService> entry : this.encryptionServices.entrySet()) { bind(entry.getKey()).toInstance(entry.getValue()); } bind(HashingService.class).to(PBKDF2HashingService.class); OptionalBinder.newOptionalBinder(binder(), SSLContext.class); if (sslContext != null) { bind(SSLContext.class).toInstance(sslContext); } if (keyManagerClass != null) { bind(X509KeyManager.class).to(keyManagerClass); } else { bind(X509KeyManager.class).toProvider(Providers.of(null)); } keyManagerAdapters.forEach(this::requestInjection); } }
CryptoModule extends AbstractModule { @Override protected void configure() { OptionalBinder.newOptionalBinder(binder(), Key.get(KeyStore.class, Names.named(TRUSTSTORE))); if (trustStore != null) { bind(KeyStore.class).annotatedWith(Names.named(TRUSTSTORE)).toInstance(trustStore); } for (Entry<String, KeyStore> keyStoreEntry : this.keyStores.entrySet()) { bind(Key.get(KeyStore.class, Names.named(keyStoreEntry.getKey()))).toInstance(keyStoreEntry.getValue()); } for (Entry<Key<EncryptionService>, EncryptionService> entry : this.encryptionServices.entrySet()) { bind(entry.getKey()).toInstance(entry.getValue()); } bind(HashingService.class).to(PBKDF2HashingService.class); OptionalBinder.newOptionalBinder(binder(), SSLContext.class); if (sslContext != null) { bind(SSLContext.class).toInstance(sslContext); } if (keyManagerClass != null) { bind(X509KeyManager.class).to(keyManagerClass); } else { bind(X509KeyManager.class).toProvider(Providers.of(null)); } keyManagerAdapters.forEach(this::requestInjection); } CryptoModule(Map<Key<EncryptionService>, EncryptionService> encryptionServices, Map<String, KeyStore> keyStores,
            KeyStore trustStore, SSLContext sslContext,
            List<KeyManagerAdapter> keyManagerAdapters,
            Class<? extends X509KeyManager> keyManagerClass); }
CryptoModule extends AbstractModule { @Override protected void configure() { OptionalBinder.newOptionalBinder(binder(), Key.get(KeyStore.class, Names.named(TRUSTSTORE))); if (trustStore != null) { bind(KeyStore.class).annotatedWith(Names.named(TRUSTSTORE)).toInstance(trustStore); } for (Entry<String, KeyStore> keyStoreEntry : this.keyStores.entrySet()) { bind(Key.get(KeyStore.class, Names.named(keyStoreEntry.getKey()))).toInstance(keyStoreEntry.getValue()); } for (Entry<Key<EncryptionService>, EncryptionService> entry : this.encryptionServices.entrySet()) { bind(entry.getKey()).toInstance(entry.getValue()); } bind(HashingService.class).to(PBKDF2HashingService.class); OptionalBinder.newOptionalBinder(binder(), SSLContext.class); if (sslContext != null) { bind(SSLContext.class).toInstance(sslContext); } if (keyManagerClass != null) { bind(X509KeyManager.class).to(keyManagerClass); } else { bind(X509KeyManager.class).toProvider(Providers.of(null)); } keyManagerAdapters.forEach(this::requestInjection); } CryptoModule(Map<Key<EncryptionService>, EncryptionService> encryptionServices, Map<String, KeyStore> keyStores,
            KeyStore trustStore, SSLContext sslContext,
            List<KeyManagerAdapter> keyManagerAdapters,
            Class<? extends X509KeyManager> keyManagerClass);  }
CryptoModule extends AbstractModule { @Override protected void configure() { OptionalBinder.newOptionalBinder(binder(), Key.get(KeyStore.class, Names.named(TRUSTSTORE))); if (trustStore != null) { bind(KeyStore.class).annotatedWith(Names.named(TRUSTSTORE)).toInstance(trustStore); } for (Entry<String, KeyStore> keyStoreEntry : this.keyStores.entrySet()) { bind(Key.get(KeyStore.class, Names.named(keyStoreEntry.getKey()))).toInstance(keyStoreEntry.getValue()); } for (Entry<Key<EncryptionService>, EncryptionService> entry : this.encryptionServices.entrySet()) { bind(entry.getKey()).toInstance(entry.getValue()); } bind(HashingService.class).to(PBKDF2HashingService.class); OptionalBinder.newOptionalBinder(binder(), SSLContext.class); if (sslContext != null) { bind(SSLContext.class).toInstance(sslContext); } if (keyManagerClass != null) { bind(X509KeyManager.class).to(keyManagerClass); } else { bind(X509KeyManager.class).toProvider(Providers.of(null)); } keyManagerAdapters.forEach(this::requestInjection); } CryptoModule(Map<Key<EncryptionService>, EncryptionService> encryptionServices, Map<String, KeyStore> keyStores,
            KeyStore trustStore, SSLContext sslContext,
            List<KeyManagerAdapter> keyManagerAdapters,
            Class<? extends X509KeyManager> keyManagerClass);   }