mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
@@ -0,0 +1,130 @@
|
||||
package org.tests.sp;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.sp.model.car.Car;
|
||||
import org.tests.sp.model.car.Door;
|
||||
import org.tests.sp.model.car.Wheel;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestManyToManySaveTwice extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void insertBatch() {
|
||||
|
||||
delete();
|
||||
|
||||
Wheel w0 = new Wheel("wx0");
|
||||
Wheel w1 = new Wheel("wx1");
|
||||
|
||||
Door d0 = new Door("dx0");
|
||||
Door d1 = new Door("dx1");
|
||||
|
||||
List<Wheel> wheels = new LinkedList<>();
|
||||
wheels.add(w0);
|
||||
wheels.add(w1);
|
||||
|
||||
List<Door> doors = new LinkedList<>();
|
||||
doors.add(d0);
|
||||
doors.add(d1);
|
||||
|
||||
DB.saveAll(wheels);
|
||||
DB.saveAll(doors);
|
||||
|
||||
Car c0 = new Car("cx0");
|
||||
c0.getWheels().add(w0);
|
||||
c0.getWheels().add(w1);
|
||||
c0.getDoors().add(d0);
|
||||
|
||||
Car c1 = new Car("cx1");
|
||||
c1.getWheels().add(w1);
|
||||
|
||||
try (Transaction transaction = DB.beginTransaction()) {
|
||||
transaction.setBatchMode(true);
|
||||
|
||||
DB.save(c0);
|
||||
DB.save(c1);
|
||||
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
c0 = DB.find(Car.class, c0.getId());
|
||||
c1 = DB.find(Car.class, c1.getId());
|
||||
|
||||
assertThat(c0.getWheels()).hasSize(2);
|
||||
assertThat(c0.getDoors()).hasSize(1);
|
||||
|
||||
assertThat(c1.getWheels()).hasSize(1);
|
||||
assertThat(c1.getDoors()).hasSize(0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void insertBasic() {
|
||||
|
||||
delete();
|
||||
|
||||
Wheel w0 = new Wheel("wx0");
|
||||
Wheel w1 = new Wheel("wx1");
|
||||
|
||||
List<Wheel> wheels = new LinkedList<>();
|
||||
wheels.add(w0);
|
||||
wheels.add(w1);
|
||||
|
||||
DB.saveAll(wheels);
|
||||
|
||||
Car c0 = new Car("cx0");
|
||||
c0.getWheels().add(w0);
|
||||
c0.getWheels().add(w1);
|
||||
|
||||
Car c1 = new Car("cx1");
|
||||
c1.getWheels().add(w1);
|
||||
|
||||
DB.save(c0);
|
||||
DB.save(c1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertCarTwice() {
|
||||
|
||||
delete();
|
||||
|
||||
List<Wheel> wheels = new LinkedList<>();
|
||||
wheels.add(new Wheel("w0"));
|
||||
wheels.add(new Wheel("w1"));
|
||||
wheels.add(new Wheel("w2"));
|
||||
wheels.add(new Wheel("w3"));
|
||||
|
||||
Car c = new Car("c1");
|
||||
c.setWheels(wheels);
|
||||
|
||||
DB.save(c); // NOTE 1ST SAVE
|
||||
assertNotNull(c.getId());
|
||||
|
||||
DB.save(c); // NOTE 2ND SAVE
|
||||
|
||||
List<Car> allCars = DB.find(Car.class).findList();
|
||||
assertEquals(1, allCars.size());
|
||||
|
||||
List<Wheel> allWheels = DB.find(Wheel.class).findList();
|
||||
assertEquals(4, allWheels.size());
|
||||
}
|
||||
|
||||
private void delete() {
|
||||
DB.sqlUpdate("delete from sp_car_car_wheels").execute();
|
||||
DB.sqlUpdate("delete from sp_car_car_doors").execute();
|
||||
DB.sqlUpdate("delete from sp_car_wheel").execute();
|
||||
DB.sqlUpdate("delete from sp_car_door").execute();
|
||||
DB.sqlUpdate("delete from sp_car_car").execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.tests.sp.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class IdEntity implements Serializable {
|
||||
private static final long serialVersionUID = 7804145008732783678L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
protected Long id;
|
||||
|
||||
@Version
|
||||
private int version;
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id == null ? 31 : 31 + 31 * id.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (this == obj)
|
||||
return true;
|
||||
|
||||
if (obj == null || !this.getClass().isInstance(obj))
|
||||
return false;
|
||||
|
||||
IdEntity o = (IdEntity) obj;
|
||||
|
||||
return id != null && o.id != null && id.equals(o.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getClass().getSimpleName() + "[" + id + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.tests.sp.model.car;
|
||||
|
||||
import org.tests.sp.model.IdEntity;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "sp_car_car")
|
||||
public class Car extends IdEntity {
|
||||
|
||||
private static final long serialVersionUID = 2579148859565507940L;
|
||||
|
||||
private final String name;
|
||||
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "sp_car_car_wheels", joinColumns = {@JoinColumn(name = "car")}, inverseJoinColumns = {@JoinColumn(name = "wheel")})
|
||||
private List<Wheel> wheels;
|
||||
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "sp_car_car_doors", joinColumns = {@JoinColumn(name = "car")}, inverseJoinColumns = {@JoinColumn(name = "door")})
|
||||
private List<Door> doors;
|
||||
|
||||
public Car(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<Wheel> getWheels() {
|
||||
return wheels;
|
||||
}
|
||||
|
||||
public void setWheels(List<Wheel> wheels) {
|
||||
this.wheels = wheels;
|
||||
}
|
||||
|
||||
public List<Door> getDoors() {
|
||||
return doors;
|
||||
}
|
||||
|
||||
public void setDoors(List<Door> doors) {
|
||||
this.doors = doors;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.tests.sp.model.car;
|
||||
|
||||
import org.tests.sp.model.IdEntity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "sp_car_door")
|
||||
public class Door extends IdEntity {
|
||||
|
||||
private static final long serialVersionUID = 2399600193947163469L;
|
||||
|
||||
private String name;
|
||||
|
||||
public Door(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.tests.sp.model.car;
|
||||
|
||||
import org.tests.sp.model.IdEntity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "sp_car_wheel")
|
||||
public class Wheel extends IdEntity {
|
||||
|
||||
private static final long serialVersionUID = 2399600193947163469L;
|
||||
|
||||
private String name;
|
||||
|
||||
public Wheel(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user