View Javadoc
1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ken.core;
17  
18  import java.sql.Timestamp;
19  
20  import org.apache.ojb.broker.query.Criteria;
21  import org.apache.ojb.broker.query.Query;
22  import org.apache.ojb.broker.query.QueryFactory;
23  import org.junit.Test;
24  import org.kuali.rice.ken.bo.Notification;
25  import org.kuali.rice.ken.bo.NotificationChannel;
26  import org.kuali.rice.ken.bo.NotificationProducer;
27  import org.kuali.rice.ken.dao.BusinessObjectDaoTestCaseBase;
28  import org.kuali.rice.ken.test.util.MockObjectsUtil;
29  import org.kuali.rice.ken.util.NotificationConstants;
30  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
31  import org.kuali.rice.test.BaselineTestCase.Mode;
32  
33  /**
34   * Scratch test case for testing aspects of OJB under test harness
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  @BaselineMode(Mode.CLEAR_DB)
38  public class TestOJBTest extends BusinessObjectDaoTestCaseBase {
39      /**
40       * Just prints the SQL generated by a query criteria 
41       */
42      @Test
43      public void testCriteria() {
44          Criteria criteria_STATUS = new Criteria();
45          criteria_STATUS.addEqualTo(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED);
46  
47          Criteria criteria_UNDELIVERED = new Criteria();
48          criteria_UNDELIVERED.addEqualTo(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.UNDELIVERED);
49  
50          // now OR the above two together
51          criteria_STATUS.addOrCriteria(criteria_UNDELIVERED);
52  
53          Criteria criteria_NOTLOCKED = new Criteria();
54          criteria_NOTLOCKED.addIsNull(NotificationConstants.BO_PROPERTY_NAMES.LOCKED_DATE);
55  
56          Criteria fullQueryCriteria = new Criteria();
57          fullQueryCriteria.addAndCriteria(criteria_NOTLOCKED);
58          fullQueryCriteria.addLessOrEqualThan(NotificationConstants.BO_PROPERTY_NAMES.NOTIFICATION_AUTO_REMOVE_DATE_TIME, new Timestamp(System.currentTimeMillis()));
59          // now add in the STATUS check
60          fullQueryCriteria.addAndCriteria(criteria_STATUS);
61  
62  
63          System.err.println(fullQueryCriteria.toString());
64  
65          Query q = QueryFactory.newQuery(Notification.class, fullQueryCriteria);
66          System.err.println(q.toString());
67  
68      }
69  
70      @Test
71      public void testUpdateRelationships() {
72          NotificationChannel channel1 = MockObjectsUtil.getTestChannel1();
73          NotificationChannel channel2 = MockObjectsUtil.getTestChannel2();
74          NotificationProducer mockProducer1 = MockObjectsUtil.getTestProducer1();
75  
76          businessObjectDao.save(mockProducer1);
77          assertEquals(0, mockProducer1.getChannels().size());
78  
79          // add in a notification channel producer join object
80          channel1.getProducers().add(mockProducer1);
81  
82          businessObjectDao.save(channel1);
83  
84          assertEquals(1, channel1.getProducers().size());
85  
86          // ojb doesn't update the collections of the child in the relationship on save, despite auto-update...
87          // so I'm forced to load it again
88          mockProducer1 = (NotificationProducer) businessObjectDao.findById(NotificationProducer.class, mockProducer1.getId());
89          assertEquals(1, mockProducer1.getChannels().size());
90  
91          channel2.getProducers().add(mockProducer1);	
92          businessObjectDao.save(channel2);
93  
94          assertEquals(1, channel2.getProducers().size());
95  
96          mockProducer1 = (NotificationProducer) businessObjectDao.findById(NotificationProducer.class, mockProducer1.getId());
97  
98          assertEquals(2, mockProducer1.getChannels().size());
99      }
100 }