/**
* Copyright (C) 2010 Olafur Gauti Gudmundsson
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.mongodb.morphia.issue80;
import org.bson.types.ObjectId;
import org.junit.Test;
import org.mongodb.morphia.TestBase;
import org.mongodb.morphia.annotations.Embedded;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Property;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Scott Hernandez
*/
public class GenericsMappingTest extends TestBase {
@Test
public void testBoundGenerics() {
getMorphia().map(Element.class, AudioElement.class);
}
@Test
public void testIt() throws Exception {
getMorphia().map(HoldsAnInteger.class, HoldsAString.class, ContainsThings.class);
final ContainsThings ct = new ContainsThings();
final HoldsAnInteger hai = new HoldsAnInteger();
hai.setThing(7);
final HoldsAString has = new HoldsAString();
has.setThing("tr");
ct.stringThing = has;
ct.integerThing = hai;
getDs().save(ct);
assertNotNull(ct.id);
assertEquals(1, getDs().getCount(ContainsThings.class));
final ContainsThings ctLoaded = getDs().find(ContainsThings.class).get();
assertNotNull(ctLoaded);
assertNotNull(ctLoaded.id);
assertNotNull(ctLoaded.stringThing);
assertNotNull(ctLoaded.integerThing);
}
public static class GenericHolder<T> {
@Property
private T thing;
public T getThing() {
return thing;
}
public void setThing(final T thing) {
this.thing = thing;
}
}
@Embedded
static class HoldsAString extends GenericHolder<String> {
}
@Embedded
static class HoldsAnInteger extends GenericHolder<Integer> {
}
@Entity
static class ContainsThings {
@Id
private String id;
private HoldsAString stringThing;
private HoldsAnInteger integerThing;
}
public abstract static class Element<T extends Number> {
@Id
private ObjectId id;
private T[] resources;
}
public static class AudioElement extends Element<Long> {
}
}