Skip to content

Commit

Permalink
OGM-810 Raise exception when WriteConcernType.ERRORS_IGNORED is used
Browse files Browse the repository at this point in the history
A better alternative is to remove the option completely if we have an
intermediary version deprecating it.
  • Loading branch information
emmanuelbernard authored and gunnarmorling committed Aug 21, 2015
1 parent 08266d9 commit 9ac6f00
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
Expand Up @@ -103,4 +103,7 @@ public interface Log extends org.hibernate.ogm.util.impl.Log {
+ " Please change name for '%s', for example by using @Column ")
MappingException fieldNameContainsNULCharacter(String fieldName);

@Message(id = 1225, value = "This WriteConcern has been deprecated or removed by MongoDB: %s")
HibernateException writeConcernDeprecated(String writeConcern);

}
Expand Up @@ -8,6 +8,9 @@

import com.mongodb.WriteConcern;

import org.hibernate.ogm.datastore.mongodb.logging.impl.Log;
import org.hibernate.ogm.datastore.mongodb.logging.impl.LoggerFactory;

/**
* Write concern options for MongoDB. Represents the non-deprecated constants from {@link WriteConcern}.
*
Expand All @@ -25,7 +28,7 @@ public enum WriteConcernType {
* @deprecated This WriteConcern is no longer supported by MongoDB
*/
@Deprecated
ERRORS_IGNORED(WriteConcern.ERRORS_IGNORED),
ERRORS_IGNORED(null),

/**
* Write operations that use this write concern will wait for acknowledgement from the primary server before
Expand Down Expand Up @@ -67,9 +70,11 @@ public enum WriteConcernType {
*/
CUSTOM( null );

private static Log log = LoggerFactory.getLogger();

private final WriteConcern writeConcern;

private WriteConcernType(WriteConcern writeConcern) {
WriteConcernType(WriteConcern writeConcern) {
this.writeConcern = writeConcern;
}

Expand All @@ -79,6 +84,11 @@ private WriteConcernType(WriteConcern writeConcern) {
* @return the {@link WriteConcern} associated with this enum value; {@code null} in the case of {@link #CUSTOM}.
*/
public WriteConcern getWriteConcern() {
if ( this.name().equals( "ERRORS_IGNORED" ) ) {
// Do a hard fail as we don't have a proper replacement
// Looks like the nicest thing to do to the user
throw log.writeConcernDeprecated( "ERRORS_IGNORED" );
}
return writeConcern;
}
}
Expand Up @@ -109,7 +109,7 @@ private OptionsContext getGlobalOptions() {
public static class MultipleDataCenters extends com.mongodb.WriteConcern {

public MultipleDataCenters() {
super( "MultipleDataCenters", 0, false, true, false );
super( "MultipleDataCenters" );
}
}
}
Expand Up @@ -31,7 +31,7 @@ public void setupBuilder() {
@Test
public void testWriteConcernForEntity() throws Exception {
OptionsContainer options = source.getEntityOptions( EntityWriteConcernExample.class );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( com.mongodb.WriteConcern.ERRORS_IGNORED );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( com.mongodb.WriteConcern.REPLICA_ACKNOWLEDGED );
}

@Test
Expand All @@ -40,7 +40,7 @@ public void testWriteConcernByTypeForEntity() throws Exception {
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( new MultipleDataCenters() );
}

@WriteConcern(WriteConcernType.ERRORS_IGNORED)
@WriteConcern(WriteConcernType.REPLICA_ACKNOWLEDGED)
private static final class EntityWriteConcernExample {
}

Expand All @@ -52,7 +52,7 @@ private static final class EntityWriteConcernUsingTypeExample {
public static class MultipleDataCenters extends com.mongodb.WriteConcern {

public MultipleDataCenters() {
super( "MultipleDataCenters", 0, false, true, false );
super( "MultipleDataCenters" );
}
}
}
Expand Up @@ -43,10 +43,10 @@ public void setupBuilder() {
@Test
public void testWriteConcernGivenByTypeOnGlobalLevel() throws Exception {
mongoOptions
.writeConcern( WriteConcernType.ERRORS_IGNORED );
.writeConcern( WriteConcernType.REPLICA_ACKNOWLEDGED );

OptionsContainer options = getSource().getGlobalOptions();
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.ERRORS_IGNORED );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.REPLICA_ACKNOWLEDGED );
}

@Test
Expand All @@ -62,14 +62,14 @@ public void testWriteConcernGivenByInstanceOnGlobalLevel() throws Exception {
@Test
public void testWriteConcernGivenByTypePriority() throws Exception {
mongoOptions
.writeConcern( WriteConcernType.ERRORS_IGNORED )
.writeConcern( WriteConcernType.REPLICA_ACKNOWLEDGED )
.entity( ExampleForMongoDBMapping.class )
.writeConcern( WriteConcernType.MAJORITY )
.property( "content", ElementType.FIELD )
.writeConcern( WriteConcernType.FSYNCED );

OptionsContainer options = getSource().getGlobalOptions();
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.ERRORS_IGNORED );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.REPLICA_ACKNOWLEDGED );

options = getSource().getEntityOptions( ExampleForMongoDBMapping.class );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.MAJORITY );
Expand Down Expand Up @@ -135,7 +135,7 @@ private static final class ExampleForMongoDBMapping {
private static class ReplicaConfigurableWriteConcern extends WriteConcern {

public ReplicaConfigurableWriteConcern(int numberOfRequiredReplicas) {
super( numberOfRequiredReplicas, 0, false, true, false );
super( numberOfRequiredReplicas, 0, false, true );
}
}
}

0 comments on commit 9ac6f00

Please sign in to comment.