/**
 * Copyright 2009-2015 the original author or authors.
 * <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.apache.ibatis.submitted.cglib_lazy_error;

import java.io.Reader;
import java.sql.Connection;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class CglibNPELazyTest {

    private static SqlSessionFactory sqlSessionFactory;

    @BeforeClass
    public static void initDatabase() throws Exception {
        // create a SqlSessionFactory
        Reader reader = Resources
                .getResourceAsReader("org/apache/ibatis/submitted/cglib_lazy_error/ibatisConfigLazy.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        reader.close();

        // populate in-memory database
        SqlSession session = sqlSessionFactory.openSession();
        Connection conn = session.getConnection();
        reader = Resources
                .getResourceAsReader("org/apache/ibatis/submitted/cglib_lazy_error/CreateDB.sql");
        ScriptRunner runner = new ScriptRunner(conn);
        runner.setLogWriter(null);
        runner.runScript(reader);
        reader.close();
        session.close();
    }

    @Test
    public void testNoParent() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person person = personMapper.selectById(1);
        Assert.assertNotNull("Persons must not be null", person);
        Person parent = person.getParent();
        Assert.assertNull("Parent must be null", parent);
        sqlSession.close();
    }

    @Test
    public void testAncestorSelf() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person person = personMapper.selectById(1);
        Assert.assertNotNull("Persons must not be null", person);
        Person ancestor = person.getAncestor();
        Assert.assertEquals("Ancestor must be John Smith sr.", person, ancestor);
        sqlSession.close();
    }

    @Test
    public void testAncestorAfterQueryingParents() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person expectedAncestor = personMapper.selectById(1);
        Person person = personMapper.selectById(3);
        // Load ancestor indirectly.
        Assert.assertNotNull("Persons must not be null", person);
        Assert.assertNotNull("Parent must not be null", person.getParent());
        Assert.assertNotNull("Grandparent must not be null", person.getParent().getParent());
        Assert.assertEquals("Ancestor must be John Smith sr.", expectedAncestor, person.getAncestor());
        sqlSession.close();
    }

    @Test
    public void testGrandParent() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person expectedParent = personMapper.selectById(2);
        Person expectedGrandParent = personMapper.selectById(1);
        Person person = personMapper.selectById(3);
        Assert.assertNotNull("Persons must not be null", person);
        final Person actualParent = person.getParent();
        final Person actualGrandParent = person.getParent().getParent();
        Assert.assertEquals(expectedParent, actualParent);
        Assert.assertEquals(expectedGrandParent, actualGrandParent);
        sqlSession.close();
    }

    @Test
    public void testAncestor() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person expectedAncestor = personMapper.selectById(1);
        Person person = personMapper.selectById(3);
        Assert.assertNotNull("Persons must not be null", person);
        final Person actualAncestor = person.getAncestor();
        Assert.assertEquals(expectedAncestor, actualAncestor);
        sqlSession.close();
    }

}

@@ -1,14 +1,17 @@
 /**%0A *
+   
  Copyrig
@@ -57,24 +57,22 @@
 hors.%0A *
- %3Cp/%3E
 %0A *
+   
  License
@@ -130,16 +130,19 @@
 se%22);%0A *
+   
  you may
@@ -201,16 +201,19 @@
 ense.%0A *
+   
  You may
@@ -247,24 +247,25 @@
 se at%0A *
- %3Cp/%3E%0A *
+%0A *      
  http://
@@ -306,16 +306,14 @@
 0%0A *
- %3Cp/%3E
 %0A *
+   
  Unl
@@ -379,16 +379,19 @@
 tware%0A *
+   
  distrib
@@ -451,16 +451,19 @@
 ASIS,%0A *
+   
  WITHOUT
@@ -530,16 +530,19 @@
 lied.%0A *
+   
  See the
@@ -604,16 +604,19 @@
 s and%0A *
+   
  limitat

add missed file.