View Javadoc
1   /**
2    * Copyright 2005-2013 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.rice.krad.test.KRADTestCase;
24  
25  import static org.junit.Assert.assertTrue;
26  
27  /**
28   * ReportQueryByCriteriaTest tests {@link Criteria#Criteria(String, String)} and
29   * {@link Criteria#toQuery(org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria.QueryByCriteriaType, String[])}
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class ReportQueryByCriteriaTest extends KRADTestCase {
34      
35      @Test
36      /**
37       * test that a query without attribute values is constructed using the given entity name
38       */
39      public void testCriteriaToReportQuery_emptySelect() throws Exception {
40          Criteria criteria = new Criteria(Account.class.getName().substring(Account.class.getPackage().getName().length()+1), "a");
41          
42          String query = criteria.toQuery(QueryByCriteriaType.SELECT, new String[0]);
43          assertTrue(StringUtils.equalsIgnoreCase(query, "select a from Account as a"));
44      }
45  
46      @Test
47  
48      /**
49       * test that a query with one attribute value is constructed as expected using the given entity name
50       */
51      public void testCriteriaToReportQuery_singleFieldSelect() throws Exception {
52          Criteria criteria = new Criteria(Account.class.getName().substring(Account.class.getPackage().getName().length()+1), "a");
53          
54          String[] attr = new String[1];
55          attr[0] = "number";
56          
57          String query = criteria.toQuery(QueryByCriteriaType.SELECT, attr);
58          assertTrue(StringUtils.equalsIgnoreCase(query, "select a.number from Account as a"));
59      }
60      
61      @Test
62      /**
63       * test that a query with multiple attribute values is constructed as expected using the given entity name
64       */
65      public void testCriteriaToReportQuery_multipleFieldSelect() throws Exception {
66          Criteria criteria = new Criteria(Account.class.getName().substring(Account.class.getPackage().getName().length()+1), "a");
67          
68          String[] attr = new String[3];
69          attr[0] = "number";
70          attr[1] = "name";
71          attr[2] = "amId";
72          
73          String query = criteria.toQuery(QueryByCriteriaType.SELECT, attr);
74          assertTrue(StringUtils.equalsIgnoreCase(query, "select a.number, a.name, a.amId from Account as a"));
75      }
76  
77  }