Skip to content

Commit

Permalink
mybatis#101: Add test with an association constructor argument
Browse files Browse the repository at this point in the history
  • Loading branch information
epochcoder committed Mar 9, 2024
1 parent 204a09c commit a54582b
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,33 @@ void shouldSelectAllHouses() {

final StringBuilder builder = new StringBuilder();
for (House house : houses) {
builder.append(house.getName()).append("\n");
builder.append("\n").append(house.getName());
for (Room room : house.getRooms()) {
builder.append("\t").append(room.getName()).append("\n");
RoomDetail roomDetail = room.getRoomDetail();
String detailString = String.format(" (size=%d, height=%d, type=%s)", roomDetail.getRoomSize(),
roomDetail.getWallHeight(), roomDetail.getWallType());
builder.append("\n").append("\t").append(room.getName()).append(detailString);
for (Furniture furniture : room.getFurniture()) {
builder.append("\t\t").append(furniture.getDescription()).append("\n");
builder.append("\n").append("\t\t").append(furniture.getDescription());
for (Defect defect : furniture.getDefects()) {
builder.append("\t\t\t").append(defect.getDefect()).append("\n");
builder.append("\n").append("\t\t\t").append(defect.getDefect());
}
}
}
}

String expected = "MyBatis Headquarters\n" + "\tKitchen\n" + "\t\tCoffee machine\n" + "\t\t\tDoes not work\n"
+ "\t\tFridge\n" + "\tDining room\n" + "\t\tTable\n" + "\tProgramming room\n" + "\t\tBig screen\n"
+ "\t\tLaptop\n" + "\t\t\tCannot run intellij\n";
String expected =
"\nMyBatis Headquarters" +
"\n\tKitchen (size=25, height=20, type=Brick)" +
"\n\t\tCoffee machine" +
"\n\t\t\tDoes not work" +
"\n\t\tFridge" +
"\n\tDining room (size=100, height=10, type=Wood)" +
"\n\t\tTable" +
"\n\tProgramming room (size=200, height=15, type=Steel)" +
"\n\t\tBig screen" +
"\n\t\tLaptop" +
"\n\t\t\tCannot run intellij";

assertThat(builder.toString()).isNotEmpty().isEqualTo(expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
public class Room {
private final int id;
private final String name;
private final int size;
private final RoomDetail roomDetail;
private final List<Furniture> furniture;

public Room(int id, String name, int size, List<Furniture> furniture) {
public Room(int id, String name, RoomDetail roomDetail, List<Furniture> furniture) {
this.id = id;
this.name = name;
this.size = size;
this.roomDetail = roomDetail;
this.furniture = furniture;
}

Expand All @@ -38,8 +38,8 @@ public String getName() {
return name;
}

public int getSize() {
return size;
public RoomDetail getRoomDetail() {
return roomDetail;
}

public List<Furniture> getFurniture() {
Expand All @@ -48,6 +48,6 @@ public List<Furniture> getFurniture() {

@Override
public String toString() {
return "Room{" + "id=" + id + ", name='" + name + '\'' + ", size=" + size + ", furniture=" + furniture + '}';
return "Room{" + "id=" + id + ", name='" + name + '\'' + ", roomDetail=" + roomDetail + ", furniture=" + furniture + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.apache.ibatis.submitted.collection_injection;

public class RoomDetail {

private final String wallType;
private final int wallHeight;
private final int roomSize;

public RoomDetail(final String wallType, final int wallHeight, final int roomSize) {
this.wallType = wallType;
this.wallHeight = wallHeight;
this.roomSize = roomSize;
}

public String getWallType() {
return wallType;
}

public int getWallHeight() {
return wallHeight;
}

public int getRoomSize() {
return roomSize;
}

@Override
public String toString() {
return "RoomDetail{" +
"wallType='" + wallType + '\'' +
", wallHeight=" + wallHeight +
", roomSize=" + roomSize +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ create table room (
id int,
name varchar(20),
house_id int,
size_m2 int
size_m2 int,
wall_type varchar(10),
wall_height int
);

alter table room add constraint fk_room_house_id
Expand All @@ -58,9 +60,9 @@ alter table defect add constraint fk_defects_furniture_id

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

insert into room (id, name, house_id, size_m2) VALUES ( 1, 'Kitchen', 1, 25 );
insert into room (id, name, house_id, size_m2) VALUES ( 2, 'Dining room', 1, 100 );
insert into room (id, name, house_id, size_m2) VALUES ( 3, 'Programming room', 1, 200 );
insert into room (id, name, house_id, size_m2, wall_type, wall_height) VALUES ( 1, 'Kitchen', 1, 25, 'Brick', 20 );
insert into room (id, name, house_id, size_m2, wall_type, wall_height) VALUES ( 2, 'Dining room', 1, 100, 'Wood', 10 );
insert into room (id, name, house_id, size_m2, wall_type, wall_height) VALUES ( 3, 'Programming room', 1, 200, 'Steel', 15 );

insert into furniture (id, description, room_id) VALUES ( 1, 'Coffee machine', 1);
insert into furniture (id, description, room_id) VALUES ( 2, 'Fridge', 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@
<constructor>
<idArg column="id" javaType="_int"/>
<arg column="name" javaType="String"/>
<arg column="size" javaType="_int"/>
<arg javaType="RoomDetail" resultMap="roomDetailMap"/>
<arg javaType="List" resultMap="furnitureMap" columnPrefix="furniture_"/>
</constructor>
</resultMap>

<resultMap id="roomDetailMap" type="RoomDetail">
<constructor>
<arg column="wall_type" javaType="String"/>
<arg column="wall_height" javaType="_int"/>
<arg column="size_m2" javaType="_int"/>
</constructor>
</resultMap>

<resultMap id="furnitureMap" type="Furniture">
<constructor>
<idArg column="id" javaType="_int"/>
Expand All @@ -59,7 +67,9 @@

, r.id as room_id
, r.name as room_name
, r.size_m2 as room_size
, r.size_m2 as room_size_m2
, r.wall_type as room_wall_type
, r.wall_height as room_wall_height

, f.id as room_furniture_id
, f.description as room_furniture_description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<typeAliases>
<typeAlias type="org.apache.ibatis.submitted.collection_injection.House" alias="House"/>
<typeAlias type="org.apache.ibatis.submitted.collection_injection.Room" alias="Room"/>
<typeAlias type="org.apache.ibatis.submitted.collection_injection.RoomDetail" alias="RoomDetail"/>
<typeAlias type="org.apache.ibatis.submitted.collection_injection.Furniture" alias="Furniture"/>
<typeAlias type="org.apache.ibatis.submitted.collection_injection.Defect" alias="Defect"/>
</typeAliases>
Expand Down

0 comments on commit a54582b

Please sign in to comment.