@Test
public void dataDrivenInvalidTransactions() throws Exception {

    JsonNode json = new ObjectMapper().readTree(new InputStreamReader(getClass().getResourceAsStream(
            "tx_invalid.json"), Charsets.UTF_8));
    for (JsonNode test : json) {
        if (test.isArray() && test.size() == 1 && test.get(0).isTextual())
            continue; // This is a comment.
        Map<TransactionOutPoint, Script> scriptPubKeys = parseScriptPubKeys(test.get(0));
        Transaction transaction = PARAMS.getDefaultSerializer().makeTransaction(HEX.decode(test.get(1).asText().toLowerCase()));
        Set<VerifyFlag> verifyFlags = parseVerifyFlags(test.get(2).asText());
        boolean valid = true;
        try {
            transaction.verify();
        } catch (VerificationException e) {
            valid = false;
        }
        // Bitcoin Core checks this case in CheckTransaction, but we leave it to
        // later where we will see an attempt to double-spend, so we explicitly check here
        HashSet<TransactionOutPoint> set = new HashSet<TransactionOutPoint>();
        for (TransactionInput input : transaction.getInputs()) {
            if (set.contains(input.getOutpoint()))
                valid = false;
            set.add(input.getOutpoint());
        }
        for (int i = 0; i < transaction.getInputs().size() && valid; i++) {
            TransactionInput input = transaction.getInputs().get(i);
            assertTrue(scriptPubKeys.containsKey(input.getOutpoint()));
            try {
                input.getScriptSig().correctlySpends(transaction, i, scriptPubKeys.get(input.getOutpoint()),
                        verifyFlags);
            } catch (VerificationException e) {
                valid = false;
            }
        }
        if (valid)
            fail();
    }
}