From 2a7cf2d3d4a5a3551346cf52a19c27ef21ae4946 Mon Sep 17 00:00:00 2001 From: GuangrongFu Date: Thu, 23 Feb 2017 17:04:23 +0800 Subject: Add a New Test Class Change-Id: I4d9eb81d197499581f672c9f65de682314eef577 Issue-ID: HOLMES-47 Signed-off-by: GuangrongFu --- .../holmes/rulemgt/db/CorrelationRuleQueryDao.java | 28 ++++++++++++++++++---- .../rulemgt/db/CorrelationRuleQueryDaoTest.java | 7 ++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java index 62f8bad..b3e6b38 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java @@ -15,7 +15,6 @@ */ package org.openo.holmes.rulemgt.db; -import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; @@ -93,10 +92,16 @@ public class CorrelationRuleQueryDao { String whereSql = " WHERE "; for (Field field : fields) { - PropertyDescriptor pd = new PropertyDescriptor(field.getName(), - clazz); - Method getMethod = pd.getReadMethod(); - Object o = getMethod.invoke(ruleQueryCondition); + String methodName = getMethodName(field); + + // If these lines are removed, Jacoco will cause an exception when calculation the coverage of UT + // Remove it if someday Jacoco solve this problem + if (methodName.contains("jacoco")){ + continue; + } + + Method method = clazz.getMethod(methodName); + Object o = method.invoke(ruleQueryCondition); if (o != null) { String tempName = field.getName(); if ("enabled".equals(tempName) && (int) o != RuleMgtConstant.STATUS_RULE_ALL) { @@ -118,4 +123,17 @@ public class CorrelationRuleQueryDao { throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED, e); } } + + private String getMethodName(Field field){ + String ret = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);; + Class clazz = field.getDeclaringClass(); + + if (clazz.equals(Boolean.class)){ + ret = "is" + ret; + }else{ + ret = "get" + ret; + } + + return ret; + } } diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java index e0331de..0b1d151 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java @@ -30,15 +30,12 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; import org.openo.holmes.common.api.entity.CorrelationRule; import org.openo.holmes.common.exception.CorrelationException; import org.openo.holmes.common.utils.DbDaoUtil; import org.openo.holmes.common.utils.I18nProxy; import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition; import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; -import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.rule.PowerMockRule; import org.powermock.reflect.Whitebox; import org.skife.jdbi.v2.Handle; @@ -77,7 +74,7 @@ public class CorrelationRuleQueryDaoTest { - + @Test public void getCorrelationRulesByCondition_db_exception() throws Exception { thrown.expect(CorrelationException.class); @@ -96,7 +93,7 @@ public class CorrelationRuleQueryDaoTest { PowerMock.verifyAll(); } - + @Test public void getCorrelationRulesByCondition_normal() throws Exception { EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle); EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query); -- cgit 1.2.3-korg