View Javadoc
1   /*
2    *    Copyright 2009-2021 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.submitted.association_nested;
17  
18  import java.io.InputStream;
19  import java.sql.Connection;
20  import java.sql.DriverManager;
21  import java.sql.Statement;
22  import java.util.List;
23  
24  import org.apache.ibatis.io.Resources;
25  import org.apache.ibatis.session.SqlSession;
26  import org.apache.ibatis.session.SqlSessionFactory;
27  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28  import org.junit.jupiter.api.Assertions;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * @author Loïc Guerrin <guerrin@fullsix.com>
33   */
34  class FolderMapperTest {
35  
36    @Test
37    void testFindWithChildren() throws Exception {
38      try (Connection conn = DriverManager.getConnection("jdbc:hsqldb:mem:association_nested", "SA", "");
39           Statement stmt = conn.createStatement()) {
40        stmt.execute("create table folder (id int, name varchar(100), parent_id int)");
41        stmt.execute("insert into folder (id, name) values(1, 'Root')");
42        stmt.execute("insert into folder values(2, 'Folder 1', 1)");
43        stmt.execute("insert into folder values(3, 'Folder 2', 1)");
44        stmt.execute("insert into folder values(4, 'Folder 2_1', 3)");
45        stmt.execute("insert into folder values(5, 'Folder 2_2', 3)");
46      }
47  
48      /**
49       * Root/
50       *    Folder 1/
51       *    Folder 2/
52       *      Folder 2_1
53       *      Folder 2_2
54       */
55  
56      String resource = "org/apache/ibatis/submitted/association_nested/mybatis-config.xml";
57      try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
58        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
59        try (SqlSession session = sqlSessionFactory.openSession()) {
60          FolderMapper postMapper = session.getMapper(FolderMapper.class);
61  
62          List<FolderFlatTree> folders = postMapper.findWithSubFolders("Root");
63  
64          Assertions.assertEquals(3, folders.size());
65        }
66      }
67    }
68  
69  }