Created
March 12, 2014 03:07
-
-
Save yangwansu/9500046 to your computer and use it in GitHub Desktop.
assert 를 등록 시켜보았음 여기서 assert 는 석진이가 말한 구아바의 기능을 간단하게 구현 한 것에 지나지 않음
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
package com.springapp.mvc; | |
public class Assert { | |
public static void isNotNull(Object expected,String message) { | |
if(null == expected){ | |
throw new AssertException(message); | |
} | |
} | |
public static void isTrue(boolean expr, String message) { | |
if(false == expr){ | |
throw new AssertException(message); | |
} | |
} | |
} |
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
package com.springapp.mvc; | |
public class AssertException extends RuntimeException { | |
public AssertException(String message) { | |
super(message); | |
} | |
} |
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
package com.springapp.mvc; | |
import static com.google.common.base.Preconditions.*; | |
public class GuavaTest1 { | |
public void testForNonNullArgument(final String parameter){ | |
Assert.isNotNull(parameter,"null값은 허용되지 않습니다."); | |
} | |
public void testDivisorNotZero(final int divisor){ | |
Assert.isTrue(divisor != 0,"0으로 나눌 수 없습니다."); | |
} | |
public void testArrayElement(final String[] strArray, final int indexNumber){ | |
Assert.isTrue( 0 <= indexNumber && strArray.length > indexNumber ,"지정된 Array 요소 위치가 벗어났습니다."); | |
} | |
public void testArrayPosition(final String[] strArray, final int indexNumber){ | |
Assert.isTrue( 0 <= indexNumber && strArray.length > indexNumber ,"지정된 Array 요소 위치가 벗어났습니다."); | |
} | |
public void testState(){ | |
Assert.isTrue(false,"반드시 true "); | |
} | |
} |
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
package com.springapp.mvc; | |
public class TestMain { | |
public static void main(String[] args) { | |
TestRunner testRunner =new TestRunner(GuavaTest1.class); | |
testRunner.test("preconditions.checkNotNull", "testArrayElement", new String[]{"Dustin", "java"}, 3); | |
testRunner.test("precondition.checkArgument", "testDivisorNotZero", 0); | |
testRunner.test("Preconditions.checkElementIndex", "testForNonNullArgument", null); | |
testRunner.test("Preconditions.checkPositionIndex", "testArrayPosition", new String[]{"Dustin", "java"}, 3); | |
testRunner.test("Preconditions.checkState", "testState"); | |
} | |
} |
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
package com.springapp.mvc; | |
import java.lang.reflect.InvocationTargetException; | |
public interface TestMethodInvoker { | |
void invoker(Object testInstance, String testName, Object[] parameters) throws InvocationTargetException, IllegalAccessException; | |
} |
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
package com.springapp.mvc; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import static java.lang.System.err; | |
public class TestRunner { | |
private final Class<?> testClass; | |
public TestRunner(Class<?> testClass) { | |
this.testClass = testClass ; | |
} | |
public void test(String message, String testName,Object... parameters) { | |
printHeader(message); | |
try{ | |
TestMethodInvoker testcase = new TestMethodInvoker() { | |
@Override | |
public void invoker(Object testInstance, String testName, Object[] parmeters) throws InvocationTargetException, IllegalAccessException { | |
for (Method each : testClass.getMethods()) { | |
if ( | |
isTestMehtod(each)) { | |
//each. | |
if (each.getName().equals(testName)) { | |
if (null == parmeters) { | |
each.invoke(testInstance); | |
} else { | |
each.invoke(testInstance, parmeters); | |
} | |
} | |
} | |
} | |
} | |
}; | |
testcase.invoker(testClass.newInstance(), testName, parameters); | |
}catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
private boolean isTestMehtod(Method each) { | |
return each.getName().startsWith("test") | |
|| each.getReturnType().equals("void"); | |
} | |
private void printHeader(final String newHeaderText){ | |
line(); | |
err.println(newHeaderText); | |
line(); | |
} | |
private static void line() { | |
err.println("=========================================="); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment