@ -82,6 +82,7 @@ import org.springframework.data.mongodb.core.index.IndexInfo;
import org.springframework.data.mongodb.core.mapping.Field ;
import org.springframework.data.mongodb.core.mapping.Field ;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext ;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext ;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener ;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener ;
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent ;
import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent ;
import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent ;
import org.springframework.data.mongodb.core.query.BasicQuery ;
import org.springframework.data.mongodb.core.query.BasicQuery ;
import org.springframework.data.mongodb.core.query.Criteria ;
import org.springframework.data.mongodb.core.query.Criteria ;
@ -118,6 +119,7 @@ import com.mongodb.client.result.UpdateResult;
* @author Komi Innocent
* @author Komi Innocent
* @author Christoph Strobl
* @author Christoph Strobl
* @author Mark Paluch
* @author Mark Paluch
* @author Laszlo Csontos
* /
* /
@RunWith ( SpringJUnit4ClassRunner . class )
@RunWith ( SpringJUnit4ClassRunner . class )
@ContextConfiguration ( "classpath:infrastructure.xml" )
@ContextConfiguration ( "classpath:infrastructure.xml" )
@ -132,13 +134,19 @@ public class MongoTemplateTests {
@Autowired MongoTemplate template ;
@Autowired MongoTemplate template ;
@Autowired MongoDbFactory factory ;
@Autowired MongoDbFactory factory ;
@Autowired ConfigurableApplicationContext context ;
ConfigurableApplicationContext context ;
MongoTemplate mappingTemplate ;
MongoTemplate mappingTemplate ;
org . springframework . data . util . Version mongoVersion ;
org . springframework . data . util . Version mongoVersion ;
@Rule public ExpectedException thrown = ExpectedException . none ( ) ;
@Rule public ExpectedException thrown = ExpectedException . none ( ) ;
@Autowired
public void setApplicationContext ( ConfigurableApplicationContext context ) {
context . addApplicationListener ( new PersonWithIdPropertyOfTypeUUIDListener ( ) ) ;
this . context = context ;
}
@Autowired
@Autowired
public void setMongo ( Mongo mongo ) throws Exception {
public void setMongo ( Mongo mongo ) throws Exception {
@ -150,7 +158,8 @@ public class MongoTemplateTests {
PersonWith_idPropertyOfTypeString . class , PersonWithIdPropertyOfTypeObjectId . class ,
PersonWith_idPropertyOfTypeString . class , PersonWithIdPropertyOfTypeObjectId . class ,
PersonWithIdPropertyOfTypeString . class , PersonWithIdPropertyOfTypeInteger . class ,
PersonWithIdPropertyOfTypeString . class , PersonWithIdPropertyOfTypeInteger . class ,
PersonWithIdPropertyOfTypeBigInteger . class , PersonWithIdPropertyOfPrimitiveInt . class ,
PersonWithIdPropertyOfTypeBigInteger . class , PersonWithIdPropertyOfPrimitiveInt . class ,
PersonWithIdPropertyOfTypeLong . class , PersonWithIdPropertyOfPrimitiveLong . class ) ) ) ;
PersonWithIdPropertyOfTypeLong . class , PersonWithIdPropertyOfPrimitiveLong . class ,
PersonWithIdPropertyOfTypeUUID . class ) ) ) ;
mappingContext . setSimpleTypeHolder ( conversions . getSimpleTypeHolder ( ) ) ;
mappingContext . setSimpleTypeHolder ( conversions . getSimpleTypeHolder ( ) ) ;
mappingContext . initialize ( ) ;
mappingContext . initialize ( ) ;
@ -193,6 +202,7 @@ public class MongoTemplateTests {
template . dropCollection ( PersonWithIdPropertyOfPrimitiveInt . class ) ;
template . dropCollection ( PersonWithIdPropertyOfPrimitiveInt . class ) ;
template . dropCollection ( PersonWithIdPropertyOfTypeLong . class ) ;
template . dropCollection ( PersonWithIdPropertyOfTypeLong . class ) ;
template . dropCollection ( PersonWithIdPropertyOfPrimitiveLong . class ) ;
template . dropCollection ( PersonWithIdPropertyOfPrimitiveLong . class ) ;
template . dropCollection ( PersonWithIdPropertyOfTypeUUID . class ) ;
template . dropCollection ( PersonWithVersionPropertyOfTypeInteger . class ) ;
template . dropCollection ( PersonWithVersionPropertyOfTypeInteger . class ) ;
template . dropCollection ( TestClass . class ) ;
template . dropCollection ( TestClass . class ) ;
template . dropCollection ( Sample . class ) ;
template . dropCollection ( Sample . class ) ;
@ -627,6 +637,23 @@ public class MongoTemplateTests {
assertThat ( p12q , notNullValue ( ) ) ;
assertThat ( p12q , notNullValue ( ) ) ;
assertThat ( p12q . getId ( ) , is ( p12 . getId ( ) ) ) ;
assertThat ( p12q . getId ( ) , is ( p12 . getId ( ) ) ) ;
checkCollectionContents ( PersonWithIdPropertyOfPrimitiveLong . class , 1 ) ;
checkCollectionContents ( PersonWithIdPropertyOfPrimitiveLong . class , 1 ) ;
// DATAMONGO-1617
// UUID id - provided
PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID ( ) ;
p13 . setFirstName ( "Sven_10" ) ;
p13 . setAge ( 22 ) ;
p13 . setId ( UUID . randomUUID ( ) ) ;
// insert
mongoTemplate . insert ( p13 ) ;
// also try save
mongoTemplate . save ( p13 ) ;
assertThat ( p13 . getId ( ) , notNullValue ( ) ) ;
PersonWithIdPropertyOfTypeUUID p13q = mongoTemplate . findOne ( new Query ( where ( "id" ) . in ( p13 . getId ( ) ) ) ,
PersonWithIdPropertyOfTypeUUID . class ) ;
assertThat ( p13q , notNullValue ( ) ) ;
assertThat ( p13q . getId ( ) , is ( p13 . getId ( ) ) ) ;
checkCollectionContents ( PersonWithIdPropertyOfTypeUUID . class , 1 ) ;
}
}
private void checkCollectionContents ( Class < ? > entityClass , int count ) {
private void checkCollectionContents ( Class < ? > entityClass , int count ) {
@ -1427,6 +1454,17 @@ public class MongoTemplateTests {
template . insert ( document , template . determineCollectionName ( PersonWithVersionPropertyOfTypeInteger . class ) ) ;
template . insert ( document , template . determineCollectionName ( PersonWithVersionPropertyOfTypeInteger . class ) ) ;
}
}
@Test // DATAMONGO-1617
public void doesNotFailOnInsertForEntityWithNonAutogeneratableId ( ) {
PersonWithIdPropertyOfTypeUUID person = new PersonWithIdPropertyOfTypeUUID ( ) ;
person . setFirstName ( "Laszlo" ) ;
person . setAge ( 33 ) ;
template . insert ( person ) ;
assertThat ( person . getId ( ) , is ( notNullValue ( ) ) ) ;
}
@Test // DATAMONGO-539
@Test // DATAMONGO-539
public void removesObjectFromExplicitCollection ( ) {
public void removesObjectFromExplicitCollection ( ) {
@ -3570,4 +3608,19 @@ public class MongoTemplateTests {
@Id String id ;
@Id String id ;
Object value ;
Object value ;
}
}
static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener < PersonWithIdPropertyOfTypeUUID > {
@Override
public void onBeforeConvert ( BeforeConvertEvent < PersonWithIdPropertyOfTypeUUID > event ) {
PersonWithIdPropertyOfTypeUUID person = event . getSource ( ) ;
if ( person . getId ( ) ! = null ) {
return ;
}
person . setId ( UUID . randomUUID ( ) ) ;
}
}
}
}