Remove most usages of junit ArgumentsProvider (#13962)

This commit is contained in:
Lauri Tulmin 2025-05-31 08:36:27 +03:00 committed by GitHub
parent c7ed10a49d
commit 3cb7ee77c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
28 changed files with 2074 additions and 2426 deletions

View File

@ -11,23 +11,187 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
class RedisCommandSanitizerTest {
@ParameterizedTest
@ArgumentsSource(SanitizeArgs.class)
@MethodSource("sanitizeArgs")
void shouldSanitizeExpected(String command, List<String> args, String expected) {
String result = RedisCommandSanitizer.create(true).sanitize(command, args);
assertThat(result).isEqualTo(expected);
}
@ParameterizedTest
@ArgumentsSource(KeepArguments.class)
@CsvSource({
// Cluster
"CLUSTER",
"READONLY",
"READWRITE",
// Connection
"CLIENT",
"ECHO",
"PING",
"QUIT",
"SELECT",
// Geo
"GEOADD",
"GEODIST",
"GEOHASH",
"GEOPOS",
"GEORADIUS",
"GEORADIUSBYMEMBER",
// Hashes
"HDEL",
"HEXISTS",
"HGET",
"HGETALL",
"HINCRBY",
"HINCRBYFLOAT",
"HKEYS",
"HLEN",
"HMGET",
"HSCAN",
"HSTRLEN",
"HVALS",
// HyperLogLog
"PFCOUNT",
"PFMERGE",
// Keys
"DEL",
"DUMP",
"EXISTS",
"EXPIRE",
"EXPIREAT",
"KEYS",
"MOVE",
"OBJECT",
"PERSIST",
"PEXPIRE",
"PEXPIREAT",
"PTTL",
"RANDOMKEY",
"RENAME",
"RENAMENX",
"RESTORE",
"SCAN",
"SORT",
"TOUCH",
"TTL",
"TYPE",
"UNLINK",
"WAIT",
// Lists
"BLMOVE",
"BLPOP",
"BRPOP",
"BRPOPLPUSH",
"LINDEX",
"LLEN",
"LMOVE",
"LPOP",
"LRANGE",
"LTRIM",
"RPOP",
"RPOPLPUSH",
// Pub/Sub
"PSUBSCRIBE",
"PUBSUB",
"PUNSUBSCRIBE",
"SUBSCRIBE",
"UNSUBSCRIBE",
// Server
"ACL",
"BGREWRITEAOF",
"BGSAVE",
"COMMAND",
"DBSIZE",
"DEBUG",
"FLUSHALL",
"FLUSHDB",
"INFO",
"LASTSAVE",
"LATENCY",
"LOLWUT",
"MEMORY",
"MODULE",
"MONITOR",
"PSYNC",
"REPLICAOF",
"ROLE",
"SAVE",
"SHUTDOWN",
"SLAVEOF",
"SLOWLOG",
"SWAPDB",
"SYNC",
"TIME",
// Sets
"SCARD",
"SDIFF",
"SDIFFSTORE",
"SINTER",
"SINTERSTORE",
"SMEMBERS",
"SPOP",
"SRANDMEMBER",
"SSCAN",
"SUNION",
"SUNIONSTORE",
// Sorted Sets
"BZPOPMAX",
"BZPOPMIN",
"ZCARD",
"ZINTER",
"ZINTERSTORE",
"ZPOPMAX",
"ZPOPMIN",
"ZRANGE",
"ZREMRANGEBYRANK",
"ZREVRANGE",
"ZSCAN",
"ZUNION",
"ZUNIONSTORE",
// Streams
"XACK",
"XCLAIM",
"XDEL",
"XGROUP",
"XINFO",
"XLEN",
"XPENDING",
"XRANGE",
"XREAD",
"XREADGROUP",
"XREVRANGE",
"XTRIM",
// Strings
"BITCOUNT",
"BITFIELD",
"BITOP",
"BITPOS",
"DECR",
"DECRBY",
"GET",
"GETBIT",
"GETRANGE",
"INCR",
"INCRBY",
"INCRBYFLOAT",
"MGET",
"SETBIT",
"STRALGO",
"STRLEN",
// Transactions
"DISCARD",
"EXEC",
"MULTI",
"UNWATCH",
"WATCH"
})
void shouldKeepAllArguments(String command) {
List<String> args = list("arg1", "arg 2");
String result = RedisCommandSanitizer.create(true).sanitize(command, args);
@ -41,260 +205,82 @@ class RedisCommandSanitizerTest {
assertThat(result).isEqualTo("NEWAUTH ? ?");
}
static class SanitizeArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
return Stream.of(
// Connection
Arguments.of("AUTH", list("password"), "AUTH ?"),
Arguments.of("HELLO", list("3", "AUTH", "username", "password"), "HELLO 3 AUTH ? ?"),
Arguments.of("HELLO", list("3"), "HELLO 3"),
// Hashes
Arguments.of(
"HMSET",
list("hash", "key1", "value1", "key2", "value2"),
"HMSET hash key1 ? key2 ?"),
Arguments.of(
"HSET", list("hash", "key1", "value1", "key2", "value2"), "HSET hash key1 ? key2 ?"),
Arguments.of("HSETNX", list("hash", "key", "value"), "HSETNX hash key ?"),
// HyperLogLog
Arguments.of("PFADD", list("hll", "a", "b", "c"), "PFADD hll ? ? ?"),
// Keys
Arguments.of(
"MIGRATE",
list("127.0.0.1", "4242", "key", "0", "5000", "AUTH", "password"),
"MIGRATE 127.0.0.1 4242 key 0 5000 AUTH ?"),
Arguments.of("RESTORE", list("key", "42", "value"), "RESTORE key 42 ?"),
// Lists
Arguments.of(
"LINSERT", list("list", "BEFORE", "value1", "value2"), "LINSERT list BEFORE ? ?"),
Arguments.of("LPOS", list("list", "value"), "LPOS list ?"),
Arguments.of("LPUSH", list("list", "value1", "value2"), "LPUSH list ? ?"),
Arguments.of("LPUSHX", list("list", "value1", "value2"), "LPUSHX list ? ?"),
Arguments.of("LREM", list("list", "2", "value"), "LREM list ? ?"),
Arguments.of("LSET", list("list", "2", "value"), "LSET list ? ?"),
Arguments.of("RPUSH", list("list", "value1", "value2"), "RPUSH list ? ?"),
Arguments.of("RPUSHX", list("list", "value1", "value2"), "RPUSHX list ? ?"),
// Pub/Sub
Arguments.of("PUBLISH", list("channel", "message"), "PUBLISH channel ?"),
// Scripting
Arguments.of(
"EVAL", list("script", "2", "key1", "key2", "value"), "EVAL script 2 key1 key2 ?"),
Arguments.of("EVALSHA", list("sha1", "0", "value1", "value2"), "EVALSHA sha1 0 ? ?"),
// Sets),
Arguments.of("SADD", list("set", "value1", "value2"), "SADD set ? ?"),
Arguments.of("SISMEMBER", list("set", "value"), "SISMEMBER set ?"),
Arguments.of("SMISMEMBER", list("set", "value1", "value2"), "SMISMEMBER set ? ?"),
Arguments.of("SMOVE", list("set1", "set2", "value"), "SMOVE set1 set2 ?"),
Arguments.of("SREM", list("set", "value1", "value2"), "SREM set ? ?"),
// Server
Arguments.of(
"CONFIG", list("SET", "masterpassword", "password"), "CONFIG SET masterpassword ?"),
// Sorted Sets
Arguments.of("ZADD", list("sset", "1", "value1", "2", "value2"), "ZADD sset ? ? ? ?"),
Arguments.of("ZCOUNT", list("sset", "1", "10"), "ZCOUNT sset ? ?"),
Arguments.of("ZINCRBY", list("sset", "1", "value"), "ZINCRBY sset ? ?"),
Arguments.of("ZLEXCOUNT", list("sset", "1", "10"), "ZLEXCOUNT sset ? ?"),
Arguments.of("ZMSCORE", list("sset", "value1", "value2"), "ZMSCORE sset ? ?"),
Arguments.of("ZRANGEBYLEX", list("sset", "1", "10"), "ZRANGEBYLEX sset ? ?"),
Arguments.of("ZRANGEBYSCORE", list("sset", "1", "10"), "ZRANGEBYSCORE sset ? ?"),
Arguments.of("ZRANK", list("sset", "value"), "ZRANK sset ?"),
Arguments.of("ZREM", list("sset", "value1", "value2"), "ZREM sset ? ?"),
Arguments.of("ZREMRANGEBYLEX", list("sset", "1", "10"), "ZREMRANGEBYLEX sset ? ?"),
Arguments.of("ZREMRANGEBYSCORE", list("sset", "1", "10"), "ZREMRANGEBYSCORE sset ? ?"),
Arguments.of("ZREVRANGEBYLEX", list("sset", "1", "10"), "ZREVRANGEBYLEX sset ? ?"),
Arguments.of("ZREVRANGEBYSCORE", list("sset", "1", "10"), "ZREVRANGEBYSCORE sset ? ?"),
Arguments.of("ZREVRANK", list("sset", "value"), "ZREVRANK sset ?"),
Arguments.of("ZSCORE", list("sset", "value"), "ZSCORE sset ?"),
// Streams
Arguments.of(
"XADD",
list("stream", "*", "key1", "value1", "key2", "value2"),
"XADD stream * key1 ? key2 ?"),
// Strings
Arguments.of("APPEND", list("key", "value"), "APPEND key ?"),
Arguments.of("GETSET", list("key", "value"), "GETSET key ?"),
Arguments.of("MSET", list("key1", "value1", "key2", "value2"), "MSET key1 ? key2 ?"),
Arguments.of("MSETNX", list("key1", "value1", "key2", "value2"), "MSETNX key1 ? key2 ?"),
Arguments.of("PSETEX", list("key", "10000", "value"), "PSETEX key 10000 ?"),
Arguments.of("SET", list("key", "value"), "SET key ?"),
Arguments.of("SETEX", list("key", "10", "value"), "SETEX key 10 ?"),
Arguments.of("SETNX", list("key", "value"), "SETNX key ?"),
Arguments.of("SETRANGE", list("key", "42", "value"), "SETRANGE key ? ?"));
}
}
static class KeepArguments implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
return Stream.of(
// Cluster
"CLUSTER",
"READONLY",
"READWRITE",
// Connection
"CLIENT",
"ECHO",
"PING",
"QUIT",
"SELECT",
// Geo
"GEOADD",
"GEODIST",
"GEOHASH",
"GEOPOS",
"GEORADIUS",
"GEORADIUSBYMEMBER",
// Hashes
"HDEL",
"HEXISTS",
"HGET",
"HGETALL",
"HINCRBY",
"HINCRBYFLOAT",
"HKEYS",
"HLEN",
"HMGET",
"HSCAN",
"HSTRLEN",
"HVALS",
// HyperLogLog
"PFCOUNT",
"PFMERGE",
// Keys
"DEL",
"DUMP",
"EXISTS",
"EXPIRE",
"EXPIREAT",
"KEYS",
"MOVE",
"OBJECT",
"PERSIST",
"PEXPIRE",
"PEXPIREAT",
"PTTL",
"RANDOMKEY",
"RENAME",
"RENAMENX",
"RESTORE",
"SCAN",
"SORT",
"TOUCH",
"TTL",
"TYPE",
"UNLINK",
"WAIT",
// Lists
"BLMOVE",
"BLPOP",
"BRPOP",
"BRPOPLPUSH",
"LINDEX",
"LLEN",
"LMOVE",
"LPOP",
"LRANGE",
"LTRIM",
"RPOP",
"RPOPLPUSH",
// Pub/Sub
"PSUBSCRIBE",
"PUBSUB",
"PUNSUBSCRIBE",
"SUBSCRIBE",
"UNSUBSCRIBE",
// Server
"ACL",
"BGREWRITEAOF",
"BGSAVE",
"COMMAND",
"DBSIZE",
"DEBUG",
"FLUSHALL",
"FLUSHDB",
"INFO",
"LASTSAVE",
"LATENCY",
"LOLWUT",
"MEMORY",
"MODULE",
"MONITOR",
"PSYNC",
"REPLICAOF",
"ROLE",
"SAVE",
"SHUTDOWN",
"SLAVEOF",
"SLOWLOG",
"SWAPDB",
"SYNC",
"TIME",
// Sets
"SCARD",
"SDIFF",
"SDIFFSTORE",
"SINTER",
"SINTERSTORE",
"SMEMBERS",
"SPOP",
"SRANDMEMBER",
"SSCAN",
"SUNION",
"SUNIONSTORE",
// Sorted Sets
"BZPOPMAX",
"BZPOPMIN",
"ZCARD",
"ZINTER",
"ZINTERSTORE",
"ZPOPMAX",
"ZPOPMIN",
"ZRANGE",
"ZREMRANGEBYRANK",
"ZREVRANGE",
"ZSCAN",
"ZUNION",
"ZUNIONSTORE",
// Streams
"XACK",
"XCLAIM",
"XDEL",
"XGROUP",
"XINFO",
"XLEN",
"XPENDING",
"XRANGE",
"XREAD",
"XREADGROUP",
"XREVRANGE",
"XTRIM",
// Strings
"BITCOUNT",
"BITFIELD",
"BITOP",
"BITPOS",
"DECR",
"DECRBY",
"GET",
"GETBIT",
"GETRANGE",
"INCR",
"INCRBY",
"INCRBYFLOAT",
"MGET",
"SETBIT",
"STRALGO",
"STRLEN",
// Transactions
"DISCARD",
"EXEC",
"MULTI",
"UNWATCH",
"WATCH")
.map(Arguments::of);
}
static Stream<Arguments> sanitizeArgs() {
return Stream.of(
// Connection
Arguments.of("AUTH", list("password"), "AUTH ?"),
Arguments.of("HELLO", list("3", "AUTH", "username", "password"), "HELLO 3 AUTH ? ?"),
Arguments.of("HELLO", list("3"), "HELLO 3"),
// Hashes
Arguments.of(
"HMSET", list("hash", "key1", "value1", "key2", "value2"), "HMSET hash key1 ? key2 ?"),
Arguments.of(
"HSET", list("hash", "key1", "value1", "key2", "value2"), "HSET hash key1 ? key2 ?"),
Arguments.of("HSETNX", list("hash", "key", "value"), "HSETNX hash key ?"),
// HyperLogLog
Arguments.of("PFADD", list("hll", "a", "b", "c"), "PFADD hll ? ? ?"),
// Keys
Arguments.of(
"MIGRATE",
list("127.0.0.1", "4242", "key", "0", "5000", "AUTH", "password"),
"MIGRATE 127.0.0.1 4242 key 0 5000 AUTH ?"),
Arguments.of("RESTORE", list("key", "42", "value"), "RESTORE key 42 ?"),
// Lists
Arguments.of(
"LINSERT", list("list", "BEFORE", "value1", "value2"), "LINSERT list BEFORE ? ?"),
Arguments.of("LPOS", list("list", "value"), "LPOS list ?"),
Arguments.of("LPUSH", list("list", "value1", "value2"), "LPUSH list ? ?"),
Arguments.of("LPUSHX", list("list", "value1", "value2"), "LPUSHX list ? ?"),
Arguments.of("LREM", list("list", "2", "value"), "LREM list ? ?"),
Arguments.of("LSET", list("list", "2", "value"), "LSET list ? ?"),
Arguments.of("RPUSH", list("list", "value1", "value2"), "RPUSH list ? ?"),
Arguments.of("RPUSHX", list("list", "value1", "value2"), "RPUSHX list ? ?"),
// Pub/Sub
Arguments.of("PUBLISH", list("channel", "message"), "PUBLISH channel ?"),
// Scripting
Arguments.of(
"EVAL", list("script", "2", "key1", "key2", "value"), "EVAL script 2 key1 key2 ?"),
Arguments.of("EVALSHA", list("sha1", "0", "value1", "value2"), "EVALSHA sha1 0 ? ?"),
// Sets),
Arguments.of("SADD", list("set", "value1", "value2"), "SADD set ? ?"),
Arguments.of("SISMEMBER", list("set", "value"), "SISMEMBER set ?"),
Arguments.of("SMISMEMBER", list("set", "value1", "value2"), "SMISMEMBER set ? ?"),
Arguments.of("SMOVE", list("set1", "set2", "value"), "SMOVE set1 set2 ?"),
Arguments.of("SREM", list("set", "value1", "value2"), "SREM set ? ?"),
// Server
Arguments.of(
"CONFIG", list("SET", "masterpassword", "password"), "CONFIG SET masterpassword ?"),
// Sorted Sets
Arguments.of("ZADD", list("sset", "1", "value1", "2", "value2"), "ZADD sset ? ? ? ?"),
Arguments.of("ZCOUNT", list("sset", "1", "10"), "ZCOUNT sset ? ?"),
Arguments.of("ZINCRBY", list("sset", "1", "value"), "ZINCRBY sset ? ?"),
Arguments.of("ZLEXCOUNT", list("sset", "1", "10"), "ZLEXCOUNT sset ? ?"),
Arguments.of("ZMSCORE", list("sset", "value1", "value2"), "ZMSCORE sset ? ?"),
Arguments.of("ZRANGEBYLEX", list("sset", "1", "10"), "ZRANGEBYLEX sset ? ?"),
Arguments.of("ZRANGEBYSCORE", list("sset", "1", "10"), "ZRANGEBYSCORE sset ? ?"),
Arguments.of("ZRANK", list("sset", "value"), "ZRANK sset ?"),
Arguments.of("ZREM", list("sset", "value1", "value2"), "ZREM sset ? ?"),
Arguments.of("ZREMRANGEBYLEX", list("sset", "1", "10"), "ZREMRANGEBYLEX sset ? ?"),
Arguments.of("ZREMRANGEBYSCORE", list("sset", "1", "10"), "ZREMRANGEBYSCORE sset ? ?"),
Arguments.of("ZREVRANGEBYLEX", list("sset", "1", "10"), "ZREVRANGEBYLEX sset ? ?"),
Arguments.of("ZREVRANGEBYSCORE", list("sset", "1", "10"), "ZREVRANGEBYSCORE sset ? ?"),
Arguments.of("ZREVRANK", list("sset", "value"), "ZREVRANK sset ?"),
Arguments.of("ZSCORE", list("sset", "value"), "ZSCORE sset ?"),
// Streams
Arguments.of(
"XADD",
list("stream", "*", "key1", "value1", "key2", "value2"),
"XADD stream * key1 ? key2 ?"),
// Strings
Arguments.of("APPEND", list("key", "value"), "APPEND key ?"),
Arguments.of("GETSET", list("key", "value"), "GETSET key ?"),
Arguments.of("MSET", list("key1", "value1", "key2", "value2"), "MSET key1 ? key2 ?"),
Arguments.of("MSETNX", list("key1", "value1", "key2", "value2"), "MSETNX key1 ? key2 ?"),
Arguments.of("PSETEX", list("key", "10000", "value"), "PSETEX key 10000 ?"),
Arguments.of("SET", list("key", "value"), "SET key ?"),
Arguments.of("SETEX", list("key", "10", "value"), "SETEX key 10 ?"),
Arguments.of("SETNX", list("key", "value"), "SETNX key ?"),
Arguments.of("SETRANGE", list("key", "42", "value"), "SETRANGE key ? ?"));
}
static List<String> list(String... args) {

View File

@ -11,23 +11,21 @@ import java.util.Random;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
public class SqlStatementSanitizerTest {
@ParameterizedTest
@ArgumentsSource(SqlArgs.class)
@MethodSource("sqlArgs")
void sanitizeSql(String original, String expected) {
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(original);
assertThat(result.getFullStatement()).isEqualTo(expected);
}
@ParameterizedTest
@ArgumentsSource(CouchbaseArgs.class)
@MethodSource("couchbaseArgs")
void normalizeCouchbase(String original, String expected) {
SqlStatementInfo result =
SqlStatementSanitizer.create(true).sanitize(original, SqlDialect.COUCHBASE);
@ -35,7 +33,7 @@ public class SqlStatementSanitizerTest {
}
@ParameterizedTest
@ArgumentsSource(SimplifyArgs.class)
@MethodSource("simplifyArgs")
void simplifySql(String original, Function<String, SqlStatementInfo> expectedFunction) {
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(original);
SqlStatementInfo expected = expectedFunction.apply(original);
@ -61,7 +59,7 @@ public class SqlStatementSanitizerTest {
}
@ParameterizedTest
@ArgumentsSource(DdlArgs.class)
@MethodSource("ddlArgs")
void checkDdlOperationStatementsAreOk(
String actual, Function<String, SqlStatementInfo> expectFunc) {
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(actual);
@ -158,273 +156,235 @@ public class SqlStatementSanitizerTest {
assertThat(SqlStatementSanitizer.isCached(largeStatement)).isFalse();
}
static class SqlArgs implements ArgumentsProvider {
private static Stream<Arguments> sqlArgs() {
return Stream.of(
Arguments.of("SELECT * FROM TABLE WHERE FIELD=1234", "SELECT * FROM TABLE WHERE FIELD=?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = 1234", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD>=-1234", "SELECT * FROM TABLE WHERE FIELD>=?"),
Arguments.of("SELECT * FROM TABLE WHERE FIELD<-1234", "SELECT * FROM TABLE WHERE FIELD<?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD <.1234", "SELECT * FROM TABLE WHERE FIELD <?"),
Arguments.of("SELECT 1.2", "SELECT ?"),
Arguments.of("SELECT -1.2", "SELECT ?"),
Arguments.of("SELECT -1.2e-9", "SELECT ?"),
Arguments.of("SELECT 2E+9", "SELECT ?"),
Arguments.of("SELECT +0.2", "SELECT ?"),
Arguments.of("SELECT .2", "SELECT ?"),
Arguments.of("7", "?"),
Arguments.of(".7", "?"),
Arguments.of("-7", "?"),
Arguments.of("+7", "?"),
Arguments.of("SELECT 0x0af764", "SELECT ?"),
Arguments.of("SELECT 0xdeadBEEF", "SELECT ?"),
Arguments.of("SELECT * FROM \"TABLE\"", "SELECT * FROM \"TABLE\""),
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("SELECT * FROM TABLE WHERE FIELD=1234", "SELECT * FROM TABLE WHERE FIELD=?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = 1234", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD>=-1234", "SELECT * FROM TABLE WHERE FIELD>=?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD<-1234", "SELECT * FROM TABLE WHERE FIELD<?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD <.1234", "SELECT * FROM TABLE WHERE FIELD <?"),
Arguments.of("SELECT 1.2", "SELECT ?"),
Arguments.of("SELECT -1.2", "SELECT ?"),
Arguments.of("SELECT -1.2e-9", "SELECT ?"),
Arguments.of("SELECT 2E+9", "SELECT ?"),
Arguments.of("SELECT +0.2", "SELECT ?"),
Arguments.of("SELECT .2", "SELECT ?"),
Arguments.of("7", "?"),
Arguments.of(".7", "?"),
Arguments.of("-7", "?"),
Arguments.of("+7", "?"),
Arguments.of("SELECT 0x0af764", "SELECT ?"),
Arguments.of("SELECT 0xdeadBEEF", "SELECT ?"),
Arguments.of("SELECT * FROM \"TABLE\"", "SELECT * FROM \"TABLE\""),
// Not numbers but could be confused as such
Arguments.of("SELECT A + B", "SELECT A + B"),
Arguments.of("SELECT -- comment", "SELECT -- comment"),
Arguments.of("SELECT * FROM TABLE123", "SELECT * FROM TABLE123"),
Arguments.of(
"SELECT FIELD2 FROM TABLE_123 WHERE X<>7", "SELECT FIELD2 FROM TABLE_123 WHERE X<>?"),
// Not numbers but could be confused as such
Arguments.of("SELECT A + B", "SELECT A + B"),
Arguments.of("SELECT -- comment", "SELECT -- comment"),
Arguments.of("SELECT * FROM TABLE123", "SELECT * FROM TABLE123"),
Arguments.of(
"SELECT FIELD2 FROM TABLE_123 WHERE X<>7", "SELECT FIELD2 FROM TABLE_123 WHERE X<>?"),
// Semi-nonsensical almost-numbers to elide or not
Arguments.of("SELECT --83--...--8e+76e3E-1", "SELECT ?"),
Arguments.of("SELECT DEADBEEF", "SELECT DEADBEEF"),
Arguments.of("SELECT 123-45-6789", "SELECT ?"),
Arguments.of("SELECT 1/2/34", "SELECT ?/?/?"),
// Semi-nonsensical almost-numbers to elide or not
Arguments.of("SELECT --83--...--8e+76e3E-1", "SELECT ?"),
Arguments.of("SELECT DEADBEEF", "SELECT DEADBEEF"),
Arguments.of("SELECT 123-45-6789", "SELECT ?"),
Arguments.of("SELECT 1/2/34", "SELECT ?/?/?"),
// Basic ' strings
Arguments.of("SELECT * FROM TABLE WHERE FIELD = ''", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = 'words and spaces'",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = ' an escaped '' quote mark inside'",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = '\\\\'", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = '\"inside doubles\"'",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = '\"$$$$\"'", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = 'a single \" doublequote inside'",
"SELECT * FROM TABLE WHERE FIELD = ?"),
// Basic ' strings
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = ''", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = 'words and spaces'",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = ' an escaped '' quote mark inside'",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = '\\\\'", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = '\"inside doubles\"'",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = '\"$$$$\"'",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = 'a single \" doublequote inside'",
"SELECT * FROM TABLE WHERE FIELD = ?"),
// Some databases allow using dollar-quoted strings
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$$$", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$words and spaces$$",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$quotes '\" inside$$",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$\"''\"$$", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$\\\\$$", "SELECT * FROM TABLE WHERE FIELD = ?"),
// Some databases allow using dollar-quoted strings
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$$$", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$words and spaces$$",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$quotes '\" inside$$",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$\"''\"$$",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$\\\\$$", "SELECT * FROM TABLE WHERE FIELD = ?"),
// PostgreSQL native parameter marker, we want to keep $1 instead of replacing it with ?
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $1", "SELECT * FROM TABLE WHERE FIELD = $1"),
// PostgreSQL native parameter marker, we want to keep $1 instead of replacing it with ?
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $1", "SELECT * FROM TABLE WHERE FIELD = $1"),
// Unicode, including a unicode identifier with a trailing number
Arguments.of(
"SELECT * FROM TABLEओ7 WHERE FIELD = 'ɣ'", "SELECT * FROM TABLEओ7 WHERE FIELD = ?"),
// Unicode, including a unicode identifier with a trailing number
Arguments.of(
"SELECT * FROM TABLEओ7 WHERE FIELD = 'ɣ'", "SELECT * FROM TABLEओ7 WHERE FIELD = ?"),
// whitespace normalization
Arguments.of(
"SELECT * \t\r\nFROM TABLE WHERE FIELD1 = 12344 AND FIELD2 = 5678",
"SELECT * FROM TABLE WHERE FIELD1 = ? AND FIELD2 = ?"),
// whitespace normalization
Arguments.of(
"SELECT * \t\r\nFROM TABLE WHERE FIELD1 = 12344 AND FIELD2 = 5678",
"SELECT * FROM TABLE WHERE FIELD1 = ? AND FIELD2 = ?"),
// hibernate/jpa query language
Arguments.of("FROM TABLE WHERE FIELD=1234", "FROM TABLE WHERE FIELD=?"));
}
// hibernate/jpa query language
Arguments.of("FROM TABLE WHERE FIELD=1234", "FROM TABLE WHERE FIELD=?"));
}
static class CouchbaseArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
// Some databases support/encourage " instead of ' with same escape rules
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"\"", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"words and spaces'\"",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \" an escaped \"\" quote mark inside\"",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"\\\\\"", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"'inside singles'\"",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"'$$$$'\"",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"a single ' singlequote inside\"",
"SELECT * FROM TABLE WHERE FIELD = ?"));
}
private static Stream<Arguments> couchbaseArgs() {
return Stream.of(
// Some databases support/encourage " instead of ' with same escape rules
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"\"", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"words and spaces'\"",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \" an escaped \"\" quote mark inside\"",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"\\\\\"", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"'inside singles'\"",
"SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"'$$$$'\"", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = \"a single ' singlequote inside\"",
"SELECT * FROM TABLE WHERE FIELD = ?"));
}
static class SimplifyArgs implements ArgumentsProvider {
static Function<String, SqlStatementInfo> expect(String operation, String identifier) {
return sql -> SqlStatementInfo.create(sql, operation, identifier);
}
static Function<String, SqlStatementInfo> expect(
String sql, String operation, String identifier) {
return ignored -> SqlStatementInfo.create(sql, operation, identifier);
}
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
// Select
Arguments.of("SELECT x, y, z FROM schema.table", expect("SELECT", "schema.table")),
Arguments.of("SELECT x, y, z FROM `schema table`", expect("SELECT", "schema table")),
Arguments.of(
"SELECT x, y, z FROM `schema`.`table`", expect("SELECT", "`schema`.`table`")),
Arguments.of("SELECT x, y, z FROM \"schema table\"", expect("SELECT", "schema table")),
Arguments.of(
"SELECT x, y, z FROM \"schema\".\"table\"", expect("SELECT", "\"schema\".\"table\"")),
Arguments.of(
"WITH subquery as (select a from b) SELECT x, y, z FROM table",
expect("SELECT", null)),
Arguments.of("SELECT x, y, (select a from b) as z FROM table", expect("SELECT", null)),
Arguments.of(
"select delete, insert into, merge, update from table", expect("SELECT", "table")),
Arguments.of("select col /* from table2 */ from table", expect("SELECT", "table")),
Arguments.of("select col from table join anotherTable", expect("SELECT", null)),
Arguments.of("select col from (select * from anotherTable)", expect("SELECT", null)),
Arguments.of(
"select col from (select * from anotherTable) alias", expect("SELECT", null)),
Arguments.of(
"select col from table1 union select col from table2", expect("SELECT", null)),
Arguments.of(
"select col from table where col in (select * from anotherTable)",
expect("SELECT", null)),
Arguments.of("select col from table1, table2", expect("SELECT", null)),
Arguments.of("select col from table1 t1, table2 t2", expect("SELECT", null)),
Arguments.of("select col from table1 as t1, table2 as t2", expect("SELECT", null)),
Arguments.of(
"select col from table where col in (1, 2, 3)",
expect("select col from table where col in (?)", "SELECT", "table")),
Arguments.of(
"select 'a' IN(x, 'b') from table where col in (1) and z IN( '3', '4' )",
expect(
"select ? IN(x, ?) from table where col in (?) and z IN(?)", "SELECT", "table")),
Arguments.of("select col from table order by col, col2", expect("SELECT", "table")),
Arguments.of("select ąś∂ń© from źćļńĶ order by col, col2", expect("SELECT", "źćļńĶ")),
Arguments.of("select 12345678", expect("select ?", "SELECT", null)),
Arguments.of("/* update comment */ select * from table1", expect("SELECT", "table1")),
Arguments.of("select /*((*/abc from table", expect("SELECT", "table")),
Arguments.of("SeLeCT * FrOm TAblE", expect("SELECT", "table")),
Arguments.of("select next value in hibernate_sequence", expect("SELECT", null)),
// hibernate/jpa
Arguments.of("FROM schema.table", expect("SELECT", "schema.table")),
Arguments.of("/* update comment */ from table1", expect("SELECT", "table1")),
// Insert
Arguments.of(" insert into table where lalala", expect("INSERT", "table")),
Arguments.of("insert insert into table where lalala", expect("INSERT", "table")),
Arguments.of("insert into db.table where lalala", expect("INSERT", "db.table")),
Arguments.of("insert into `db table` where lalala", expect("INSERT", "db table")),
Arguments.of("insert into \"db table\" where lalala", expect("INSERT", "db table")),
Arguments.of("insert without i-n-t-o", expect("INSERT", null)),
// Delete
Arguments.of("delete from table where something something", expect("DELETE", "table")),
Arguments.of(
"delete from `my table` where something something", expect("DELETE", "my table")),
Arguments.of(
"delete from \"my table\" where something something", expect("DELETE", "my table")),
Arguments.of(
"delete from foo where x IN (1,2,3)",
expect("delete from foo where x IN (?)", "DELETE", "foo")),
Arguments.of("delete from 12345678", expect("delete from ?", "DELETE", null)),
Arguments.of("delete (((", expect("delete (((", "DELETE", null)),
// Update
Arguments.of(
"update table set answer=42", expect("update table set answer=?", "UPDATE", "table")),
Arguments.of(
"update `my table` set answer=42",
expect("update `my table` set answer=?", "UPDATE", "my table")),
Arguments.of(
"update `my table` set answer=42 where x IN('a', 'b') AND y In ('a', 'b')",
expect(
"update `my table` set answer=? where x IN(?) AND y In (?)",
"UPDATE",
"my table")),
Arguments.of(
"update \"my table\" set answer=42",
expect("update \"my table\" set answer=?", "UPDATE", "my table")),
Arguments.of("update /*table", expect("UPDATE", null)),
// Call
Arguments.of("call test_proc()", expect("CALL", "test_proc")),
Arguments.of("call test_proc", expect("CALL", "test_proc")),
Arguments.of("call next value in hibernate_sequence", expect("CALL", null)),
Arguments.of("call db.test_proc", expect("CALL", "db.test_proc")),
// Merge
Arguments.of("merge into table", expect("MERGE", "table")),
Arguments.of("merge into `my table`", expect("MERGE", "my table")),
Arguments.of("merge into \"my table\"", expect("MERGE", "my table")),
Arguments.of("merge table (into is optional in some dbs)", expect("MERGE", "table")),
Arguments.of("merge (into )))", expect("MERGE", null)),
// Unknown operation
Arguments.of("and now for something completely different", expect(null, null)),
Arguments.of("", expect(null, null)),
Arguments.of(null, expect(null, null)));
}
private static Function<String, SqlStatementInfo> expect(String operation, String identifier) {
return sql -> SqlStatementInfo.create(sql, operation, identifier);
}
static class DdlArgs implements ArgumentsProvider {
private static Function<String, SqlStatementInfo> expect(
String sql, String operation, String identifier) {
return ignored -> SqlStatementInfo.create(sql, operation, identifier);
}
static Function<String, SqlStatementInfo> expect(String operation, String identifier) {
return sql -> SqlStatementInfo.create(sql, operation, identifier);
}
private static Stream<Arguments> simplifyArgs() {
return Stream.of(
// Select
Arguments.of("SELECT x, y, z FROM schema.table", expect("SELECT", "schema.table")),
Arguments.of("SELECT x, y, z FROM `schema table`", expect("SELECT", "schema table")),
Arguments.of("SELECT x, y, z FROM `schema`.`table`", expect("SELECT", "`schema`.`table`")),
Arguments.of("SELECT x, y, z FROM \"schema table\"", expect("SELECT", "schema table")),
Arguments.of(
"SELECT x, y, z FROM \"schema\".\"table\"", expect("SELECT", "\"schema\".\"table\"")),
Arguments.of(
"WITH subquery as (select a from b) SELECT x, y, z FROM table", expect("SELECT", null)),
Arguments.of("SELECT x, y, (select a from b) as z FROM table", expect("SELECT", null)),
Arguments.of(
"select delete, insert into, merge, update from table", expect("SELECT", "table")),
Arguments.of("select col /* from table2 */ from table", expect("SELECT", "table")),
Arguments.of("select col from table join anotherTable", expect("SELECT", null)),
Arguments.of("select col from (select * from anotherTable)", expect("SELECT", null)),
Arguments.of("select col from (select * from anotherTable) alias", expect("SELECT", null)),
Arguments.of("select col from table1 union select col from table2", expect("SELECT", null)),
Arguments.of(
"select col from table where col in (select * from anotherTable)",
expect("SELECT", null)),
Arguments.of("select col from table1, table2", expect("SELECT", null)),
Arguments.of("select col from table1 t1, table2 t2", expect("SELECT", null)),
Arguments.of("select col from table1 as t1, table2 as t2", expect("SELECT", null)),
Arguments.of(
"select col from table where col in (1, 2, 3)",
expect("select col from table where col in (?)", "SELECT", "table")),
Arguments.of(
"select 'a' IN(x, 'b') from table where col in (1) and z IN( '3', '4' )",
expect("select ? IN(x, ?) from table where col in (?) and z IN(?)", "SELECT", "table")),
Arguments.of("select col from table order by col, col2", expect("SELECT", "table")),
Arguments.of("select ąś∂ń© from źćļńĶ order by col, col2", expect("SELECT", "źćļńĶ")),
Arguments.of("select 12345678", expect("select ?", "SELECT", null)),
Arguments.of("/* update comment */ select * from table1", expect("SELECT", "table1")),
Arguments.of("select /*((*/abc from table", expect("SELECT", "table")),
Arguments.of("SeLeCT * FrOm TAblE", expect("SELECT", "table")),
Arguments.of("select next value in hibernate_sequence", expect("SELECT", null)),
static Function<String, SqlStatementInfo> expect(
String sql, String operation, String identifier) {
return ignored -> SqlStatementInfo.create(sql, operation, identifier);
}
// hibernate/jpa
Arguments.of("FROM schema.table", expect("SELECT", "schema.table")),
Arguments.of("/* update comment */ from table1", expect("SELECT", "table1")),
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("CREATE TABLE `table`", expect("CREATE TABLE", "table")),
Arguments.of("CREATE TABLE IF NOT EXISTS table", expect("CREATE TABLE", "table")),
Arguments.of("DROP TABLE `if`", expect("DROP TABLE", "if")),
Arguments.of(
"ALTER TABLE table ADD CONSTRAINT c FOREIGN KEY (foreign_id) REFERENCES ref (id)",
expect("ALTER TABLE", "table")),
Arguments.of("CREATE INDEX types_name ON types (name)", expect("CREATE INDEX", null)),
Arguments.of("DROP INDEX types_name ON types (name)", expect("DROP INDEX", null)),
Arguments.of(
"CREATE VIEW tmp AS SELECT type FROM table WHERE id = ?",
expect("CREATE VIEW", null)),
Arguments.of(
"CREATE PROCEDURE p AS SELECT * FROM table GO", expect("CREATE PROCEDURE", null)));
}
// Insert
Arguments.of(" insert into table where lalala", expect("INSERT", "table")),
Arguments.of("insert insert into table where lalala", expect("INSERT", "table")),
Arguments.of("insert into db.table where lalala", expect("INSERT", "db.table")),
Arguments.of("insert into `db table` where lalala", expect("INSERT", "db table")),
Arguments.of("insert into \"db table\" where lalala", expect("INSERT", "db table")),
Arguments.of("insert without i-n-t-o", expect("INSERT", null)),
// Delete
Arguments.of("delete from table where something something", expect("DELETE", "table")),
Arguments.of(
"delete from `my table` where something something", expect("DELETE", "my table")),
Arguments.of(
"delete from \"my table\" where something something", expect("DELETE", "my table")),
Arguments.of(
"delete from foo where x IN (1,2,3)",
expect("delete from foo where x IN (?)", "DELETE", "foo")),
Arguments.of("delete from 12345678", expect("delete from ?", "DELETE", null)),
Arguments.of("delete (((", expect("delete (((", "DELETE", null)),
// Update
Arguments.of(
"update table set answer=42", expect("update table set answer=?", "UPDATE", "table")),
Arguments.of(
"update `my table` set answer=42",
expect("update `my table` set answer=?", "UPDATE", "my table")),
Arguments.of(
"update `my table` set answer=42 where x IN('a', 'b') AND y In ('a', 'b')",
expect(
"update `my table` set answer=? where x IN(?) AND y In (?)", "UPDATE", "my table")),
Arguments.of(
"update \"my table\" set answer=42",
expect("update \"my table\" set answer=?", "UPDATE", "my table")),
Arguments.of("update /*table", expect("UPDATE", null)),
// Call
Arguments.of("call test_proc()", expect("CALL", "test_proc")),
Arguments.of("call test_proc", expect("CALL", "test_proc")),
Arguments.of("call next value in hibernate_sequence", expect("CALL", null)),
Arguments.of("call db.test_proc", expect("CALL", "db.test_proc")),
// Merge
Arguments.of("merge into table", expect("MERGE", "table")),
Arguments.of("merge into `my table`", expect("MERGE", "my table")),
Arguments.of("merge into \"my table\"", expect("MERGE", "my table")),
Arguments.of("merge table (into is optional in some dbs)", expect("MERGE", "table")),
Arguments.of("merge (into )))", expect("MERGE", null)),
// Unknown operation
Arguments.of("and now for something completely different", expect(null, null)),
Arguments.of("", expect(null, null)),
Arguments.of(null, expect(null, null)));
}
private static Stream<Arguments> ddlArgs() {
return Stream.of(
Arguments.of("CREATE TABLE `table`", expect("CREATE TABLE", "table")),
Arguments.of("CREATE TABLE IF NOT EXISTS table", expect("CREATE TABLE", "table")),
Arguments.of("DROP TABLE `if`", expect("DROP TABLE", "if")),
Arguments.of(
"ALTER TABLE table ADD CONSTRAINT c FOREIGN KEY (foreign_id) REFERENCES ref (id)",
expect("ALTER TABLE", "table")),
Arguments.of("CREATE INDEX types_name ON types (name)", expect("CREATE INDEX", null)),
Arguments.of("DROP INDEX types_name ON types (name)", expect("DROP INDEX", null)),
Arguments.of(
"CREATE VIEW tmp AS SELECT type FROM table WHERE id = ?", expect("CREATE VIEW", null)),
Arguments.of(
"CREATE PROCEDURE p AS SELECT * FROM table GO", expect("CREATE PROCEDURE", null)));
}
}

View File

@ -22,41 +22,35 @@ import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
class SpanSuppressionStrategyTest {
static final Span span = Span.getInvalid();
@ParameterizedTest
@ArgumentsSource(ConfigArgs.class)
@MethodSource("configArgs")
void shouldParseConfig(String value, SpanSuppressionStrategy expectedStrategy) {
assertEquals(expectedStrategy, SpanSuppressionStrategy.fromConfig(value));
}
static final class ConfigArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("none", SpanSuppressionStrategy.NONE),
Arguments.of("NONE", SpanSuppressionStrategy.NONE),
Arguments.of("span-kind", SpanSuppressionStrategy.SPAN_KIND),
Arguments.of("Span-Kind", SpanSuppressionStrategy.SPAN_KIND),
Arguments.of("semconv", SpanSuppressionStrategy.SEMCONV),
Arguments.of("SemConv", SpanSuppressionStrategy.SEMCONV),
Arguments.of("asdfasdfasdf", SpanSuppressionStrategy.SEMCONV),
Arguments.of(null, SpanSuppressionStrategy.SEMCONV));
}
private static Stream<Arguments> configArgs() {
return Stream.of(
Arguments.of("none", SpanSuppressionStrategy.NONE),
Arguments.of("NONE", SpanSuppressionStrategy.NONE),
Arguments.of("span-kind", SpanSuppressionStrategy.SPAN_KIND),
Arguments.of("Span-Kind", SpanSuppressionStrategy.SPAN_KIND),
Arguments.of("semconv", SpanSuppressionStrategy.SEMCONV),
Arguments.of("SemConv", SpanSuppressionStrategy.SEMCONV),
Arguments.of("asdfasdfasdf", SpanSuppressionStrategy.SEMCONV),
Arguments.of(null, SpanSuppressionStrategy.SEMCONV));
}
@ParameterizedTest
@ArgumentsSource(SpanKindsAndKeys.class)
@MethodSource("spanKindsAndKeys")
void none_shouldNotSuppressAnything(SpanKind spanKind, SpanKey spanKey) {
SpanSuppressor suppressor = SpanSuppressionStrategy.NONE.create(emptySet());
@ -77,7 +71,7 @@ class SpanSuppressionStrategyTest {
}
@ParameterizedTest
@ArgumentsSource(SpanKindsAndKeys.class)
@MethodSource("spanKindsAndKeys")
void spanKind_shouldStoreInContext(SpanKind spanKind, SpanKey spanKey) {
SpanSuppressor suppressor = SpanSuppressionStrategy.SPAN_KIND.create(emptySet());
Context context = Context.root();
@ -89,7 +83,7 @@ class SpanSuppressionStrategyTest {
}
@ParameterizedTest
@ArgumentsSource(SpanKindsAndKeys.class)
@MethodSource("spanKindsAndKeys")
void spanKind_shouldSuppressSameKind(SpanKind spanKind, SpanKey spanKey) {
SpanSuppressor suppressor = SpanSuppressionStrategy.SPAN_KIND.create(emptySet());
Context context = Context.root();
@ -100,16 +94,12 @@ class SpanSuppressionStrategyTest {
assertSame(span, spanKey.fromContextOrNull(newContext));
}
static final class SpanKindsAndKeys implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of(SpanKind.SERVER, SpanKey.KIND_SERVER),
Arguments.of(SpanKind.CLIENT, SpanKey.KIND_CLIENT),
Arguments.of(SpanKind.CONSUMER, SpanKey.KIND_CONSUMER),
Arguments.of(SpanKind.PRODUCER, SpanKey.KIND_PRODUCER));
}
private static Stream<Arguments> spanKindsAndKeys() {
return Stream.of(
Arguments.of(SpanKind.SERVER, SpanKey.KIND_SERVER),
Arguments.of(SpanKind.CLIENT, SpanKey.KIND_CLIENT),
Arguments.of(SpanKind.CONSUMER, SpanKey.KIND_CONSUMER),
Arguments.of(SpanKind.PRODUCER, SpanKey.KIND_PRODUCER));
}
@Test

View File

@ -18,11 +18,9 @@ import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ -37,7 +35,7 @@ class ForwardedHostAddressAndPortExtractorTest {
@InjectMocks ForwardedHostAddressAndPortExtractor<String> underTest;
@ParameterizedTest
@ArgumentsSource(ForwardedArgs.class)
@MethodSource("forwardedArgs")
void shouldParseForwarded(
List<String> headers, @Nullable String expectedAddress, @Nullable Integer expectedPort) {
when(getter.getHttpRequestHeader(REQUEST, "forwarded")).thenReturn(headers);
@ -49,40 +47,36 @@ class ForwardedHostAddressAndPortExtractorTest {
assertThat(sink.getPort()).isEqualTo(expectedPort);
}
static final class ForwardedArgs implements ArgumentsProvider {
private static Stream<Arguments> forwardedArgs() {
return Stream.of(
// empty/invalid headers
arguments(singletonList(""), null, null),
arguments(singletonList("host="), null, null),
arguments(singletonList("host=;"), null, null),
arguments(singletonList("host=\""), null, null),
arguments(singletonList("host=\"\""), null, null),
arguments(singletonList("host=\"example.com"), null, null),
arguments(singletonList("by=1.2.3.4, test=abc"), null, null),
arguments(singletonList("host=example.com"), "example.com", null),
arguments(singletonList("host=\"example.com\""), "example.com", null),
arguments(singletonList("host=example.com; test=abc:1234"), "example.com", null),
arguments(singletonList("host=\"example.com\"; test=abc:1234"), "example.com", null),
arguments(singletonList("host=example.com:port"), "example.com", null),
arguments(singletonList("host=\"example.com:port\""), "example.com", null),
arguments(singletonList("host=example.com:42"), "example.com", 42),
arguments(singletonList("host=\"example.com:42\""), "example.com", 42),
arguments(singletonList("host=example.com:42; test=abc:1234"), "example.com", 42),
arguments(singletonList("host=\"example.com:42\"; test=abc:1234"), "example.com", 42),
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
return Stream.of(
// empty/invalid headers
arguments(singletonList(""), null, null),
arguments(singletonList("host="), null, null),
arguments(singletonList("host=;"), null, null),
arguments(singletonList("host=\""), null, null),
arguments(singletonList("host=\"\""), null, null),
arguments(singletonList("host=\"example.com"), null, null),
arguments(singletonList("by=1.2.3.4, test=abc"), null, null),
arguments(singletonList("host=example.com"), "example.com", null),
arguments(singletonList("host=\"example.com\""), "example.com", null),
arguments(singletonList("host=example.com; test=abc:1234"), "example.com", null),
arguments(singletonList("host=\"example.com\"; test=abc:1234"), "example.com", null),
arguments(singletonList("host=example.com:port"), "example.com", null),
arguments(singletonList("host=\"example.com:port\""), "example.com", null),
arguments(singletonList("host=example.com:42"), "example.com", 42),
arguments(singletonList("host=\"example.com:42\""), "example.com", 42),
arguments(singletonList("host=example.com:42; test=abc:1234"), "example.com", 42),
arguments(singletonList("host=\"example.com:42\"; test=abc:1234"), "example.com", 42),
// multiple headers
arguments(
asList("proto=https", "host=example.com", "host=github.com:1234"),
"example.com",
null));
}
// multiple headers
arguments(
asList("proto=https", "host=example.com", "host=github.com:1234"),
"example.com",
null));
}
@ParameterizedTest
@ArgumentsSource(HostArgs.class)
@MethodSource("hostArgs")
@SuppressWarnings("MockitoDoSetup")
void shouldParseForwardedHost(
List<String> headers, @Nullable String expectedAddress, @Nullable Integer expectedPort) {
@ -97,7 +91,7 @@ class ForwardedHostAddressAndPortExtractorTest {
}
@ParameterizedTest
@ArgumentsSource(HostArgs.class)
@MethodSource("hostArgs")
@SuppressWarnings("MockitoDoSetup")
void shouldParsePseudoAuthority(
List<String> headers, @Nullable String expectedAddress, @Nullable Integer expectedPort) {
@ -113,7 +107,7 @@ class ForwardedHostAddressAndPortExtractorTest {
}
@ParameterizedTest
@ArgumentsSource(HostArgs.class)
@MethodSource("hostArgs")
@SuppressWarnings("MockitoDoSetup")
void shouldParseHost(
List<String> headers, @Nullable String expectedAddress, @Nullable Integer expectedPort) {
@ -129,24 +123,20 @@ class ForwardedHostAddressAndPortExtractorTest {
assertThat(sink.getPort()).isEqualTo(expectedPort);
}
static final class HostArgs implements ArgumentsProvider {
private static Stream<Arguments> hostArgs() {
return Stream.of(
// empty/invalid headers
arguments(singletonList(""), null, null),
arguments(singletonList("\""), null, null),
arguments(singletonList("\"\""), null, null),
arguments(singletonList("example.com"), "example.com", null),
arguments(singletonList("example.com:port"), "example.com", null),
arguments(singletonList("example.com:42"), "example.com", 42),
arguments(singletonList("\"example.com\""), "example.com", null),
arguments(singletonList("\"example.com:port\""), "example.com", null),
arguments(singletonList("\"example.com:42\""), "example.com", 42),
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
return Stream.of(
// empty/invalid headers
arguments(singletonList(""), null, null),
arguments(singletonList("\""), null, null),
arguments(singletonList("\"\""), null, null),
arguments(singletonList("example.com"), "example.com", null),
arguments(singletonList("example.com:port"), "example.com", null),
arguments(singletonList("example.com:42"), "example.com", 42),
arguments(singletonList("\"example.com\""), "example.com", null),
arguments(singletonList("\"example.com:port\""), "example.com", null),
arguments(singletonList("\"example.com:42\""), "example.com", 42),
// multiple headers
arguments(asList("example.com", "github.com:1234"), "example.com", null));
}
// multiple headers
arguments(asList("example.com", "github.com:1234"), "example.com", null));
}
}

View File

@ -19,11 +19,9 @@ import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ -44,36 +42,32 @@ class ForwardedUrlSchemeProviderTest {
}
@ParameterizedTest
@ArgumentsSource(ForwardedHeaderValues.class)
@MethodSource("forwardedHeaderValues")
void parseForwardedHeader(List<String> values, String expectedScheme) {
when(getter.getHttpRequestHeader(REQUEST, "forwarded")).thenReturn(values);
assertThat(underTest.apply(REQUEST)).isEqualTo(expectedScheme);
}
static final class ForwardedHeaderValues implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
return Stream.of(
arguments(singletonList("for=1.1.1.1;proto=xyz"), "xyz"),
arguments(singletonList("for=1.1.1.1;proto=xyz;"), "xyz"),
arguments(singletonList("for=1.1.1.1;proto=xyz,"), "xyz"),
arguments(singletonList("for=1.1.1.1;proto="), null),
arguments(singletonList("for=1.1.1.1;proto=;"), null),
arguments(singletonList("for=1.1.1.1;proto=,"), null),
arguments(singletonList("for=1.1.1.1;proto=\"xyz\""), "xyz"),
arguments(singletonList("for=1.1.1.1;proto=\"xyz\";"), "xyz"),
arguments(singletonList("for=1.1.1.1;proto=\"xyz\","), "xyz"),
arguments(singletonList("for=1.1.1.1;proto=\""), null),
arguments(singletonList("for=1.1.1.1;proto=\"\""), null),
arguments(singletonList("for=1.1.1.1;proto=\"\";"), null),
arguments(singletonList("for=1.1.1.1;proto=\"\","), null),
arguments(asList("for=1.1.1.1", "proto=xyz", "proto=abc"), "xyz"));
}
private static Stream<Arguments> forwardedHeaderValues() {
return Stream.of(
arguments(singletonList("for=1.1.1.1;proto=xyz"), "xyz"),
arguments(singletonList("for=1.1.1.1;proto=xyz;"), "xyz"),
arguments(singletonList("for=1.1.1.1;proto=xyz,"), "xyz"),
arguments(singletonList("for=1.1.1.1;proto="), null),
arguments(singletonList("for=1.1.1.1;proto=;"), null),
arguments(singletonList("for=1.1.1.1;proto=,"), null),
arguments(singletonList("for=1.1.1.1;proto=\"xyz\""), "xyz"),
arguments(singletonList("for=1.1.1.1;proto=\"xyz\";"), "xyz"),
arguments(singletonList("for=1.1.1.1;proto=\"xyz\","), "xyz"),
arguments(singletonList("for=1.1.1.1;proto=\""), null),
arguments(singletonList("for=1.1.1.1;proto=\"\""), null),
arguments(singletonList("for=1.1.1.1;proto=\"\";"), null),
arguments(singletonList("for=1.1.1.1;proto=\"\","), null),
arguments(asList("for=1.1.1.1", "proto=xyz", "proto=abc"), "xyz"));
}
@ParameterizedTest
@ArgumentsSource(ForwardedProtoHeaderValues.class)
@MethodSource("forwardedProtoHeaderValues")
@SuppressWarnings("MockitoDoSetup")
void parseForwardedProtoHeader(List<String> values, String expectedScheme) {
doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "forwarded");
@ -81,15 +75,11 @@ class ForwardedUrlSchemeProviderTest {
assertThat(underTest.apply(REQUEST)).isEqualTo(expectedScheme);
}
static final class ForwardedProtoHeaderValues implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
return Stream.of(
arguments(singletonList("xyz"), "xyz"),
arguments(singletonList("\"xyz\""), "xyz"),
arguments(singletonList("\""), null),
arguments(asList("xyz", "abc"), "xyz"));
}
private static Stream<Arguments> forwardedProtoHeaderValues() {
return Stream.of(
arguments(singletonList("xyz"), "xyz"),
arguments(singletonList("\"xyz\""), "xyz"),
arguments(singletonList("\""), null),
arguments(asList("xyz", "abc"), "xyz"));
}
}

View File

@ -23,7 +23,6 @@ import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
@ -38,14 +37,11 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.ToIntFunction;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
class HttpClientAttributesExtractorTest {
@ -207,7 +203,32 @@ class HttpClientAttributesExtractorTest {
}
@ParameterizedTest
@ArgumentsSource(UrlSourceToRedact.class)
@CsvSource({
"https://user1:secret@github.com, https://REDACTED:REDACTED@github.com",
"https://user1:secret@github.com/path/, https://REDACTED:REDACTED@github.com/path/",
"https://user1:secret@github.com#test.html, https://REDACTED:REDACTED@github.com#test.html",
"https://user1:secret@github.com?foo=b@r, https://REDACTED:REDACTED@github.com?foo=b@r",
"https://user1:secret@github.com/p@th?foo=b@r, https://REDACTED:REDACTED@github.com/p@th?foo=b@r",
"https://github.com/p@th?foo=b@r, https://github.com/p@th?foo=b@r",
"https://github.com#t@st.html, https://github.com#t@st.html",
"user1:secret@github.com, user1:secret@github.com",
"https://github.com@, https://github.com@",
"https://service.com?paramA=valA&paramB=valB, https://service.com?paramA=valA&paramB=valB",
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7, https://service.com?AWSAccessKeyId=REDACTED",
"https://service.com?Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0%3A377, https://service.com?Signature=REDACTED",
"https://service.com?sig=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0, https://service.com?sig=REDACTED",
"https://service.com?X-Goog-Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0, https://service.com?X-Goog-Signature=REDACTED",
"https://service.com?paramA=valA&AWSAccessKeyId=AKIAIOSFODNN7&paramB=valB, https://service.com?paramA=valA&AWSAccessKeyId=REDACTED&paramB=valB",
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&paramA=valA, https://service.com?AWSAccessKeyId=REDACTED&paramA=valA",
"https://service.com?paramA=valA&AWSAccessKeyId=AKIAIOSFODNN7, https://service.com?paramA=valA&AWSAccessKeyId=REDACTED",
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&AWSAccessKeyId=ZGIAIOSFODNN7, https://service.com?AWSAccessKeyId=REDACTED&AWSAccessKeyId=REDACTED",
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7#ref, https://service.com?AWSAccessKeyId=REDACTED#ref",
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&aa&bb, https://service.com?AWSAccessKeyId=REDACTED&aa&bb",
"https://service.com?aa&bb&AWSAccessKeyId=AKIAIOSFODNN7, https://service.com?aa&bb&AWSAccessKeyId=REDACTED",
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&&, https://service.com?AWSAccessKeyId=REDACTED&&",
"https://service.com?&&AWSAccessKeyId=AKIAIOSFODNN7, https://service.com?&&AWSAccessKeyId=REDACTED",
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&a&b#fragment, https://service.com?AWSAccessKeyId=REDACTED&a&b#fragment"
})
void shouldRedactUserInfoAndQueryParameters(String url, String expectedResult) {
Map<String, String> request = new HashMap<>();
request.put("urlFull", url);
@ -223,76 +244,6 @@ class HttpClientAttributesExtractorTest {
assertThat(attributes.build()).containsOnly(entry(URL_FULL, expectedResult));
}
static final class UrlSourceToRedact implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
arguments("https://user1:secret@github.com", "https://REDACTED:REDACTED@github.com"),
arguments(
"https://user1:secret@github.com/path/",
"https://REDACTED:REDACTED@github.com/path/"),
arguments(
"https://user1:secret@github.com#test.html",
"https://REDACTED:REDACTED@github.com#test.html"),
arguments(
"https://user1:secret@github.com?foo=b@r",
"https://REDACTED:REDACTED@github.com?foo=b@r"),
arguments(
"https://user1:secret@github.com/p@th?foo=b@r",
"https://REDACTED:REDACTED@github.com/p@th?foo=b@r"),
arguments("https://github.com/p@th?foo=b@r", "https://github.com/p@th?foo=b@r"),
arguments("https://github.com#t@st.html", "https://github.com#t@st.html"),
arguments("user1:secret@github.com", "user1:secret@github.com"),
arguments("https://github.com@", "https://github.com@"),
arguments(
"https://service.com?paramA=valA&paramB=valB",
"https://service.com?paramA=valA&paramB=valB"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7",
"https://service.com?AWSAccessKeyId=REDACTED"),
arguments(
"https://service.com?Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0%3A377",
"https://service.com?Signature=REDACTED"),
arguments(
"https://service.com?sig=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0",
"https://service.com?sig=REDACTED"),
arguments(
"https://service.com?X-Goog-Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0",
"https://service.com?X-Goog-Signature=REDACTED"),
arguments(
"https://service.com?paramA=valA&AWSAccessKeyId=AKIAIOSFODNN7&paramB=valB",
"https://service.com?paramA=valA&AWSAccessKeyId=REDACTED&paramB=valB"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&paramA=valA",
"https://service.com?AWSAccessKeyId=REDACTED&paramA=valA"),
arguments(
"https://service.com?paramA=valA&AWSAccessKeyId=AKIAIOSFODNN7",
"https://service.com?paramA=valA&AWSAccessKeyId=REDACTED"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&AWSAccessKeyId=ZGIAIOSFODNN7",
"https://service.com?AWSAccessKeyId=REDACTED&AWSAccessKeyId=REDACTED"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7#ref",
"https://service.com?AWSAccessKeyId=REDACTED#ref"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&aa&bb",
"https://service.com?AWSAccessKeyId=REDACTED&aa&bb"),
arguments(
"https://service.com?aa&bb&AWSAccessKeyId=AKIAIOSFODNN7",
"https://service.com?aa&bb&AWSAccessKeyId=REDACTED"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&&",
"https://service.com?AWSAccessKeyId=REDACTED&&"),
arguments(
"https://service.com?&&AWSAccessKeyId=AKIAIOSFODNN7",
"https://service.com?&&AWSAccessKeyId=REDACTED"),
arguments(
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&a&b#fragment",
"https://service.com?AWSAccessKeyId=REDACTED&a&b#fragment"));
}
}
@ParameterizedTest
@ArgumentsSource(ValidRequestMethodsProvider.class)
void shouldExtractKnownMethods(String requestMethod) {

View File

@ -18,11 +18,9 @@ import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ -35,7 +33,7 @@ class HttpServerAddressAndPortExtractorTest {
@InjectMocks HttpServerAddressAndPortExtractor<String> underTest;
@ParameterizedTest
@ArgumentsSource(ForwardedArgs.class)
@MethodSource("forwardedArgs")
void shouldParseForwarded(List<String> headers, @Nullable String expectedAddress) {
when(getter.getHttpRequestHeader("request", "forwarded")).thenReturn(headers);
@ -46,52 +44,48 @@ class HttpServerAddressAndPortExtractorTest {
assertThat(sink.getPort()).isNull();
}
static final class ForwardedArgs implements ArgumentsProvider {
private static Stream<Arguments> forwardedArgs() {
return Stream.of(
// empty/invalid headers
arguments(singletonList(""), null),
arguments(singletonList("for="), null),
arguments(singletonList("for=;"), null),
arguments(singletonList("for=\""), null),
arguments(singletonList("for=\"\""), null),
arguments(singletonList("for=\"1.2.3.4"), null),
arguments(singletonList("for=\"[::1]"), null),
arguments(singletonList("for=[::1"), null),
arguments(singletonList("for=\"[::1\""), null),
arguments(singletonList("for=\"[::1\"]"), null),
arguments(singletonList("by=1.2.3.4, test=abc"), null),
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
return Stream.of(
// empty/invalid headers
arguments(singletonList(""), null),
arguments(singletonList("for="), null),
arguments(singletonList("for=;"), null),
arguments(singletonList("for=\""), null),
arguments(singletonList("for=\"\""), null),
arguments(singletonList("for=\"1.2.3.4"), null),
arguments(singletonList("for=\"[::1]"), null),
arguments(singletonList("for=[::1"), null),
arguments(singletonList("for=\"[::1\""), null),
arguments(singletonList("for=\"[::1\"]"), null),
arguments(singletonList("by=1.2.3.4, test=abc"), null),
// ipv6
arguments(singletonList("for=[::1]"), "::1"),
arguments(singletonList("For=[::1]"), "::1"),
arguments(singletonList("for=\"[::1]\":42"), "::1"),
arguments(singletonList("for=[::1]:42"), "::1"),
arguments(singletonList("for=\"[::1]:42\""), "::1"),
arguments(singletonList("for=[::1], for=1.2.3.4"), "::1"),
arguments(singletonList("for=[::1]; for=1.2.3.4:42"), "::1"),
arguments(singletonList("for=[::1]:42abc"), "::1"),
arguments(singletonList("for=[::1]:abc"), "::1"),
// ipv6
arguments(singletonList("for=[::1]"), "::1"),
arguments(singletonList("For=[::1]"), "::1"),
arguments(singletonList("for=\"[::1]\":42"), "::1"),
arguments(singletonList("for=[::1]:42"), "::1"),
arguments(singletonList("for=\"[::1]:42\""), "::1"),
arguments(singletonList("for=[::1], for=1.2.3.4"), "::1"),
arguments(singletonList("for=[::1]; for=1.2.3.4:42"), "::1"),
arguments(singletonList("for=[::1]:42abc"), "::1"),
arguments(singletonList("for=[::1]:abc"), "::1"),
// ipv4
arguments(singletonList("for=1.2.3.4"), "1.2.3.4"),
arguments(singletonList("FOR=1.2.3.4"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4, :42"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4;proto=https;by=4.3.2.1"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4:42"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4:42abc"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4:abc"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4; for=4.3.2.1:42"), "1.2.3.4"),
// ipv4
arguments(singletonList("for=1.2.3.4"), "1.2.3.4"),
arguments(singletonList("FOR=1.2.3.4"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4, :42"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4;proto=https;by=4.3.2.1"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4:42"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4:42abc"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4:abc"), "1.2.3.4"),
arguments(singletonList("for=1.2.3.4; for=4.3.2.1:42"), "1.2.3.4"),
// multiple headers
arguments(asList("proto=https", "for=1.2.3.4", "for=[::1]:42"), "1.2.3.4"));
}
// multiple headers
arguments(asList("proto=https", "for=1.2.3.4", "for=[::1]:42"), "1.2.3.4"));
}
@ParameterizedTest
@ArgumentsSource(ForwardedForArgs.class)
@MethodSource("forwardedForArgs")
@SuppressWarnings("MockitoDoSetup")
void shouldParseForwardedFor(List<String> headers, @Nullable String expectedAddress) {
doReturn(emptyList()).when(getter).getHttpRequestHeader("request", "forwarded");
@ -104,47 +98,43 @@ class HttpServerAddressAndPortExtractorTest {
assertThat(sink.getPort()).isNull();
}
static final class ForwardedForArgs implements ArgumentsProvider {
private static Stream<Arguments> forwardedForArgs() {
return Stream.of(
// empty/invalid headers
arguments(singletonList(""), null),
arguments(singletonList(";"), null),
arguments(singletonList("\""), null),
arguments(singletonList("\"\""), null),
arguments(singletonList("\"1.2.3.4"), null),
arguments(singletonList("\"[::1]"), null),
arguments(singletonList("[::1"), null),
arguments(singletonList("\"[::1\""), null),
arguments(singletonList("\"[::1\"]"), null),
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
return Stream.of(
// empty/invalid headers
arguments(singletonList(""), null),
arguments(singletonList(";"), null),
arguments(singletonList("\""), null),
arguments(singletonList("\"\""), null),
arguments(singletonList("\"1.2.3.4"), null),
arguments(singletonList("\"[::1]"), null),
arguments(singletonList("[::1"), null),
arguments(singletonList("\"[::1\""), null),
arguments(singletonList("\"[::1\"]"), null),
// ipv6
arguments(singletonList("[::1]"), "::1"),
arguments(singletonList("\"[::1]\":42"), "::1"),
arguments(singletonList("[::1]:42"), "::1"),
arguments(singletonList("\"[::1]:42\""), "::1"),
arguments(singletonList("[::1],1.2.3.4"), "::1"),
arguments(singletonList("[::1];1.2.3.4:42"), "::1"),
arguments(singletonList("[::1]:42abc"), "::1"),
arguments(singletonList("[::1]:abc"), "::1"),
// ipv6
arguments(singletonList("[::1]"), "::1"),
arguments(singletonList("\"[::1]\":42"), "::1"),
arguments(singletonList("[::1]:42"), "::1"),
arguments(singletonList("\"[::1]:42\""), "::1"),
arguments(singletonList("[::1],1.2.3.4"), "::1"),
arguments(singletonList("[::1];1.2.3.4:42"), "::1"),
arguments(singletonList("[::1]:42abc"), "::1"),
arguments(singletonList("[::1]:abc"), "::1"),
// ipv4
arguments(singletonList("1.2.3.4"), "1.2.3.4"),
arguments(singletonList("1.2.3.4, :42"), "1.2.3.4"),
arguments(singletonList("1.2.3.4,4.3.2.1"), "1.2.3.4"),
arguments(singletonList("1.2.3.4:42"), "1.2.3.4"),
arguments(singletonList("1.2.3.4:42abc"), "1.2.3.4"),
arguments(singletonList("1.2.3.4:abc"), "1.2.3.4"),
// ipv4
arguments(singletonList("1.2.3.4"), "1.2.3.4"),
arguments(singletonList("1.2.3.4, :42"), "1.2.3.4"),
arguments(singletonList("1.2.3.4,4.3.2.1"), "1.2.3.4"),
arguments(singletonList("1.2.3.4:42"), "1.2.3.4"),
arguments(singletonList("1.2.3.4:42abc"), "1.2.3.4"),
arguments(singletonList("1.2.3.4:abc"), "1.2.3.4"),
// ipv6 without brackets
arguments(singletonList("::1"), "::1"),
arguments(singletonList("::1,::2,1.2.3.4"), "::1"),
arguments(singletonList("::1;::2;1.2.3.4"), "::1"),
// ipv6 without brackets
arguments(singletonList("::1"), "::1"),
arguments(singletonList("::1,::2,1.2.3.4"), "::1"),
arguments(singletonList("::1;::2;1.2.3.4"), "::1"),
// multiple headers
arguments(asList("1.2.3.4", "::1"), "1.2.3.4"));
}
// multiple headers
arguments(asList("1.2.3.4", "::1"), "1.2.3.4"));
}
}

View File

@ -9,31 +9,21 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.stream.Stream;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.CsvSource;
class SanitizationTest {
static class CqlArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("FROM TABLE WHERE FIELD>=-1234", "FROM TABLE WHERE FIELD>=?"),
Arguments.of(
"SELECT Name, Phone.Number FROM Contact WHERE Address.State = 'NY'",
"SELECT Name, Phone.Number FROM Contact WHERE Address.State = ?"),
Arguments.of("FROM col WHERE @Tag='Something'", "FROM col WHERE @Tag=?"));
}
}
@ParameterizedTest
@ArgumentsSource(CqlArgs.class)
@CsvSource(
delimiter = '|',
value = {
"FROM TABLE WHERE FIELD>=-1234 | FROM TABLE WHERE FIELD>=?",
"SELECT Name, Phone.Number FROM Contact WHERE Address.State = 'NY' | SELECT Name, Phone.Number FROM Contact WHERE Address.State = ?",
"FROM col WHERE @Tag='Something' | FROM col WHERE @Tag=?"
})
void sanitizeCql(String original, String expected) {
DbSpanDecorator decorator = new DbSpanDecorator("cql", "");
@ -46,23 +36,13 @@ class SanitizationTest {
assertThat(actualSanitized).isEqualTo(expected);
}
static class JdbcArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("SELECT 3", "SELECT ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = 1234", "SELECT * FROM TABLE WHERE FIELD = ?"),
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD<-1234", "SELECT * FROM TABLE WHERE FIELD<?"),
Arguments.of(
"SELECT col1 AS col2 FROM users WHERE field=1234",
"SELECT col1 AS col2 FROM users WHERE field=?"));
}
}
@ParameterizedTest
@ArgumentsSource(JdbcArgs.class)
@CsvSource({
"SELECT 3, SELECT ?",
"SELECT * FROM TABLE WHERE FIELD = 1234, SELECT * FROM TABLE WHERE FIELD = ?",
"SELECT * FROM TABLE WHERE FIELD<-1234, SELECT * FROM TABLE WHERE FIELD<?",
"SELECT col1 AS col2 FROM users WHERE field=1234, SELECT col1 AS col2 FROM users WHERE field=?"
})
void sanitizeJdbc(String original, String expected) {
DbSpanDecorator decorator = new DbSpanDecorator("jdbc", "");
@ -75,20 +55,12 @@ class SanitizationTest {
assertThat(actualSanitized).isEqualTo(expected);
}
static class SqlArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of(
"SELECT * FROM table WHERE col1=1234 AND col2>3",
"SELECT * FROM table WHERE col1=? AND col2>?"),
Arguments.of("UPDATE table SET col=12", "UPDATE table SET col=?"),
Arguments.of("insert into table where col=321", "insert into table where col=?"));
}
}
@ParameterizedTest
@ArgumentsSource(SqlArgs.class)
@CsvSource({
"SELECT * FROM table WHERE col1=1234 AND col2>3, SELECT * FROM table WHERE col1=? AND col2>?",
"UPDATE table SET col=12, UPDATE table SET col=?",
"insert into table where col=321, insert into table where col=?"
})
void sanitizeSql(String original, String expected) {
DbSpanDecorator decorator = new DbSpanDecorator("sql", "");

View File

@ -73,11 +73,9 @@ import java.util.stream.Stream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
@SuppressWarnings("deprecation") // using deprecated semconv
@ -562,7 +560,7 @@ public abstract class AbstractGrpcTest {
}
@ParameterizedTest
@ArgumentsSource(ErrorProvider.class)
@MethodSource("provideErrorArguments")
void errorReturned(Status status) throws Exception {
BindableService greeter =
new GreeterGrpc.GreeterImplBase() {
@ -691,7 +689,7 @@ public abstract class AbstractGrpcTest {
}
@ParameterizedTest
@ArgumentsSource(ErrorProvider.class)
@MethodSource("provideErrorArguments")
void errorThrown(Status status) throws Exception {
BindableService greeter =
new GreeterGrpc.GreeterImplBase() {
@ -824,25 +822,22 @@ public abstract class AbstractGrpcTest {
(long) Status.Code.UNKNOWN.value()))))));
}
static class ErrorProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
arguments(Status.UNKNOWN.withCause(new RuntimeException("some error"))),
arguments(Status.DEADLINE_EXCEEDED.withCause(new RuntimeException("some error"))),
arguments(Status.UNIMPLEMENTED.withCause(new RuntimeException("some error"))),
arguments(Status.INTERNAL.withCause(new RuntimeException("some error"))),
arguments(Status.UNAVAILABLE.withCause(new RuntimeException("some error"))),
arguments(Status.DATA_LOSS.withCause(new RuntimeException("some error"))),
arguments(Status.NOT_FOUND.withCause(new RuntimeException("some error"))),
arguments(Status.UNKNOWN.withDescription("some description")),
arguments(Status.DEADLINE_EXCEEDED.withDescription("some description")),
arguments(Status.UNIMPLEMENTED.withDescription("some description")),
arguments(Status.INTERNAL.withDescription("some description")),
arguments(Status.UNAVAILABLE.withDescription("some description")),
arguments(Status.DATA_LOSS.withDescription("some description")),
arguments(Status.NOT_FOUND.withDescription("some description")));
}
private static Stream<Arguments> provideErrorArguments() {
return Stream.of(
arguments(Status.UNKNOWN.withCause(new RuntimeException("some error"))),
arguments(Status.DEADLINE_EXCEEDED.withCause(new RuntimeException("some error"))),
arguments(Status.UNIMPLEMENTED.withCause(new RuntimeException("some error"))),
arguments(Status.INTERNAL.withCause(new RuntimeException("some error"))),
arguments(Status.UNAVAILABLE.withCause(new RuntimeException("some error"))),
arguments(Status.DATA_LOSS.withCause(new RuntimeException("some error"))),
arguments(Status.NOT_FOUND.withCause(new RuntimeException("some error"))),
arguments(Status.UNKNOWN.withDescription("some description")),
arguments(Status.DEADLINE_EXCEEDED.withDescription("some description")),
arguments(Status.UNIMPLEMENTED.withDescription("some description")),
arguments(Status.INTERNAL.withDescription("some description")),
arguments(Status.UNAVAILABLE.withDescription("some description")),
arguments(Status.DATA_LOSS.withDescription("some description")),
arguments(Status.NOT_FOUND.withDescription("some description")));
}
@Test

View File

@ -25,12 +25,10 @@ import java.sql.Connection;
import java.sql.SQLException;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
class OpenTelemetryDataSourceTest {
@ -39,7 +37,7 @@ class OpenTelemetryDataSourceTest {
@SuppressWarnings("deprecation") // using deprecated semconv
@ParameterizedTest
@ArgumentsSource(GetConnectionMethods.class)
@MethodSource("getConnectionMethodsArguments")
void shouldEmitGetConnectionSpans(GetConnectionFunction getConnection) throws SQLException {
JdbcTelemetry telemetry = JdbcTelemetry.create(testing.getOpenTelemetry());
DataSource dataSource = telemetry.wrap(new TestDataSource());
@ -73,7 +71,7 @@ class OpenTelemetryDataSourceTest {
}
@ParameterizedTest
@ArgumentsSource(GetConnectionMethods.class)
@MethodSource("getConnectionMethodsArguments")
void shouldNotEmitGetConnectionSpansWithoutParentSpan(GetConnectionFunction getConnection)
throws SQLException {
JdbcTelemetry telemetry = JdbcTelemetry.create(testing.getOpenTelemetry());
@ -88,14 +86,10 @@ class OpenTelemetryDataSourceTest {
assertDbInfo(dbInfo);
}
static class GetConnectionMethods implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
GetConnectionFunction getConnection = DataSource::getConnection;
GetConnectionFunction getConnectionWithUserAndPass = ds -> ds.getConnection(null, null);
return Stream.of(arguments(getConnection), arguments(getConnectionWithUserAndPass));
}
private static Stream<Arguments> getConnectionMethodsArguments() {
GetConnectionFunction getConnection = DataSource::getConnection;
GetConnectionFunction getConnectionWithUserAndPass = ds -> ds.getConnection(null, null);
return Stream.of(arguments(getConnection), arguments(getConnectionWithUserAndPass));
}
@FunctionalInterface

View File

@ -57,12 +57,10 @@ import org.hornetq.core.server.HornetQServers;
import org.hornetq.jms.client.HornetQConnectionFactory;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
@SuppressWarnings("deprecation") // using deprecated semconv
public class Jms2InstrumentationTest {
@ -136,7 +134,7 @@ public class Jms2InstrumentationTest {
}
}
@ArgumentsSource(DestinationsProvider.class)
@MethodSource("destinationArguments")
@ParameterizedTest
void testMessageConsumer(
DestinationFactory destinationFactory, String destinationName, boolean isTemporary)
@ -196,7 +194,7 @@ public class Jms2InstrumentationTest {
messagingTempDestination(isTemporary))));
}
@ArgumentsSource(DestinationsProvider.class)
@MethodSource("destinationArguments")
@ParameterizedTest
void testMessageListener(
DestinationFactory destinationFactory, String destinationName, boolean isTemporary)
@ -253,7 +251,7 @@ public class Jms2InstrumentationTest {
span -> span.hasName("consumer").hasParent(trace.getSpan(2))));
}
@ArgumentsSource(EmptyReceiveArgumentsProvider.class)
@MethodSource("emptyReceiveArguments")
@ParameterizedTest
void shouldNotEmitTelemetryOnEmptyReceive(
DestinationFactory destinationFactory, MessageReceiver receiver) throws JMSException {
@ -279,38 +277,30 @@ public class Jms2InstrumentationTest {
: satisfies(MESSAGING_DESTINATION_TEMPORARY, AbstractAssert::isNull);
}
static final class EmptyReceiveArgumentsProvider implements ArgumentsProvider {
private static Stream<Arguments> emptyReceiveArguments() {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
MessageReceiver receive = consumer -> consumer.receive(100);
MessageReceiver receiveNoWait = MessageConsumer::receiveNoWait;
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
MessageReceiver receive = consumer -> consumer.receive(100);
MessageReceiver receiveNoWait = MessageConsumer::receiveNoWait;
return Stream.of(
arguments(topic, receive),
arguments(queue, receive),
arguments(topic, receiveNoWait),
arguments(queue, receiveNoWait));
}
return Stream.of(
arguments(topic, receive),
arguments(queue, receive),
arguments(topic, receiveNoWait),
arguments(queue, receiveNoWait));
}
static final class DestinationsProvider implements ArgumentsProvider {
private static Stream<Arguments> destinationArguments() {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
DestinationFactory tempTopic = Session::createTemporaryTopic;
DestinationFactory tempQueue = Session::createTemporaryQueue;
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
DestinationFactory tempTopic = Session::createTemporaryTopic;
DestinationFactory tempQueue = Session::createTemporaryQueue;
return Stream.of(
arguments(topic, "someTopic", false),
arguments(queue, "someQueue", false),
arguments(tempTopic, "(temporary)", true),
arguments(tempQueue, "(temporary)", true));
}
return Stream.of(
arguments(topic, "someTopic", false),
arguments(queue, "someQueue", false),
arguments(tempTopic, "(temporary)", true),
arguments(tempQueue, "(temporary)", true));
}
@FunctionalInterface

View File

@ -39,12 +39,10 @@ import org.apache.activemq.command.ActiveMQTextMessage;
import org.assertj.core.api.AbstractAssert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
@ -93,8 +91,8 @@ abstract class AbstractJms1Test {
}
}
@ArgumentsSource(DestinationsProvider.class)
@ParameterizedTest
@MethodSource("destinationArguments")
void testMessageListener(
DestinationFactory destinationFactory, String destinationName, boolean isTemporary)
throws Exception {
@ -150,8 +148,8 @@ abstract class AbstractJms1Test {
span -> span.hasName("consumer").hasParent(trace.getSpan(2))));
}
@ArgumentsSource(EmptyReceiveArgumentsProvider.class)
@ParameterizedTest
@MethodSource("emptyReceiveArguments")
void shouldNotEmitTelemetryOnEmptyReceive(
DestinationFactory destinationFactory, MessageReceiver receiver) throws JMSException {
@ -170,8 +168,8 @@ abstract class AbstractJms1Test {
testing.waitForTraces(0);
}
@ArgumentsSource(DestinationsProvider.class)
@ParameterizedTest
@MethodSource("destinationArguments")
void shouldCaptureMessageHeaders(
DestinationFactory destinationFactory, String destinationName, boolean isTemporary)
throws Exception {
@ -241,8 +239,8 @@ abstract class AbstractJms1Test {
span -> span.hasName("consumer").hasParent(trace.getSpan(2))));
}
@ArgumentsSource(DestinationsProvider.class)
@ParameterizedTest
@MethodSource("destinationArguments")
void shouldFailWhenSendingReadOnlyMessage(
DestinationFactory destinationFactory, String destinationName, boolean isTemporary)
throws Exception {
@ -307,38 +305,30 @@ abstract class AbstractJms1Test {
: satisfies(MESSAGING_DESTINATION_TEMPORARY, AbstractAssert::isNull);
}
static final class EmptyReceiveArgumentsProvider implements ArgumentsProvider {
private static Stream<Arguments> emptyReceiveArguments() {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
MessageReceiver receive = consumer -> consumer.receive(100);
MessageReceiver receiveNoWait = MessageConsumer::receiveNoWait;
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
MessageReceiver receive = consumer -> consumer.receive(100);
MessageReceiver receiveNoWait = MessageConsumer::receiveNoWait;
return Stream.of(
arguments(topic, receive),
arguments(queue, receive),
arguments(topic, receiveNoWait),
arguments(queue, receiveNoWait));
}
return Stream.of(
arguments(topic, receive),
arguments(queue, receive),
arguments(topic, receiveNoWait),
arguments(queue, receiveNoWait));
}
static final class DestinationsProvider implements ArgumentsProvider {
protected static Stream<Arguments> destinationArguments() {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
DestinationFactory tempTopic = Session::createTemporaryTopic;
DestinationFactory tempQueue = Session::createTemporaryQueue;
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
DestinationFactory tempTopic = Session::createTemporaryTopic;
DestinationFactory tempQueue = Session::createTemporaryQueue;
return Stream.of(
arguments(topic, "someTopic", false),
arguments(queue, "someQueue", false),
arguments(tempTopic, "(temporary)", true),
arguments(tempQueue, "(temporary)", true));
}
return Stream.of(
arguments(topic, "someTopic", false),
arguments(queue, "someQueue", false),
arguments(tempTopic, "(temporary)", true),
arguments(tempQueue, "(temporary)", true));
}
@FunctionalInterface

View File

@ -23,13 +23,13 @@ import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
class Jms1InstrumentationTest extends AbstractJms1Test {
@SuppressWarnings("deprecation") // using deprecated semconv
@ArgumentsSource(DestinationsProvider.class)
@ParameterizedTest
@MethodSource("destinationArguments")
void testMessageConsumer(
DestinationFactory destinationFactory, String destinationName, boolean isTemporary)
throws JMSException {

View File

@ -20,13 +20,13 @@ import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
class Jms1SuppressReceiveSpansTest extends AbstractJms1Test {
@SuppressWarnings("deprecation") // using deprecated semconv
@ArgumentsSource(DestinationsProvider.class)
@ParameterizedTest
@MethodSource("destinationArguments")
void testMessageConsumer(
DestinationFactory destinationFactory, String destinationName, boolean isTemporary)
throws JMSException {

View File

@ -40,12 +40,10 @@ import org.apache.activemq.artemis.jms.client.ActiveMQDestination;
import org.assertj.core.api.AbstractAssert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
@ -107,8 +105,8 @@ abstract class AbstractJms3Test {
}
}
@ArgumentsSource(DestinationsProvider.class)
@ParameterizedTest
@MethodSource("destinationArguments")
void testMessageListener(DestinationFactory destinationFactory, boolean isTemporary)
throws Exception {
@ -165,8 +163,8 @@ abstract class AbstractJms3Test {
span -> span.hasName("consumer").hasParent(trace.getSpan(2))));
}
@ArgumentsSource(EmptyReceiveArgumentsProvider.class)
@ParameterizedTest
@MethodSource("emptyReceiveArguments")
void shouldNotEmitTelemetryOnEmptyReceive(
DestinationFactory destinationFactory, MessageReceiver receiver) throws JMSException {
@ -185,8 +183,8 @@ abstract class AbstractJms3Test {
testing.waitForTraces(0);
}
@ArgumentsSource(DestinationsProvider.class)
@ParameterizedTest
@MethodSource("destinationArguments")
void shouldCaptureMessageHeaders(DestinationFactory destinationFactory, boolean isTemporary)
throws Exception {
@ -263,38 +261,30 @@ abstract class AbstractJms3Test {
: satisfies(MESSAGING_DESTINATION_TEMPORARY, AbstractAssert::isNull);
}
static final class EmptyReceiveArgumentsProvider implements ArgumentsProvider {
private static Stream<Arguments> emptyReceiveArguments() {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
MessageReceiver receive = consumer -> consumer.receive(100);
MessageReceiver receiveNoWait = MessageConsumer::receiveNoWait;
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
MessageReceiver receive = consumer -> consumer.receive(100);
MessageReceiver receiveNoWait = MessageConsumer::receiveNoWait;
return Stream.of(
arguments(topic, receive),
arguments(queue, receive),
arguments(topic, receiveNoWait),
arguments(queue, receiveNoWait));
}
return Stream.of(
arguments(topic, receive),
arguments(queue, receive),
arguments(topic, receiveNoWait),
arguments(queue, receiveNoWait));
}
static final class DestinationsProvider implements ArgumentsProvider {
private static Stream<Arguments> destinationArguments() {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
DestinationFactory tempTopic = Session::createTemporaryTopic;
DestinationFactory tempQueue = Session::createTemporaryQueue;
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
DestinationFactory topic = session -> session.createTopic("someTopic");
DestinationFactory queue = session -> session.createQueue("someQueue");
DestinationFactory tempTopic = Session::createTemporaryTopic;
DestinationFactory tempQueue = Session::createTemporaryQueue;
return Stream.of(
arguments(topic, false),
arguments(queue, false),
arguments(tempTopic, true),
arguments(tempQueue, true));
}
return Stream.of(
arguments(topic, false),
arguments(queue, false),
arguments(tempTopic, true),
arguments(tempQueue, true));
}
@FunctionalInterface

View File

@ -24,13 +24,13 @@ import jakarta.jms.TextMessage;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.activemq.artemis.jms.client.ActiveMQDestination;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
class Jms3InstrumentationTest extends AbstractJms3Test {
@SuppressWarnings("deprecation") // using deprecated semconv
@ArgumentsSource(DestinationsProvider.class)
@ParameterizedTest
@MethodSource("destinationArguments")
void testMessageConsumer(DestinationFactory destinationFactory, boolean isTemporary)
throws JMSException {

View File

@ -21,13 +21,13 @@ import jakarta.jms.MessageProducer;
import jakarta.jms.TextMessage;
import org.apache.activemq.artemis.jms.client.ActiveMQDestination;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
class Jms3SuppressReceiveSpansTest extends AbstractJms3Test {
@SuppressWarnings("deprecation") // using deprecated semconv
@ArgumentsSource(DestinationsProvider.class)
@ParameterizedTest
@MethodSource("destinationArguments")
void testMessageConsumer(DestinationFactory destinationFactory, boolean isTemporary)
throws JMSException {

View File

@ -39,7 +39,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;
@ -48,12 +47,9 @@ import org.jsoup.nodes.Document;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.CsvSource;
public abstract class BaseJsfTest extends AbstractHttpServerUsingTest<Server> {
@ -102,7 +98,7 @@ public abstract class BaseJsfTest extends AbstractHttpServerUsingTest<Server> {
}
@ParameterizedTest
@ArgumentsSource(PathTestArgs.class)
@CsvSource({"hello.jsf, *.jsf", "faces/hello.xhtml, faces/*"})
void testPath(String path, String route) {
AggregatedHttpResponse response =
client.get(address.resolve(path).toString()).aggregate().join();
@ -135,14 +131,6 @@ public abstract class BaseJsfTest extends AbstractHttpServerUsingTest<Server> {
v -> assertThat(v).isNull())))));
}
static class PathTestArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("hello.xhtml", "*.xhtml"), Arguments.of("faces/hello.xhtml", "faces/*"));
}
}
@Test
void testGreeting() {
AggregatedHttpResponse response =

View File

@ -40,7 +40,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
@ -50,12 +49,9 @@ import org.jsoup.nodes.Document;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.CsvSource;
public abstract class BaseJsfTest extends AbstractHttpServerUsingTest<Server> {
@RegisterExtension
@ -111,7 +107,7 @@ public abstract class BaseJsfTest extends AbstractHttpServerUsingTest<Server> {
}
@ParameterizedTest
@ArgumentsSource(PathTestArgs.class)
@CsvSource({"hello.jsf, *.jsf", "faces/hello.xhtml, faces/*"})
void testPath(String path, String route) {
AggregatedHttpResponse response =
client.get(address.resolve(path).toString()).aggregate().join();
@ -144,14 +140,6 @@ public abstract class BaseJsfTest extends AbstractHttpServerUsingTest<Server> {
v -> assertThat(v).isNull())))));
}
static class PathTestArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("hello.jsf", "*.jsf"), Arguments.of("faces/hello.xhtml", "faces/*"));
}
}
@Test
void testGreeting() {
// we need to display the page first before posting data to it

View File

@ -35,12 +35,10 @@ import org.apache.jasper.JasperException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
class JspInstrumentationBasicTests extends AbstractHttpServerUsingTest<Tomcat> {
@ -103,7 +101,7 @@ class JspInstrumentationBasicTests extends AbstractHttpServerUsingTest<Tomcat> {
}
@ParameterizedTest(name = "GET {0}")
@ArgumentsSource(NonErroneousArgs.class)
@MethodSource("nonErroneousArgs")
void testNonErroneousGet(
String testName, String jspFileName, String jspClassName, String jspClassNamePrefix) {
AggregatedHttpResponse res = client.get(jspFileName).aggregate().join();
@ -137,14 +135,11 @@ class JspInstrumentationBasicTests extends AbstractHttpServerUsingTest<Tomcat> {
.build())));
}
static class NonErroneousArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("no java jsp", "/nojava.jsp", "nojava_jsp", ""),
Arguments.of("basic loop jsp", "/common/loop.jsp", "loop_jsp", "common."),
Arguments.of("invalid HTML markup", "/invalidMarkup.jsp", "invalidMarkup_jsp", ""));
}
private static Stream<Arguments> nonErroneousArgs() {
return Stream.of(
Arguments.of("no java jsp", "/nojava.jsp", "nojava_jsp", ""),
Arguments.of("basic loop jsp", "/common/loop.jsp", "loop_jsp", "common."),
Arguments.of("invalid HTML markup", "/invalidMarkup.jsp", "invalidMarkup_jsp", ""));
}
@Test
@ -236,7 +231,7 @@ class JspInstrumentationBasicTests extends AbstractHttpServerUsingTest<Tomcat> {
}
@ParameterizedTest(name = "GET jsp with {0}")
@ArgumentsSource(ErroneousRuntimeErrorsArgs.class)
@MethodSource("erroneousRuntimeErrorsArgs")
void testErroneousRuntimeErrorsGet(
String testName,
String jspFileName,
@ -277,25 +272,22 @@ class JspInstrumentationBasicTests extends AbstractHttpServerUsingTest<Tomcat> {
.build())));
}
static class ErroneousRuntimeErrorsArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of(
"java runtime error",
"/runtimeError.jsp",
"runtimeError_jsp",
ArithmeticException.class,
false),
Arguments.of(
"invalid write",
"/invalidWrite.jsp",
"invalidWrite_jsp",
IndexOutOfBoundsException.class,
true),
Arguments.of(
"invalid write", "/getQuery.jsp", "getQuery_jsp", NullPointerException.class, true));
}
private static Stream<Arguments> erroneousRuntimeErrorsArgs() {
return Stream.of(
Arguments.of(
"java runtime error",
"/runtimeError.jsp",
"runtimeError_jsp",
ArithmeticException.class,
false),
Arguments.of(
"invalid write",
"/invalidWrite.jsp",
"invalidWrite_jsp",
IndexOutOfBoundsException.class,
true),
Arguments.of(
"invalid write", "/getQuery.jsp", "getQuery_jsp", NullPointerException.class, true));
}
@Test
@ -397,7 +389,7 @@ class JspInstrumentationBasicTests extends AbstractHttpServerUsingTest<Tomcat> {
}
@ParameterizedTest
@ArgumentsSource(CompileErrorsArgs.class)
@MethodSource("compileErrorsArgs")
void testCompileErrorShouldNotProduceRenderTracesAndSpans(
String jspFileName, String jspClassName, String jspClassNamePrefix) {
AggregatedHttpResponse res = client.get(jspFileName).aggregate().join();
@ -442,14 +434,11 @@ class JspInstrumentationBasicTests extends AbstractHttpServerUsingTest<Tomcat> {
"org.apache.jasper.compiler.JDTCompiler"))));
}
static class CompileErrorsArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("/compileError.jsp", "compileError_jsp", ""),
Arguments.of(
"/forwards/forwardWithCompileError.jsp", "forwardWithCompileError_jsp", "forwards."));
}
private static Stream<Arguments> compileErrorsArgs() {
return Stream.of(
Arguments.of("/compileError.jsp", "compileError_jsp", ""),
Arguments.of(
"/forwards/forwardWithCompileError.jsp", "forwardWithCompileError_jsp", "forwards."));
}
@ParameterizedTest

View File

@ -30,12 +30,10 @@ import org.apache.jasper.JasperException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
class JspInstrumentationForwardTests extends AbstractHttpServerUsingTest<Tomcat> {
@ -97,7 +95,7 @@ class JspInstrumentationForwardTests extends AbstractHttpServerUsingTest<Tomcat>
}
@ParameterizedTest(name = "Forward to {0}")
@ArgumentsSource(NonErroneousGetForwardArgs.class)
@MethodSource("nonErroneousGetForwardArgs")
void testNonErroneousGetForwardTo(
String name,
String forwardFromFileName,
@ -152,27 +150,24 @@ class JspInstrumentationForwardTests extends AbstractHttpServerUsingTest<Tomcat>
.build())));
}
static class NonErroneousGetForwardArgs implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of(
"no java jsp",
"/forwards/forwardToNoJavaJsp.jsp",
"/nojava.jsp",
"forwardToNoJavaJsp_jsp",
"forwards.",
"nojava_jsp",
""),
Arguments.of(
"normal java jsp",
"/forwards/forwardToSimpleJava.jsp",
"/common/loop.jsp",
"forwardToSimpleJava_jsp",
"forwards.",
"loop_jsp",
"common."));
}
private static Stream<Arguments> nonErroneousGetForwardArgs() {
return Stream.of(
Arguments.of(
"no java jsp",
"/forwards/forwardToNoJavaJsp.jsp",
"/nojava.jsp",
"forwardToNoJavaJsp_jsp",
"forwards.",
"nojava_jsp",
""),
Arguments.of(
"normal java jsp",
"/forwards/forwardToSimpleJava.jsp",
"/common/loop.jsp",
"forwardToSimpleJava_jsp",
"forwards.",
"loop_jsp",
"common."));
}
@Test

View File

@ -9,11 +9,9 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
class KubernetesRequestUtilsTest {
@ -41,7 +39,7 @@ class KubernetesRequestUtilsTest {
}
@ParameterizedTest
@ArgumentsSource(ParseCoreResourceArgumentsProvider.class)
@MethodSource("parseCoreResourceArguments")
void parseCoreResource(
String urlPath,
String apiGroup,
@ -61,7 +59,7 @@ class KubernetesRequestUtilsTest {
}
@ParameterizedTest
@ArgumentsSource(ParseRegularResourceArgumentsProvider.class)
@MethodSource("parseRegularResourceArguments")
void parseRegularResource(
String urlPath,
String apiGroup,
@ -83,7 +81,7 @@ class KubernetesRequestUtilsTest {
}
@ParameterizedTest
@ArgumentsSource(K8sRequestVerbsArgumentsProvider.class)
@MethodSource("k8sRequestVerbsArguments")
void k8sRequestVerbs(
String httpVerb,
boolean hasNamePathParam,
@ -93,100 +91,86 @@ class KubernetesRequestUtilsTest {
.isEqualTo(kubernetesVerb);
}
private static class K8sRequestVerbsArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext)
throws Exception {
return Stream.of(
Arguments.of("GET", true, false, KubernetesVerb.GET),
Arguments.of("GET", false, true, KubernetesVerb.WATCH),
Arguments.of("GET", false, false, KubernetesVerb.LIST),
Arguments.of("POST", false, false, KubernetesVerb.CREATE),
Arguments.of("PUT", false, false, KubernetesVerb.UPDATE),
Arguments.of("PATCH", false, false, KubernetesVerb.PATCH),
Arguments.of("DELETE", true, false, KubernetesVerb.DELETE),
Arguments.of("DELETE", false, false, KubernetesVerb.DELETE_COLLECTION));
}
private static Stream<Arguments> k8sRequestVerbsArguments() {
return Stream.of(
Arguments.of("GET", true, false, KubernetesVerb.GET),
Arguments.of("GET", false, true, KubernetesVerb.WATCH),
Arguments.of("GET", false, false, KubernetesVerb.LIST),
Arguments.of("POST", false, false, KubernetesVerb.CREATE),
Arguments.of("PUT", false, false, KubernetesVerb.UPDATE),
Arguments.of("PATCH", false, false, KubernetesVerb.PATCH),
Arguments.of("DELETE", true, false, KubernetesVerb.DELETE),
Arguments.of("DELETE", false, false, KubernetesVerb.DELETE_COLLECTION));
}
private static class ParseRegularResourceArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext)
throws Exception {
return Stream.of(
Arguments.of("/apis/apps/v1/deployments", "apps", "v1", "deployments", null, null, null),
Arguments.of(
"/apis/apps/v1/namespaces/default/deployments",
"apps",
"v1",
"deployments",
null,
"default",
null),
Arguments.of(
"/apis/apps/v1/namespaces/default/deployments/foo",
"apps",
"v1",
"deployments",
null,
"default",
"foo"),
Arguments.of(
"/apis/apps/v1/namespaces/default/deployments/foo/status",
"apps",
"v1",
"deployments",
"status",
"default",
"foo"),
Arguments.of(
"/apis/example.io/v1alpha1/foos", "example.io", "v1alpha1", "foos", null, null, null),
Arguments.of(
"/apis/example.io/v1alpha1/namespaces/default/foos",
"example.io",
"v1alpha1",
"foos",
null,
"default",
null),
Arguments.of(
"/apis/example.io/v1alpha1/namespaces/default/foos/foo",
"example.io",
"v1alpha1",
"foos",
null,
"default",
"foo"),
Arguments.of(
"/apis/example.io/v1alpha1/namespaces/default/foos/foo/status",
"example.io",
"v1alpha1",
"foos",
"status",
"default",
"foo"));
}
private static Stream<Arguments> parseRegularResourceArguments() {
return Stream.of(
Arguments.of("/apis/apps/v1/deployments", "apps", "v1", "deployments", null, null, null),
Arguments.of(
"/apis/apps/v1/namespaces/default/deployments",
"apps",
"v1",
"deployments",
null,
"default",
null),
Arguments.of(
"/apis/apps/v1/namespaces/default/deployments/foo",
"apps",
"v1",
"deployments",
null,
"default",
"foo"),
Arguments.of(
"/apis/apps/v1/namespaces/default/deployments/foo/status",
"apps",
"v1",
"deployments",
"status",
"default",
"foo"),
Arguments.of(
"/apis/example.io/v1alpha1/foos", "example.io", "v1alpha1", "foos", null, null, null),
Arguments.of(
"/apis/example.io/v1alpha1/namespaces/default/foos",
"example.io",
"v1alpha1",
"foos",
null,
"default",
null),
Arguments.of(
"/apis/example.io/v1alpha1/namespaces/default/foos/foo",
"example.io",
"v1alpha1",
"foos",
null,
"default",
"foo"),
Arguments.of(
"/apis/example.io/v1alpha1/namespaces/default/foos/foo/status",
"example.io",
"v1alpha1",
"foos",
"status",
"default",
"foo"));
}
private static class ParseCoreResourceArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("/api/v1/pods", "", "v1", "pods", null, null, null),
Arguments.of("/api/v1/namespaces/default/pods", "", "v1", "pods", null, "default", null),
Arguments.of(
"/api/v1/namespaces/default/pods/foo", "", "v1", "pods", null, "default", "foo"),
Arguments.of(
"/api/v1/namespaces/default/pods/foo/exec",
"",
"v1",
"pods",
"exec",
"default",
"foo"));
}
private static Stream<Arguments> parseCoreResourceArguments() {
return Stream.of(
Arguments.of("/api/v1/pods", "", "v1", "pods", null, null, null),
Arguments.of("/api/v1/namespaces/default/pods", "", "v1", "pods", null, "default", null),
Arguments.of(
"/api/v1/namespaces/default/pods/foo", "", "v1", "pods", null, "default", "foo"),
Arguments.of(
"/api/v1/namespaces/default/pods/foo/exec",
"",
"v1",
"pods",
"exec",
"default",
"foo"));
}
}

View File

@ -6,20 +6,15 @@
package io.opentelemetry.javaagent.instrumentation.reactornetty.v1_0;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
import java.net.InetSocketAddress;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ -68,7 +63,10 @@ class FailedRequestWithUrlMakerTest {
}
@ParameterizedTest
@ArgumentsSource(DefaultPortsArguments.class)
@CsvSource({
"80, false",
"443, true",
})
@SuppressWarnings("MockitoDoSetup")
void shouldSkipDefaultPorts(int port, boolean isSecure) {
when(config.baseUrl()).thenReturn("/");
@ -83,12 +81,4 @@ class FailedRequestWithUrlMakerTest {
assertThat(request.resourceUrl())
.isEqualTo((isSecure ? "https" : "http") + "://opentelemetry.io/docs");
}
static final class DefaultPortsArguments implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
return Stream.of(arguments(80, false), arguments(443, true));
}
}
}

View File

@ -18,11 +18,9 @@ import java.util.function.Predicate;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ -101,7 +99,7 @@ class JarServiceNameDetectorTest {
}
@ParameterizedTest
@ArgumentsSource(SunCommandLineProvider.class)
@MethodSource("sunCommandLineArguments")
void createResource_sunCommandLine(String commandLine, Path jarPath) {
Function<String, String> getProperty =
key -> "sun.java.command".equals(key) ? commandLine : null;
@ -131,18 +129,14 @@ class JarServiceNameDetectorTest {
assertThat(resource.getAttributes()).isEmpty();
}
static final class SunCommandLineProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
Path path = Paths.get("path", "to", "my-service.jar");
Path pathWithSpaces = Paths.get("path to app", "with spaces", "my-service.jar");
Path pathWithoutExtension = Paths.get("path to app", "with spaces", "my-service");
return Stream.of(
arguments(path.toString(), path),
arguments(pathWithSpaces + " 1 2 3", pathWithSpaces),
arguments(pathWithoutExtension + " 1 2 3", pathWithoutExtension));
}
private static Stream<Arguments> sunCommandLineArguments() {
Path path = Paths.get("path", "to", "my-service.jar");
Path pathWithSpaces = Paths.get("path to app", "with spaces", "my-service.jar");
Path pathWithoutExtension = Paths.get("path to app", "with spaces", "my-service");
return Stream.of(
arguments(path.toString(), path),
arguments(pathWithSpaces + " 1 2 3", pathWithSpaces),
arguments(pathWithoutExtension + " 1 2 3", pathWithoutExtension));
}
static boolean failPath(Path file) {

View File

@ -6,7 +6,6 @@
package io.opentelemetry.javaagent.instrumentation.spring.jms.v6_0;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
@ -19,15 +18,11 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
@ -68,8 +63,8 @@ abstract class AbstractSpringJmsListenerTest {
}
}
@ArgumentsSource(SpringJmsListenerTest.ConfigClasses.class)
@ParameterizedTest
@ValueSource(classes = {AnnotatedListenerConfig.class, ManualListenerConfig.class})
@SuppressWarnings("unchecked")
void testSpringJmsListener(Class<?> configClass)
throws ExecutionException, InterruptedException, TimeoutException {
@ -102,13 +97,4 @@ abstract class AbstractSpringJmsListenerTest {
props.put("test.broker-url", "tcp://" + broker.getHost() + ":" + broker.getMappedPort(61616));
return props;
}
static final class ConfigClasses implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
arguments(AnnotatedListenerConfig.class), arguments(ManualListenerConfig.class));
}
}
}

View File

@ -29,7 +29,7 @@ import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import org.assertj.core.api.AbstractStringAssert;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jms.core.JmsTemplate;
@ -82,8 +82,8 @@ class SpringJmsListenerTest extends AbstractSpringJmsListenerTest {
span -> span.hasName("consumer").hasParent(trace.getSpan(1))));
}
@ArgumentsSource(ConfigClasses.class)
@ParameterizedTest
@ValueSource(classes = {AnnotatedListenerConfig.class, ManualListenerConfig.class})
@SuppressWarnings("unchecked")
void shouldCaptureHeaders(Class<?> configClass)
throws ExecutionException, InterruptedException, TimeoutException {

View File

@ -13,16 +13,14 @@ import static org.mockito.Mockito.when;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import java.util.TreeSet;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
class AgentConfigTest {
@ParameterizedTest(name = "isInstrumentationEnabled({0}) = {4}")
@ArgumentsSource(InstrumentationEnabledParams.class)
@MethodSource("instrumentationEnabledParams")
void testIsInstrumentationEnabled(
@SuppressWarnings("unused") String description,
Boolean firstEnabled,
@ -40,48 +38,43 @@ class AgentConfigTest {
config, new TreeSet<>(asList("first", "second")), defaultEnabled));
}
private static class InstrumentationEnabledParams implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of(
"enabled by default, both instrumentations are off", false, false, true, false),
Arguments.of("enabled by default, first instrumentation is on", true, null, true, true),
Arguments.of("enabled by default, second instrumentation is on", null, true, true, true),
Arguments.of("enabled by default, both instrumentations are on", true, true, true, true),
Arguments.of(
"enabled by default, first instrumentation is off, second is on",
false,
true,
true,
false),
Arguments.of(
"enabled by default, first instrumentation is on, second is off",
true,
false,
true,
true),
Arguments.of("enabled by default", null, null, true, true),
Arguments.of(
"disabled by default, both instrumentations are off", false, false, false, false),
Arguments.of("disabled by default, first instrumentation is on", true, null, false, true),
Arguments.of(
"disabled by default, second instrumentation is on", null, true, false, true),
Arguments.of("disabled by default, both instrumentation are on", true, true, false, true),
Arguments.of(
"disabled by default, first instrumentation is off, second is on",
false,
true,
false,
false),
Arguments.of(
"disabled by default, first instrumentation is on, second is off",
true,
false,
false,
true),
Arguments.of("disabled by default", null, null, false, false));
}
private static Stream<Arguments> instrumentationEnabledParams() {
return Stream.of(
Arguments.of(
"enabled by default, both instrumentations are off", false, false, true, false),
Arguments.of("enabled by default, first instrumentation is on", true, null, true, true),
Arguments.of("enabled by default, second instrumentation is on", null, true, true, true),
Arguments.of("enabled by default, both instrumentations are on", true, true, true, true),
Arguments.of(
"enabled by default, first instrumentation is off, second is on",
false,
true,
true,
false),
Arguments.of(
"enabled by default, first instrumentation is on, second is off",
true,
false,
true,
true),
Arguments.of("enabled by default", null, null, true, true),
Arguments.of(
"disabled by default, both instrumentations are off", false, false, false, false),
Arguments.of("disabled by default, first instrumentation is on", true, null, false, true),
Arguments.of("disabled by default, second instrumentation is on", null, true, false, true),
Arguments.of("disabled by default, both instrumentation are on", true, true, false, true),
Arguments.of(
"disabled by default, first instrumentation is off, second is on",
false,
true,
false,
false),
Arguments.of(
"disabled by default, first instrumentation is on, second is off",
true,
false,
false,
true),
Arguments.of("disabled by default", null, null, false, false));
}
}