### Question:
JpaMatchupStorage implements MatchupStorage { static String applyPatternAndCondition(String queryString, String condition, long pattern) { if (condition != null) { if (pattern != 0) { queryString = queryString.replaceAll("where r.time", "where m.pattern & ?4 = ?4 and " + condition + " and r.time"); } else { queryString = queryString.replaceAll("where r.time", "where " + condition + " and r.time"); } } else if (pattern != 0) { queryString = queryString.replaceAll("where r.time", "where m.pattern & ?4 = ?4 and r.time"); } return queryString; } JpaMatchupStorage(PersistenceManager persistenceManager); @Override int getCount(MatchupQueryParameter parameter); @SuppressWarnings("unchecked") @Override List<Matchup> get(MatchupQueryParameter parameter); @SuppressWarnings("unchecked") @Override List<Matchup> getForMmd(MatchupQueryParameter parameter); @Override Matchup get(int matchupId); static final String SQL_FOR_MATCHUPS; }### Answer:
@Test public void testApplyPatternAndCondition_patternAndCondition() { final String sql = "bla bla bla where r.time = yesterday"; final String condition = "absolutely nonsense"; final String sqlApplied = JpaMatchupStorage.applyPatternAndCondition(sql, condition, 987); assertEquals("bla bla bla where m.pattern & ?4 = ?4 and absolutely nonsense and r.time = yesterday", sqlApplied); }
@Test public void testApplyPatternAndCondition_onlyCondition() { final String sql = "yada yada where r.time = christmas"; final String condition = "want_gift = TRUE"; final String sqlApplied = JpaMatchupStorage.applyPatternAndCondition(sql, condition, 0); assertEquals("yada yada where want_gift = TRUE and r.time = christmas", sqlApplied); }
@Test public void testApplyPatternAndCondition_onlyPattern() { final String sql = "select something cool where r.time = easter_last_year"; final String sqlApplied = JpaMatchupStorage.applyPatternAndCondition(sql, null, 564); assertEquals("select something cool where m.pattern & ?4 = ?4 and r.time = easter_last_year", sqlApplied); }
@Test public void testApplyPatternAndCondition_neitherPatternNorCondition() { final String sql = "select beer where r.time = this_evening"; final String sqlApplied = JpaMatchupStorage.applyPatternAndCondition(sql, null, 0); assertEquals(sql, sqlApplied); }