We have uograded the clover to the current version and many of the test cases are failing becasue of the cast failing.
For example :
java.lang.reflect.Field field;
String fieldValue = (String) field.get(segment);
my test case failed with this error
java.lang.ClassCastException: com_atlassian_clover.TestNameSniffer$1 cannot be cast to java.lang.String
I have tried //CLOVER:OFF for that function but that didn't help, any idea how to get around this problem?
Thanks in advance
Thanks.But why ///CLOVER:OFF will not disable the Clover static field TestNameSniffer.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
OK. So this is an explanation, as I wrote before.
You have to exclude the entire class from being instrumented.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have created a test case scenario that replicate this error.
Junit test
import static org.junit.Assert.*; import java.lang.reflect.Field; import org.junit.BeforeClass; import org.junit.Test; import my.util.FillerClover; public class CloverCastTest { static Foo foo = null; @BeforeClass public static void setupBefoerClass(){ foo = new Foo(); foo.setS("foo"); } @Test public void testReflection() throws Exception { String fieldValue = ""; for (Field field : foo.getClass().getDeclaredFields()) { if (field.getType() == String.class) { fieldValue = (String) field.get(foo); } } FillerClover.fillSpacesAndZeros(foo); assertTrue("String value ?", fieldValue.equals("foo")); } } class Foo { public String s; public String getS() { return s; } public void setS(String s) { this.s = s; } }
FillerClover source
import my.annotation.Character; import my.annotation.Number; import java.lang.reflect.Field; import org.apache.commons.lang3.StringUtils; public class FillerClover { public static Object fillSpacesAndZeros(Object object) { for (Field field : object.getClass().getDeclaredFields()) { fillSpacesOrZeros(object, field); } return object; } private static void fillSpacesOrZeros(Object object, Field field) { field.setAccessible(true); String fieldValue = StringUtils.EMPTY; try { if (field.getName().equals("lineItemList")) { return ; } fieldValue = (String) field.get(object); if (fieldValue == null ) fieldValue = StringUtils.EMPTY; Character character = field.getAnnotation(Character.class); Number number = field.getAnnotation(Number.class); if (character != null) { fieldValue = StringUtils.left(fieldValue, character.value()); field.set(object, StringUtils.rightPad(fieldValue, character.value())); } else if (number != null) { fieldValue = StringUtils.right(fieldValue, number.value()); field.set(object, StringUtils.leftPad(fieldValue, number.value(), '0')); } } catch (IllegalArgumentException e) { e.printStackTrace(); StringBuilder sb = new StringBuilder(); sb.append("error while parsing ").append(field).append(" with ").append(field.getName()); System.out.println(sb.toString()); } catch (IllegalAccessException e) { e.printStackTrace(); StringBuilder sb = new StringBuilder(); sb.append("error while parsing ").append(field).append(" with ").append(field.getName()); System.out.println(sb.toString()); } } }
The error
------------------------------------------------------------------------------- Test set: my.se.CloverCastTest ------------------------------------------------------------------------------- Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.124 sec <<< FAILURE! testReflection(gov.dhs.cbp.se.CloverCastTest) Time elapsed: 0.015 sec <<< ERROR! java.lang.ClassCastException: com_atlassian_clover.TestNameSniffer$1 cannot be cast to java.lang.String at my.util.FillerClover.fillSpacesOrZeros(FillerClover.java:36) at my.util.FillerClover.fillSpacesAndZeros(FillerClover.java:18) at my.se.CloverCastTest.__CLR4_0_0syumm48ng(CloverCastTest.java:39) at my.se.CloverCastTest.testReflection(CloverCastTest.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) ...
One interestiing point is that the cast error did not happend in the main line code
if (field.getType() == String.class) { fieldValue = (String) field.get(foo); // <---- no error }
Error occur in the FillerClover Static class???
java.lang.ClassCastException: com_atlassian_clover.TestNameSniffer$1 cannot be cast to java.lang.String
at my.util.FillerClover.fillSpacesOrZeros(FillerClover.java:36)
fieldValue = (String) field.get(object); //<--- line 36
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.