polishing MongoDB availability rule code

This commit is contained in:
Mark Fisher
2011-08-02 17:08:01 -04:00
parent 950c05e6ab
commit ca1fa6b727
5 changed files with 41 additions and 25 deletions

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.mongodb.rules;
import java.lang.annotation.ElementType;
@@ -21,11 +22,13 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation used for any test method that requires a running MongoDb process.
*
* @author Oleg Zhurakousky
*
* @since 2.1
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MongodbAvailable {
public @interface MongoDbAvailable {
}

View File

@@ -13,8 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.mongodb.rules;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
@@ -22,30 +26,34 @@ import org.junit.runners.model.Statement;
import com.mongodb.Mongo;
/**
* A {@link MethodRule} implementation that checks for a running MongoDB process.
*
* @author Oleg Zhurakousky
*
* @since 2.1
*/
public final class MongodbAvailableRule implements MethodRule{
public final class MongoDbAvailableRule implements MethodRule {
public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
return new Statement(){
private final Log logger = LogFactory.getLog(this.getClass());
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
MongodbAvailable redisAvailable = method.getAnnotation(MongodbAvailable.class);
if (redisAvailable != null){
MongoDbAvailable redisAvailable = method.getAnnotation(MongoDbAvailable.class);
if (redisAvailable != null) {
try {
Mongo mongo = new Mongo();
mongo.getDatabaseNames();
} catch (Exception e) {
System.out.println("Mongodb is not available. Skipping the test.");
}
catch (Exception e) {
logger.warn("MongoDb is not available. Skipping the test: " +
target.getClass().getSimpleName() + "." + method.getName() + "()");
return;
}
}
base.evaluate();
}
}
};
}
}

View File

@@ -13,15 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.mongodb.rules;
import org.junit.Rule;
/**
* Convenience base class that enables unit test methods to rely upon the {@link MongoDbAvailable} annotation.
*
* @author Oleg Zhurakousky
*
* @since 2.1
*/
public class MongodbAvailableTests {
public abstract class MongoDbAvailableTests {
@Rule
public MongodbAvailableRule redisAvailableRule = new MongodbAvailableRule();
public MongoDbAvailableRule redisAvailableRule = new MongoDbAvailableRule();
}

View File

@@ -20,8 +20,8 @@ import org.junit.Test;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.integration.Message;
import org.springframework.integration.mongodb.rules.MongodbAvailable;
import org.springframework.integration.mongodb.rules.MongodbAvailableTests;
import org.springframework.integration.mongodb.rules.MongoDbAvailable;
import org.springframework.integration.mongodb.rules.MongoDbAvailableTests;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.transformer.ClaimCheckInTransformer;
import org.springframework.integration.transformer.ClaimCheckOutTransformer;
@@ -33,10 +33,10 @@ import static org.junit.Assert.assertEquals;
/**
* @author Mark Fisher
*/
public class MongoClaimCheckIntegrationTests extends MongodbAvailableTests{
public class MongoClaimCheckIntegrationTests extends MongoDbAvailableTests{
@Test
@MongodbAvailable
@MongoDbAvailable
public void stringPayload() throws Exception {
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new Mongo(), "test");
MongoMessageStore messageStore = new MongoMessageStore(mongoDbFactory);
@@ -55,7 +55,7 @@ public class MongoClaimCheckIntegrationTests extends MongodbAvailableTests{
}
@Test
@MongodbAvailable
@MongoDbAvailable
public void objectPayload() throws Exception {
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new Mongo(), "test");
MongoMessageStore messageStore = new MongoMessageStore(mongoDbFactory);

View File

@@ -21,8 +21,8 @@ import org.junit.Test;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.integration.Message;
import org.springframework.integration.mongodb.rules.MongodbAvailable;
import org.springframework.integration.mongodb.rules.MongodbAvailableTests;
import org.springframework.integration.mongodb.rules.MongoDbAvailable;
import org.springframework.integration.mongodb.rules.MongoDbAvailableTests;
import org.springframework.integration.support.MessageBuilder;
import com.mongodb.Mongo;
@@ -33,10 +33,10 @@ import static org.junit.Assert.assertNotNull;
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class MongoMessageStoreTests extends MongodbAvailableTests{
public class MongoMessageStoreTests extends MongoDbAvailableTests{
@Test
@MongodbAvailable
@MongoDbAvailable
public void addGetWithStringPayload() throws Exception {
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new Mongo(), "test");
MongoMessageStore store = new MongoMessageStore(mongoDbFactory);
@@ -50,7 +50,7 @@ public class MongoMessageStoreTests extends MongodbAvailableTests{
@Test
@MongodbAvailable
@MongoDbAvailable
public void addGetWithObjectDefaultConstructorPayload() throws Exception {
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new Mongo(), "test");
MongoMessageStore store = new MongoMessageStore(mongoDbFactory);