/**
 * Copyright (C) 2010 Olafur Gauti Gudmundsson
 *
 * 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
 *
 *         http://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 com.google.code.morphia;


import org.bson.types.ObjectId;
import org.junit.Test;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Index;
import com.google.code.morphia.annotations.Indexed;
import com.google.code.morphia.annotations.Indexes;
import com.google.code.morphia.mapping.MappedClass;
import com.mongodb.DBCollection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;


/**
 * @author Scott Hernandez
 */
public class TestIndexInheritance extends TestBase {

  @Indexes(@Index("description"))
  private abstract static class Shape {
    @Id ObjectId id;
    String description;
    @Indexed String foo;
  }

  @Indexes(@Index("radius"))
  private static class Circle extends Shape {
    double radius = 1;

    public Circle() {
      description = "Circles are round and can be rolled along the ground.";
    }
  }


  @Test
  public void testClassIndexInherit() throws Exception {
    morphia.map(Circle.class).map(Shape.class);
    final MappedClass mc = morphia.getMapper().getMappedClass(Circle.class);
    assertNotNull(mc);

    assertEquals(2, mc.getAnnotations(Indexes.class).size());

    ds.ensureIndexes();
    final DBCollection coll = ds.getCollection(Circle.class);

    assertEquals(4, coll.getIndexInfo().size());
  }

  @Test
  public void testInheritedFieldIndex() throws Exception {
    morphia.map(Circle.class).map(Shape.class);
    morphia.getMapper().getMappedClass(Circle.class);

    ds.ensureIndexes();
    final DBCollection coll = ds.getCollection(Circle.class);

    assertEquals(4, coll.getIndexInfo().size());
  }

}

@@ -696,20 +696,96 @@
 g.junit.
-Test
+Assert;%0Aimport org.junit.Test;%0Aimport com.google.code.morphia.annotations.Entity
 ;%0Aimport
@@ -1328,16 +1328,20 @@
 %0A    @Id
+%0A   
  ObjectI
@@ -1382,16 +1382,20 @@
 @Indexed
+%0A   
  String 
@@ -1613,16 +1613,536 @@
  %7D%0A  %7D%0A%0A
+  @Entity%0A  private static class Child extends Father %7B%0A    private Child() %7B%0A    %7D%0A%0A    private Child(final String name) %7B%0A      super(name);%0A    %7D%0A  %7D%0A%0A  @Entity%0A  private static class Father extends GrandFather %7B%0A    private Father() %7B%0A    %7D%0A%0A    private Father(final String name) %7B%0A      super(name);%0A    %7D%0A  %7D%0A%0A  @Entity%0A  private static class GrandFather %7B%0A    @Id%0A    ObjectId id;%0A    String name;%0A%0A    private GrandFather() %7B%0A    %7D%0A%0A    private GrandFather(final String name) %7B%0A      this.name = name;%0A    %7D%0A  %7D%0A
 %0A  @Test
@@ -2220,32 +2220,39 @@
 ap(Circle.class)
+%0A      
 .map(Shape.class
@@ -2292,32 +2292,39 @@
 phia.getMapper()
+%0A      
 .getMappedClass(
@@ -2414,16 +2414,23 @@
 s.class)
+%0A      
 .size())
@@ -2550,32 +2550,39 @@
 l.getIndexInfo()
+%0A      
 .size());%0A  %7D%0A%0A 
@@ -2676,16 +2676,23 @@
 e.class)
+%0A      
 .map(Sha
@@ -2725,16 +2725,23 @@
 Mapper()
+%0A      
 .getMapp
@@ -2890,16 +2890,23 @@
 exInfo()
+%0A      
 .size())
@@ -2912,10 +2912,254 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void deepTree() %7B%0A    final Child jimmy = new Child(%22Jimmy%22);%0A    ds.save(jimmy);%0A%0A    final Child loaded = ds.get(Child.class, jimmy.id);%0A    Assert.assertNotNull(loaded);%0A    Assert.assertEquals(jimmy.name, loaded.name);%0A  %7D%0A
 %7D%0A

test to validate and resolve #480