下面列出了怎么用org.junit.jupiter.params.converter.ConvertWith的API类实例代码及写法,或者点击链接到github查看源代码。
@ParameterizedTest
@CsvSource({
"0.0.65537, 10, 20, admin-key, submit-key, '', 1000000, 1, 30",
"0.0.2147483647, 9223372036854775807, 2147483647, admin-key, '', memo, 1000001, 1, 30",
"0.0.1, -9223372036854775808, -2147483648, '', '', memo, 1000002, , ,",
"0.0.55, 10, 20, admin-key, submit-key, memo, 1000003, 1, 30"
})
void createTopicTest(@ConvertWith(TopicIdConverter.class) TopicID topicId, long expirationTimeSeconds,
int expirationTimeNanos, @ConvertWith(KeyConverter.class) Key adminKey,
@ConvertWith(KeyConverter.class) Key submitKey, String memo, long consensusTimestamp,
Long autoRenewAccountNum, Long autoRenewPeriod) throws Exception {
var responseCode = ResponseCodeEnum.SUCCESS;
var transaction = createCreateTopicTransaction(adminKey, submitKey, memo, autoRenewAccountNum, autoRenewPeriod);
var transactionRecord = createTransactionRecord(topicId, consensusTimestamp, responseCode);
parseRecordItemAndCommit(new RecordItem(transaction, transactionRecord));
long entityCount = autoRenewAccountNum != null ? 4 : 3; // Node, payer, topic & optionally autorenew
var entity = getTopicEntity(topicId);
assertTransactionInRepository(responseCode, consensusTimestamp, entity.getId());
assertEquals(entityCount, entityRepository.count());
var expectedEntity = createTopicEntity(topicId, null, null, adminKey, submitKey, memo, autoRenewAccountNum,
autoRenewPeriod);
assertThat(entity).isEqualTo(expectedEntity);
}
@ParameterizedTest(name = "verifyHashChain {5}")
@CsvSource({
// @formatter:off
"'', '', 1970-01-01T00:00:00Z, 2000-01-01T10_00_00.000000Z.rcd, true, passes if both hashes are empty",
"xx, '', 1970-01-01T00:00:00Z, 2000-01-01T10_00_00.000000Z.rcd, true, passes if hash mismatch and expected hash is empty", // starting stream in middle
"'', xx, 1970-01-01T00:00:00Z, 2000-01-01T10_00_00.000000Z.rcd, false, fails if hash mismatch and actual hash is empty", // bad db state
"xx, yy, 1970-01-01T00:00:00Z, 2000-01-01T10_00_00.000000Z.rcd, false, fails if hash mismatch and hashes are non-empty",
"xx, yy, 2000-01-01T10:00:00.000001Z, 2000-01-01T10_00_00.000000Z.rcd, true, passes if hash mismatch but verifyHashAfter is after filename",
"xx, yy, 2000-01-01T10:00:00.000001Z, 2000-01-01T10_00_00.000000Z.rcd, true, passes if hash mismatch but verifyHashAfter is same as filename",
"xx, yy, 2000-01-01T09:59:59.999999Z, 2000-01-01T10_00_00.000000Z.rcd, false, fails if hash mismatch and verifyHashAfter is before filename",
"xx, xx, 1970-01-01T00:00:00Z, 2000-01-01T10_00_00.000000Z.rcd, true, passes if hashes are equal"
// @formatter:on
})
public void testVerifyHashChain(String actualPrevFileHash, String expectedPrevFileHash,
@ConvertWith(InstantConverter.class) Instant verifyHashAfter, String fileName,
Boolean expectedResult, String testName) {
assertThat(Utility.verifyHashChain(actualPrevFileHash, expectedPrevFileHash, verifyHashAfter, fileName))
.isEqualTo(expectedResult);
}
@ParameterizedTest
@CsvSource({
"JRE_VERSION_160, 1.6.0_45, 0",
"JRE_VERSION_170, 1.6.0_45, 1",
"JRE_VERSION_170, 1.7.0_101, 0",
"JRE_VERSION_180, 1.7.0_101, 1",
"JRE_VERSION_180, 1.8.0_20, 0",
"JRE_VERSION_170, 1.8.0_20, -1",
"JRE_VERSION_160, 1.8.0_20, -1",
"JRE_VERSION_9, 1.8.0_20, 1",
"JRE_VERSION_9, 9.0.0, 0",
"JRE_VERSION_9, 9.0.1, -1",
"JRE_VERSION_9, 10.0.1, -1",
"JRE_VERSION_10, 9.0.1, 1",
"JRE_VERSION_10, 10.0.0, 0",
"JRE_VERSION_10, 10.0.1, -1",
"JRE_VERSION_10, 11.0.1, -1",
"JRE_VERSION_11, 10.0.1, 1",
"JRE_VERSION_11, 11.0.0, 0",
"JRE_VERSION_11, 11.0.1, -1",
})
public void compareTo(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion verConst, String verStr,
int result) {
assertEquals(Integer.signum(verConst.compareTo(new JavaVersion(verStr))), result);
}
@ParameterizedTest
@CsvSource({
"JRE_VERSION_160, JRE_VERSION_170, true",
"JRE_VERSION_170, JRE_VERSION_180, true",
"JRE_VERSION_180, JRE_VERSION_9, true",
"JRE_VERSION_9, JRE_VERSION_180, false",
"JRE_VERSION_170, JRE_VERSION_160, false",
"JRE_VERSION_170, JRE_VERSION_170, false",
"JRE_VERSION_180, JRE_VERSION_160, false",
"JRE_VERSION_180, JRE_VERSION_170, false",
"JRE_VERSION_180, JRE_VERSION_180, false",
})
public void isBelow(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version1,
@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version2, boolean result) {
assertEquals(version1.isBelow(version2), result);
}
@ParameterizedTest
@CsvSource({
"0.0.1300, 10, 20, admin-key, submit-key, memo, 11, 21, updated-admin-key, updated-submit-key, " +
"updated-memo, 4000000, 1, 30",
"0.0.1301, 10, 20, admin-key, submit-key, memo, 11, 21, '', '', '', 4000001, 1, 30",
"0.0.1302, 0, 0, '', '', '', 11, 21, updated-admin-key, updated-submit-key, updated-memo, 4000002, ,"
})
void updateTopicTest(@ConvertWith(TopicIdConverter.class) TopicID topicId, long expirationTimeSeconds,
int expirationTimeNanos, @ConvertWith(KeyConverter.class) Key adminKey,
@ConvertWith(KeyConverter.class) Key submitKey, String memo,
long updatedExpirationTimeSeconds, int updatedExpirationTimeNanos,
@ConvertWith(KeyConverter.class) Key updatedAdminKey,
@ConvertWith(KeyConverter.class) Key updatedSubmitKey,
String updatedMemo, long consensusTimestamp, Long autoRenewAccountId, Long autoRenewPeriod) {
// Store topic to be updated.
var topic = createTopicEntity(topicId, expirationTimeSeconds, expirationTimeNanos, adminKey, submitKey, memo,
autoRenewAccountId, autoRenewPeriod);
entityRepository.save(topic);
var responseCode = ResponseCodeEnum.SUCCESS;
var transaction = createUpdateTopicTransaction(topicId, updatedExpirationTimeSeconds,
updatedExpirationTimeNanos, updatedAdminKey, updatedSubmitKey, updatedMemo, autoRenewAccountId,
autoRenewPeriod);
var transactionRecord = createTransactionRecord(topicId, consensusTimestamp, responseCode);
var expectedEntity = createTopicEntity(topicId, updatedExpirationTimeSeconds, updatedExpirationTimeNanos,
updatedAdminKey, updatedSubmitKey, updatedMemo, autoRenewAccountId, autoRenewPeriod);
parseRecordItemAndCommit(new RecordItem(transaction, transactionRecord));
long entityCount = autoRenewAccountId != null ? 4 : 3; // Node, payer, topic & optionally autorenew
var entity = getTopicEntity(topicId);
assertTransactionInRepository(responseCode, consensusTimestamp, entity.getId());
assertEquals(entityCount, entityRepository.count());
assertThat(entity).isEqualTo(expectedEntity);
}
@ParameterizedTest
@CsvSource({
"0.0.9000, test-message0, 9000000, runninghash, 1, 1",
"0.0.9001, '', 9000001, '', 9223372036854775807, 2"
})
void submitMessageTest(@ConvertWith(TopicIdConverter.class) TopicID topicId, String message,
long consensusTimestamp, String runningHash,
long sequenceNumber, int runningHashVersion) throws Exception {
var responseCode = ResponseCodeEnum.SUCCESS;
var topic = createTopicEntity(topicId, 10L, 20, null, null, null, null, null);
entityRepository.save(topic);
var topicMessage = createTopicMessage(topicId, message, sequenceNumber, runningHash, consensusTimestamp,
runningHashVersion);
var transaction = createSubmitMessageTransaction(topicId, message);
var transactionRecord = createTransactionRecord(topicId, sequenceNumber, runningHash
.getBytes(), runningHashVersion, consensusTimestamp, responseCode);
parseRecordItemAndCommit(new RecordItem(transaction, transactionRecord));
var entity = getTopicEntity(topicId);
assertTransactionInRepository(responseCode, consensusTimestamp, entity.getId());
assertEquals(3L, entityRepository.count()); // Node, payer, topic
assertThat(entity).isEqualTo(topic);
assertThat(topicMessageRepository.findById(consensusTimestamp)).get()
.isEqualTo(topicMessage);
}
@ParameterizedTest
@Disabled("Doesn't yet work as only the first parameter gets a value")
@JsonSource("{'stringKey':'value', 'boolKey': true, 'intKey': 1 }")
@DisplayName("provides multiple objects")
void multipleObjects(
@ConvertWith(JsonConverter.class) String stringKey,
@ConvertWith(JsonConverter.class) int intKey,
@ConvertWith(JsonConverter.class) boolean boolKey) {
assertEquals("value", stringKey);
assertTrue(boolKey);
assertEquals(1, intKey);
}
@ParameterizedTest
@EnumSource(TimeUnit.class)
void testWithExplicitArgumentConversion(
@ConvertWith(CustomArgumentsConverter.class) String argument) {
System.out.println("Argument " + argument + " is a type of "
+ argument.getClass());
assertNotNull(argument);
}
@ParameterizedTest
@ValueSource(strings = {
"java.util.List",
"java.util.Collection",
"java.lang.Object",
})
void anySuperclass(
@ConvertWith(ClassArgumentConverter.class) Class<? super List<?>> superclassOfList) {
assertThat(superclassOfList).isNotNull();
}
@ParameterizedTest
@ValueSource(strings = {
"java.util.List",
"java.util.ArrayList",
})
void anySubclass(@ConvertWith(ClassArgumentConverter.class) Class<? extends List<?>> anything) {
assertThat(anything).isNotNull();
}
@ExpectFailure({
@Cause(type = ParameterResolutionException.class),
@Cause(type = ArgumentConversionException.class),
@Cause(type = ClassNotFoundException.class)
})
@ParameterizedTest
@ValueSource(strings = "123ClassDoesNotExist")
void classNotFound(@ConvertWith(ClassArgumentConverter.class) Class<?> clazz) {}
@ExpectFailure({
@Cause(type = ParameterResolutionException.class),
@Cause(type = ArgumentConversionException.class, message = "Invalid parameter type")
})
@ParameterizedTest
@ValueSource(strings = "java.lang.Object")
void badParameterType(@ConvertWith(ClassArgumentConverter.class) String clazz) {}
@ExpectFailure({
@Cause(type = ParameterResolutionException.class),
@Cause(
type = ArgumentConversionException.class,
message = "java.lang.Class<java.util.List> is not assignable to"
+ " java.lang.Class<java.util.Collection<?>>"
)
})
@ParameterizedTest
@ValueSource(strings = "java.util.List")
void wrongClass(@ConvertWith(ClassArgumentConverter.class) Class<Collection<?>> clazz) {}
@ExpectFailure({
@Cause(type = ParameterResolutionException.class),
@Cause(type = ArgumentConversionException.class, message = "is not assignable to")
})
@ParameterizedTest
@ValueSource(strings = "java.util.List")
void badLowerBound(
@ConvertWith(ClassArgumentConverter.class) Class<? super Collection<?>> clazz) {}
@ExpectFailure({
@Cause(type = ParameterResolutionException.class),
@Cause(type = ArgumentConversionException.class, message = "is not assignable to")
})
@ParameterizedTest
@ValueSource(strings = "java.lang.Object")
void badUpperBound(
@ConvertWith(ClassArgumentConverter.class) Class<? extends Collection<?>> clazz) {}
@ParameterizedTest
@EnumSource(TimeUnit.class)
void testWithExplicitArgumentConversion(
@ConvertWith(CustomArgumentsConverter.class) String argument) {
System.out.println("Argument " + argument + " is a type of "
+ argument.getClass());
assertNotNull(argument);
}
@ParameterizedTest
@CsvSource({
"JRE_VERSION_160, 1.6.0",
"JRE_VERSION_170, 1.7.0",
"JRE_VERSION_180, 1.8.0",
"JRE_VERSION_9, 9",
"JRE_VERSION_9, 9.0.0",
"JRE_VERSION_10, 10.0.0",
"JRE_VERSION_11, 11.0.0",
})
public void equals(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion verConst, String verStr) {
assertEquals(verConst, new JavaVersion(verStr));
}
@ParameterizedTest
@CsvSource({
"JRE_VERSION_9, JRE_VERSION_160, true",
"JRE_VERSION_9, JRE_VERSION_170, true",
"JRE_VERSION_9, JRE_VERSION_180, true",
"JRE_VERSION_9, JRE_VERSION_9, true",
"JRE_VERSION_10, JRE_VERSION_160, true",
"JRE_VERSION_10, JRE_VERSION_170, true",
"JRE_VERSION_10, JRE_VERSION_180, true",
"JRE_VERSION_10, JRE_VERSION_9, true",
"JRE_VERSION_180, JRE_VERSION_160, true",
"JRE_VERSION_180, JRE_VERSION_170, true",
"JRE_VERSION_180, JRE_VERSION_180, true",
"JRE_VERSION_170, JRE_VERSION_160, true",
"JRE_VERSION_170, JRE_VERSION_170, true",
"JRE_VERSION_180, JRE_VERSION_9, false",
"JRE_VERSION_170, JRE_VERSION_180, false",
"JRE_VERSION_170, JRE_VERSION_9, false",
"JRE_VERSION_9, JRE_VERSION_9, true",
"JRE_VERSION_10, JRE_VERSION_9, true",
"JRE_VERSION_9, JRE_VERSION_10, false",
"JRE_VERSION_10, JRE_VERSION_9, true",
"JRE_VERSION_10, JRE_VERSION_10, true",
"JRE_VERSION_10, JRE_VERSION_11, false",
})
public void isAtLeast(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version1,
@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version2, boolean result) {
assertEquals(version1.isAtLeast(version2), result);
}
@ParameterizedTest
@CsvSource({
"0.0.1500, 10, 20, admin-key, submit-key, memo, 5000000, 0, 0, , , , 1, 30, , 0",
"0.0.1500, 10, 20, admin-key, submit-key, memo, 5000000, , , , , , 1, 30, , ",
"0.0.1501, 0, 0, '', '', '', 5000001, 0, 0, , , , , , ,",
"0.0.1502, , , admin-key, submit-key, memo, 5000002, 10, 20, updated-admin-key, updated-submit-key, " +
"updated-memo, 1, 30, 11, 31",
"0.0.1503, , , , , , 5000003, 11, 21, admin-key, submit-key, memo, , , 1, 30"
})
void updateTopicTestPartialUpdates(@ConvertWith(TopicIdConverter.class) TopicID topicId, Long expirationTimeSeconds,
Integer expirationTimeNanos, @ConvertWith(KeyConverter.class) Key adminKey,
@ConvertWith(KeyConverter.class) Key submitKey, String memo,
long consensusTimestamp, Long updatedExpirationTimeSeconds,
Integer updatedExpirationTimeNanos,
@ConvertWith(KeyConverter.class) Key updatedAdminKey,
@ConvertWith(KeyConverter.class) Key updatedSubmitKey, String updatedMemo,
Long autoRenewAccountNum, Long autoRenewPeriod, Long updatedAutoRenewAccountNum,
Long updatedAutoRenewPeriod) {
// Store topic to be updated.
var topic = createTopicEntity(topicId, expirationTimeSeconds, expirationTimeNanos, adminKey, submitKey, memo,
autoRenewAccountNum, autoRenewPeriod);
entityRepository.save(topic);
if (updatedAutoRenewPeriod != null) {
topic.setAutoRenewPeriod(updatedAutoRenewPeriod);
}
if (updatedExpirationTimeSeconds != null && updatedExpirationTimeNanos != null) {
topic.setExpiryTimeNs(Utility.convertToNanosMax(updatedExpirationTimeSeconds, updatedExpirationTimeNanos));
}
if (updatedAdminKey != null) {
topic.setKey(updatedAdminKey.toByteArray());
}
if (updatedSubmitKey != null) {
topic.setSubmitKey(updatedSubmitKey.toByteArray());
}
if (updatedMemo != null) {
topic.setMemo(updatedMemo);
}
var responseCode = ResponseCodeEnum.SUCCESS;
var transaction = createUpdateTopicTransaction(topicId, updatedExpirationTimeSeconds,
updatedExpirationTimeNanos, updatedAdminKey, updatedSubmitKey, updatedMemo, updatedAutoRenewAccountNum,
updatedAutoRenewPeriod);
var transactionRecord = createTransactionRecord(topicId, consensusTimestamp, responseCode);
parseRecordItemAndCommit(new RecordItem(transaction, transactionRecord));
long entityCount = 3;
if (autoRenewAccountNum != null) {
++entityCount;
}
if (updatedAutoRenewAccountNum != null) {
++entityCount;
topic.setAutoRenewAccountId(EntityId.of(0L, 0L, updatedAutoRenewAccountNum, EntityTypeEnum.ACCOUNT));
}
var entity = getTopicEntity(topicId);
assertTransactionInRepository(responseCode, consensusTimestamp, entity.getId());
assertEquals(entityCount, entityRepository.count());
assertThat(entity).isEqualTo(topic);
}
@ParameterizedTest
@JsonSource("{'key':'value'}")
@DisplayName("Converts to Strings")
void strings(@ConvertWith(JsonConverter.class) String key) {
assertEquals("value", key);
}
@ParameterizedTest
@JsonSource("{'key': true}")
@DisplayName("Converts to booleans")
void ints(@ConvertWith(JsonConverter.class) boolean key) {
assertTrue(key);
}
@ParameterizedTest
@JsonSource("{'key': 1 }")
@DisplayName("Converts to ints")
void booleans(@ConvertWith(JsonConverter.class) int key) {
assertEquals(1, key);
}
@ParameterizedTest
@ValueSource(strings = { "(0/0)", "(0/1)", "(1/0)", "(1/1)", "(2/1)" })
void testPointNotNull_1(@ConvertWith(PointConverter.class) Point point) {
assertNotNull(point);
}
@ParameterizedTest
@CsvSource({"2018/12/25,2018", "2019/02/11,2019"})
void getYear_ShouldWorkAsExpected(@ConvertWith(SlashyDateConverter.class) LocalDate date, int expected) {
assertEquals(expected, date.getYear());
}