From 83560d0854cd0aea5119c551aa4131aa11bf1139 Mon Sep 17 00:00:00 2001 From: Michael Dick Date: Fri, 13 Aug 2010 20:18:48 +0000 Subject: [PATCH] OPENJPA-1424: Adding testcase. Submitted By: Heath Thomann and Daryl Stultz git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.0.x@985343 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/persistence/query/Case.java | 76 +++++++++++ .../openjpa/persistence/query/Lookup.java | 54 ++++++++ .../openjpa/persistence/query/Role.java | 61 +++++++++ .../persistence/query/ScheduleDay.java | 63 +++++++++ .../query/ScheduledAssignment.java | 126 ++++++++++++++++++ .../persistence/query/TestOutOfBoundsEx.java | 110 +++++++++++++++ 6 files changed, 490 insertions(+) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Case.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Lookup.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Role.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/ScheduleDay.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/ScheduledAssignment.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestOutOfBoundsEx.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Case.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Case.java new file mode 100644 index 000000000..290cf8f4d --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Case.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.openjpa.persistence.query; + +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +@Entity +@Table(name = "cases") +public class Case { + + @Id + @GeneratedValue + @Column(name = "caseid") + private Integer id; + + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "scheduledayid", nullable = false) + private ScheduleDay scheduleDay; + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "caze") + private List scheduledAssignments; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public ScheduleDay getScheduleDay() { + return scheduleDay; + } + + public void setScheduleDay(ScheduleDay scheduleDay) { + this.scheduleDay = scheduleDay; + } + + public List getScheduledAssignments() { + return scheduledAssignments; + } + + public void setScheduledAssignments(List scheduledAssignments) { + this.scheduledAssignments = scheduledAssignments; + } + + public void addScheduledAssignment(ScheduledAssignment scheduledAssignment) { + scheduledAssignments.add(scheduledAssignment); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Lookup.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Lookup.java new file mode 100644 index 000000000..7ede47d79 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Lookup.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.openjpa.persistence.query; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "lookups") +public class Lookup { + + @Id + @Column(name = "ruleid") + private Integer id; + @Column(name = "name", nullable = false, length = 100) + private String name; + + public Lookup() { + } + + public Integer getId() { + return this.id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Role.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Role.java new file mode 100644 index 000000000..35d547b13 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Role.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.openjpa.persistence.query; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "roles") +public class Role { + + @Id + @GeneratedValue + @Column(name = "roleid") + private Integer id; + @ManyToOne(fetch = FetchType.EAGER, optional = false) + @JoinColumn(name = "lookupid", nullable = false) + private Lookup lookup; + + public Role() { + super(); + } + + public Integer getId() { + return this.id; + } + + public void setId(Integer roleid) { + this.id = roleid; + } + + public Lookup getLookup() { + return lookup; + } + + public void setLookup(Lookup lookup) { + this.lookup = lookup; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/ScheduleDay.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/ScheduleDay.java new file mode 100644 index 000000000..483a71c29 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/ScheduleDay.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.openjpa.persistence.query; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.UniqueConstraint; + +@Entity +@Table(name = "scheduledays", uniqueConstraints = @UniqueConstraint(columnNames = "scheduledate")) +public class ScheduleDay { + + @Id + @GeneratedValue + @Column(name = "scheduledayid") + private Integer id; + @Temporal(TemporalType.DATE) + @Column(name = "scheduledate", unique = true, nullable = false, length = 4) + private Date date; + + public ScheduleDay() { + super(); + } + + public Integer getId() { + return this.id; + } + + public void setId(Integer scheduledayid) { + this.id = scheduledayid; + } + + public Date getDate() { + return this.date; + } + + public void setDate(Date scheduledate) { + this.date = scheduledate; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/ScheduledAssignment.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/ScheduledAssignment.java new file mode 100644 index 000000000..759ba9ede --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/ScheduledAssignment.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.openjpa.persistence.query; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "scheduledassignments") +public class ScheduledAssignment { + + @Id + @GeneratedValue + @Column(name = "scheduledassignmentid") + private Integer id; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "autoassignid") + private ScheduledAssignment parentScheduledAssignment; + + @ManyToOne(fetch = FetchType.EAGER, optional = false) + @JoinColumn(name = "scheduledayid", nullable = false) + private ScheduleDay scheduleDay; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "caseid") + private Case caze; + + @ManyToOne(fetch = FetchType.EAGER, optional = false) + @JoinColumn(name = "roleid", nullable = false) + private Role role; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "lookupId") + private Lookup brokenRuleLookup; + + @Column(name = "brokencustomruleexplanation") + private String brokenCustomRuleExplanation; // somehow, removing this has an effect + + public ScheduledAssignment() { + super(); + } + + public Integer getId() { + return this.id; + } + + public void setId(Integer id) { + this.id = id; + } + + public ScheduledAssignment getParentScheduledAssignment() { + return this.parentScheduledAssignment; + } + + public void setParentScheduledAssignment(ScheduledAssignment scheduledassignments) { + this.parentScheduledAssignment = scheduledassignments; + } + + public ScheduleDay getScheduleDay() { + return this.scheduleDay; + } + + public void setScheduleDay(ScheduleDay scheduleDay) { + this.scheduleDay = scheduleDay; + } + + public Case getCase() { + return caze; + } + + public void setCase(Case caze) { + this.caze = caze; + } + + public Role getRole() { + return this.role; + } + + public void setRole(Role roles) { + this.role = roles; + } + + public Lookup getBrokenRuleLookup() { + return brokenRuleLookup; + } + + public void setBrokenRuleLookup(Lookup brokenRuleLookup) { + this.brokenRuleLookup = brokenRuleLookup; + } + + public Case getCaze() { + return caze; + } + + public void setCaze(Case caze) { + this.caze = caze; + } + + public String getBrokenCustomRuleExplanation() { + return brokenCustomRuleExplanation; + } + + public void setBrokenCustomRuleExplanation(String brokenCustomRuleExplanation) { + this.brokenCustomRuleExplanation = brokenCustomRuleExplanation; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestOutOfBoundsEx.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestOutOfBoundsEx.java new file mode 100644 index 000000000..fc265b732 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestOutOfBoundsEx.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.openjpa.persistence.query; + +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; + +import org.apache.openjpa.persistence.FetchPlan; +import org.apache.openjpa.persistence.QueryImpl; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestOutOfBoundsEx extends SingleEMFTestCase { + private EntityManager em = null; + private Lookup lookup; + + public void setUp() throws Exception { + super.setUp(Lookup.class, Case.class, Role.class, ScheduledAssignment.class, ScheduleDay.class, + DROP_TABLES); + em = emf.createEntityManager(); + insertLookups(); + } + + public void testOutOfBounds() throws Exception { + Calendar cal = Calendar.getInstance(); + final Date date = cal.getTime(); + ScheduleDay sd = insertScheduleDay(date); + + Role role1 = insertJob(); + Role role2 = insertJob(); + Case kase1 = insertCase(sd); + Case kase2 = insertCase(sd); + insertScheduledAssignmentInCase(role1, kase2); + + // simulate new web transaction on different em + em.close(); + em = emf.createEntityManager(); + + Query query = em.createQuery("select o from Case as o" + + " where o.scheduleDay = :sd"); + query.setParameter("sd", sd); + FetchPlan fetchPlan = ((QueryImpl) query).getFetchPlan(); + fetchPlan.addField(Case.class, "scheduledAssignments"); + + //Without the changes of OJ1424, this next call would cause an + //ArrayIndexOutOfBoundsException. + List allCases = query.getResultList(); + } + + public void insertLookups() { + lookup = new Lookup(); + lookup.setName("XYZ"); + lookup.setId(1); + save(lookup); + } + + public void save(Object obj) { + em.getTransaction().begin(); + em.persist(obj); + em.getTransaction().commit(); + } + + public Role insertJob() { + Role role = new Role(); + role.setLookup(lookup); + save(role); + return role; + } + + public Case insertCase(ScheduleDay sd) throws Exception { + Case kase = new Case(); + kase.setScheduleDay(sd); + save(kase); + return kase; + } + + public void insertScheduledAssignmentInCase(Role job, Case kase) { + ScheduledAssignment sa = new ScheduledAssignment(); + sa.setRole(job); + sa.setCase(kase); + sa.setScheduleDay(kase.getScheduleDay()); + save(sa); + } + + public ScheduleDay insertScheduleDay(Date date) { + ScheduleDay sd = new ScheduleDay(); + sd.setDate(date); + save(sd); + return sd; + } +}