View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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       * <pre>
50       * Root/
51       *  Folder 1/
52       *  Folder 2/
53       *    Folder 2_1
54       *    Folder 2_2
55       * </pre>
56       */
57      String resource = "org/apache/ibatis/submitted/association_nested/mybatis-config.xml";
58      try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
59        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
60        try (SqlSession session = sqlSessionFactory.openSession()) {
61          FolderMapper postMapper = session.getMapper(FolderMapper.class);
62  
63          List<FolderFlatTree> folders = postMapper.findWithSubFolders("Root");
64  
65          Assertions.assertEquals(3, folders.size());
66        }
67      }
68    }
69  
70  }