OPENJPA-838: check in testcase

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@738918 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-01-29 16:43:44 +00:00
parent 30b7c05468
commit 963e541fd2
4 changed files with 226 additions and 3 deletions

View File

@ -0,0 +1,80 @@
/**
* 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.jdbc.query.cache;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
/**
* @version $Revision$ $Date$
*/
@Entity
@IdClass(InvoiceKey.class)
public class Invoice {
@Id
private int id;
@Id
private String brandName;
private double price;
@OneToMany(cascade={CascadeType.ALL})
private List<LineItem> lineItems = new ArrayList<LineItem>();
public Invoice() {
}
public Invoice(int id, String brandName, double price) {
this.id = id;
this.brandName = brandName;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public List<LineItem> getLineItems() {
return lineItems;
}
public void setLineItems(List<LineItem> lineItems) {
this.lineItems = lineItems;
}
}

View File

@ -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.jdbc.query.cache;
/**
* @version $Revision$ $Date$
*/
public class InvoiceKey {
private int id;
private String brandName;
public InvoiceKey() {
}
public InvoiceKey(int id, String brandName) {
this.id = id;
this.brandName = brandName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InvoiceKey invoiceKey = (InvoiceKey) o;
if (id != invoiceKey.id) return false;
if (!brandName.equals(invoiceKey.brandName)) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + brandName.hashCode();
return result;
}
}

View File

@ -0,0 +1,65 @@
/**
* 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.jdbc.query.cache;
import javax.persistence.*;
/**
* @version $Revision$ $Date$
*/
@Entity
public class LineItem {
@Id
private String id;
private int quantity;
@ManyToOne
private Invoice invoice;
public LineItem() {
}
public LineItem(String id, int quantity) {
this.id = id;
this.quantity = quantity;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public Invoice getInvoice() {
return invoice;
}
public void setInvoice(Invoice invoice) {
this.invoice = invoice;
}
}

View File

@ -18,10 +18,10 @@
*/
package org.apache.openjpa.persistence.jdbc.query.cache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
@ -45,18 +45,21 @@ import org.apache.openjpa.persistence.test.SQLListenerTestCase;
*
* @author Pinaki Poddar
* @author Vikram Bhatia
*
* @author David Blevins
*/
public class TestNonPrimaryKeyQueryParameters extends SQLListenerTestCase {
private static final int FULLTIME_EMPLOYEE_COUNT = 3;
private static final int PARTTIME_EMPLOYEE_COUNT = 2;
private static final int LINEITEM_PER_INVOICE = 1;
private static final String DEPT_NAME = "ENGINEERING";
public void setUp() {
super.setUp(CLEAR_TABLES, Department.class, Employee.class,
FullTimeEmployee.class, PartTimeEmployee.class,
Invoice.class, LineItem.class,
"openjpa.jdbc.QuerySQLCache", "true");
createDepartment(DEPT_NAME);
createInvoice();
sql.clear();
}
@ -80,6 +83,7 @@ public class TestNonPrimaryKeyQueryParameters extends SQLListenerTestCase {
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("SELECT d from Department d");
Department dept = (Department) query.getSingleResult();
assertEquals(FULLTIME_EMPLOYEE_COUNT, dept.getFullTimeEmployees()
@ -102,6 +106,10 @@ public class TestNonPrimaryKeyQueryParameters extends SQLListenerTestCase {
.size());
assertSQL(".* AND t0.TYPE = .*");
Invoice invoice = em.find(Invoice.class, new InvoiceKey(1, "Red"));
List<LineItem> list = invoice.getLineItems();
assertEquals(LINEITEM_PER_INVOICE, list.size());
em.close();
}
@ -153,4 +161,20 @@ public class TestNonPrimaryKeyQueryParameters extends SQLListenerTestCase {
em.close();
}
private void createInvoice() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Invoice invoice = new Invoice(1, "Red", 1.30);
for (int i = 1; i <= LINEITEM_PER_INVOICE; i++) {
LineItem item = new LineItem(String.valueOf(i), 10);
item.setInvoice(invoice);
invoice.getLineItems().add(item);
em.persist(invoice);
}
em.flush();
tran.commit();
em.close();
}
}