|
| 1 | +/* |
| 2 | + * Copyright 2009-2024 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.collection_injection; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.io.Reader; |
| 21 | + |
| 22 | +import org.apache.ibatis.BaseDataTest; |
| 23 | +import org.apache.ibatis.io.Resources; |
| 24 | +import org.apache.ibatis.session.SqlSession; |
| 25 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 26 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 27 | +import org.apache.ibatis.submitted.collection_injection.immutable.*; |
| 28 | +import org.apache.ibatis.submitted.collection_injection.immutable.ImmutableRoomDetail; |
| 29 | +import org.apache.ibatis.submitted.collection_injection.property.Defect; |
| 30 | +import org.apache.ibatis.submitted.collection_injection.property.Furniture; |
| 31 | +import org.apache.ibatis.submitted.collection_injection.property.House; |
| 32 | +import org.apache.ibatis.submitted.collection_injection.property.HouseMapper; |
| 33 | +import org.apache.ibatis.submitted.collection_injection.property.Room; |
| 34 | +import org.apache.ibatis.submitted.collection_injection.property.RoomDetail; |
| 35 | +import org.junit.jupiter.api.Assertions; |
| 36 | +import org.junit.jupiter.api.BeforeAll; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +class CollectionInjectionTest { |
| 40 | + |
| 41 | + private static SqlSessionFactory sqlSessionFactory; |
| 42 | + |
| 43 | + @BeforeAll |
| 44 | + static void setUp() throws Exception { |
| 45 | + try (Reader reader = Resources |
| 46 | + .getResourceAsReader("org/apache/ibatis/submitted/collection_injection/mybatis_config.xml")) { |
| 47 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); |
| 48 | + } |
| 49 | + |
| 50 | + BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), |
| 51 | + "org/apache/ibatis/submitted/collection_injection/create_db.sql"); |
| 52 | + BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), |
| 53 | + "org/apache/ibatis/submitted/collection_injection/data_load_small.sql"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void shouldSelectAllHousesUsingConstructorInjection() { |
| 58 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 59 | + final ImmutableHouseMapper mapper = sqlSession.getMapper(ImmutableHouseMapper.class); |
| 60 | + ImmutableHouse house = mapper.getHouse(1); |
| 61 | + Assertions.assertNotNull(house); |
| 62 | + |
| 63 | + final StringBuilder builder = new StringBuilder(); |
| 64 | + builder.append("\n").append(house.getName()); |
| 65 | + for (ImmutableRoom room : house.getRooms()) { |
| 66 | + ImmutableRoomDetail roomDetail = room.getRoomDetail(); |
| 67 | + String detailString = String.format(" (size=%d, height=%d, type=%s)", roomDetail.getRoomSize(), |
| 68 | + roomDetail.getWallHeight(), roomDetail.getWallType()); |
| 69 | + builder.append("\n").append("\t").append(room.getName()).append(detailString); |
| 70 | + for (ImmutableFurniture furniture : room.getFurniture()) { |
| 71 | + builder.append("\n").append("\t\t").append(furniture.getDescription()); |
| 72 | + for (ImmutableDefect defect : furniture.getDefects()) { |
| 73 | + builder.append("\n").append("\t\t\t").append(defect.getDefect()); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + assertResult(builder); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void shouldSelectAllHousesUsingPropertyInjection() { |
| 84 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 85 | + final HouseMapper mapper = sqlSession.getMapper(HouseMapper.class); |
| 86 | + final House house = mapper.getHouse(1); |
| 87 | + Assertions.assertNotNull(house); |
| 88 | + |
| 89 | + final StringBuilder builder = new StringBuilder(); |
| 90 | + builder.append("\n").append(house.getName()); |
| 91 | + for (Room room : house.getRooms()) { |
| 92 | + RoomDetail roomDetail = room.getRoomDetail(); |
| 93 | + String detailString = String.format(" (size=%d, height=%d, type=%s)", roomDetail.getRoomSize(), |
| 94 | + roomDetail.getWallHeight(), roomDetail.getWallType()); |
| 95 | + builder.append("\n").append("\t").append(room.getName()).append(detailString); |
| 96 | + for (Furniture furniture : room.getFurniture()) { |
| 97 | + builder.append("\n").append("\t\t").append(furniture.getDescription()); |
| 98 | + for (Defect defect : furniture.getDefects()) { |
| 99 | + builder.append("\n").append("\t\t\t").append(defect.getDefect()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + assertResult(builder); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static void assertResult(StringBuilder builder) { |
| 109 | + String expected = "\nMyBatis Headquarters" + "\n\tKitchen (size=25, height=20, type=Brick)" + "\n\t\tCoffee machine" |
| 110 | + + "\n\t\t\tDoes not work" + "\n\t\tFridge" + "\n\tDining room (size=100, height=10, type=Wood)" + "\n\t\tTable" |
| 111 | + + "\n\tProgramming room (size=200, height=15, type=Steel)" + "\n\t\tBig screen" + "\n\t\tLaptop" |
| 112 | + + "\n\t\t\tCannot run intellij"; |
| 113 | + |
| 114 | + assertThat(builder.toString()).isNotEmpty().isEqualTo(expected); |
| 115 | + } |
| 116 | +} |
0 commit comments