*/\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
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
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
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
\r
\r
\r
-\r
+ @Test\r
public void getCorrelationRulesByCondition_db_exception() throws Exception {\r
\r
thrown.expect(CorrelationException.class);\r
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