Browse Source

Hacking

issue/DATAMONGO-2347
Christoph Strobl 6 years ago
parent
commit
1a39010ac2
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  2. 139
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java
  3. 60
      spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/convert/Entities.kt

2
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

@ -252,6 +252,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App @@ -252,6 +252,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(typeToUse);
// hacking - use below will allow kotlin test to pass
// MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(rawType);
if (entity == null) {
throw new MappingException(String.format(INVALID_TYPE_TO_READ, target, typeToUse.getType()));
}

139
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

@ -29,6 +29,7 @@ import java.net.URL; @@ -29,6 +29,7 @@ import java.net.URL;
import java.time.LocalDateTime;
import java.util.*;
import org.bson.json.JsonWriterSettings;
import org.bson.types.Code;
import org.bson.types.Decimal128;
import org.bson.types.ObjectId;
@ -60,12 +61,14 @@ import org.springframework.data.geo.Point; @@ -60,12 +61,14 @@ import org.springframework.data.geo.Point;
import org.springframework.data.geo.Polygon;
import org.springframework.data.geo.Shape;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.model.MappingInstantiationException;
import org.springframework.data.mongodb.core.DocumentTestUtils;
import org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.NestedType;
import org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.ProjectingType;
import org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.ClassWithMapUsingEnumAsKey.FooBarEnum;
import org.springframework.data.mongodb.core.geo.Sphere;
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.FieldType;
@ -74,6 +77,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; @@ -74,6 +77,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.mapping.PersonPojoStringId;
import org.springframework.data.mongodb.core.mapping.TextScore;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.BasicDBList;
@ -2529,4 +2533,139 @@ public class MappingMongoConverterUnitTests { @@ -2529,4 +2533,139 @@ public class MappingMongoConverterUnitTests {
Date dateAsObjectId;
}
static class Model<EVT, ID> {
private final @Id Long id;
private final EVT event;
public Model(Long id, EVT event) {
this.id = id;
this.event = event;
}
@Override
public String toString() {
return "Model{" + "id=" + id + ", event=" + event + '}';
}
}
static abstract class DomainEvent<EVT, REF_ID> {
private final REF_ID aggregateId;
private final EVT root;
DomainEvent(REF_ID aggregateId, EVT root) {
this.aggregateId = aggregateId;
this.root = root;
}
public EVT getRoot() {
return root;
}
public REF_ID getAggregateId() {
return aggregateId;
}
}
final static class ConcreteEvent extends DomainEvent<Details, Long> {
ConcreteEvent(Long aggregateId, Details root) {
super(aggregateId, root);
}
@Override
public String toString() {
return "ConcreteEvent{" + "id=" + getAggregateId() + ", root=" + getRoot() + '}';
}
}
final static class Details {
private final String value;
public Details(String value) {
this.value = value;
}
@Override
public String toString() {
return "Details{" + "value='" + value + '\'' + '}';
}
}
@Test
public void javaReadWithGenerics() {
Model model = new Model(1L, new ConcreteEvent(100L, new Details("details")));
org.bson.Document target = new org.bson.Document();
converter.write(model, target);
System.out.println("target: " + target.toJson(JsonWriterSettings.builder().indent(true).build()));
Model back = converter.read(Model.class, target);
System.out.println("back: " + back);
}
@Test
public void kotlinReadWithGenerics() {
StoredEvent event = new StoredEvent(1L, new OfferCreated(100L, new OfferDetails("details")));
org.bson.Document target = new org.bson.Document();
converter.write(event, target);
System.out.println("target: " + target.toJson(JsonWriterSettings.builder().indent(true).build()));
// KotlinClassGeneratingEntityInstantiator kci = new KotlinClassGeneratingEntityInstantiator().createInstance()
StoredEvent back = converter.read(StoredEvent.class, target);
System.out.println("back: " + back);
// OfferCreated
}
// @Test
// public void cti() {
//
// ClassTypeInformation<ConcreteEvent> javaInfo = ClassTypeInformation.from(ConcreteEvent.class);
// System.out.println("javaInfo: " + javaInfo);
// TypeInformation<?> javaProperty = javaInfo.getProperty("root");
// System.out.println("javaProperty: " + javaProperty.getType());
//
// ClassTypeInformation<OfferCreated> kotlinInfo = ClassTypeInformation.from(OfferCreated.class);
// System.out.println("kotlinInfo: " + kotlinInfo);
// TypeInformation<?> kotlinProperty = kotlinInfo.getProperty("root");
// System.out.println("kotlinProperty: " + kotlinProperty.getType());
//
// MongoMappingContext mappingContext = new MongoMappingContext();
// mappingContext.afterPropertiesSet();
//
// BasicMongoPersistentEntity<?> mpe = mappingContext.getPersistentEntity(OfferCreated.class);
//
// PersistentProperty mpp = mpe.getPersistentProperty("root");
// TypeInformation ti = mpp.getTypeInformation();
//
// BasicMongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(ti);
// System.out.println("entity type: " + entity.getType());
// System.out.println("ti: " + ti);
// System.out.println("kotlinPersistentProperty: " + ti.getType());
//
// }
//
// @Test
// public void c4() {
//
//
// TypeInformation<? extends DomainEvent> cti = ClassTypeInformation.from(DomainEvent.class);
// TypeInformation<? extends DomainEvent> evt = cti.specialize(ClassTypeInformation.from(OfferCreated.class));
//
// TypeInformation<?> x = evt.getProperty("root");
// System.out.println("x.getType(): " + x.getType());
//
//
// }
}

60
spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/convert/Entities.kt

@ -0,0 +1,60 @@ @@ -0,0 +1,60 @@
/*
* Copyright 2019 the original author or authors.
*
* 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
*
* https://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.springframework.data.mongodb.core.convert
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
@Document(collection = "domain_events")
data class StoredEvent<AGGREGATE, ID>(@Id val sequenceNo: Long = 0, val event: DomainEvent<AGGREGATE, ID>? = null)
abstract class DomainEvent<AGGREGATE, ID>(val aggregateId: ID, val root: AGGREGATE) {
override fun toString(): String {
return "DomainEvent(aggregateId=$aggregateId, root=$root)"
}
}
class OfferCreated(aggregateId: Long, root: OfferDetails)
: DomainEvent<OfferDetails, Long>(aggregateId, root) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as OfferCreated
if (aggregateId != other.aggregateId) return false
if (root != other.root) return false
return true
}
override fun hashCode(): Int {
var result = aggregateId.hashCode()
result = 31 * result + root.hashCode()
return result
}
override fun toString(): String {
return "OfferCreated(aggregateId=$aggregateId, root=$root)"
}
}
data class OfferDetails(val name: String)
Loading…
Cancel
Save