/*
 *    Copyright 2010-2012 The MyBatis Team
 *
 *    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.
 */

/**
 * @version $Id: AbstractSampleTest.java 4871 2012-03-12 07:50:06Z eduardo.macarron@gmail.com $
 */
package org.mybatis.spring.sample;

import javax.sql.DataSource;

import junit.framework.Assert;

import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.sample.dao.UserDao;
import org.mybatis.spring.sample.dao.UserDaoImpl;
import org.mybatis.spring.sample.domain.User;
import org.mybatis.spring.sample.service.FooService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.transaction.PlatformTransactionManager;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class ConfigurationSampleTest {

  @Configuration
  static class ContextConfiguration {

    @Bean
    public FooService fooService() {
      FooService fooService = new FooService();
      fooService.setUserDao(userDao());
      return fooService;
    }

    @Bean
    public UserDao userDao() {
      UserDaoImpl userDao = new UserDaoImpl();
      userDao.setSqlSessionFactory(sqlSessionFactory());
      return userDao;
    }

    @Bean
    public PlatformTransactionManager transactionalManagerOne() {
      return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public DataSource dataSource() {
      return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
          .addScript("org/mybatis/spring/sample/db/database-schema.sql")
          .addScript("org/mybatis/spring/sample/db/database-test-data.sql")
          .build();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() {
      SqlSessionFactoryBean ss = new SqlSessionFactoryBean();
      ss.setDataSource(dataSource());
      ss.setMapperLocations(new Resource[] { new ClassPathResource(
          "org/mybatis/spring/sample/dao/UserDao.xml") });
      try {
        return (SqlSessionFactory) ss.getObject();
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
  }

  @Autowired
  private FooService fooService;

  @Test
  public void test() {
    User user = fooService.doSomeBusinessStuff("u1");
    Assert.assertEquals("Pocoyo", user.getName());
  }

}

@@ -20,17 +20,17 @@
 2010-201
-2
+3
  The MyB
@@ -632,16 +632,60 @@
 */%0A%0A/**%0A
+ * MyBatis @Configuration style sample %0A * %0A
  * @vers
@@ -846,40 +846,8 @@
 e;%0A%0A
-import junit.framework.Assert;%0A%0A
 impo
@@ -894,16 +894,41 @@
 actory;%0A
+import org.junit.Assert;%0A
 import o
@@ -1050,34 +1050,40 @@
 .spring.
-sample.dao.UserDao
+mapper.MapperFactoryBean
 ;%0Aimport
@@ -1120,20 +1120,16 @@
 .UserDao
-Impl
 ;%0Aimport
@@ -2245,24 +2245,41 @@
 fooService()
+ throws Exception
  %7B%0A      Foo
@@ -2393,182 +2393,8 @@
  %7D%0A%0A
-    @Bean%0A    public UserDao userDao() %7B%0A      UserDaoImpl userDao = new UserDaoImpl();%0A      userDao.setSqlSessionFactory(sqlSessionFactory());%0A      return userDao;%0A    %7D%0A%0A
     
@@ -2622,16 +2622,27 @@
 uilder()
+%0A          
 .setType
@@ -2903,16 +2903,33 @@
 actory()
+ throws Exception
  %7B%0A     
@@ -3090,27 +3090,16 @@
 esource(
-%0A          
 %22org/myb
@@ -3143,22 +3143,8 @@
 %7D);%0A
-      try %7B%0A  
     
@@ -3196,19 +3196,57 @@
     
-  %7D catch (
+%7D%0A%0A    @Bean%0A    public UserDao userDao() throws 
 Exce
@@ -3254,11 +3254,8 @@
 tion
- e)
  %7B%0A 
@@ -3263,48 +3263,304 @@
     
-  throw new RuntimeException(e);%0A      %7D
+MapperFactoryBean%3CUserDao%3E mapperFactoryBean = new MapperFactoryBean%3CUserDao%3E();%0A      mapperFactoryBean.setMapperInterface(UserDao.class);%0A      mapperFactoryBean.setSqlSessionFactory(sqlSessionFactory());%0A      mapperFactoryBean.afterPropertiesSet();%0A      return mapperFactoryBean.getObject();
 %0A   

include a mapper in the @Configuration sample