001: /*
002: * TestSimple.java
003: *
004: * Created on October 13, 2006, 5:03 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009: /*
010: * Licensed to the Apache Software Foundation (ASF) under one
011: * or more contributor license agreements. See the NOTICE file
012: * distributed with this work for additional information
013: * regarding copyright ownership. The ASF licenses this file
014: * to you under the Apache License, Version 2.0 (the
015: * "License"); you may not use this file except in compliance
016: * with the License. You may obtain a copy of the License at
017: *
018: * http://www.apache.org/licenses/LICENSE-2.0
019: *
020: * Unless required by applicable law or agreed to in writing,
021: * software distributed under the License is distributed on an
022: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
023: * KIND, either express or implied. See the License for the
024: * specific language governing permissions and limitations
025: * under the License.
026: */
027: package org.apache.openjpa.persistence.kernel;
028:
029: import java.util.List;
030:
031: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
032:
033: import org.apache.openjpa.persistence.Extent;
034: import org.apache.openjpa.persistence.OpenJPAEntityManager;
035: import org.apache.openjpa.persistence.OpenJPAQuery;
036:
037: public class TestSimple extends BaseKernelTest {
038:
039: /**
040: * Creates a new instance of TestSimple
041: */
042: public TestSimple(String name) {
043: super (name);
044: }
045:
046: public void setUp() {
047: deleteAll(RuntimeTest1.class);
048: }
049:
050: public void testSimple() {
051: // test create
052: {
053: OpenJPAEntityManager pm = getPM();
054: startTx(pm);
055: pm.persist(new RuntimeTest1("testSimple", 12));
056: endTx(pm);
057: endEm(pm);
058: }
059:
060: // test Query
061: {
062: OpenJPAEntityManager pm = getPM();
063: startTx(pm);
064: String theQuery = "SELECT r FROM RuntimeTest1 r WHERE r.stringField = \'testSimple\'";
065: OpenJPAQuery query = pm.createQuery(theQuery);
066: List list = query.getResultList();
067: assertSize(1, list);
068: endTx(pm);
069: endEm(pm);
070: }
071:
072: // test Update
073: {
074: OpenJPAEntityManager pm = getPM();
075: startTx(pm);
076: String theQuery = "SELECT r FROM RuntimeTest1 r WHERE r.stringField = \'testSimple\'";
077: OpenJPAQuery query = pm.createQuery(theQuery);
078: RuntimeTest1 toUpdate = (RuntimeTest1) query
079: .getSingleResult();
080: toUpdate.setStringField("testSimple2");
081: endTx(pm);
082: endEm(pm);
083:
084: pm = getPM();
085: startTx(pm);
086: String query1 = "SELECT r FROM RuntimeTest1 r WHERE r.stringField = \'testSimple\'";
087: String query2 = "SELECT r FROM RuntimeTest1 r WHERE r.stringField = \'testSimple2\'";
088: OpenJPAQuery q1 = pm.createQuery(query1);
089: OpenJPAQuery q2 = pm.createQuery(query2);
090: assertSize(0, q1.getResultList());
091: assertSize(1, q2.getResultList());
092: endTx(pm);
093: endEm(pm);
094: }
095:
096: // test Extent
097: {
098: OpenJPAEntityManager pm = getPM();
099: startTx(pm);
100: Extent e = pm.createExtent(RuntimeTest1.class, true);
101: assertTrue(e.iterator().hasNext());
102: assertEquals("testSimple2", ((RuntimeTest1) e.iterator()
103: .next()).getStringField());
104: endTx(pm);
105: endEm(pm);
106: }
107:
108: // test delete
109: {
110: OpenJPAEntityManager pm = getPM();
111: startTx(pm);
112: String delete = "DELETE FROM RuntimeTest1 r WHERE r.stringField = \'testSimple2\'";
113: OpenJPAQuery deleteQuery = pm.createQuery(delete);
114: int deleted = deleteQuery.executeUpdate();
115: endTx(pm);
116: endEm(pm);
117:
118: pm = getPM();
119: startTx(pm);
120: String select = "SELECT r FROM RuntimeTest1 r WHERE r.stringField = \'testSimple2\'";
121: OpenJPAQuery selectQuery = pm.createQuery(select);
122:
123: assertSize(0, selectQuery.getResultList());
124:
125: endTx(pm);
126: endEm(pm);
127: }
128: }
129: }
|