diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-12-15 23:28:52 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-12-16 00:24:47 +0400 |
commit | 8384ad87cde7d957485062d28913bf688f908bc8 (patch) | |
tree | 30c9dba46b6ff87d8297dcbd60054a3a3e030b6a /sonar-core | |
parent | 5bde32c2ca82a35e3a862db9361728b41fca843a (diff) | |
download | sonarqube-8384ad87cde7d957485062d28913bf688f908bc8.tar.gz sonarqube-8384ad87cde7d957485062d28913bf688f908bc8.zip |
Fix violation, add test
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java | 18 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/jpa/session/JpaDatabaseSessionTest.java | 13 |
2 files changed, 23 insertions, 8 deletions
diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java b/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java index 91317cba0f4..e2a06f97997 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java +++ b/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java @@ -19,6 +19,7 @@ */ package org.sonar.jpa.session; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; import org.sonar.api.database.DatabaseSession; @@ -223,10 +224,10 @@ public class JpaDatabaseSession extends DatabaseSession { } public <T> List<T> getResults(Class<T> entityClass) { - return getQueryForCriterias(entityClass, false, null).getResultList(); + return getQueryForCriterias(entityClass, false, (Object[]) null).getResultList(); } - Query getQueryForCriterias(Class<?> entityClass, boolean raiseError, Object... criterias) { + private Query getQueryForCriterias(Class<?> entityClass, boolean raiseError, Object... criterias) { if (criterias == null && raiseError) { throw new IllegalStateException("criterias parameter must be provided"); } @@ -251,14 +252,15 @@ public class JpaDatabaseSession extends DatabaseSession { return getEntityManager().createQuery(hql.toString()); } - private void buildCriteriasHQL(StringBuilder hql, Map<String, Object> mappedCriterias) { - for (Iterator<String> i = mappedCriterias.keySet().iterator(); i.hasNext(); ) { - String criteria = i.next(); - hql.append("o.").append(criteria); - if (mappedCriterias.get(criteria) == null) { + @VisibleForTesting + void buildCriteriasHQL(StringBuilder hql, Map<String, Object> mappedCriterias) { + for (Iterator<Map.Entry<String, Object>> i = mappedCriterias.entrySet().iterator(); i.hasNext();) { + Map.Entry<String, Object> entry = i.next(); + hql.append("o.").append(entry.getKey()); + if (entry.getValue() == null) { hql.append(" IS NULL"); } else { - hql.append("=:").append(criteria); + hql.append("=:").append(entry.getKey()); } if (i.hasNext()) { hql.append(" AND "); diff --git a/sonar-core/src/test/java/org/sonar/jpa/session/JpaDatabaseSessionTest.java b/sonar-core/src/test/java/org/sonar/jpa/session/JpaDatabaseSessionTest.java index d1314ea4316..697fe1ec035 100644 --- a/sonar-core/src/test/java/org/sonar/jpa/session/JpaDatabaseSessionTest.java +++ b/sonar-core/src/test/java/org/sonar/jpa/session/JpaDatabaseSessionTest.java @@ -27,8 +27,11 @@ import static org.mockito.Mockito.when; import org.junit.Before; import org.junit.Test; +import com.google.common.collect.Maps; + import java.util.Arrays; import java.util.Collections; +import java.util.Map; import javax.persistence.NonUniqueResultException; import javax.persistence.Query; @@ -65,4 +68,14 @@ public class JpaDatabaseSessionTest { assertThat(session.getSingleResult(query, "default"), is("default")); } + @Test + public void shouldBuildCriteriasHQL() { + StringBuilder hql = new StringBuilder(); + Map<String, Object> mappedCriterias = Maps.newHashMap(); + mappedCriterias.put("foo", "value"); + mappedCriterias.put("bar", null); + session.buildCriteriasHQL(hql, mappedCriterias); + assertThat(hql.toString(), is("o.foo=:foo AND o.bar IS NULL")); + } + } |