Scenario.java
:
package models;
import java.util.*;
import javax.persistence.*;
import play.db.ebean.*;
@Entity
public class Scenario extends Model {
@Id
public Long id;
public String name;
public boolean isPublic;
public boolean isAccepted;
public Date expirationDate;
@ManyToOne
public User owner;
@ManyToMany
public List<User> members = new ArrayList<User>();
@OneToMany(cascade = CascadeType.ALL)
public List<Checkpoint> checkpoints = new ArrayList<Checkpoint>();
public static Model.Finder<Long, Scenario> find =
new Finder<Long, Scenario>(Long.class, Scenario.class);
public Scenario(String name, boolean isPublic, Date expirationDate, User owner) {
this.name = name;
this.isPublic = isPublic;
this.expirationDate = expirationDate;
this.owner = owner;
this.isAccepted = false;
this.members.add(owner);
}
public static Scenario create(String name, boolean isPublic, Date expirationDate,
String owner) {
Scenario scenario = new Scenario(name, isPublic, expirationDate, User.find.ref(owner));
scenario.save();
scenario.saveManyToManyAssociations("members");
return scenario;
}
public static List<Scenario> findNotExpired(Date date) {
return find.where().or(
com.avaje.ebean.Expr.lt("expirationDate", date),
com.avaje.ebean.Expr.isNull("expirationDate")
).findList();
}
}
考试:
@Test
public void findScenariosNotExpired() {
new User("bob@gmail.com", "Bob", "secret").save();
new User("jane@gmail.com", "Jane", "secret").save();
SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Scenario.create("Scenario 1", false, null, "bob@gmail.com");
try {
Scenario.create("Scenario 2", false, dt.parse("2013-12-12 00:00:00"), "jane@gmail.com");
Scenario.create("Scenario 3", false, dt.parse("2013-10-10 00:00:00"), "jane@gmail.com");
} catch (ParseException e) {
System.err.println("Problem with date parsing");
e.printStackTrace();
}
List<Scenario> results = Scenario.findNotExpired(new Date());
assertEquals(2, results.size());
}
测试结果:
[error] Test models.ModelsTest.findScenariosNotExpired failed: expected:<2> but was:<3>
在测试中我通过new Date()
了,因为它应该包括今天的日期(2013 年 10 月 13 日)。这就是为什么我期望 2 的大小(一个 null 和一个在今天之前的日期)。
我应该改变什么?我想问题可能出在com.avaje.ebean.Expr.lt("expirationDate", date)
- 可以像这样比较日期吗?