diff --git a/junit/beforeAfter/Makefile b/junit/beforeAfter/Makefile index 026163b..0bd76dd 100644 --- a/junit/beforeAfter/Makefile +++ b/junit/beforeAfter/Makefile @@ -2,10 +2,10 @@ all: compile compile: mkdir -p bin - javac testcase/TestBeforeAfter.java testcase/TestRunner.java -d bin + javac -classpath /usr/share/java/junit4.jar testcase/TestBeforeAfter.java testcase/TestRunner.java -d bin test: - cd bin ; java TestRunner + cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner clean: rm bin/*.class diff --git a/junit/junit_init.sh b/junit/junit_init.sh index e772c95..631d3c0 100755 --- a/junit/junit_init.sh +++ b/junit/junit_init.sh @@ -2,4 +2,4 @@ sudo apt update sudo apt install openjdk-8-jdk-headless junit4 make -export CLASSPATH=/usr/share/java/junit4.jar:. +#export CLASSPATH=/usr/share/java/junit4.jar:. diff --git a/junit/onlyTest/Makefile b/junit/onlyTest/Makefile index 498afd0..c6f9ad5 100644 --- a/junit/onlyTest/Makefile +++ b/junit/onlyTest/Makefile @@ -2,10 +2,10 @@ all: compile compile: mkdir -p bin - javac src/TestJunit.java src/TestRunner.java -d bin + javac -classpath /usr/share/java/junit4.jar src/TestJunit.java src/TestRunner.java -d bin test: - cd bin ; java TestRunner + cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner clean: rm bin/*.class diff --git a/junit/stack/Makefile b/junit/stack/Makefile index 4143d39..14022a5 100644 --- a/junit/stack/Makefile +++ b/junit/stack/Makefile @@ -6,10 +6,10 @@ compile: compileTest: mkdir -p bin - javac src/Stack.java testcase/TestStack.java testcase/TestRunner.java -d bin + javac -classpath /usr/share/java/junit4.jar src/Stack.java testcase/TestStack.java testcase/TestRunner.java -d bin test: - cd bin ; java TestRunner + cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner clean: rm bin/*.class diff --git a/junit/testCode/Makefile b/junit/testCode/Makefile index 97ca8f0..f2f66ab 100644 --- a/junit/testCode/Makefile +++ b/junit/testCode/Makefile @@ -6,10 +6,10 @@ compile: compileTest: mkdir -p bin - javac src/Distant.java testcase/TestDistant.java testcase/TestRunner.java -d bin + javac -classpath /usr/share/java/junit4.jar src/Distant.java testcase/TestDistant.java testcase/TestRunner.java -d bin test: - cd bin ; java TestRunner + cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner clean: rm bin/*.class diff --git a/junit/testException/Makefile b/junit/testException/Makefile new file mode 100644 index 0000000..62e75df --- /dev/null +++ b/junit/testException/Makefile @@ -0,0 +1,17 @@ +all: compile compileTest + +compile: + mkdir -p bin + javac src/Stack.java src/EmptyStackException.java src/FullStackException.java -d bin + +compileTest: + mkdir -p bin + javac -classpath /usr/share/java/junit4.jar src/Stack.java \ + src/EmptyStackException.java src/FullStackException.java \ + testcase/TestException.java testcase/TestRunner.java -d bin + +test: + cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner + +clean: + rm bin/*.class diff --git a/junit/testException/src/EmptyStackException.java b/junit/testException/src/EmptyStackException.java new file mode 100644 index 0000000..9e78d7c --- /dev/null +++ b/junit/testException/src/EmptyStackException.java @@ -0,0 +1,5 @@ +public class EmptyStackException extends Exception{ + public EmptyStackException (String msg){ + super (msg); + } +} diff --git a/junit/testException/src/FullStackException.java b/junit/testException/src/FullStackException.java new file mode 100644 index 0000000..5ec580e --- /dev/null +++ b/junit/testException/src/FullStackException.java @@ -0,0 +1,5 @@ +public class FullStackException extends Exception{ + public FullStackException (String msg){ + super (msg); + } +} diff --git a/junit/testException/src/Stack.java b/junit/testException/src/Stack.java new file mode 100644 index 0000000..5d68d90 --- /dev/null +++ b/junit/testException/src/Stack.java @@ -0,0 +1,45 @@ +public class Stack { + private Node head; + private int size; + private int max_size; + + class Node { + Object data; + Node next; + } + + public Stack(int max_size) { + head = null; + size = 0; + this.max_size = max_size; + } + + public Stack() { + this(10); + } + + public int size() { + return this.size; + } + + public void push(Object obj) throws FullStackException { + if (size >= max_size) { + throw new FullStackException("Push a full stack"); + } + Node tmp = new Node(); + tmp.next = head; + tmp.data = obj; + size++; + head = tmp; + } + + public Object pop() throws EmptyStackException { + if (head == null) { + throw new EmptyStackException("Pop an empty stack"); + } + Object tmp = head.data; + head = head.next; + size--; + return tmp; + } +} diff --git a/junit/testException/testcase/TestException.java b/junit/testException/testcase/TestException.java new file mode 100644 index 0000000..d98e721 --- /dev/null +++ b/junit/testException/testcase/TestException.java @@ -0,0 +1,67 @@ +import org.junit.Test; +import org.junit.*; // before after ... +import static org.junit.Assert.assertEquals; +import org.junit.rules.ExpectedException; + +public class TestException { + private static Stack stack; + + @BeforeClass + public static void beforeClass() { + stack = new Stack(); + } + + @Before + public void before() throws Exception { + // initial some node in stack structure + stack.push(3); + stack.push(2); + } + + @After + public void after() throws Exception { + // clean all node in stack + while (stack.size() != 0) { + stack.pop(); + } + // try to create some exception + //stack.pop(); + } + + @AfterClass + public static void afterClass() { + stack = null; + } + + @Test + public void CaseInt() throws Exception { + int value = 0xc8763; + stack.push(value); + assertEquals(value, stack.pop()); + } + + @Test + public void CaseMagic() throws Exception { + String magic = "c8763"; + stack.push(magic); + assertEquals(magic, stack.pop()); + } + + @Test (expected = FullStackException.class) + public void CaseFullException() throws Exception { + // try to overflow + while (true) { + stack.push("overflow"); + } + } + + @Test (expected = EmptyStackException.class) + public void CaseEmptyException() throws Exception { + // clean all node in stack + while (stack.size() != 0) { + stack.pop(); + } + // try to create some exception + stack.pop(); + } +} diff --git a/junit/testException/testcase/TestRunner.java b/junit/testException/testcase/TestRunner.java new file mode 100644 index 0000000..c591747 --- /dev/null +++ b/junit/testException/testcase/TestRunner.java @@ -0,0 +1,13 @@ +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +public class TestRunner { + public static void main(String[] args) { + Result result = JUnitCore.runClasses(TestException.class); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/junit/testException/testcase/TestStack.java b/junit/testException/testcase/TestStack.java new file mode 100644 index 0000000..a4adcdf --- /dev/null +++ b/junit/testException/testcase/TestStack.java @@ -0,0 +1,48 @@ +import org.junit.Test; +import org.junit.*; // before after ... +import static org.junit.Assert.assertEquals; + +public class TestStack { + private static Stack stack; + + @BeforeClass + public static void beforeClass() { + stack = new Stack(); + } + + @Before + public void before() { + // initial some node in stack structure + stack.push(3); + stack.push(2); + } + + @After + public void after() throws Exception { + // clean all node in stack + while (stack.size() != 0) { + stack.pop(); + } + // try to create some exception + //stack.pop(); + } + + @AfterClass + public static void afterClass() { + stack = null; + } + + @Test + public void CaseInt() throws Exception { + int value = 0xc8763; + stack.push(value); + assertEquals(value, stack.pop()); + } + + @Test + public void CaseMagic() throws Exception { + String magic = "c8763"; + stack.push(magic); + assertEquals(magic, stack.pop()); + } +}