001 package org.hackystat.sensorbase.db.derby; 002 003 import static org.junit.Assert.assertEquals; 004 005 import java.util.ArrayList; 006 import java.util.List; 007 import org.junit.Test; 008 009 010 /** 011 * Tests the constructUriPattern method, which does a non-trivial transformation of the UriPatterns 012 * into LIKE clauses. This also helps document the behavior of the transformation by illustrating 013 * canonical input-output pairs. 014 * 015 * @author Philip Johnson 016 */ 017 public class TestConstructLikeClauses { 018 019 /** 020 * Tests that the LIKE clauses are constructed correctly. 021 */ 022 @Test 023 public void testUriPatterns() { 024 //Test special cases. 025 assertEquals("Test null", "", DerbyImplementation.constructLikeClauses(null)); 026 List<String> patts = new ArrayList<String>(); 027 patts.add("*"); 028 assertEquals("Test *", "", DerbyImplementation.constructLikeClauses(patts)); 029 patts.set(0, "**"); 030 assertEquals("Test **", "", DerbyImplementation.constructLikeClauses(patts)); 031 // Note that we need to escape occurrences of '\' character below. 032 patts.set(0, "*.java"); 033 assertEquals("Test single pattern, no path separator", 034 " AND ((RESOURCE LIKE '%.java' ESCAPE '`') )", 035 DerbyImplementation.constructLikeClauses(patts)); 036 037 // Same return value, whether */foo/* or *\foo\*. 038 String singleSlash = 039 " AND ((RESOURCE LIKE '%/foo/%' ESCAPE '`') OR (RESOURCE LIKE '%\\foo\\%' ESCAPE '`') )"; 040 patts.set(0, "*/foo/*"); 041 assertEquals("Test single pattern, forward slash", singleSlash, 042 DerbyImplementation.constructLikeClauses(patts)); 043 044 patts.set(0, "*\\foo\\*"); 045 assertEquals("Test single pattern, backward slash", singleSlash, 046 DerbyImplementation.constructLikeClauses(patts)); 047 048 // Test escape of an SQL wildcard. 049 patts.set(0, "foo_bar"); 050 assertEquals("Test single pattern, wildcard", " AND ((RESOURCE LIKE 'foo`_bar' ESCAPE '`') )", 051 DerbyImplementation.constructLikeClauses(patts)); 052 053 // Test multiple URIs, path separators, and escapes. 054 patts.set(0, "*/foo_bar/*"); 055 patts.add("*\\Test*.java"); 056 patts.add("baz.c"); 057 058 String results = 059 " AND ((RESOURCE LIKE '%/foo`_bar/%' ESCAPE '`') OR " + 060 "(RESOURCE LIKE '%\\foo`_bar\\%' ESCAPE '`') OR " + 061 "(RESOURCE LIKE '%/Test%.java' ESCAPE '`') OR " + 062 "(RESOURCE LIKE '%\\Test%.java' ESCAPE '`') OR " + 063 "(RESOURCE LIKE 'baz.c' ESCAPE '`') )"; 064 065 //System.out.println(DerbyImplementation.constructLikeClauses(patts)); 066 067 assertEquals("Test multiple URIs, path separators, escapes", results, 068 DerbyImplementation.constructLikeClauses(patts)); 069 } 070 }