You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/04/21 03:57:13 UTC
[14/17] james-project git commit: JAMES-2004 Adding tests for default
configuration
JAMES-2004 Adding tests for default configuration
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/50c481d6
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/50c481d6
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/50c481d6
Branch: refs/heads/master
Commit: 50c481d60ec08758af630fa881c7f52de20158b1
Parents: e0f2a4c
Author: benwa <bt...@linagora.com>
Authored: Tue Apr 18 15:07:20 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Apr 21 08:29:34 2017 +0700
----------------------------------------------------------------------
.../james/DefaultCassandraJamesServerTest.java | 59 +++++++++
.../java/org/apache/james/GuiceJamesServer.java | 7 ++
.../james/utils/FailingPropertiesProvider.java | 41 +++++++
.../james/DefaultMemoryJamesServerTest.java | 59 +++++++++
.../org/apache/james/GuiceJamesServerTest.java | 121 +++++++++++++++++++
5 files changed, 287 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/50c481d6/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DefaultCassandraJamesServerTest.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DefaultCassandraJamesServerTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DefaultCassandraJamesServerTest.java
new file mode 100644
index 0000000..4c41e12
--- /dev/null
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DefaultCassandraJamesServerTest.java
@@ -0,0 +1,59 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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. *
+ ****************************************************************/
+
+package org.apache.james;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.james.utils.ConfigurationProvider;
+import org.apache.james.utils.FailingPropertiesProvider;
+import org.apache.james.utils.PropertiesProvider;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class DefaultCassandraJamesServerTest {
+
+ @Rule
+ public CassandraJmapTestRule cassandraJmap = CassandraJmapTestRule.defaultTestRule();
+
+ private GuiceJamesServer guiceJamesServer;
+
+ @Before
+ public void setUp() {
+ guiceJamesServer = cassandraJmap.jmapServer()
+ .overrideWith(binder -> binder.bind(PropertiesProvider.class).to(FailingPropertiesProvider.class))
+ .overrideWith(binder -> binder.bind(ConfigurationProvider.class).toInstance(s -> new HierarchicalConfiguration()));
+ }
+
+ @After
+ public void clean() {
+ guiceJamesServer.stop();
+ }
+
+ @Test
+ public void memoryJamesServerShouldStartWithNoConfigurationFile() throws Exception {
+ guiceJamesServer.start();
+
+ assertThat(guiceJamesServer.isStarted()).isTrue();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/50c481d6/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceJamesServer.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceJamesServer.java b/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceJamesServer.java
index b9a5bd0..075f5c4 100644
--- a/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceJamesServer.java
+++ b/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceJamesServer.java
@@ -42,6 +42,7 @@ public class GuiceJamesServer {
protected final Module module;
private Stager<PreDestroy> preDestroy;
private GuiceProbeProvider guiceProbeProvider;
+ private boolean isStarted = false;
public GuiceJamesServer() {
this(Modules.combine(
@@ -66,14 +67,20 @@ public class GuiceJamesServer {
preDestroy = injector.getInstance(Key.get(new TypeLiteral<Stager<PreDestroy>>() {}));
injector.getInstance(ConfigurationsPerformer.class).initModules();
guiceProbeProvider = injector.getInstance(GuiceProbeProvider.class);
+ isStarted = true;
}
public void stop() {
if (preDestroy != null) {
preDestroy.stage();
+ isStarted = false;
}
}
+ public boolean isStarted() {
+ return isStarted;
+ }
+
public <T extends GuiceProbe> T getProbe(Class<T> probe) {
return guiceProbeProvider.getProbe(probe);
}
http://git-wip-us.apache.org/repos/asf/james-project/blob/50c481d6/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FailingPropertiesProvider.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FailingPropertiesProvider.java b/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FailingPropertiesProvider.java
new file mode 100644
index 0000000..c134952
--- /dev/null
+++ b/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FailingPropertiesProvider.java
@@ -0,0 +1,41 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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. *
+ ****************************************************************/
+
+package org.apache.james.utils;
+
+import java.io.FileNotFoundException;
+
+import javax.inject.Inject;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.james.filesystem.api.FileSystem;
+
+public class FailingPropertiesProvider extends PropertiesProvider {
+
+ @Inject
+ public FailingPropertiesProvider(FileSystem fileSystem) {
+ super(fileSystem);
+ }
+
+ @Override
+ public PropertiesConfiguration getConfiguration(String fileName) throws FileNotFoundException, ConfigurationException {
+ throw new FileNotFoundException();
+ }
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/50c481d6/server/container/guice/memory-guice/src/test/java/org/apache/james/DefaultMemoryJamesServerTest.java
----------------------------------------------------------------------
diff --git a/server/container/guice/memory-guice/src/test/java/org/apache/james/DefaultMemoryJamesServerTest.java b/server/container/guice/memory-guice/src/test/java/org/apache/james/DefaultMemoryJamesServerTest.java
new file mode 100644
index 0000000..ec90ac2
--- /dev/null
+++ b/server/container/guice/memory-guice/src/test/java/org/apache/james/DefaultMemoryJamesServerTest.java
@@ -0,0 +1,59 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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. *
+ ****************************************************************/
+
+package org.apache.james;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.james.utils.ConfigurationProvider;
+import org.apache.james.utils.FailingPropertiesProvider;
+import org.apache.james.utils.PropertiesProvider;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class DefaultMemoryJamesServerTest {
+
+ @Rule
+ public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule();
+
+ private GuiceJamesServer guiceJamesServer;
+
+ @Before
+ public void setUp() {
+ guiceJamesServer = memoryJmap.jmapServer()
+ .overrideWith(binder -> binder.bind(PropertiesProvider.class).to(FailingPropertiesProvider.class))
+ .overrideWith(binder -> binder.bind(ConfigurationProvider.class).toInstance(s -> new HierarchicalConfiguration()));
+ }
+
+ @After
+ public void clean() {
+ guiceJamesServer.stop();
+ }
+
+ @Test
+ public void memoryJamesServerShouldStartWithNoConfigurationFile() throws Exception {
+ guiceJamesServer.start();
+
+ assertThat(guiceJamesServer.isStarted()).isTrue();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/50c481d6/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceJamesServerTest.java
----------------------------------------------------------------------
diff --git a/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceJamesServerTest.java b/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceJamesServerTest.java
new file mode 100644
index 0000000..59327bb
--- /dev/null
+++ b/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceJamesServerTest.java
@@ -0,0 +1,121 @@
+package org.apache.james;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.List;
+
+import org.apache.james.lifecycle.api.Configurable;
+import org.apache.james.utils.ConfigurationPerformer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import com.google.common.collect.ImmutableList;
+import com.google.inject.multibindings.Multibinder;
+
+public class GuiceJamesServerTest {
+
+ public static final ConfigurationPerformer THROWING_CONFIGURATION_PERFORMER = new ConfigurationPerformer() {
+ @Override
+ public void initModule() {
+ throw new RuntimeException();
+ }
+
+ @Override
+ public List<Class<? extends Configurable>> forClasses() {
+ return ImmutableList.of();
+ }
+ };
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Rule
+ public MemoryJmapTestRule memoryJmapTestRule = new MemoryJmapTestRule();
+ private GuiceJamesServer guiceJamesServer;
+
+
+ @Before
+ public void setUp() {
+ guiceJamesServer = memoryJmapTestRule.jmapServer();
+ }
+
+ @After
+ public void tearDown() {
+ guiceJamesServer.stop();
+ }
+
+ @Test
+ public void serverShouldBeStartedAfterCallingStart() throws Exception {
+ guiceJamesServer.start();
+
+ assertThat(guiceJamesServer.isStarted()).isTrue();
+ }
+
+ @Test
+ public void serverShouldNotBeStartedAfterCallingStop() throws Exception {
+ guiceJamesServer.start();
+
+ guiceJamesServer.stop();
+
+ assertThat(guiceJamesServer.isStarted()).isFalse();
+ }
+
+ @Test
+ public void serverShouldNotBeStartedBeforeCallingStart() throws Exception {
+ assertThat(guiceJamesServer.isStarted()).isFalse();
+ }
+
+ @Test
+ public void serverShouldPropagateUncaughtConfigurationException() throws Exception {
+ expectedException.expect(RuntimeException.class);
+
+ GuiceJamesServer overWrittenServer = null;
+
+ try {
+ overWrittenServer = this.guiceJamesServer.overrideWith(
+ binder -> Multibinder.newSetBinder(binder, ConfigurationPerformer.class).addBinding().toInstance(
+ new ConfigurationPerformer() {
+ @Override
+ public void initModule() {
+ throw new RuntimeException();
+ }
+
+ @Override
+ public List<Class<? extends Configurable>> forClasses() {
+ return ImmutableList.of();
+ }
+ }
+ )
+ );
+ overWrittenServer.start();
+ } finally {
+ if (overWrittenServer != null) {
+ overWrittenServer.stop();
+ }
+ }
+ }
+
+ @Test
+ public void serverShouldNotBeStartedOnUncaughtException() throws Exception {
+ GuiceJamesServer overWrittenServer = null;
+
+ try {
+ overWrittenServer = this.guiceJamesServer.overrideWith(
+ binder -> Multibinder.newSetBinder(binder, ConfigurationPerformer.class)
+ .addBinding()
+ .toInstance(THROWING_CONFIGURATION_PERFORMER));
+
+ try {
+ overWrittenServer.start();
+ } catch (RuntimeException e) {}
+
+ assertThat(overWrittenServer.isStarted()).isFalse();
+ } finally {
+ if (overWrittenServer != null) {
+ overWrittenServer.stop();
+ }
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org