001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.annotations;
020:
021: import junit.framework.*;
022: import java.util.Calendar;
023: import java.util.Date;
024: import java.util.List;
025: import javax.persistence.*;
026:
027: import org.apache.openjpa.persistence.annotations.common.apps.annotApp.annotype.*;
028: import org.apache.openjpa.persistence.common.utils.*;
029: import org.apache.openjpa.persistence.OpenJPAEntityManager;
030:
031: public class TestAdvAnnot extends AnnotationTestCase {
032: public TestAdvAnnot(String name) {
033: super (name, "annotationcactusapp");
034: }
035:
036: public void setUp() {
037: deleteAll(Schedule.class);
038: deleteAll(FlightSchedule.class);
039:
040: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
041: startTx(em);
042:
043: for (int i = 0; i < 5; i++) {
044: Calendar cal = Calendar.getInstance();
045: Date date = new Date(01012006l);
046: Date edate = new Date(10031980l);
047: String name = "AmericaWest" + i;
048: String schedname = "Schedule" + i;
049: FlightSchedule sched = new FlightSchedule(schedname, cal,
050: date, edate, name);
051:
052: em.persist(sched);
053:
054: }
055:
056: endTx(em);
057: endEm(em);
058: }
059:
060: public void testTimeTemporalAnnot() {
061: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
062:
063: String query = "SELECT d.startDate FROM FlightSchedule d WHERE d.name = :name";
064:
065: Date obj = (Date) em.createQuery(query).setParameter("name",
066: "Schedule3").getSingleResult();
067:
068: assertNotNull(obj);
069: assertTrue(obj instanceof Date);
070:
071: endEm(em);
072: }
073:
074: public void testCalendarTemporalAnnot() {
075: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
076: String query = "SELECT d FROM FlightSchedule d";
077:
078: List list = em.createQuery(query).getResultList();
079: assertNotNull(list);
080: assertEquals(5, list.size());
081: /*
082: String query = "SELECT d.dob FROM FlightSchedule d WHERE d.name = :name";
083:
084: List list = em.createQuery(query)
085: .setParameter("name", "Schedule3")
086: .getResultList();
087:
088: assertNotNull(list);
089: assertTrue(list.get(0) instanceof Calendar);*/
090:
091: endEm(em);
092: }
093:
094: public void testUniqueConstraintAnnot() {
095: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
096: startTx(em);
097:
098: String query = "Update FlightSchedule f SET f.planeName = :plane WHERE f.name = :sname";
099: int upd = 0;
100:
101: try {
102: upd = em.createQuery(query).setParameter("plane",
103: "AmericaWest3").setParameter("sname", "Schedule2")
104: .executeUpdate();
105:
106: fail("Violated unique constraint rule...@Unique Constraint annotation needs to be supported");
107: } catch (Exception e) {
108: //suppose to throw exception based on the unique constraint rule
109: }
110:
111: assertNotNull(upd);
112: assertEquals(0, upd);
113:
114: endTx(em);
115: endEm(em);
116: }
117:
118: }
|