Add a New Test Class
authorGuangrongFu <fu.guangrong@zte.com.cn>
Thu, 23 Feb 2017 09:04:23 +0000 (17:04 +0800)
committerGuangrongFu <fu.guangrong@zte.com.cn>
Thu, 23 Feb 2017 09:05:03 +0000 (17:05 +0800)
Change-Id: I4d9eb81d197499581f672c9f65de682314eef577
Issue-ID: HOLMES-47
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java
rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java

index 62f8bad..b3e6b38 100644 (file)
@@ -15,7 +15,6 @@
  */\r
 package org.openo.holmes.rulemgt.db;\r
 \r
-import java.beans.PropertyDescriptor;\r
 import java.lang.reflect.Field;\r
 import java.lang.reflect.Method;\r
 import java.util.ArrayList;\r
@@ -93,10 +92,16 @@ public class CorrelationRuleQueryDao {
             String whereSql = " WHERE ";\r
 \r
             for (Field field : fields) {\r
-                PropertyDescriptor pd = new PropertyDescriptor(field.getName(),\r
-                        clazz);\r
-                Method getMethod = pd.getReadMethod();\r
-                Object o = getMethod.invoke(ruleQueryCondition);\r
+                String methodName = getMethodName(field);\r
+\r
+                // If these lines are removed, Jacoco will cause an exception when calculation the coverage of UT\r
+                // Remove it if someday Jacoco solve this problem\r
+                if (methodName.contains("jacoco")){\r
+                    continue;\r
+                }\r
+\r
+                Method method = clazz.getMethod(methodName);\r
+                Object o = method.invoke(ruleQueryCondition);\r
                 if (o != null) {\r
                     String tempName = field.getName();\r
                     if ("enabled".equals(tempName) && (int) o != RuleMgtConstant.STATUS_RULE_ALL) {\r
@@ -118,4 +123,17 @@ public class CorrelationRuleQueryDao {
             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED, e);\r
         }\r
     }\r
+\r
+    private String getMethodName(Field field){\r
+        String ret = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);;\r
+        Class clazz = field.getDeclaringClass();\r
+\r
+        if (clazz.equals(Boolean.class)){\r
+            ret = "is" + ret;\r
+        }else{\r
+            ret = "get" + ret;\r
+        }\r
+\r
+        return ret;\r
+    }\r
 }\r
index e0331de..0b1d151 100644 (file)
@@ -30,15 +30,12 @@ import org.junit.Before;
 import org.junit.Rule;\r
 import org.junit.Test;\r
 import org.junit.rules.ExpectedException;\r
-import org.junit.runner.RunWith;\r
 import org.openo.holmes.common.api.entity.CorrelationRule;\r
 import org.openo.holmes.common.exception.CorrelationException;\r
 import org.openo.holmes.common.utils.DbDaoUtil;\r
 import org.openo.holmes.common.utils.I18nProxy;\r
 import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;\r
 import org.powermock.api.easymock.PowerMock;\r
-import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;\r
-import org.powermock.modules.junit4.PowerMockRunner;\r
 import org.powermock.modules.junit4.rule.PowerMockRule;\r
 import org.powermock.reflect.Whitebox;\r
 import org.skife.jdbi.v2.Handle;\r
@@ -77,7 +74,7 @@ public class CorrelationRuleQueryDaoTest {
 \r
 \r
 \r
-\r
+    @Test\r
     public void getCorrelationRulesByCondition_db_exception() throws Exception {\r
 \r
         thrown.expect(CorrelationException.class);\r
@@ -96,7 +93,7 @@ public class CorrelationRuleQueryDaoTest {
         PowerMock.verifyAll();\r
     }\r
 \r
-    \r
+    @Test\r
     public void getCorrelationRulesByCondition_normal() throws Exception {\r
         EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);\r
         EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);\r