/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.pegadi.sqlsearch; import junit.framework.TestCase; import java.util.Arrays; import java.util.List; public class SearchTermTest extends TestCase { SearchTerm one; SearchTerm two; public void setUp() { one = new SearchTerm() { public String whereClause() { return "Article.ID1 = 1"; } public List<String> getTables() { return Arrays.asList("Article"); } }; two = new SearchTerm() { public String whereClause() { return "Section.ID2 = 2"; } public List<String> getTables() { return Arrays.asList("Section"); } }; } public void testGetQuery() { String query1 = "SELECT ID1 FROM Article WHERE Article.ID1 = 1"; String query2 = "SELECT ID2 FROM Section WHERE Section.ID2 = 2"; String andClause = "SELECT Article.ID1 FROM Article, Section WHERE (Article.ID1 = 1 AND Section.ID2 = 2)"; assertEquals(query1, one.getQuery("ID1")); assertEquals(query2, two.getQuery("ID2")); AndTerm andTerm = new AndTerm(one, two); assertEquals(andClause, andTerm.getQuery("Article.ID1")); } public void testIgnoreTables() { String query1 = "SELECT ID1 FROM Article WHERE Article.ID1 = 1"; SearchTerm one = new SearchTerm() { public String whereClause() { return "(Article.refJournalist = '1' OR CoJournalists.refJournalist = '1')"; } public List<String> getTables() { return Arrays.asList("Article LEFT JOIN CoJournalists ON Article.ID=CoJournalists.refArticle"); } public List<String> getIgnoreTables() { return Arrays.asList("Article", "CoJournalists"); } }; SearchTerm two = new SearchTerm() { public String whereClause() { return "Section.ID = 7"; } public List<String> getTables() { return Arrays.asList("Section"); } }; AndTerm andTerm = new AndTerm(one, two); String query = "SELECT Article.ID FROM Article LEFT JOIN CoJournalists ON Article.ID=CoJournalists.refArticle, Section WHERE ((Article.refJournalist = '1' OR CoJournalists.refJournalist = '1') AND Section.ID = 7)"; assertEquals(query, andTerm.getQuery("Article.ID")); } public void testExistsInVector() { SearchTerm one = new SearchTerm() { public String whereClause() { return "Article.ID = 1"; } public List<String> getTables() { return Arrays.asList("Article"); } }; SearchTerm two = new SearchTerm() { public String whereClause() { return "Article.ID = 7"; } public List<String> getTables() { return Arrays.asList("Article"); } }; String query = "SELECT Article.ID FROM Article WHERE (Article.ID = 1 OR Article.ID = 7)"; OrTerm orTerm = new OrTerm(one, two); assertEquals(query, orTerm.getQuery("Article.ID")); } }