001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krad.jpa;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.junit.Test;
020 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
021 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria.QueryByCriteriaType;
022 import org.kuali.rice.krad.test.document.bo.Account;
023 import org.kuali.test.KRADTestCase;
024
025 import static org.junit.Assert.assertTrue;
026
027 /**
028 * Junit test cases for org.kuali.rice.core.jpa.criteria.ReportQueryByCriteria
029 *
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032 public class ReportQueryByCriteriaTest extends KRADTestCase {
033
034 @Test
035 public void testCriteriaToReportQuery_emptySelect() throws Exception {
036 Criteria criteria = new Criteria(Account.class.getName().substring(Account.class.getPackage().getName().length()+1), "a");
037
038 String query = criteria.toQuery(QueryByCriteriaType.SELECT, new String[0]);
039 assertTrue(StringUtils.equalsIgnoreCase(query, "select a from Account as a"));
040 }
041
042 @Test
043 public void testCriteriaToReportQuery_singleFieldSelect() throws Exception {
044 Criteria criteria = new Criteria(Account.class.getName().substring(Account.class.getPackage().getName().length()+1), "a");
045
046 String[] attr = new String[1];
047 attr[0] = "number";
048
049 String query = criteria.toQuery(QueryByCriteriaType.SELECT, attr);
050 assertTrue(StringUtils.equalsIgnoreCase(query, "select a.number from Account as a"));
051 }
052
053 @Test
054 public void testCriteriaToReportQuery_multipleFieldSelect() throws Exception {
055 Criteria criteria = new Criteria(Account.class.getName().substring(Account.class.getPackage().getName().length()+1), "a");
056
057 String[] attr = new String[3];
058 attr[0] = "number";
059 attr[1] = "name";
060 attr[2] = "amId";
061
062 String query = criteria.toQuery(QueryByCriteriaType.SELECT, attr);
063 assertTrue(StringUtils.equalsIgnoreCase(query, "select a.number, a.name, a.amId from Account as a"));
064 }
065
066 }