forked from pinpoint-apm/pinpoint
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pinpoint-apm#9642] Add Generic ArgumentValidator
- Loading branch information
Showing
13 changed files
with
367 additions
and
228 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/ArgumentValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2018 NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.pinpoint.bootstrap.util.argument; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public class ArgumentValidator implements Validator { | ||
private final Predicate[] predicates; | ||
|
||
private final int minArgsSize; | ||
|
||
public ArgumentValidator(List<Predicate> argumentList) { | ||
this(argumentList, 0); | ||
} | ||
|
||
public ArgumentValidator(List<Predicate> predicates, int minArgsSize) { | ||
this.predicates = predicates.toArray(new Predicate[0]); | ||
this.minArgsSize = minIndex(this.predicates, minArgsSize); | ||
} | ||
|
||
private static int minIndex(Predicate[] predicates, int minArgsSize) { | ||
if (minArgsSize != 0) { | ||
return minArgsSize; | ||
} | ||
int max = -1; | ||
for (Predicate predicate : predicates) { | ||
max = Math.max(max, predicate.index()); | ||
} | ||
return max + 1; | ||
} | ||
|
||
@Override | ||
public boolean validate(Object[] args) { | ||
if (args == null) { | ||
return false; | ||
} | ||
if (args.length < minArgsSize) { | ||
return false; | ||
} | ||
|
||
for (Predicate predicate : this.predicates) { | ||
if (!predicate.test(args)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...rap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/ClassPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.navercorp.pinpoint.bootstrap.util.argument; | ||
|
||
import com.navercorp.pinpoint.bootstrap.logging.PLogger; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public class ClassPredicate implements Predicate { | ||
private final PLogger logger; | ||
|
||
private final Class<?> clazz; | ||
private final int index; | ||
|
||
|
||
public ClassPredicate(PLogger logger, Class<?> clazz, int index) { | ||
this.logger = logger; | ||
this.clazz = clazz; | ||
this.index = index; | ||
} | ||
|
||
@Override | ||
public boolean test(Object[] args) { | ||
final Object param = args[index]; | ||
if (!clazz.isInstance(param)) { | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("Invalid args[{}] object, Not implemented of {}. args[{}]={}", index, clazz, index, param); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public int index() { | ||
return index; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Predicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.navercorp.pinpoint.bootstrap.util.argument; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public interface Predicate { | ||
boolean test(Object[] args); | ||
|
||
int index(); | ||
} |
43 changes: 43 additions & 0 deletions
43
...otstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Validation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.navercorp.pinpoint.bootstrap.util.argument; | ||
|
||
import com.navercorp.pinpoint.bootstrap.logging.PLogger; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public class Validation { | ||
private final PLogger logger; | ||
private final List<Predicate> list = new ArrayList<>(4); | ||
private int min; | ||
|
||
public Validation(PLogger logger) { | ||
this.logger = Objects.requireNonNull(logger, "logger"); | ||
} | ||
|
||
public Validation addArgument(Class<?> clazz, int index) { | ||
this.list.add(new ClassPredicate(logger, clazz, index)); | ||
return this; | ||
} | ||
|
||
public Validation addPredicate(Predicate predicate) { | ||
Objects.requireNonNull(predicate, "predicate"); | ||
|
||
this.list.add(predicate); | ||
return this; | ||
} | ||
|
||
public Validation minArgsSize(int min) { | ||
this.min = min; | ||
return this; | ||
} | ||
|
||
public Validator build() { | ||
List<Predicate> list = new ArrayList<>(this.list); | ||
return new ArgumentValidator(list, min); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...rap-core/src/test/java/com/navercorp/pinpoint/bootstrap/util/argument/ValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.navercorp.pinpoint.bootstrap.util.argument; | ||
|
||
import com.navercorp.pinpoint.bootstrap.logging.PLogger; | ||
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
class ValidationTest { | ||
|
||
private final Validator validator = newArgumentValidator(); | ||
|
||
public Validator newArgumentValidator() { | ||
PLogger logger = PLoggerFactory.getLogger(this.getClass()); | ||
|
||
Validation validation = new Validation(logger); | ||
validation.addArgument(String.class, 0); | ||
validation.addArgument(Long.class, 1); | ||
return validation.build(); | ||
} | ||
|
||
|
||
@Test | ||
void valid() { | ||
Object[] arguments = new Object[2]; | ||
arguments[0] = "abc"; | ||
arguments[1] = 123L; | ||
Assertions.assertTrue(validator.validate(arguments)); | ||
} | ||
|
||
@Test | ||
void empty() { | ||
PLogger logger = PLoggerFactory.getLogger(this.getClass()); | ||
|
||
Validation validation = new Validation(logger); | ||
Validator validator = validation.build(); | ||
|
||
Assertions.assertTrue(validator.validate(new Object[0])); | ||
Assertions.assertTrue(validator.validate(new Object[2])); | ||
} | ||
|
||
@Test | ||
void predicate() { | ||
PLogger logger = PLoggerFactory.getLogger(this.getClass()); | ||
|
||
Validation validation = new Validation(logger); | ||
validation.addPredicate(new Predicate() { | ||
@Override | ||
public boolean test(Object[] args) { | ||
Object arg = args[index()]; | ||
return arg instanceof String; | ||
} | ||
|
||
@Override | ||
public int index() { | ||
return 0; | ||
} | ||
}); | ||
Validator validator = validation.build(); | ||
|
||
Assertions.assertFalse(validator.validate(new Object[]{1})); | ||
Assertions.assertTrue(validator.validate(new Object[]{"abc"})); | ||
} | ||
|
||
@Test | ||
void valid_boundary_check() { | ||
|
||
Assertions.assertFalse(validator.validate(null)); | ||
Assertions.assertFalse(validator.validate(new Object[0])); | ||
Assertions.assertFalse(validator.validate(new Object[1])); | ||
|
||
Object[] arguments = new Object[10]; | ||
arguments[0] = "abc"; | ||
arguments[1] = 123L; | ||
Assertions.assertTrue(validator.validate(arguments)); | ||
} | ||
|
||
@Test | ||
void valid_fail1() { | ||
Object[] arguments = new Object[2]; | ||
arguments[0] = "abc"; | ||
arguments[1] = new Object(); | ||
Assertions.assertFalse(validator.validate(arguments)); | ||
} | ||
|
||
|
||
@Test | ||
void valid_fail2() { | ||
Object[] arguments = new Object[2]; | ||
arguments[0] = new Object(); | ||
arguments[1] = 123L; | ||
Assertions.assertFalse(validator.validate(arguments)); | ||
} | ||
|
||
@Test | ||
void valid_min() { | ||
PLogger logger = PLoggerFactory.getLogger(this.getClass()); | ||
|
||
Validation validation = new Validation(logger); | ||
validation.addArgument(String.class, 0); | ||
validation.addArgument(Long.class, 1); | ||
validation.minArgsSize(3); | ||
Validator validator = validation.build(); | ||
|
||
Object[] arguments1 = new Object[2]; | ||
arguments1[0] = "abc"; | ||
arguments1[1] = 123L; | ||
Assertions.assertFalse(validator.validate(arguments1)); | ||
|
||
Object[] arguments2 = new Object[5]; | ||
arguments2[0] = "abc"; | ||
arguments2[1] = 123L; | ||
Assertions.assertTrue(validator.validate(arguments2)); | ||
|
||
|
||
} | ||
} |
86 changes: 0 additions & 86 deletions
86
...main/java/com/navercorp/pinpoint/plugin/common/servlet/util/ServletArgumentValidator.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.