diff --git a/bb-email/pom.xml b/bb-email/pom.xml
index 4517ea08..bcb23741 100644
--- a/bb-email/pom.xml
+++ b/bb-email/pom.xml
@@ -35,6 +35,10 @@
com.google.inject
guice
+
+ com.google.inject.extensions
+ guice-assistedinject
+
org.apache.commons
commons-lang3
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailClient.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailClient.java
index cbf2bac4..b99965e7 100644
--- a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailClient.java
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailClient.java
@@ -33,7 +33,9 @@
import org.slf4j.LoggerFactory;
import com.cognifide.qa.bb.email.connector.Connector;
-import com.google.inject.Inject;
+import com.cognifide.qa.bb.email.connector.ConnectorFactory;
+import com.google.inject.assistedinject.Assisted;
+import com.google.inject.assistedinject.AssistedInject;
/**
* This class represents email client.
@@ -49,13 +51,16 @@ public class EmailClient {
private Folder folder;
/**
- * @param connection MailBox protocol connection.
- * @param emailDataFactory Email data factory
+ * @param id Identification of configuration.
+ * @param connectionFactory Email config factory
+ * @param emailConfigFactory Email config factory
*/
- @Inject
- public EmailClient(Connector connection, EmailDataFactory emailDataFactory) {
- this.connection = connection;
- this.emailDataFactory = emailDataFactory;
+ @AssistedInject
+ public EmailClient(@Assisted String id, ConnectorFactory connectionFactory,
+ EmailConfigFactory emailConfigFactory) {
+ EmailConfig emailConfig = emailConfigFactory.create(id);
+ this.connection = connectionFactory.create(id).get();
+ this.emailDataFactory = new EmailDataFactory(emailConfig);
}
/**
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailClientFactory.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailClientFactory.java
new file mode 100644
index 00000000..8243d3d0
--- /dev/null
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailClientFactory.java
@@ -0,0 +1,25 @@
+/*-
+ * #%L
+ * Bobcat
+ * %%
+ * Copyright (C) 2016 Cognifide Ltd.
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+package com.cognifide.qa.bb.email;
+
+public interface EmailClientFactory {
+
+ EmailClient create(String id);
+}
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailConfig.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailConfig.java
new file mode 100644
index 00000000..a9c81155
--- /dev/null
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailConfig.java
@@ -0,0 +1,53 @@
+/*-
+ * #%L
+ * Bobcat
+ * %%
+ * Copyright (C) 2016 Cognifide Ltd.
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+package com.cognifide.qa.bb.email;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.commons.lang3.text.StrSubstitutor;
+
+import com.cognifide.qa.bb.email.constants.EmailConfigKeys;
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.assistedinject.Assisted;
+import com.google.inject.assistedinject.AssistedInject;
+import com.google.inject.name.Names;
+
+public class EmailConfig {
+
+ private final Map map;
+ private final Injector injector;
+
+ @AssistedInject
+ public EmailConfig(@Assisted String id, Injector injector) {
+ this.map = Collections.singletonMap(EmailConfigKeys.ID, id);
+ this.injector = injector;
+ }
+
+ public String getParameter(String name) {
+ return getParameter(String.class, name);
+ }
+
+ public T getParameter(Class type, String name) {
+ String realId = StrSubstitutor.replace(name, map);
+ return injector.getInstance(Key.get(type, Names.named(realId)));
+ }
+}
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailConfigFactory.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailConfigFactory.java
new file mode 100644
index 00000000..55fe21cd
--- /dev/null
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailConfigFactory.java
@@ -0,0 +1,25 @@
+/*-
+ * #%L
+ * Bobcat
+ * %%
+ * Copyright (C) 2016 Cognifide Ltd.
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+package com.cognifide.qa.bb.email;
+
+public interface EmailConfigFactory {
+
+ EmailConfig create(String id);
+}
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailDataFactory.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailDataFactory.java
index 313a2bb3..64a6e10e 100644
--- a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailDataFactory.java
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailDataFactory.java
@@ -36,7 +36,6 @@
import com.cognifide.qa.bb.email.connector.EmailException;
import com.cognifide.qa.bb.email.constants.EmailConfigKeys;
import com.google.inject.Inject;
-import com.google.inject.name.Named;
/**
* This class creates EmailData.
@@ -45,11 +44,11 @@ public final class EmailDataFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(EmailDataFactory.class);
- private final Pattern addressPattern;
+ private Pattern addressPattern;
@Inject
- public EmailDataFactory(
- @Named(EmailConfigKeys.EMAIL_ADDRESS_PATTERN) String addressPatternString) {
+ public EmailDataFactory(EmailConfig emailConfig) {
+ String addressPatternString = emailConfig.getParameter(EmailConfigKeys.EMAIL_ADDRESS_PATTERN);
this.addressPattern = Pattern.compile(addressPatternString);
}
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailModule.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailModule.java
index 0632846d..6f5c347a 100644
--- a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailModule.java
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailModule.java
@@ -19,9 +19,9 @@
*/
package com.cognifide.qa.bb.email;
-import com.cognifide.qa.bb.email.connector.Connector;
-import com.cognifide.qa.bb.email.connector.ConnectorProvider;
+import com.cognifide.qa.bb.email.connector.ConnectorFactory;
import com.google.inject.AbstractModule;
+import com.google.inject.assistedinject.FactoryModuleBuilder;
/**
* Install this module to enable email service. After installation, following email protocols will
@@ -36,6 +36,9 @@ public class EmailModule extends AbstractModule {
@Override
protected void configure() {
- bind(Connector.class).toProvider(ConnectorProvider.class);
+ install(new FactoryModuleBuilder().build(ConnectorFactory.class));
+ install(new FactoryModuleBuilder().build(EmailConfigFactory.class));
+ install(new FactoryModuleBuilder().build(EmailClientFactory.class));
+ install(new FactoryModuleBuilder().build(EmailSenderFactory.class));
}
}
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailSender.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailSender.java
index cd49f245..6d49fe65 100644
--- a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailSender.java
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailSender.java
@@ -25,9 +25,9 @@
import com.cognifide.qa.bb.email.connector.EmailException;
import com.cognifide.qa.bb.email.constants.EmailConfigKeys;
-import com.google.inject.Inject;
import com.google.inject.Singleton;
-import com.google.inject.name.Named;
+import com.google.inject.assistedinject.Assisted;
+import com.google.inject.assistedinject.AssistedInject;
/**
* Simple class for sending emails. It uses org.apache.commons.mail library.
@@ -35,26 +35,26 @@
@Singleton
public class EmailSender {
- @Inject
- @Named(EmailConfigKeys.EMAIL_USERNAME)
private String username;
- @Inject
- @Named(EmailConfigKeys.EMAIL_PASSWORD)
private String password;
- @Inject
- @Named(EmailConfigKeys.SMTP_SERVER_ADDRESS)
private String smtpServer;
- @Inject
- @Named(EmailConfigKeys.SMTP_SERVER_PORT)
private int smtpPort;
- @Inject
- @Named(EmailConfigKeys.SMTP_SERVER_SECURE)
private boolean secure;
+ @AssistedInject
+ public EmailSender(@Assisted String id, EmailConfigFactory factory) {
+ EmailConfig emailConfig = factory.create(id);
+ this.username = emailConfig.getParameter(EmailConfigKeys.EMAIL_USERNAME);
+ this.password = emailConfig.getParameter(EmailConfigKeys.EMAIL_PASSWORD);
+ this.smtpServer = emailConfig.getParameter(EmailConfigKeys.SMTP_SERVER_ADDRESS);
+ this.smtpPort = emailConfig.getParameter(Integer.class, EmailConfigKeys.SMTP_SERVER_PORT);
+ this.secure = emailConfig.getParameter(Boolean.class, EmailConfigKeys.SMTP_SERVER_SECURE);
+ }
+
public void sendEmail(final EmailData emailData) {
try {
Email email = new SimpleEmail();
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailSenderFactory.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailSenderFactory.java
new file mode 100644
index 00000000..f97f5009
--- /dev/null
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/EmailSenderFactory.java
@@ -0,0 +1,25 @@
+/*-
+ * #%L
+ * Bobcat
+ * %%
+ * Copyright (C) 2016 Cognifide Ltd.
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+package com.cognifide.qa.bb.email;
+
+public interface EmailSenderFactory {
+
+ EmailSender create(String id);
+}
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorConfig.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorConfig.java
index f49503d5..d9091d98 100644
--- a/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorConfig.java
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorConfig.java
@@ -19,37 +19,32 @@
*/
package com.cognifide.qa.bb.email.connector;
+import com.cognifide.qa.bb.email.EmailConfig;
import com.cognifide.qa.bb.email.constants.EmailConfigKeys;
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
-import com.google.inject.name.Named;
/**
* This class configures email connector using data from property file.
*/
-@Singleton
public class ConnectorConfig {
- @Inject
- @Named(EmailConfigKeys.EMAIL_USERNAME)
private String username;
- @Inject
- @Named(EmailConfigKeys.EMAIL_PASSWORD)
private String password;
- @Inject
- @Named(EmailConfigKeys.EMAIL_SERVER_PORT)
private int port;
- @Inject
- @Named(EmailConfigKeys.EMAIL_FOLDER_NAME)
private String folderName;
- @Inject
- @Named(EmailConfigKeys.EMAIL_SERVER_ADDRESS)
private String server;
+ public ConnectorConfig(EmailConfig emailConfig) {
+ this.username = emailConfig.getParameter(EmailConfigKeys.EMAIL_USERNAME);
+ this.password = emailConfig.getParameter(EmailConfigKeys.EMAIL_PASSWORD);
+ this.port = emailConfig.getParameter(Integer.class, EmailConfigKeys.EMAIL_SERVER_PORT);
+ this.folderName = emailConfig.getParameter(EmailConfigKeys.EMAIL_FOLDER_NAME);
+ this.server = emailConfig.getParameter(EmailConfigKeys.EMAIL_SERVER_ADDRESS);
+ }
+
/**
* @return Name of inbox folder.
*/
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorFactory.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorFactory.java
new file mode 100644
index 00000000..29abd46e
--- /dev/null
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorFactory.java
@@ -0,0 +1,25 @@
+/*-
+ * #%L
+ * Bobcat
+ * %%
+ * Copyright (C) 2016 Cognifide Ltd.
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+package com.cognifide.qa.bb.email.connector;
+
+public interface ConnectorFactory {
+
+ ConnectorProvider create(String id);
+}
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorProvider.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorProvider.java
index ca9deecd..6fe7fb97 100644
--- a/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorProvider.java
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/connector/ConnectorProvider.java
@@ -19,24 +19,28 @@
*/
package com.cognifide.qa.bb.email.connector;
+import com.cognifide.qa.bb.email.EmailConfig;
+import com.cognifide.qa.bb.email.EmailConfigFactory;
import com.cognifide.qa.bb.email.constants.EmailConfigKeys;
-import com.google.inject.Inject;
-import com.google.inject.Provider;
-import com.google.inject.name.Named;
+import com.google.inject.assistedinject.Assisted;
+import com.google.inject.assistedinject.AssistedInject;
/**
* This class is a provider of mailbox connector.
*/
-public class ConnectorProvider implements Provider {
+public class ConnectorProvider {
- @Inject
private ConnectorConfig configuration;
- @Inject
- @Named(EmailConfigKeys.EMAIL_SERVER_PROTOCOL)
private String protocol;
- @Override
+ @AssistedInject
+ public ConnectorProvider(@Assisted String id, EmailConfigFactory emailConfigFactory) {
+ EmailConfig emailConfig = emailConfigFactory.create(id);
+ this.configuration = new ConnectorConfig(emailConfig);
+ this.protocol = emailConfig.getParameter(EmailConfigKeys.EMAIL_SERVER_PROTOCOL);
+ }
+
public Connector get() {
ConnectorType type = ConnectorType.valueOf(protocol.trim().toUpperCase());
return type.getInstance(configuration);
diff --git a/bb-email/src/main/java/com/cognifide/qa/bb/email/constants/EmailConfigKeys.java b/bb-email/src/main/java/com/cognifide/qa/bb/email/constants/EmailConfigKeys.java
index 6c00a020..9ff92264 100644
--- a/bb-email/src/main/java/com/cognifide/qa/bb/email/constants/EmailConfigKeys.java
+++ b/bb-email/src/main/java/com/cognifide/qa/bb/email/constants/EmailConfigKeys.java
@@ -21,26 +21,31 @@
public final class EmailConfigKeys {
- public static final String EMAIL_SERVER_ADDRESS = "email.server.address";
+ public static final String EMAIL_SERVER_ADDRESS = "email.${id}.server.address";
- public static final String EMAIL_SERVER_PORT = "email.server.port";
+ public static final String EMAIL_SERVER_PORT = "email.${id}.server.port";
- public static final String EMAIL_SERVER_PROTOCOL = "email.server.protocol";
+ public static final String EMAIL_SERVER_PROTOCOL = "email.${id}.server.protocol";
- public static final String EMAIL_USERNAME = "email.username";
+ public static final String EMAIL_USERNAME = "email.${id}.username";
- public static final String EMAIL_PASSWORD = "email.password";
+ public static final String EMAIL_PASSWORD = "email.${id}.password";
- public static final String EMAIL_FOLDER_NAME = "email.folder.name";
+ public static final String EMAIL_ADDRESS = "email.${id}.address";
- public static final String EMAIL_ADDRESS_PATTERN = "email.address.pattern";
+ public static final String EMAIL_FOLDER_NAME = "email.${id}.folder.name";
- public static final String SMTP_SERVER_SECURE = "smtp.server.secure";
+ public static final String EMAIL_ADDRESS_PATTERN = "email.${id}.address.pattern";
- public static final String SMTP_SERVER_PORT = "smtp.server.port";
+ public static final String SMTP_SERVER_SECURE = "smtp.${id}.server.secure";
- public static final String SMTP_SERVER_ADDRESS = "smtp.server.address";
+ public static final String SMTP_SERVER_PORT = "smtp.${id}.server.port";
- private EmailConfigKeys() {}
+ public static final String SMTP_SERVER_ADDRESS = "smtp.${id}.server.address";
+
+ public static final String ID = "id";
+
+ private EmailConfigKeys() {
+ }
}
diff --git a/bb-email/src/test/java/com/cognifide/qa/bb/email/EmailTest.java b/bb-email/src/test/java/com/cognifide/qa/bb/email/EmailTest.java
index 66d199aa..c30dbb7a 100644
--- a/bb-email/src/test/java/com/cognifide/qa/bb/email/EmailTest.java
+++ b/bb-email/src/test/java/com/cognifide/qa/bb/email/EmailTest.java
@@ -33,46 +33,52 @@
import org.junit.runners.Parameterized.Parameters;
import com.cognifide.qa.bb.email.helpers.EmailDataGenerator;
+import com.cognifide.qa.bb.email.helpers.EmailDataGeneratorFactory;
import com.cognifide.qa.bb.module.PropertiesLoaderModule;
import com.google.inject.Guice;
-import com.google.inject.Inject;
import com.google.inject.Injector;
+import com.google.inject.assistedinject.FactoryModuleBuilder;
@RunWith(Parameterized.class)
public class EmailTest {
- private final String propertiesFileName;
+ private final String mailId;
- @Inject
private EmailClient client;
- @Inject
private EmailSender sender;
- @Inject
private EmailDataGenerator dataGenerator;
- @Inject
private MailServer mailServer;
- public EmailTest(String propertiesFileName) {
- this.propertiesFileName = propertiesFileName;
+
+ public EmailTest(String mailId) {
+ this.mailId = mailId;
}
@Parameters
public static Collection