@Test public void testTupleRangeCheck() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { // test private static final int[] rangeCheckFields(int[] fields, int maxAllowedField) Method rangeCheckFieldsMethod = Whitebox.getMethod(Keys.class, "rangeCheckFields", int[].class, int.class); // valid indexes rangeCheckFieldsMethod.invoke(null, new int[]{1, 2, 3, 4}, 4); // corner case tests rangeCheckFieldsMethod.invoke(null, new int[] {0}, 0); Throwable ex = null; try { // throws illegal argument. rangeCheckFieldsMethod.invoke(null, new int[] {5}, 0); } catch(Throwable iae) { ex = iae; } Assert.assertNotNull(ex); }
@Test public void testQualifiedClassName() throws Exception { final Method method = Whitebox.getMethod(FinalClassCheck.class, "getQualifiedClassName", String.class, String.class, String.class); Assert.assertEquals("unexpected result", "ClassName", method.invoke(null, "", null, "ClassName")); }
@Test public void testCalculateBufferLength() throws Exception { final Method calculateBufferLengthMethod = Whitebox.getMethod(AuditEventDefaultFormatter.class, "calculateBufferLength", AuditEvent.class, int.class); final LocalizedMessage localizedMessage = new LocalizedMessage(1, 1, "messages.properties", "key", null, SeverityLevel.ERROR, null, getClass(), null); final AuditEvent auditEvent = new AuditEvent(new Object(), "fileName", localizedMessage); final int result = (int) calculateBufferLengthMethod.invoke(null, auditEvent, SeverityLevel.ERROR.ordinal()); assertEquals("Buffer length is not expected", 54, result); }
/** * Method countMatches is used only inside isOnlyUnicodeValidChars method, and when * pitest mutates 316:13 countMatches++ to countMatches-- it makes no difference for * isOnlyUnicodeValidChars method as it applies countMatches to both cases in comparison. * It is possible to kill mutation in countMatches method by changing code in * isOnlyUnicodeValidChars, but it creates new uncoverable mutations and makes code harder * to understand. * * @throws Exception when code tested throws some exception */ @Test public void testCountMatches() throws Exception { final Method countMatches = Whitebox.getMethod(AvoidEscapedUnicodeCharactersCheck.class, "countMatches", Pattern.class, String.class); final AvoidEscapedUnicodeCharactersCheck check = new AvoidEscapedUnicodeCharactersCheck(); final int actual = (int) countMatches.invoke(check, Pattern.compile("\\\\u[a-fA-F0-9]{4}"), "\\u1234"); assertEquals("Unexpected matches count", 1, actual); }
/** * We can not cover this mutation because it force all imports to be non static, * but static imports are ignored, so we will not see any affect on validation. * We could remove this method at all, and it will work correctly as we can not use * class with name "", but in this case internal collection will have short names * as "" that will not make problems, but will be weird in debug. * * @throws Exception when exception occurred during execution. */ @Test public void testIsStarImportNullAst() throws Exception { final DetailAST importAst = JavaParser.parseFile( new File(getPath("InputVisibilityModifierIsStarImport.java")), JavaParser.Options.WITHOUT_COMMENTS).getNextSibling(); final VisibilityModifierCheck check = new VisibilityModifierCheck(); final Method isStarImport = Whitebox.getMethod(VisibilityModifierCheck.class, "isStarImport", DetailAST.class); assertTrue("Should return true when star import is passed", (boolean) isStarImport.invoke(check, importAst)); }
@Test public void getGetSignature_Serializable() throws Exception { LogChannel log = mock( LogChannel.class ); whenNew( LogChannel.class ).withAnyArguments().thenReturn( log ); ValueMetaSerializable v = new ValueMetaSerializable( "Data" ); String accessor = FieldHelper.getAccessor( true, "Data" ); assertEquals( "Object Data = get(Fields.In, \"Data\").getObject(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getObject", Object[].class ) ); }
@Test public void getGetSignature_Boolean() { ValueMetaBoolean v = new ValueMetaBoolean( "Value" ); String accessor = FieldHelper.getAccessor( true, "Value" ); assertEquals( "Boolean Value = get(Fields.In, \"Value\").getBoolean(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getBoolean", Object[].class ) ); }
@Test public void getGetSignature_String() { ValueMetaString v = new ValueMetaString( "Name" ); String accessor = FieldHelper.getAccessor( true, "Name" ); assertEquals( "String Name = get(Fields.In, \"Name\").getString(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getString", Object[].class ) ); }
@Test public void getGetSignature_Date() { ValueMetaDate v = new ValueMetaDate( "DT" ); String accessor = FieldHelper.getAccessor( true, "DT" ); assertEquals( "Date DT = get(Fields.In, \"DT\").getDate(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getDate", Object[].class ) ); }
@Test public void getGetSignature_InetAddress() { ValueMetaInternetAddress v = new ValueMetaInternetAddress( "IP" ); String accessor = FieldHelper.getAccessor( true, "IP" ); assertEquals( "InetAddress IP = get(Fields.In, \"IP\").getInetAddress(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getInetAddress", Object[].class ) ); }
@Test public void getGetSignature_Timestamp() { ValueMetaTimestamp v = new ValueMetaTimestamp( "TS" ); String accessor = FieldHelper.getAccessor( true, "TS" ); assertEquals( "Timestamp TS = get(Fields.In, \"TS\").getTimestamp(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getTimestamp", Object[].class ) ); }
@Test public void getGetSignature_Binary() { ValueMetaBinary v = new ValueMetaBinary( "Data" ); String accessor = FieldHelper.getAccessor( true, "Data" ); assertEquals( "byte[] Data = get(Fields.In, \"Data\").getBinary(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getBinary", Object[].class ) ); }
@Test public void getGetSignature_BigNumber() { ValueMetaBigNumber v = new ValueMetaBigNumber( "Number" ); String accessor = FieldHelper.getAccessor( true, "Number" ); assertEquals( "BigDecimal Number = get(Fields.In, \"Number\").getBigDecimal(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getBigDecimal", Object[].class ) ); }
@Test public void getGetSignature_Integer() { ValueMetaInteger v = new ValueMetaInteger( "Value" ); String accessor = FieldHelper.getAccessor( true, "Value" ); assertEquals( "Long Value = get(Fields.In, \"Value\").getLong(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getLong", Object[].class ) ); }
@Test public void getGetSignature_Number() { ValueMetaNumber v = new ValueMetaNumber( "Value" ); String accessor = FieldHelper.getAccessor( true, "Value" ); assertEquals( "Double Value = get(Fields.In, \"Value\").getDouble(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getDouble", Object[].class ) ); }
/** * Pitest requires us to specify more concrete lower bound for condition for * ParenPadCheck#isAcceptableToken as nodes of first several types like CTOR_DEF, * METHOD_DEF will never reach this method. It is hard to recreate conditions for * all tokens to go through this method. We do not want to change main code to have * this set ok tokens more exact, because it will not be ease to understand. * So we have to use reflection to be sure all * acceptable tokens pass that check. */ @Test public void testIsAcceptableToken() throws Exception { final ParenPadCheck check = new ParenPadCheck(); final Method method = Whitebox.getMethod(ParenPadCheck.class, "isAcceptableToken", DetailAST.class); final DetailAST ast = new DetailAST(); final String message = "Expected that all acceptable tokens will pass isAcceptableToken " + "method, but some token don't: "; for (int token : check.getAcceptableTokens()) { ast.setType(token); assertTrue(message + TokenUtil.getTokenName(token), (boolean) method.invoke(check, ast)); } }
@SuppressWarnings("unchecked") public <T> T execute(Callable<T> callable) { assertArgumentNotNull(callable, "callable"); return (T) execute(callable, Whitebox.getMethod(callable.getClass(), "call")); }
/** * Suppress a specific method call. Use this for overloaded methods. */ public static synchronized void suppressMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) { Method method = null; if (parameterTypes.length > 0) { method = Whitebox.getMethod(clazz, methodName, parameterTypes); } else { method = WhiteboxImpl.findMethodOrThrowException(clazz, methodName, parameterTypes); } MockRepository.addMethodToSuppress(method); }
/** * Suppress a specific method call. Use this for overloaded methods. */ public static synchronized void suppressMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) { Method method = null; if (parameterTypes.length > 0) { method = Whitebox.getMethod(clazz, methodName, parameterTypes); } else { method = WhiteboxImpl.findMethodOrThrowException(clazz, methodName, parameterTypes); } MockRepository.addMethodToSuppress(method); }
/** * Every time the {@link Dependency#getData()} method is invoked we return a * custom instance of a {@link DataObject}. */ @Override public void applyInterceptionPolicy(MockPolicyInterceptionSettings settings) { final Method getDataMethod = Whitebox.getMethod(Dependency.class); final DataObject dataObject = new DataObject("Policy generated data object"); settings.stubMethod(getDataMethod, dataObject); } }