001: /*
002: * $Id: MappingTest.java 819 2007-01-18 07:59:50Z hengels $
003: * (c) Copyright 2004 con:cern development team.
004: *
005: * This file is part of con:cern (http://concern.org).
006: *
007: * con:cern is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU Lesser General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014: package org.concern.controller;
015:
016: import junit.framework.TestCase;
017: import org.hibernate.*;
018: import org.hibernate.cfg.Configuration;
019:
020: import java.sql.Timestamp;
021: import java.util.*;
022:
023: public class MappingTest extends TestCase {
024: private SessionFactory sessionFactory;
025: private Subject subject;
026: private ArchiveSubject archiveSubject;
027: private long millis = System.currentTimeMillis();
028:
029: public MappingTest(String name) {
030: super (name);
031: }
032:
033: protected void setUp() throws Exception {
034: Configuration configuration = new Configuration();
035: configuration
036: .setNamingStrategy(new org.concern.controller.Configuration.PrefixNamingStrategy(
037: "MT"));
038: configuration.configure("/controller-hibernate.cfg.xml");
039: configuration.addResource("Controller.hbm.xml");
040: sessionFactory = configuration.buildSessionFactory();
041:
042: subject = new Subject("subject");
043: subject.setProcess("ControllerTest");
044: subject.setSubjectLines(new HashSet(Arrays
045: .asList(new SubjectLine[] {
046: new SubjectLine("de", "hallo"),
047: new SubjectLine("en", "hello"), })));
048: subject.setLogs(Arrays.asList(new Log[] {
049: new Log("activity1", new Timestamp(millis + 3600000)),
050: new Log("activity2", new Timestamp(millis - 1000)),
051: new Log("activity3", new Timestamp(millis - 2000)), }));
052: Enlistment firstEnlistment;
053: subject.setEnlistments(new TreeSet(Arrays
054: .asList(new Enlistment[] {
055: firstEnlistment = new Enlistment("TIMEOUT",
056: "activity1", millis + 3600000),
057: new Enlistment("EXECUTE", "activity2",
058: millis - 1000),
059: new Enlistment("EXECUTE", "activity3",
060: millis - 2000), })));
061: firstEnlistment.setAssignments(new HashSet(Arrays
062: .asList(new Assignment[] { new Assignment("you"),
063: new Assignment("me"), })));
064:
065: archiveSubject = new ArchiveSubject("archiveSubject");
066: archiveSubject.setProcess("ControllerTest");
067: archiveSubject.setSubjectLines(new HashSet(Arrays
068: .asList(new ArchiveSubjectLine[] {
069: new ArchiveSubjectLine("de", "hallo"),
070: new ArchiveSubjectLine("en", "hello"), })));
071: archiveSubject.setLogs(Arrays.asList(new ArchiveLog[] {
072: new ArchiveLog("activity1", new Timestamp(
073: millis + 3600000)),
074: new ArchiveLog("activity2",
075: new Timestamp(millis - 1000)),
076: new ArchiveLog("activity3",
077: new Timestamp(millis - 2000)), }));
078:
079: Session session = sessionFactory.openSession();
080: Transaction transaction = session.beginTransaction();
081: Query query = session
082: .createQuery("FROM org.concern.controller.Subject subject");
083: List list = query.list();
084: for (Iterator iterator = list.iterator(); iterator.hasNext();) {
085: Subject subject = (Subject) iterator.next();
086: System.out.println("deleting subject = " + subject);
087: session.delete(subject);
088: }
089: session.save(subject);
090: session.save(archiveSubject);
091: transaction.commit();
092: session.close();
093: }
094:
095: protected void tearDown() throws Exception {
096: Session session = sessionFactory.openSession();
097: Transaction transaction = session.beginTransaction();
098: Query query = session
099: .createQuery("FROM org.concern.controller.Subject subject");
100: List list = query.list();
101: for (Iterator iterator = list.iterator(); iterator.hasNext();) {
102: Subject subject = (Subject) iterator.next();
103: System.out.println("deleting subject = " + subject);
104: session.delete(subject);
105: }
106: transaction.commit();
107: session.close();
108:
109: sessionFactory.close();
110: }
111:
112: public void testMapping() throws Exception {
113: Session session = sessionFactory.openSession();
114: Transaction transaction = session.beginTransaction();
115: Subject loaded = (Subject) session.load(Subject.class, subject
116: .getId(), LockMode.UPGRADE_NOWAIT);
117: assertEquals(2, loaded.getSubjectLines().size());
118: assertEquals(3, loaded.getLogs().size());
119: assertEquals(3, loaded.getEnlistments().size());
120: assertEquals(2, loaded.getTimeoutEnlistment("activity1")
121: .getAssignments().size());
122: Iterator iterator = loaded.getEnlistments().iterator();
123:
124: assertEquals(millis - 2000, ((Enlistment) iterator.next())
125: .getTimeout());
126: assertEquals(millis - 1000, ((Enlistment) iterator.next())
127: .getTimeout());
128: assertEquals(millis + 3600000, ((Enlistment) iterator.next())
129: .getTimeout());
130: loaded.getEnlistments()
131: .add(
132: new Enlistment("EXECUTE", "activity1",
133: millis + 3600000));
134: //loaded.getAssignments().add(new Assignment("watch", "others", new Timestamp(millis + 3600000)));
135: transaction.commit();
136: session.close();
137:
138: session = sessionFactory.openSession();
139: transaction = session.beginTransaction();
140: loaded = (Subject) session.load(Subject.class, subject.getId(),
141: LockMode.UPGRADE_NOWAIT);
142: assertEquals(3, loaded.getLogs().size());
143: assertEquals(4, loaded.getEnlistments().size());
144: transaction.commit();
145: session.close();
146:
147: session = sessionFactory.openSession();
148: transaction = session.beginTransaction();
149: ArchiveSubject archiveLoaded = (ArchiveSubject) session.load(
150: ArchiveSubject.class, archiveSubject.getId(),
151: LockMode.UPGRADE_NOWAIT);
152: assertEquals(2, archiveLoaded.getSubjectLines().size());
153: assertEquals(3, archiveLoaded.getLogs().size());
154: transaction.commit();
155: session.close();
156: }
157:
158: public void testTimeoutQuery() throws Exception {
159: Session session = sessionFactory.openSession();
160: Transaction transaction = session.beginTransaction();
161: Query query = session
162: .createQuery("select subject.userValue from org.concern.controller.Subject subject"
163: + " join subject.enlistments enlistment"
164: + " where enlistment.timeout < :now");
165: query.setLong("now", millis);
166: Set userValues = new HashSet();
167: for (Iterator iterator = query.iterate(); iterator.hasNext();)
168: userValues.add(iterator.next());
169:
170: assertEquals(1, userValues.size());
171:
172: transaction.commit();
173: session.close();
174: }
175:
176: public void testEnlistmentQuery() throws Exception {
177: Session session = sessionFactory.openSession();
178: Transaction transaction = session.beginTransaction();
179: Query query = session
180: .createQuery("select subject.userValue from org.concern.controller.Subject subject"
181: + " join subject.enlistments enlistment"
182: + " where enlistment.activity = :activity");
183: query.setString("activity", "activity1");
184: assertEquals(1, query.list().size());
185:
186: transaction.commit();
187: session.close();
188: }
189:
190: public void testEnlistment() throws Exception {
191: Session session = sessionFactory.openSession();
192: Transaction transaction = session.beginTransaction();
193: Subject loaded = (Subject) session.load(Subject.class, subject
194: .getId(), LockMode.UPGRADE_NOWAIT);
195: for (Iterator iterator = loaded.getEnlistments().iterator(); iterator
196: .hasNext();) {
197: Enlistment enlistment = (Enlistment) iterator.next();
198: if ("activity2".equals(enlistment.getActivity()))
199: iterator.remove();
200: }
201: session.update(loaded);
202: transaction.commit();
203: session.close();
204:
205: session = sessionFactory.openSession();
206: transaction = session.beginTransaction();
207: loaded = (Subject) session.load(Subject.class, subject.getId(),
208: LockMode.UPGRADE_NOWAIT);
209: assertEquals(2, loaded.getEnlistments().size());
210: Enlistment enlistment = new Enlistment("activity2", millis);
211: loaded.getEnlistments().add(enlistment);
212: transaction.commit();
213: session.close();
214:
215: session = sessionFactory.openSession();
216: transaction = session.beginTransaction();
217: loaded = (Subject) session.load(Subject.class, subject.getId(),
218: LockMode.UPGRADE_NOWAIT);
219: assertEquals(3, loaded.getEnlistments().size());
220: transaction.commit();
221: session.close();
222: }
223:
224: public void testAssignmentQuery() throws Exception {
225: Session session = sessionFactory.openSession();
226: Transaction transaction = session.beginTransaction();
227: Query query = session
228: .createQuery("select subject.userValue from org.concern.controller.Subject subject"
229: + " inner join subject.enlistments as enlistment"
230: + " inner join enlistment.assignments as assignment"
231: + " where assignment.assignee = :assignee");
232: query.setString("assignee", "me");
233: assertEquals(1, query.list().size());
234: transaction.commit();
235: session.close();
236:
237: session = sessionFactory.openSession();
238: transaction = session.beginTransaction();
239: Subject loaded = (Subject) session.load(Subject.class, subject
240: .getId(), LockMode.UPGRADE_NOWAIT);
241: assertEquals(2, loaded.getTimeoutEnlistment("activity1")
242: .getAssignments().size());
243: Assignment assignment = new Assignment("others");
244: loaded.getTimeoutEnlistment("activity1").getAssignments().add(
245: assignment);
246: transaction.commit();
247: session.close();
248:
249: session = sessionFactory.openSession();
250: transaction = session.beginTransaction();
251: query = session
252: .createQuery("select subject.userValue from org.concern.controller.Subject subject"
253: + " inner join subject.enlistments as enlistment"
254: + " inner join enlistment.assignments as assignment"
255: + " where assignment.assignee = :assignee");
256: query.setString("assignee", "others");
257: assertEquals(1, query.list().size());
258: transaction.commit();
259: session.close();
260: }
261: }
|