Skip to content

Commit a54582b

Browse files
committed
mybatis#101: Add test with an association constructor argument
1 parent 204a09c commit a54582b

File tree

6 files changed

+94
-19
lines changed

6 files changed

+94
-19
lines changed

src/test/java/org/apache/ibatis/submitted/collection_injection/ImmutableCollectionConstructorTest.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,33 @@ void shouldSelectAllHouses() {
5555

5656
final StringBuilder builder = new StringBuilder();
5757
for (House house : houses) {
58-
builder.append(house.getName()).append("\n");
58+
builder.append("\n").append(house.getName());
5959
for (Room room : house.getRooms()) {
60-
builder.append("\t").append(room.getName()).append("\n");
60+
RoomDetail roomDetail = room.getRoomDetail();
61+
String detailString = String.format(" (size=%d, height=%d, type=%s)", roomDetail.getRoomSize(),
62+
roomDetail.getWallHeight(), roomDetail.getWallType());
63+
builder.append("\n").append("\t").append(room.getName()).append(detailString);
6164
for (Furniture furniture : room.getFurniture()) {
62-
builder.append("\t\t").append(furniture.getDescription()).append("\n");
65+
builder.append("\n").append("\t\t").append(furniture.getDescription());
6366
for (Defect defect : furniture.getDefects()) {
64-
builder.append("\t\t\t").append(defect.getDefect()).append("\n");
67+
builder.append("\n").append("\t\t\t").append(defect.getDefect());
6568
}
6669
}
6770
}
6871
}
6972

70-
String expected = "MyBatis Headquarters\n" + "\tKitchen\n" + "\t\tCoffee machine\n" + "\t\t\tDoes not work\n"
71-
+ "\t\tFridge\n" + "\tDining room\n" + "\t\tTable\n" + "\tProgramming room\n" + "\t\tBig screen\n"
72-
+ "\t\tLaptop\n" + "\t\t\tCannot run intellij\n";
73+
String expected =
74+
"\nMyBatis Headquarters" +
75+
"\n\tKitchen (size=25, height=20, type=Brick)" +
76+
"\n\t\tCoffee machine" +
77+
"\n\t\t\tDoes not work" +
78+
"\n\t\tFridge" +
79+
"\n\tDining room (size=100, height=10, type=Wood)" +
80+
"\n\t\tTable" +
81+
"\n\tProgramming room (size=200, height=15, type=Steel)" +
82+
"\n\t\tBig screen" +
83+
"\n\t\tLaptop" +
84+
"\n\t\t\tCannot run intellij";
7385

7486
assertThat(builder.toString()).isNotEmpty().isEqualTo(expected);
7587
}

src/test/java/org/apache/ibatis/submitted/collection_injection/Room.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
public class Room {
2121
private final int id;
2222
private final String name;
23-
private final int size;
23+
private final RoomDetail roomDetail;
2424
private final List<Furniture> furniture;
2525

26-
public Room(int id, String name, int size, List<Furniture> furniture) {
26+
public Room(int id, String name, RoomDetail roomDetail, List<Furniture> furniture) {
2727
this.id = id;
2828
this.name = name;
29-
this.size = size;
29+
this.roomDetail = roomDetail;
3030
this.furniture = furniture;
3131
}
3232

@@ -38,8 +38,8 @@ public String getName() {
3838
return name;
3939
}
4040

41-
public int getSize() {
42-
return size;
41+
public RoomDetail getRoomDetail() {
42+
return roomDetail;
4343
}
4444

4545
public List<Furniture> getFurniture() {
@@ -48,6 +48,6 @@ public List<Furniture> getFurniture() {
4848

4949
@Override
5050
public String toString() {
51-
return "Room{" + "id=" + id + ", name='" + name + '\'' + ", size=" + size + ", furniture=" + furniture + '}';
51+
return "Room{" + "id=" + id + ", name='" + name + '\'' + ", roomDetail=" + roomDetail + ", furniture=" + furniture + '}';
5252
}
5353
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
public class RoomDetail {
19+
20+
private final String wallType;
21+
private final int wallHeight;
22+
private final int roomSize;
23+
24+
public RoomDetail(final String wallType, final int wallHeight, final int roomSize) {
25+
this.wallType = wallType;
26+
this.wallHeight = wallHeight;
27+
this.roomSize = roomSize;
28+
}
29+
30+
public String getWallType() {
31+
return wallType;
32+
}
33+
34+
public int getWallHeight() {
35+
return wallHeight;
36+
}
37+
38+
public int getRoomSize() {
39+
return roomSize;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "RoomDetail{" +
45+
"wallType='" + wallType + '\'' +
46+
", wallHeight=" + wallHeight +
47+
", roomSize=" + roomSize +
48+
'}';
49+
}
50+
}

src/test/resources/org/apache/ibatis/submitted/collection_injection/CreateDB.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ create table room (
3232
id int,
3333
name varchar(20),
3434
house_id int,
35-
size_m2 int
35+
size_m2 int,
36+
wall_type varchar(10),
37+
wall_height int
3638
);
3739

3840
alter table room add constraint fk_room_house_id
@@ -58,9 +60,9 @@ alter table defect add constraint fk_defects_furniture_id
5860

5961
insert into house (id, name) values ( 1, 'MyBatis Headquarters' );
6062

61-
insert into room (id, name, house_id, size_m2) VALUES ( 1, 'Kitchen', 1, 25 );
62-
insert into room (id, name, house_id, size_m2) VALUES ( 2, 'Dining room', 1, 100 );
63-
insert into room (id, name, house_id, size_m2) VALUES ( 3, 'Programming room', 1, 200 );
63+
insert into room (id, name, house_id, size_m2, wall_type, wall_height) VALUES ( 1, 'Kitchen', 1, 25, 'Brick', 20 );
64+
insert into room (id, name, house_id, size_m2, wall_type, wall_height) VALUES ( 2, 'Dining room', 1, 100, 'Wood', 10 );
65+
insert into room (id, name, house_id, size_m2, wall_type, wall_height) VALUES ( 3, 'Programming room', 1, 200, 'Steel', 15 );
6466

6567
insert into furniture (id, description, room_id) VALUES ( 1, 'Coffee machine', 1);
6668
insert into furniture (id, description, room_id) VALUES ( 2, 'Fridge', 1);

src/test/resources/org/apache/ibatis/submitted/collection_injection/HouseMapper.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,19 @@
3434
<constructor>
3535
<idArg column="id" javaType="_int"/>
3636
<arg column="name" javaType="String"/>
37-
<arg column="size" javaType="_int"/>
37+
<arg javaType="RoomDetail" resultMap="roomDetailMap"/>
3838
<arg javaType="List" resultMap="furnitureMap" columnPrefix="furniture_"/>
3939
</constructor>
4040
</resultMap>
4141

42+
<resultMap id="roomDetailMap" type="RoomDetail">
43+
<constructor>
44+
<arg column="wall_type" javaType="String"/>
45+
<arg column="wall_height" javaType="_int"/>
46+
<arg column="size_m2" javaType="_int"/>
47+
</constructor>
48+
</resultMap>
49+
4250
<resultMap id="furnitureMap" type="Furniture">
4351
<constructor>
4452
<idArg column="id" javaType="_int"/>
@@ -59,7 +67,9 @@
5967

6068
, r.id as room_id
6169
, r.name as room_name
62-
, r.size_m2 as room_size
70+
, r.size_m2 as room_size_m2
71+
, r.wall_type as room_wall_type
72+
, r.wall_height as room_wall_height
6373

6474
, f.id as room_furniture_id
6575
, f.description as room_furniture_description

src/test/resources/org/apache/ibatis/submitted/collection_injection/mybatis-config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<typeAliases>
2929
<typeAlias type="org.apache.ibatis.submitted.collection_injection.House" alias="House"/>
3030
<typeAlias type="org.apache.ibatis.submitted.collection_injection.Room" alias="Room"/>
31+
<typeAlias type="org.apache.ibatis.submitted.collection_injection.RoomDetail" alias="RoomDetail"/>
3132
<typeAlias type="org.apache.ibatis.submitted.collection_injection.Furniture" alias="Furniture"/>
3233
<typeAlias type="org.apache.ibatis.submitted.collection_injection.Defect" alias="Defect"/>
3334
</typeAliases>

0 commit comments

Comments
 (0)