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.krad.jpa;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.junit.Test;
20  import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
21  import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria.QueryByCriteriaType;
22  import org.kuali.rice.krad.test.document.bo.Account;
23  import org.kuali.test.KRADTestCase;
24  
25  import static org.junit.Assert.assertTrue;
26  
27  /**
28   * Junit test cases for org.kuali.rice.core.jpa.criteria.ReportQueryByCriteria
29   * 
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class ReportQueryByCriteriaTest extends KRADTestCase {
33      
34      @Test
35      public void testCriteriaToReportQuery_emptySelect() throws Exception {
36          Criteria criteria = new Criteria(Account.class.getName().substring(Account.class.getPackage().getName().length()+1), "a");
37          
38          String query = criteria.toQuery(QueryByCriteriaType.SELECT, new String[0]);
39          assertTrue(StringUtils.equalsIgnoreCase(query, "select a from Account as a"));
40      }
41  
42      @Test
43      public void testCriteriaToReportQuery_singleFieldSelect() throws Exception {
44          Criteria criteria = new Criteria(Account.class.getName().substring(Account.class.getPackage().getName().length()+1), "a");
45          
46          String[] attr = new String[1];
47          attr[0] = "number";
48          
49          String query = criteria.toQuery(QueryByCriteriaType.SELECT, attr);
50          assertTrue(StringUtils.equalsIgnoreCase(query, "select a.number from Account as a"));
51      }
52      
53      @Test
54      public void testCriteriaToReportQuery_multipleFieldSelect() throws Exception {
55          Criteria criteria = new Criteria(Account.class.getName().substring(Account.class.getPackage().getName().length()+1), "a");
56          
57          String[] attr = new String[3];
58          attr[0] = "number";
59          attr[1] = "name";
60          attr[2] = "amId";
61          
62          String query = criteria.toQuery(QueryByCriteriaType.SELECT, attr);
63          assertTrue(StringUtils.equalsIgnoreCase(query, "select a.number, a.name, a.amId from Account as a"));
64      }
65  
66  }