View Javadoc

1   /**
2    * Copyright 2005-2011 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 org.apache.ojb.broker.query.Criteria;
19  import org.apache.ojb.broker.query.Query;
20  import org.apache.ojb.broker.query.QueryFactory;
21  import org.junit.Test;
22  import org.kuali.rice.ken.bo.NotificationBo;
23  import org.kuali.rice.ken.bo.NotificationChannelBo;
24  import org.kuali.rice.ken.bo.NotificationProducerBo;
25  import org.kuali.rice.ken.dao.BusinessObjectDaoTestCaseBase;
26  import org.kuali.rice.ken.test.util.MockObjectsUtil;
27  import org.kuali.rice.ken.util.NotificationConstants;
28  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
29  import org.kuali.rice.test.BaselineTestCase.Mode;
30  
31  import java.sql.Timestamp;
32  
33  import static org.junit.Assert.assertEquals;
34  
35  //import org.apache.ojb.broker.query.Criteria;
36  //import org.kuali.rice.core.jpa.criteria.Criteria;
37  //import org.kuali.rice.core.jpa.criteria.QueryByCriteria;
38  
39  /**
40   * Scratch test case for testing aspects of OJB under test harness
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  @BaselineMode(Mode.CLEAR_DB)
44  public class TestOJBTest extends BusinessObjectDaoTestCaseBase {
45      /**
46       * Just prints the SQL generated by a query criteria 
47       */
48  	/*
49  	@Ignore
50      @Test
51      public void testJPACriteria() {
52          Criteria criteria_STATUS = new Criteria(Notification.class.getName());
53          criteria_STATUS.eq(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED);
54  
55          Criteria criteria_UNDELIVERED = new Criteria(Notification.class.getName());
56          criteria_UNDELIVERED.eq(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.UNDELIVERED);
57  
58          // now OR the above two together
59          criteria_STATUS.or(criteria_UNDELIVERED);
60  
61          Criteria criteria_NOTLOCKED = new Criteria(Notification.class.getName());
62          criteria_NOTLOCKED.isNull(NotificationConstants.BO_PROPERTY_NAMES.LOCKED_DATE);
63  
64          Criteria fullQueryCriteria = new Criteria(Notification.class.getName());
65          fullQueryCriteria.and(criteria_NOTLOCKED);
66          fullQueryCriteria.lte(NotificationConstants.BO_PROPERTY_NAMES.NOTIFICATION_AUTO_REMOVE_DATE_TIME, new Timestamp(System.currentTimeMillis()));
67          // now add in the STATUS check
68          fullQueryCriteria.and(criteria_STATUS);
69  
70  
71          System.err.println(fullQueryCriteria.toString());
72  
73          
74          QueryByCriteria q = new QueryByCriteria(KRADServiceLocatorInternal.getApplicationEntityManagerFactory().createEntityManager(), fullQueryCriteria);
75         
76          System.err.println(q.toString());
77  
78      }
79  	*/
80  	@Test
81      public void testCriteria() {
82          Criteria criteria_STATUS = new Criteria();
83          criteria_STATUS.addEqualTo(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED);
84  
85          Criteria criteria_UNDELIVERED = new Criteria();
86          criteria_UNDELIVERED.addEqualTo(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.UNDELIVERED);
87  
88          // now OR the above two together
89          criteria_STATUS.addOrCriteria(criteria_UNDELIVERED);
90  
91          Criteria criteria_NOTLOCKED = new Criteria();
92          criteria_NOTLOCKED.addIsNull(NotificationConstants.BO_PROPERTY_NAMES.LOCKED_DATE);
93  
94          Criteria fullQueryCriteria = new Criteria();
95          fullQueryCriteria.addAndCriteria(criteria_NOTLOCKED);
96          fullQueryCriteria.addLessOrEqualThan(NotificationConstants.BO_PROPERTY_NAMES.NOTIFICATION_AUTO_REMOVE_DATE_TIME, new Timestamp(System.currentTimeMillis()));
97          // now add in the STATUS check
98          fullQueryCriteria.addAndCriteria(criteria_STATUS);
99  
100 
101         System.err.println(fullQueryCriteria.toString());
102 
103         Query q = QueryFactory.newQuery(NotificationBo.class, fullQueryCriteria);
104         System.err.println(q.toString());
105 
106     }
107     @Test
108     public void testUpdateRelationships() {
109         NotificationChannelBo channel1 = MockObjectsUtil.getTestChannel1();
110         NotificationChannelBo channel2 = MockObjectsUtil.getTestChannel2();
111         NotificationProducerBo mockProducer1 = MockObjectsUtil.getTestProducer1();
112 
113         businessObjectDao.save(mockProducer1);
114         assertEquals(0, mockProducer1.getChannels().size());
115 
116         // add in a notification channel producer join object
117         channel1.getProducers().add(mockProducer1);
118 
119         businessObjectDao.save(channel1);
120 
121         assertEquals(1, channel1.getProducers().size());
122 
123         // ojb doesn't update the collections of the child in the relationship on save, despite auto-update...
124         // so I'm forced to load it again
125         mockProducer1 = (NotificationProducerBo) businessObjectDao.findById(NotificationProducerBo.class, mockProducer1.getId());
126         assertEquals(1, mockProducer1.getChannels().size());
127 
128         channel2.getProducers().add(mockProducer1);	
129         businessObjectDao.save(channel2);
130 
131         assertEquals(1, channel2.getProducers().size());
132 
133         mockProducer1 = (NotificationProducerBo) businessObjectDao.findById(NotificationProducerBo.class, mockProducer1.getId());
134 
135         assertEquals(2, mockProducer1.getChannels().size());
136     }
137 }