Skip to content

Instantly share code, notes, and snippets.

@yangwansu
Created March 11, 2014 04:44
Show Gist options
  • Save yangwansu/9479645 to your computer and use it in GitHub Desktop.
Save yangwansu/9479645 to your computer and use it in GitHub Desktop.
중복제거가 아닌 중복 만들기 ...
package com.springapp.mvc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static com.google.common.base.Preconditions.*;
import static java.lang.System.err;
public class GuavaTest1 {
private final boolean initialzed = false;
public void testForNonNullArgument(final String parameter)
{
final String localPrameter = checkNotNull(parameter, "null값은"
+ "허용되지 않습니다.");
}
public void testDivisorNotZero(final int divisor){
checkArgument(divisor != 0, "0으로 나눌 수 없습니다.");
}
public void testArrayElement(final String[] strArray,
final int indexNumber){
final int index = checkElementIndex(indexNumber, strArray.length,
"지정된 Array 요소 위치가 벗어났습니다.");
}
public void testarrayPosition(final String[] strarray, final int indexNumber){
final int index = checkPositionIndex(indexNumber, strarray.length,
"지정된 Array 요소 위치가 벗어났습니다.");
}
public void testState(){
checkState(this.initialzed, "초기화 되지 않았습니다.");
}
public static void printHeader(final String newHeaderText){
err.println("\n=====================");
err.println("===" + newHeaderText);
err.println("=======================");
}
public static void main(String[] args) {
GuavaTest1 testInstance = new GuavaTest1();
test(new TestCase() {
@Override
public void run(Object testInstance, String testName, Object[] parameters) throws InvocationTargetException, IllegalAccessException {
for (Method each : GuavaTest1.class.getMethods()){
if(
each.getName().startsWith("test")
||each.getReturnType().equals("void")){
//each.
if(each.getName().equals(testName)){
if(null == parameters){
each.invoke(testInstance);
}else{
each.invoke(testInstance,parameters);
}
}
}
}
}
}, "preconditions.checkNotNull", testInstance, "testArrayElement",new String[]{"Dustin", "java"}, 3);
test(new TestCase() {
@Override
public void run(Object testInstance,String testName, Object[] parameters) throws InvocationTargetException, IllegalAccessException {
for (Method each : GuavaTest1.class.getMethods()){
if(
each.getName().startsWith("test")
||each.getReturnType().equals("void")){
//each.
if(each.getName().equals(testName)){
if(null == parameters){
each.invoke(testInstance);
}else{
each.invoke(testInstance,parameters);
}
}
}
}
}
}, "precondition.checkArgument", testInstance,"testDivisorNotZero",0);
test(new TestCase() {
@Override
public void run(Object testInstance, String testName, Object[] parameters) throws InvocationTargetException, IllegalAccessException {
for (Method each : GuavaTest1.class.getMethods()){
if(
each.getName().startsWith("test")
||each.getReturnType().equals("void")){
//each.
if(each.getName().equals(testName)){
if(null == parameters){
each.invoke(testInstance);
}else{
each.invoke(testInstance,parameters);
}
}
}
}
}
}, "Preconditions.checkElementIndex", testInstance,"testForNonNullArgument",null);
test(new TestCase() {
@Override
public void run(Object testInstance, String testName, Object[] parameters) throws InvocationTargetException, IllegalAccessException {
for (Method each : GuavaTest1.class.getMethods()){
if(
each.getName().startsWith("test")
||each.getReturnType().equals("void")){
//each.
if(each.getName().equals(testName)){
if(null == parameters){
each.invoke(testInstance);
}else{
each.invoke(testInstance,parameters);
}
}
}
}
}
}, "Preconditions.checkPositionIndex", testInstance,"testarrayPosition", new String[]{"Dustin", "java"}, 3);
test(new TestCase() {
@Override
public void run(Object testInstance, String testName, Object[] parmeters) throws InvocationTargetException, IllegalAccessException {
for (Method each : GuavaTest1.class.getMethods()){
if(
each.getName().startsWith("test")
||each.getReturnType().equals("void")){
//each.
if(each.getName().equals(testName)){
if(null == parmeters){
each.invoke(testInstance);
}else{
each.invoke(testInstance,parmeters);
}
}
}
}
}
}, "Preconditions.checkState", testInstance,"testState");
}
private static void test(TestCase testcase, String message, Object testInstance, String testName,Object... parameters) {
printHeader(message);
try{
testcase.run(testInstance,testName, parameters);
}catch(Exception e){
e.printStackTrace();
}
}
}
package com.springapp.mvc;
import java.lang.reflect.InvocationTargetException;
public interface TestCase {
void run(Object testInstance, String testName, Object[] parameters) throws InvocationTargetException, IllegalAccessException;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment