diff --git a/README.md b/README.md index 261fe2f..92f7315 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,23 @@ # Software Testing class 2019 ## [JUnit](junit) -Week 3: 2019/03/07 -Week 4: 2019/03/14 +* Week 3: 2019/03/07 +* Week 4: 2019/03/14 +* Week 5: 2019/03/21 +* Week 6: 2019/03/28 +* Week 7: 2019/04/04 +* Week 8: 2019/04/11 +* Week 9: 2019/04/16 +* Week 10: 2019/04/23 +* Week 11: 2019/04/30 + +## [travis](https://github.com/lihungte96/Software-testing-2019-travis) +* Week 12: 2019/05/07 +* Week 13: 2019/05/14 + +## [Selenium](selenium) +* Week 14: 2019/05/21 +* Week 16: 2019/06/04 + +## [afl](afl) +* Week 17: 2019/06/11 diff --git a/afl/.gitignore b/afl/.gitignore new file mode 100644 index 0000000..d64d20d --- /dev/null +++ b/afl/.gitignore @@ -0,0 +1,3 @@ +fuzz_output +sample1 +sample2 diff --git a/afl/sample1_positive/Makefile b/afl/sample1_positive/Makefile new file mode 100644 index 0000000..ea93f98 --- /dev/null +++ b/afl/sample1_positive/Makefile @@ -0,0 +1,13 @@ +all: sample1 + +sample1: + $(CXX) sample1.cpp -o sample1 + +fuzz: sample1 + afl-fuzz -i testcase/ -o fuzz_output/ ./sample1 + +fuzz_non_instrument: sample1 + afl-fuzz -n -i testcase/ -o fuzz_output/ ./sample1 + +clean: + rm sample1 diff --git a/afl/sample1_positive/sample1.cpp b/afl/sample1_positive/sample1.cpp new file mode 100644 index 0000000..44a82bc --- /dev/null +++ b/afl/sample1_positive/sample1.cpp @@ -0,0 +1,12 @@ +#include +using namespace std; +int main(){ + int a =0; + cin >> a; + if (a >= 0) + cout << "positive number!" << endl; + else + cout << "negative number!" << endl; + return 0; +} + diff --git a/afl/sample1_positive/testcase/a b/afl/sample1_positive/testcase/a new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/afl/sample1_positive/testcase/a @@ -0,0 +1 @@ +3 diff --git a/afl/sample2_ctf/Makefile b/afl/sample2_ctf/Makefile new file mode 100644 index 0000000..00d74bb --- /dev/null +++ b/afl/sample2_ctf/Makefile @@ -0,0 +1,14 @@ +all: sample2 + +sample2: sample2.cpp + afl-g++ sample2.cpp -o sample2 + +asan: sample2.cpp + afl-g++ -fsanitize=address sample2.cpp -o sample2 + +fuzz: sample2 + afl-fuzz -m 30000000 -i testcase/ -o fuzz_output/ ./sample2 + +clean: + rm sample2 + diff --git a/afl/sample2_ctf/sample2.cpp b/afl/sample2_ctf/sample2.cpp new file mode 100644 index 0000000..664823d --- /dev/null +++ b/afl/sample2_ctf/sample2.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +int main() +{ + int a[20]; + int index = 0; + + cout << "Index: "; + // chose an index to write + cin >> index; + + // check index position + if (index >= 20) + return -1; + + cout << "Value: "; + // write data to index + cin >> a[index]; + return 0; +} diff --git a/afl/sample2_ctf/testcase/input1 b/afl/sample2_ctf/testcase/input1 new file mode 100644 index 0000000..b94e93f --- /dev/null +++ b/afl/sample2_ctf/testcase/input1 @@ -0,0 +1 @@ +10 50 diff --git a/afl/sample3_crash_hang/Makefile b/afl/sample3_crash_hang/Makefile new file mode 100644 index 0000000..e717098 --- /dev/null +++ b/afl/sample3_crash_hang/Makefile @@ -0,0 +1,10 @@ +all: sample3 + +sample3: sample3.cpp + afl-g++ -fsanitize=address sample3.cpp -o sample3 + +fuzz: sample3 + afl-fuzz -m 300000000 -i testcase/ -o fuzz_output/ ./sample3 + +clean: + rm sample3 diff --git a/afl/sample3_crash_hang/sample3 b/afl/sample3_crash_hang/sample3 new file mode 100755 index 0000000..2df594a Binary files /dev/null and b/afl/sample3_crash_hang/sample3 differ diff --git a/afl/sample3_crash_hang/sample3.cpp b/afl/sample3_crash_hang/sample3.cpp new file mode 100644 index 0000000..e3019e3 --- /dev/null +++ b/afl/sample3_crash_hang/sample3.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; +#define length 20 +int main() +{ + int index = 0; + int array[length]; + cout << "Index: "; + cin >> index; + + // hang + if (index >= length) + while (true) + ; + + // crash + if (index == -6666) + throw std::invalid_argument("You did it"); + + //array[index] = 0; + return 0; +} diff --git a/afl/sample3_crash_hang/testcase/input1 b/afl/sample3_crash_hang/testcase/input1 new file mode 100644 index 0000000..556cf17 --- /dev/null +++ b/afl/sample3_crash_hang/testcase/input1 @@ -0,0 +1 @@ ++1 diff --git a/afl/sample3_crash_hang/testcase/input2 b/afl/sample3_crash_hang/testcase/input2 new file mode 100644 index 0000000..3a2e3f4 --- /dev/null +++ b/afl/sample3_crash_hang/testcase/input2 @@ -0,0 +1 @@ +-1 diff --git a/afl/sample3_crash_hang/testcase/input3 b/afl/sample3_crash_hang/testcase/input3 new file mode 100644 index 0000000..42640be --- /dev/null +++ b/afl/sample3_crash_hang/testcase/input3 @@ -0,0 +1 @@ +0 1 2 3 4 5 6 7 8 9 diff --git a/junit/Dockerfile b/junit/Dockerfile new file mode 100644 index 0000000..082d27c --- /dev/null +++ b/junit/Dockerfile @@ -0,0 +1,5 @@ +FROM ubuntu:18.04 +MAINTAINER robert + +RUN apt-get update -y && + apt-get install git openjdk-8-jdk-headless junit4 make libmockito-java -y diff --git a/junit/README.md b/junit/README.md index fd57475..7466fa0 100644 --- a/junit/README.md +++ b/junit/README.md @@ -15,14 +15,33 @@ $ make ## syllabus ### week3 -#### [onlyTest](onlyTest): Only JUint test -#### [testCode](testCode): Simple assert +#### [OnlyTest](onlyTest): Only JUint test +#### [TestCode](testCode): Simple assert ### week4 -#### [beforeAfter](beforeAfter): Show execution path of before and after annotation +#### [BeforeAfter](beforeAfter): Show execution path of before and after annotation #### [Stack](Stack): Use stack example show why use before and after annotation ### week5 -#### [parameterized](parameterized): Use JUnit @parameterized annotation to generate multiple tese case -#### [testException1](testException1): Use stack example show how to check exception with test -#### [testException2](testException2): Use stack example show how to check exception with test rule +#### [Parameterized](parameterized): Use JUnit @parameterized annotation to generate multiple tese case + +### week6 +#### [Parameterized2.0](parameterized2.0): Use JUnit @parameterized annotation to generate multiple tese case(homework version) +#### [TestException1](testException1): Use stack example show how to check exception with test +#### [TestException2](testException2): Use stack example show how to check exception with test rule + +### week7 +#### [Jacoco](jacoco): Use jacoco to run & cal & show coverage information + +### week8 +#### [Jacoco Labwork](jacocoLW): Labwork for jacoco, need add testcase and achieve 100% coverage + +### week9 +#### [Stub](stub): Stub example, and use Mockito to stub + +### week10 +#### [Mock](mock): Mock example, and use Mockito to mock +#### [Mock2](mock2): Mock Mazu example, and use Mockito to mock + +### week11 +#### [StubLW](stubLW): Labwork for Stub, complete testcase & achieve 100% coverage in multiplicationWithArray method diff --git a/junit/jacoco/Makefile b/junit/jacoco/Makefile index 4f3f62f..25fb883 100644 --- a/junit/jacoco/Makefile +++ b/junit/jacoco/Makefile @@ -1,5 +1,9 @@ all: compile compileTest +Simple: compileTest runCov calCov showCov +Positive: compileTestPositive runCovPositive calCovPositive showCov +Negative: compileTestNegative runCovNegative calCovNegative showCov + compile: mkdir -p bin javac src/Distant.java -d bin @@ -9,11 +13,15 @@ compileTest: javac -classpath /usr/share/java/junit4.jar src/Distant.java \ testcase/TestDistant.java testcase/TestRunner.java -d bin +compileTestPositive: + mkdir -p bin javac -classpath /usr/share/java/junit4.jar src/Distant.java \ - testcase/TestDistantPositive.java testcase/TestRunnerPositive.java -d bin + testcase/Positive/TestDistantPositive.java testcase/Positive/TestRunnerPositive.java -d bin +compileTestNegative: + mkdir -p bin javac -classpath /usr/share/java/junit4.jar src/Distant.java \ - testcase/TestDistantNegative.java testcase/TestRunnerNegative.java -d bin + testcase/Negative/TestDistantNegative.java testcase/Negative/TestRunnerNegative.java -d bin test: cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner @@ -29,10 +37,22 @@ runCovNegative: calCov: cd bin ; java -jar ../lib/jacococli.jar report jacoco.exec --classfiles ../bin \ - --sourcefiles ../src --sourcefiles ../testcase --html report + --sourcefiles ../src --sourcefiles ../testcase --html reportDir + +calCovPositive: + cd bin ; java -jar ../lib/jacococli.jar report jacoco.exec --classfiles ../bin \ + --sourcefiles ../src --sourcefiles ../testcase/Positive --html reportDir + +calCovNegative: + cd bin ; java -jar ../lib/jacococli.jar report jacoco.exec --classfiles ../bin \ + --sourcefiles ../src --sourcefiles ../testcase/Negative --html reportDir showCov: - cd bin/report ; python3 -m http.server 8000 + RED="\033[0;31m" ; NC='\033[0m' ; printf "$${RED}http://" ; curl -s https://api.ipify.org/ ; printf ":8000/$${NC}\n" + cd bin/reportDir ; python3 -m http.server 8000 + +kill: + killall python3 clean: - rm bin/*.class bin/*.exec + rm -r bin/*.class bin/*.exec bin/reportDir bin/Positive bin/Negative || true diff --git a/junit/jacoco/README.md b/junit/jacoco/README.md new file mode 100644 index 0000000..af0f18b --- /dev/null +++ b/junit/jacoco/README.md @@ -0,0 +1,12 @@ +## Makefile +make [all] - Compile JAVA class + +make compileTest - Compile Junit testing class + +make runCov - Execute & record code coverage + +make calCov - Calculate coverage & report to a HTML file + +make showCov - Use python HTTP server to show HTML + +make clean - removes all files generated by make diff --git a/junit/jacoco/testcase/TestDistantNegative.java b/junit/jacoco/testcase/Negative/TestDistantNegative.java similarity index 100% rename from junit/jacoco/testcase/TestDistantNegative.java rename to junit/jacoco/testcase/Negative/TestDistantNegative.java diff --git a/junit/jacoco/testcase/TestRunnerNegative.java b/junit/jacoco/testcase/Negative/TestRunnerNegative.java similarity index 100% rename from junit/jacoco/testcase/TestRunnerNegative.java rename to junit/jacoco/testcase/Negative/TestRunnerNegative.java diff --git a/junit/jacoco/testcase/TestDistantPositive.java b/junit/jacoco/testcase/Positive/TestDistantPositive.java similarity index 100% rename from junit/jacoco/testcase/TestDistantPositive.java rename to junit/jacoco/testcase/Positive/TestDistantPositive.java diff --git a/junit/jacoco/testcase/TestRunnerPositive.java b/junit/jacoco/testcase/Positive/TestRunnerPositive.java similarity index 100% rename from junit/jacoco/testcase/TestRunnerPositive.java rename to junit/jacoco/testcase/Positive/TestRunnerPositive.java diff --git a/junit/jacocoLW/Makefile b/junit/jacocoLW/Makefile new file mode 100644 index 0000000..36af492 --- /dev/null +++ b/junit/jacocoLW/Makefile @@ -0,0 +1,58 @@ +all: compile compileTest + +Simple: compileTest runCov calCov showCov +Positive: compileTestPositive runCovPositive calCovPositive showCov +Negative: compileTestNegative runCovNegative calCovNegative showCov + +compile: + mkdir -p bin + javac src/Distant.java src/UnhandledCaseException.java -d bin + +compileTest: + mkdir -p bin + javac -classpath /usr/share/java/junit4.jar src/Distant.java src/UnhandledCaseException.java \ + testcase/TestDistant.java testcase/TestRunner.java -d bin + +compileTestPositive: + mkdir -p bin + javac -classpath /usr/share/java/junit4.jar src/Distant.java src/UnhandledCaseException.java \ + testcase/Positive/TestDistantPositive.java testcase/Positive/TestRunnerPositive.java -d bin + +compileTestNegative: + mkdir -p bin + javac -classpath /usr/share/java/junit4.jar src/Distant.java src/UnhandledCaseException.java \ + testcase/Negative/TestDistantNegative.java testcase/Negative/TestRunnerNegative.java -d bin + +test: + cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner + +runCov: + cd bin ; java -javaagent:../lib/jacocoagent.jar -cp /usr/share/java/junit4.jar:. TestRunner + +runCovPositive: + cd bin ; java -javaagent:../lib/jacocoagent.jar -cp /usr/share/java/junit4.jar:. TestRunnerPositive + +runCovNegative: + cd bin ; java -javaagent:../lib/jacocoagent.jar -cp /usr/share/java/junit4.jar:. TestRunnerNegative + +calCov: + cd bin ; java -jar ../lib/jacococli.jar report jacoco.exec --classfiles ../bin \ + --sourcefiles ../src --sourcefiles ../testcase --html reportDir + +calCovPositive: + cd bin ; java -jar ../lib/jacococli.jar report jacoco.exec --classfiles ../bin \ + --sourcefiles ../src --sourcefiles ../testcase/Positive --html reportDir + +calCovNegative: + cd bin ; java -jar ../lib/jacococli.jar report jacoco.exec --classfiles ../bin \ + --sourcefiles ../src --sourcefiles ../testcase/Negative --html reportDir + +showCov: + RED="\033[0;31m" ; NC='\033[0m' ; printf "$${RED}http://" ; curl -s https://api.ipify.org/ ; printf ":8000/$${NC}\n" + cd bin/reportDir ; python3 -m http.server 8000 + +kill: + killall python3 + +clean: + rm -r bin/*.class bin/*.exec bin/reportDir bin/Positive bin/Negative || true diff --git a/junit/jacocoLW/lib/META-INF/MANIFEST.MF b/junit/jacocoLW/lib/META-INF/MANIFEST.MF new file mode 100644 index 0000000..8870b89 --- /dev/null +++ b/junit/jacocoLW/lib/META-INF/MANIFEST.MF @@ -0,0 +1,11 @@ +Manifest-Version: 1.0 +Implementation-Title: JaCoCo Java Agent +Automatic-Module-Name: org.jacoco.agent.rt +Premain-Class: org.jacoco.agent.rt.internal_1f1cc91.PreMain +Implementation-Version: 0.8.3 +Archiver-Version: Plexus Archiver +Built-By: godin +Created-By: Apache Maven +Build-Jdk: 1.8.0_152 +Implementation-Vendor: Mountainminds GmbH & Co. KG + diff --git a/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties b/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties new file mode 100644 index 0000000..d77a8ec --- /dev/null +++ b/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Jan 23 02:22:02 CET 2019 +version=0.8.3 +groupId=org.jacoco +artifactId=org.jacoco.agent.rt diff --git a/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml b/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml new file mode 100644 index 0000000..9e05cc1 --- /dev/null +++ b/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml @@ -0,0 +1,96 @@ + + + + 4.0.0 + + + org.jacoco + org.jacoco.build + 0.8.3 + ../org.jacoco.build + + + org.jacoco.agent.rt + + + JaCoCo :: Agent RT + JaCoCo Java Agent + + + true + true + + + + + ${project.groupId} + org.jacoco.core + + + + + src + + + + org.apache.maven.plugins + maven-shade-plugin + + + package + + shade + + + true + all + true + + + org.jacoco.agent.rt.internal + ${jacoco.runtime.package.name} + + + org.jacoco.core + ${jacoco.runtime.package.name}.core + + + org.objectweb.asm + ${jacoco.runtime.package.name}.asm + + + + + org.ow2.asm:* + + module-info.class + + + + + + + ${jacoco.runtime.package.name}.PreMain + ${project.artifactId} + ${project.description} + ${project.organization.name} + ${project.version} + + + + + + + + + + diff --git a/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties b/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties new file mode 100644 index 0000000..8e440db --- /dev/null +++ b/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Jan 23 02:21:51 CET 2019 +version=0.8.3 +groupId=org.jacoco +artifactId=org.jacoco.core diff --git a/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml b/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml new file mode 100644 index 0000000..d1b6886 --- /dev/null +++ b/junit/jacocoLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml @@ -0,0 +1,69 @@ + + + + 4.0.0 + + + org.jacoco + org.jacoco.build + 0.8.3 + ../org.jacoco.build + + + org.jacoco.core + + JaCoCo :: Core + JaCoCo Core + + + + org.ow2.asm + asm + + + org.ow2.asm + asm-commons + + + org.ow2.asm + asm-tree + + + + + src + + + + org.apache.felix + maven-bundle-plugin + + + process-classes + + manifest + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + + + diff --git a/junit/jacocoLW/lib/about.html b/junit/jacocoLW/lib/about.html new file mode 100644 index 0000000..766ff54 --- /dev/null +++ b/junit/jacocoLW/lib/about.html @@ -0,0 +1,73 @@ + + + + +About + + + +

About This Content

+ +

+ 2019/01/23 +

+ +

License

+ +

+ All Content in this plug-in is made available by Mountainminds GmbH & Co. + KG, Munich. Unless otherwise indicated below, the Content is provided to you + under the terms and conditions of the Eclipse Public License Version 1.0 + ("EPL"). A copy of the EPL is available at + http://www.eclipse.org/legal/epl-v10.html. + For purposes of the EPL, "Program" will mean the Content. +

+ +

Third Party Content

+ +

+ The Content includes items that have been sourced from third parties as set + out below. +

+ +

ASM 3.x

+ +

+ This plug-in contains the ASM library + which is subject to the terms and conditions of the following license: +

+ +
+Copyright (c) 2000-2005 INRIA, France Telecom
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holders nor the names of its
+   contributors may be used to endorse or promote products derived from
+   this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+THE POSSIBILITY OF SUCH DAMAGE.
+
+ + + \ No newline at end of file diff --git a/junit/jacocoLW/lib/jacocoagent.jar b/junit/jacocoLW/lib/jacocoagent.jar new file mode 100644 index 0000000..0b25a00 Binary files /dev/null and b/junit/jacocoLW/lib/jacocoagent.jar differ diff --git a/junit/jacocoLW/lib/jacocoant.jar b/junit/jacocoLW/lib/jacocoant.jar new file mode 100644 index 0000000..09ac4a8 Binary files /dev/null and b/junit/jacocoLW/lib/jacocoant.jar differ diff --git a/junit/jacocoLW/lib/jacococli.jar b/junit/jacocoLW/lib/jacococli.jar new file mode 100644 index 0000000..fea7a1c Binary files /dev/null and b/junit/jacocoLW/lib/jacococli.jar differ diff --git a/junit/jacocoLW/lib/org.jacoco.agent-0.8.3.201901230119.jar b/junit/jacocoLW/lib/org.jacoco.agent-0.8.3.201901230119.jar new file mode 100644 index 0000000..1ca448b Binary files /dev/null and b/junit/jacocoLW/lib/org.jacoco.agent-0.8.3.201901230119.jar differ diff --git a/junit/jacocoLW/lib/org.jacoco.ant-0.8.3.201901230119.jar b/junit/jacocoLW/lib/org.jacoco.ant-0.8.3.201901230119.jar new file mode 100644 index 0000000..6cb0624 Binary files /dev/null and b/junit/jacocoLW/lib/org.jacoco.ant-0.8.3.201901230119.jar differ diff --git a/junit/jacocoLW/lib/org.jacoco.core-0.8.3.201901230119.jar b/junit/jacocoLW/lib/org.jacoco.core-0.8.3.201901230119.jar new file mode 100644 index 0000000..22f5594 Binary files /dev/null and b/junit/jacocoLW/lib/org.jacoco.core-0.8.3.201901230119.jar differ diff --git a/junit/jacocoLW/lib/org.jacoco.report-0.8.3.201901230119.jar b/junit/jacocoLW/lib/org.jacoco.report-0.8.3.201901230119.jar new file mode 100644 index 0000000..8b36f81 Binary files /dev/null and b/junit/jacocoLW/lib/org.jacoco.report-0.8.3.201901230119.jar differ diff --git a/junit/jacocoLW/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties b/junit/jacocoLW/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties new file mode 100644 index 0000000..f585044 --- /dev/null +++ b/junit/jacocoLW/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties @@ -0,0 +1,3 @@ +VERSION=0.8.3.201901230119 +HOMEURL=http://www.jacoco.org/jacoco +RUNTIMEPACKAGE=org.jacoco.agent.rt.internal_1f1cc91 \ No newline at end of file diff --git a/junit/jacocoLW/src/Distant.java b/junit/jacocoLW/src/Distant.java new file mode 100644 index 0000000..df9cd18 --- /dev/null +++ b/junit/jacocoLW/src/Distant.java @@ -0,0 +1,12 @@ +public class Distant { + public static int distant(int a, int b) throws UnhandledCaseException { + if (a == b) { + throw new UnhandledCaseException("No Distance"); + } + if (a >= b) { + return a - b; + } else { + return b - a; + } + } +} diff --git a/junit/jacocoLW/src/UnhandledCaseException.java b/junit/jacocoLW/src/UnhandledCaseException.java new file mode 100644 index 0000000..1eff58f --- /dev/null +++ b/junit/jacocoLW/src/UnhandledCaseException.java @@ -0,0 +1,5 @@ +public class UnhandledCaseException extends Exception{ + public UnhandledCaseException (String msg){ + super (msg); + } +} diff --git a/junit/jacocoLW/testcase/Negative/TestDistantNegative.java b/junit/jacocoLW/testcase/Negative/TestDistantNegative.java new file mode 100644 index 0000000..4000c18 --- /dev/null +++ b/junit/jacocoLW/testcase/Negative/TestDistantNegative.java @@ -0,0 +1,13 @@ +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class TestDistantNegative { + @Test + public void testDistant2() throws UnhandledCaseException { + int a = -50; + int b = 30; + int expected = 80; // 30 + 50 + int result = Distant.distant(a, b); + assertEquals(expected, result); + } +} diff --git a/junit/jacocoLW/testcase/Negative/TestRunnerNegative.java b/junit/jacocoLW/testcase/Negative/TestRunnerNegative.java new file mode 100644 index 0000000..af5a622 --- /dev/null +++ b/junit/jacocoLW/testcase/Negative/TestRunnerNegative.java @@ -0,0 +1,13 @@ +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +public class TestRunnerNegative { + public static void main(String[] args) { + Result result = JUnitCore.runClasses(TestDistantNegative.class); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/junit/jacocoLW/testcase/Positive/TestDistantPositive.java b/junit/jacocoLW/testcase/Positive/TestDistantPositive.java new file mode 100644 index 0000000..b770342 --- /dev/null +++ b/junit/jacocoLW/testcase/Positive/TestDistantPositive.java @@ -0,0 +1,13 @@ +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class TestDistantPositive { + @Test + public void testDistantPositive() throws UnhandledCaseException { + int a = 50; + int b = 30; + int expected = 20; // 50 - 30 + int result = Distant.distant(a, b); + assertEquals(expected, result); + } +} diff --git a/junit/jacocoLW/testcase/Positive/TestRunnerPositive.java b/junit/jacocoLW/testcase/Positive/TestRunnerPositive.java new file mode 100644 index 0000000..2fcab53 --- /dev/null +++ b/junit/jacocoLW/testcase/Positive/TestRunnerPositive.java @@ -0,0 +1,13 @@ +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +public class TestRunnerPositive { + public static void main(String[] args) { + Result result = JUnitCore.runClasses(TestDistantPositive.class); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/junit/jacocoLW/testcase/TestDistant.java b/junit/jacocoLW/testcase/TestDistant.java new file mode 100644 index 0000000..b4f6cae --- /dev/null +++ b/junit/jacocoLW/testcase/TestDistant.java @@ -0,0 +1,22 @@ +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class TestDistant { + @Test + public void testDistant1() throws UnhandledCaseException { + int a = 50; + int b = 30; + int expected = 20; // 50 - 30 + int result = Distant.distant(a, b); + assertEquals(expected, result); + } + + @Test + public void testDistant2() throws UnhandledCaseException { + int a = -50; + int b = 30; + int expected = 80; // 30 + 50 + int result = Distant.distant(a, b); + assertEquals(expected, result); + } +} diff --git a/junit/jacocoLW/testcase/TestRunner.java b/junit/jacocoLW/testcase/TestRunner.java new file mode 100644 index 0000000..2a55841 --- /dev/null +++ b/junit/jacocoLW/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(TestDistant.class); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/junit/junit_init.sh b/junit/junit_init.sh index 631d3c0..b6ec4fb 100755 --- a/junit/junit_init.sh +++ b/junit/junit_init.sh @@ -1,5 +1,5 @@ #!/bin/bash sudo apt update -sudo apt install openjdk-8-jdk-headless junit4 make -#export CLASSPATH=/usr/share/java/junit4.jar:. +sudo apt remove openjdk-11-jdk-headless -y +sudo apt install git openjdk-8-jdk-headless junit4 make libmockito-java -y diff --git a/junit/mock/Makefile b/junit/mock/Makefile new file mode 100644 index 0000000..b22e630 --- /dev/null +++ b/junit/mock/Makefile @@ -0,0 +1,40 @@ +all: compile compileTest + +Simple: compileTest runCov calCov showCov + +compile: + mkdir -p bin + javac src/TcpExample.java src/TcpClient.java src/TcpServer.java -d bin + +runServer: + cd bin ; java TcpExample server + +runClient: + cd bin ; java TcpExample client + +compileTest: + mkdir -p bin + javac -classpath /usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar \ + src/TcpClient.java \ + testcase/TestTcpClient.java testcase/TestRunner.java -d bin + +test: + cd bin ; java -classpath .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunner + +runCov: + rm bin/*.exec 2> /dev/null || true + cd bin ; java -javaagent:../lib/jacocoagent.jar -cp .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunner + +calCov: + cd bin ; java -jar ../lib/jacococli.jar report jacoco.exec --classfiles ../bin \ + --sourcefiles ../src --sourcefiles ../testcase --html reportDir + +showCov: + RED="\033[0;31m" ; NC='\033[0m' ; printf "$${RED}http://" ; curl -s https://api.ipify.org/ ; printf ":8000/$${NC}\n" + cd bin/reportDir ; python3 -m http.server 8000 + +kill: + killall python3 + +clean: + rm -r bin/*.class bin/*.exec bin/reportDir 2> /dev/null || true diff --git a/junit/mock/README.md b/junit/mock/README.md new file mode 100644 index 0000000..fd04412 --- /dev/null +++ b/junit/mock/README.md @@ -0,0 +1,28 @@ +## Makefile + +### Run Java program + +make [all] - Compile JAVA class + +make runServer - Run TCP server example + +make runClient - Run TCP client example + +### Run unittest +Test client only + +make compileTest - Compile Junit testing class + +make test - Do unittest + +### Show Jacoco coverage Web page + +make Simple - make {clean, compileTest, runCov, calCov, showCov} + +make runCov - Execute & record code coverage + +make calCov - Calculate coverage & report to a HTML file + +make showCov - Use python HTTP server to show HTML + +make clean - removes all files generated by make diff --git a/junit/mock/lib/META-INF/MANIFEST.MF b/junit/mock/lib/META-INF/MANIFEST.MF new file mode 100644 index 0000000..8870b89 --- /dev/null +++ b/junit/mock/lib/META-INF/MANIFEST.MF @@ -0,0 +1,11 @@ +Manifest-Version: 1.0 +Implementation-Title: JaCoCo Java Agent +Automatic-Module-Name: org.jacoco.agent.rt +Premain-Class: org.jacoco.agent.rt.internal_1f1cc91.PreMain +Implementation-Version: 0.8.3 +Archiver-Version: Plexus Archiver +Built-By: godin +Created-By: Apache Maven +Build-Jdk: 1.8.0_152 +Implementation-Vendor: Mountainminds GmbH & Co. KG + diff --git a/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties b/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties new file mode 100644 index 0000000..d77a8ec --- /dev/null +++ b/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Jan 23 02:22:02 CET 2019 +version=0.8.3 +groupId=org.jacoco +artifactId=org.jacoco.agent.rt diff --git a/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml b/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml new file mode 100644 index 0000000..9e05cc1 --- /dev/null +++ b/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml @@ -0,0 +1,96 @@ + + + + 4.0.0 + + + org.jacoco + org.jacoco.build + 0.8.3 + ../org.jacoco.build + + + org.jacoco.agent.rt + + + JaCoCo :: Agent RT + JaCoCo Java Agent + + + true + true + + + + + ${project.groupId} + org.jacoco.core + + + + + src + + + + org.apache.maven.plugins + maven-shade-plugin + + + package + + shade + + + true + all + true + + + org.jacoco.agent.rt.internal + ${jacoco.runtime.package.name} + + + org.jacoco.core + ${jacoco.runtime.package.name}.core + + + org.objectweb.asm + ${jacoco.runtime.package.name}.asm + + + + + org.ow2.asm:* + + module-info.class + + + + + + + ${jacoco.runtime.package.name}.PreMain + ${project.artifactId} + ${project.description} + ${project.organization.name} + ${project.version} + + + + + + + + + + diff --git a/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties b/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties new file mode 100644 index 0000000..8e440db --- /dev/null +++ b/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Jan 23 02:21:51 CET 2019 +version=0.8.3 +groupId=org.jacoco +artifactId=org.jacoco.core diff --git a/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml b/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml new file mode 100644 index 0000000..d1b6886 --- /dev/null +++ b/junit/mock/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml @@ -0,0 +1,69 @@ + + + + 4.0.0 + + + org.jacoco + org.jacoco.build + 0.8.3 + ../org.jacoco.build + + + org.jacoco.core + + JaCoCo :: Core + JaCoCo Core + + + + org.ow2.asm + asm + + + org.ow2.asm + asm-commons + + + org.ow2.asm + asm-tree + + + + + src + + + + org.apache.felix + maven-bundle-plugin + + + process-classes + + manifest + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + + + diff --git a/junit/mock/lib/about.html b/junit/mock/lib/about.html new file mode 100644 index 0000000..766ff54 --- /dev/null +++ b/junit/mock/lib/about.html @@ -0,0 +1,73 @@ + + + + +About + + + +

About This Content

+ +

+ 2019/01/23 +

+ +

License

+ +

+ All Content in this plug-in is made available by Mountainminds GmbH & Co. + KG, Munich. Unless otherwise indicated below, the Content is provided to you + under the terms and conditions of the Eclipse Public License Version 1.0 + ("EPL"). A copy of the EPL is available at + http://www.eclipse.org/legal/epl-v10.html. + For purposes of the EPL, "Program" will mean the Content. +

+ +

Third Party Content

+ +

+ The Content includes items that have been sourced from third parties as set + out below. +

+ +

ASM 3.x

+ +

+ This plug-in contains the ASM library + which is subject to the terms and conditions of the following license: +

+ +
+Copyright (c) 2000-2005 INRIA, France Telecom
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holders nor the names of its
+   contributors may be used to endorse or promote products derived from
+   this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+THE POSSIBILITY OF SUCH DAMAGE.
+
+ + + \ No newline at end of file diff --git a/junit/mock/lib/jacocoagent.jar b/junit/mock/lib/jacocoagent.jar new file mode 100644 index 0000000..0b25a00 Binary files /dev/null and b/junit/mock/lib/jacocoagent.jar differ diff --git a/junit/mock/lib/jacocoant.jar b/junit/mock/lib/jacocoant.jar new file mode 100644 index 0000000..09ac4a8 Binary files /dev/null and b/junit/mock/lib/jacocoant.jar differ diff --git a/junit/mock/lib/jacococli.jar b/junit/mock/lib/jacococli.jar new file mode 100644 index 0000000..fea7a1c Binary files /dev/null and b/junit/mock/lib/jacococli.jar differ diff --git a/junit/mock/lib/org.jacoco.agent-0.8.3.201901230119.jar b/junit/mock/lib/org.jacoco.agent-0.8.3.201901230119.jar new file mode 100644 index 0000000..1ca448b Binary files /dev/null and b/junit/mock/lib/org.jacoco.agent-0.8.3.201901230119.jar differ diff --git a/junit/mock/lib/org.jacoco.ant-0.8.3.201901230119.jar b/junit/mock/lib/org.jacoco.ant-0.8.3.201901230119.jar new file mode 100644 index 0000000..6cb0624 Binary files /dev/null and b/junit/mock/lib/org.jacoco.ant-0.8.3.201901230119.jar differ diff --git a/junit/mock/lib/org.jacoco.core-0.8.3.201901230119.jar b/junit/mock/lib/org.jacoco.core-0.8.3.201901230119.jar new file mode 100644 index 0000000..22f5594 Binary files /dev/null and b/junit/mock/lib/org.jacoco.core-0.8.3.201901230119.jar differ diff --git a/junit/mock/lib/org.jacoco.report-0.8.3.201901230119.jar b/junit/mock/lib/org.jacoco.report-0.8.3.201901230119.jar new file mode 100644 index 0000000..8b36f81 Binary files /dev/null and b/junit/mock/lib/org.jacoco.report-0.8.3.201901230119.jar differ diff --git a/junit/mock/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties b/junit/mock/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties new file mode 100644 index 0000000..f585044 --- /dev/null +++ b/junit/mock/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties @@ -0,0 +1,3 @@ +VERSION=0.8.3.201901230119 +HOMEURL=http://www.jacoco.org/jacoco +RUNTIMEPACKAGE=org.jacoco.agent.rt.internal_1f1cc91 \ No newline at end of file diff --git a/junit/mock/src/TcpClient.java b/junit/mock/src/TcpClient.java new file mode 100644 index 0000000..4dc6d5d --- /dev/null +++ b/junit/mock/src/TcpClient.java @@ -0,0 +1,61 @@ +import java.net.*; +import java.io.*; + +// 1. 本程式必須與 TcpServer.java 程式搭配執行,先執行 TcpServer 再執行本程式。 +// 2. 本程式必須有一個參數,指定伺服器的 IP。 +// 用法範例: java TcpClient + +class TcpClientParseCommunicate { + private Socket client; + private InputStream inputStream; + private StringBuffer buf; + + public TcpClientParseCommunicate(Socket client) { + this.client = client; + } + + public StringBuffer getBuf() { + return buf; + } + + public void communicate() throws Exception { + this.inputStream = client.getInputStream(); // 取得輸入訊息的串流 + } + + public void parseInput() throws Exception { + try { + buf = new StringBuffer(); // 建立讀取字串。 + while (true) { // 不斷讀取。 + int x = inputStream.read(); // 讀取一個 byte。(read 傳回 -1 代表串流結束) + if (x == -1) + break; // x = -1 代表串流結束,讀取完畢,用 break 跳開。 + byte b = (byte) x; // 將 x 轉為 byte,放入變數 b. + buf.append((char) b);// 假設傳送ASCII字元都是 ASCII。 + } + } catch (Exception e) { + inputStream.close(); // 關閉輸入串流。 + } + } + +} + +public class TcpClient { + private Socket client; + private TcpClientParseCommunicate tcpClientParseCommunicate; + + public TcpClient(String ip, int port) throws Exception { + client = new Socket(ip, port); // 連接至遠端機器 + } + + public void read() throws Exception { + tcpClientParseCommunicate = new TcpClientParseCommunicate(client); + tcpClientParseCommunicate.communicate(); + tcpClientParseCommunicate.parseInput(); + } + + public void output() throws Exception { + StringBuffer buf = tcpClientParseCommunicate.getBuf(); + System.out.println(buf); // 印出接收到的訊息。 + client.close(); + } +} diff --git a/junit/mock/src/TcpExample.java b/junit/mock/src/TcpExample.java new file mode 100644 index 0000000..199a855 --- /dev/null +++ b/junit/mock/src/TcpExample.java @@ -0,0 +1,19 @@ +//import TcpServer; +//import TcpClient; + +public class TcpExample { + public static int port = 6666; // 設定傳送埠為 6666。 + + public static void main(String args[]) throws Exception { + System.out.println("===== " + args[0] + " Mode ====="); // 印出接收到的訊息。 + if ("server".equals(args[0])) { + TcpServer tcpServer = new TcpServer(port); + tcpServer.tcpServer(); + } else if ("client".equals(args[0])) { + TcpClient tcpClient = new TcpClient("127.0.0.1", port); + tcpClient.read(); + tcpClient.output(); + } + } + +} \ No newline at end of file diff --git a/junit/mock/src/TcpServer.java b/junit/mock/src/TcpServer.java new file mode 100644 index 0000000..b4daa50 --- /dev/null +++ b/junit/mock/src/TcpServer.java @@ -0,0 +1,24 @@ +import java.net.*; +import java.io.*; + +// 1. 本程式必須與 TcpClient.java 程式搭配執行,先執行本程式再執行 TcpClient。 +// 2. 執行方法 : java TcpServer + +public class TcpServer { + private int port; + + public TcpServer(int port) { + this.port = port; + } + + public void tcpServer() throws Exception { + ServerSocket ss = new ServerSocket(port); // 建立 TCP 伺服器。 + while (true) { // 不斷的接收處理輸入訊息。 + Socket sc = ss.accept(); // 接收輸入訊息。 + OutputStream os = sc.getOutputStream(); // 取得輸出串流。 + os.write("From Server : Hi !".getBytes("UTF-8"));// 送訊息到 Client 端。 + os.close(); // 關閉輸出串流。 + sc.close(); // 關閉 TCP 伺服器。 + } + } +} \ No newline at end of file diff --git a/junit/mock/testcase/TestRunner.java b/junit/mock/testcase/TestRunner.java new file mode 100644 index 0000000..d08388e --- /dev/null +++ b/junit/mock/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(TestTcpClient.class); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/junit/mock/testcase/TestTcpClient.java b/junit/mock/testcase/TestTcpClient.java new file mode 100644 index 0000000..fe41ea8 --- /dev/null +++ b/junit/mock/testcase/TestTcpClient.java @@ -0,0 +1,34 @@ +import org.junit.Test; +import org.junit.*; // before after ... + +import static org.junit.Assert.assertEquals; + +import java.io.InputStream; +import java.io.ByteArrayInputStream; + +import java.net.*; // Socket + +import org.mockito.Mockito.*; // java mock: Mockito +import static org.mockito.Mockito.*; + +public class TestTcpClient { + + @Test + public void testTcpClientWithMockMockito() throws Exception { + Socket clientMock = mock(Socket.class); + + TcpClientParseCommunicate tcpClientParseCommunicate = new TcpClientParseCommunicate(clientMock); + tcpClientParseCommunicate.communicate(); + + verify(clientMock).getInputStream(); + } + + @Test + public void testTcpClientWithMockMockitoVerify() throws Exception { + Socket clientMock = mock(Socket.class); + + TcpClientParseCommunicate tcpClientParseCommunicate = new TcpClientParseCommunicate(clientMock); + + verify(clientMock, never()).getInputStream(); + } +} diff --git a/junit/mock2/Makefile b/junit/mock2/Makefile new file mode 100644 index 0000000..8906575 --- /dev/null +++ b/junit/mock2/Makefile @@ -0,0 +1,30 @@ +all: compileTest + +Simple: compileTest runCov calCov showCov + +compileTest: + mkdir -p bin + javac -classpath /usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar \ + src/Mazu.java src/Dream.java \ + testcase/TestMazu.java testcase/TestRunner.java -d bin + +test: + cd bin ; java -classpath .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunner + +runCov: + rm bin/*.exec 2> /dev/null || true + cd bin ; java -javaagent:../lib/jacocoagent.jar -cp .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunner + +calCov: + cd bin ; java -jar ../lib/jacococli.jar report jacoco.exec --classfiles ../bin \ + --sourcefiles ../src --sourcefiles ../testcase --html reportDir + +showCov: + RED="\033[0;31m" ; NC='\033[0m' ; printf "$${RED}http://" ; curl -s https://api.ipify.org/ ; printf ":8000/$${NC}\n" + cd bin/reportDir ; python3 -m http.server 8000 + +kill: + killall python3 + +clean: + rm -r bin/*.class bin/*.exec bin/reportDir 2> /dev/null || true diff --git a/junit/mock2/README.md b/junit/mock2/README.md new file mode 100644 index 0000000..779112d --- /dev/null +++ b/junit/mock2/README.md @@ -0,0 +1,17 @@ +## Makefile + +### Compile Java program + +make [all] - Compile JAVA class + +### Run unittest + +make compileTest - Compile Junit testing class + +make test - Do unittest + +### Show Jacoco coverage Web page + +make Simple - make {clean, compileTest, runCov, calCov, showCov} + +make clean - removes all files generated by make diff --git a/junit/mock2/lib/META-INF/MANIFEST.MF b/junit/mock2/lib/META-INF/MANIFEST.MF new file mode 100644 index 0000000..8870b89 --- /dev/null +++ b/junit/mock2/lib/META-INF/MANIFEST.MF @@ -0,0 +1,11 @@ +Manifest-Version: 1.0 +Implementation-Title: JaCoCo Java Agent +Automatic-Module-Name: org.jacoco.agent.rt +Premain-Class: org.jacoco.agent.rt.internal_1f1cc91.PreMain +Implementation-Version: 0.8.3 +Archiver-Version: Plexus Archiver +Built-By: godin +Created-By: Apache Maven +Build-Jdk: 1.8.0_152 +Implementation-Vendor: Mountainminds GmbH & Co. KG + diff --git a/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties b/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties new file mode 100644 index 0000000..d77a8ec --- /dev/null +++ b/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Jan 23 02:22:02 CET 2019 +version=0.8.3 +groupId=org.jacoco +artifactId=org.jacoco.agent.rt diff --git a/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml b/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml new file mode 100644 index 0000000..9e05cc1 --- /dev/null +++ b/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml @@ -0,0 +1,96 @@ + + + + 4.0.0 + + + org.jacoco + org.jacoco.build + 0.8.3 + ../org.jacoco.build + + + org.jacoco.agent.rt + + + JaCoCo :: Agent RT + JaCoCo Java Agent + + + true + true + + + + + ${project.groupId} + org.jacoco.core + + + + + src + + + + org.apache.maven.plugins + maven-shade-plugin + + + package + + shade + + + true + all + true + + + org.jacoco.agent.rt.internal + ${jacoco.runtime.package.name} + + + org.jacoco.core + ${jacoco.runtime.package.name}.core + + + org.objectweb.asm + ${jacoco.runtime.package.name}.asm + + + + + org.ow2.asm:* + + module-info.class + + + + + + + ${jacoco.runtime.package.name}.PreMain + ${project.artifactId} + ${project.description} + ${project.organization.name} + ${project.version} + + + + + + + + + + diff --git a/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties b/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties new file mode 100644 index 0000000..8e440db --- /dev/null +++ b/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Jan 23 02:21:51 CET 2019 +version=0.8.3 +groupId=org.jacoco +artifactId=org.jacoco.core diff --git a/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml b/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml new file mode 100644 index 0000000..d1b6886 --- /dev/null +++ b/junit/mock2/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml @@ -0,0 +1,69 @@ + + + + 4.0.0 + + + org.jacoco + org.jacoco.build + 0.8.3 + ../org.jacoco.build + + + org.jacoco.core + + JaCoCo :: Core + JaCoCo Core + + + + org.ow2.asm + asm + + + org.ow2.asm + asm-commons + + + org.ow2.asm + asm-tree + + + + + src + + + + org.apache.felix + maven-bundle-plugin + + + process-classes + + manifest + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + + + diff --git a/junit/mock2/lib/about.html b/junit/mock2/lib/about.html new file mode 100644 index 0000000..766ff54 --- /dev/null +++ b/junit/mock2/lib/about.html @@ -0,0 +1,73 @@ + + + + +About + + + +

About This Content

+ +

+ 2019/01/23 +

+ +

License

+ +

+ All Content in this plug-in is made available by Mountainminds GmbH & Co. + KG, Munich. Unless otherwise indicated below, the Content is provided to you + under the terms and conditions of the Eclipse Public License Version 1.0 + ("EPL"). A copy of the EPL is available at + http://www.eclipse.org/legal/epl-v10.html. + For purposes of the EPL, "Program" will mean the Content. +

+ +

Third Party Content

+ +

+ The Content includes items that have been sourced from third parties as set + out below. +

+ +

ASM 3.x

+ +

+ This plug-in contains the ASM library + which is subject to the terms and conditions of the following license: +

+ +
+Copyright (c) 2000-2005 INRIA, France Telecom
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holders nor the names of its
+   contributors may be used to endorse or promote products derived from
+   this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+THE POSSIBILITY OF SUCH DAMAGE.
+
+ + + \ No newline at end of file diff --git a/junit/mock2/lib/jacocoagent.jar b/junit/mock2/lib/jacocoagent.jar new file mode 100644 index 0000000..0b25a00 Binary files /dev/null and b/junit/mock2/lib/jacocoagent.jar differ diff --git a/junit/mock2/lib/jacocoant.jar b/junit/mock2/lib/jacocoant.jar new file mode 100644 index 0000000..09ac4a8 Binary files /dev/null and b/junit/mock2/lib/jacocoant.jar differ diff --git a/junit/mock2/lib/jacococli.jar b/junit/mock2/lib/jacococli.jar new file mode 100644 index 0000000..fea7a1c Binary files /dev/null and b/junit/mock2/lib/jacococli.jar differ diff --git a/junit/mock2/lib/org.jacoco.agent-0.8.3.201901230119.jar b/junit/mock2/lib/org.jacoco.agent-0.8.3.201901230119.jar new file mode 100644 index 0000000..1ca448b Binary files /dev/null and b/junit/mock2/lib/org.jacoco.agent-0.8.3.201901230119.jar differ diff --git a/junit/mock2/lib/org.jacoco.ant-0.8.3.201901230119.jar b/junit/mock2/lib/org.jacoco.ant-0.8.3.201901230119.jar new file mode 100644 index 0000000..6cb0624 Binary files /dev/null and b/junit/mock2/lib/org.jacoco.ant-0.8.3.201901230119.jar differ diff --git a/junit/mock2/lib/org.jacoco.core-0.8.3.201901230119.jar b/junit/mock2/lib/org.jacoco.core-0.8.3.201901230119.jar new file mode 100644 index 0000000..22f5594 Binary files /dev/null and b/junit/mock2/lib/org.jacoco.core-0.8.3.201901230119.jar differ diff --git a/junit/mock2/lib/org.jacoco.report-0.8.3.201901230119.jar b/junit/mock2/lib/org.jacoco.report-0.8.3.201901230119.jar new file mode 100644 index 0000000..8b36f81 Binary files /dev/null and b/junit/mock2/lib/org.jacoco.report-0.8.3.201901230119.jar differ diff --git a/junit/mock2/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties b/junit/mock2/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties new file mode 100644 index 0000000..f585044 --- /dev/null +++ b/junit/mock2/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties @@ -0,0 +1,3 @@ +VERSION=0.8.3.201901230119 +HOMEURL=http://www.jacoco.org/jacoco +RUNTIMEPACKAGE=org.jacoco.agent.rt.internal_1f1cc91 \ No newline at end of file diff --git a/junit/mock2/src/Dream.java b/junit/mock2/src/Dream.java new file mode 100644 index 0000000..552c479 --- /dev/null +++ b/junit/mock2/src/Dream.java @@ -0,0 +1,7 @@ +public interface Dream { + public void setReceiver(String name); + + public void setDate(int year, int mounth, int date); + + public void show(); +} \ No newline at end of file diff --git a/junit/mock2/src/Mazu.java b/junit/mock2/src/Mazu.java new file mode 100644 index 0000000..8c7364d --- /dev/null +++ b/junit/mock2/src/Mazu.java @@ -0,0 +1,13 @@ +public class Mazu { + private int year; + + public Mazu(int year) { + this.year = year; + } + + public void avatara(Dream dream, String receiverName, int mounth, int date) { + dream.setReceiver(receiverName); + dream.setDate(year, mounth, date); + dream.show(); + } +} \ No newline at end of file diff --git a/junit/mock2/testcase/TestMazu.java b/junit/mock2/testcase/TestMazu.java new file mode 100644 index 0000000..1be4705 --- /dev/null +++ b/junit/mock2/testcase/TestMazu.java @@ -0,0 +1,103 @@ +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +import java.util.List; +import static java.util.Arrays.asList; + +import org.junit.Before; // before after ... +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +public class TestMazu { + private String receiverName; + private int yearInDate; + private int mounthInDate; + private int dateInDate; + private Dream dreamMock; + + @Before + public void setupWithGou() { + receiverName = "Terry Gou"; + yearInDate = 2019; + mounthInDate = 4; + dateInDate = 16; + dreamMock = mock(Dream.class); + + } + + @Test + public void testMazuMockDreamShow() { + Mazu cihuiMazu = new Mazu(yearInDate); + cihuiMazu.avatara(dreamMock, receiverName, mounthInDate, dateInDate); + + verify(dreamMock).show(); + } + + @Test + public void testMazuMockDreamShowTwice() { + Mazu cihuiMazu = new Mazu(yearInDate); + cihuiMazu.avatara(dreamMock, receiverName, mounthInDate, dateInDate); + cihuiMazu.avatara(dreamMock, receiverName, mounthInDate, dateInDate); + + verify(dreamMock, times(2)).show(); + } + + @Test + public void testMazuMockDreamShowNever() { + Mazu cihuiMazu = new Mazu(yearInDate); + + verify(dreamMock, never()).show(); + } + + @Test + public void testMazuMockDreamSetReceiver() { + Mazu cihuiMazu = new Mazu(yearInDate); + cihuiMazu.avatara(dreamMock, receiverName, mounthInDate, dateInDate); + + verify(dreamMock).setReceiver(receiverName); + // verify(dreamMock).setReceiver("Me"); + } + + @Test + public void testMazuMockDreamSetReceiverArgumentAny() { + + Mazu cihuiMazu = new Mazu(yearInDate); + cihuiMazu.avatara(dreamMock, receiverName, mounthInDate, dateInDate); + + verify(dreamMock).setReceiver(anyString()); + } + + @Test + public void testMazuMockDreamSetReceiverArgument() { + ArgumentCaptor argument = ArgumentCaptor.forClass(String.class); + + Mazu cihuiMazu = new Mazu(yearInDate); + cihuiMazu.avatara(dreamMock, receiverName, mounthInDate, dateInDate); + + verify(dreamMock).setReceiver(argument.capture()); + String actualReceiverName = argument.getValue(); + + assertEquals(receiverName, actualReceiverName); + } + + @Test + public void testMazuMockDreamSetDateArgumentAny() { + Mazu cihuiMazu = new Mazu(yearInDate); + cihuiMazu.avatara(dreamMock, receiverName, mounthInDate, dateInDate); + + verify(dreamMock).setDate(anyInt(), anyInt(), anyInt()); + } + + @Test + public void testMazuMockDreamSetDateArgument() { + List expectedList = asList(yearInDate, mounthInDate, dateInDate); + ArgumentCaptor argument = ArgumentCaptor.forClass(Integer.class); + + Mazu cihuiMazu = new Mazu(yearInDate); + cihuiMazu.avatara(dreamMock, receiverName, mounthInDate, dateInDate); + + verify(dreamMock).setDate(argument.capture(), argument.capture(), argument.capture()); + List actualReceiverName = argument.getAllValues(); + assertEquals(expectedList, actualReceiverName); + } +} diff --git a/junit/mock2/testcase/TestRunner.java b/junit/mock2/testcase/TestRunner.java new file mode 100644 index 0000000..d708c85 --- /dev/null +++ b/junit/mock2/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(TestMazu.class); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/junit/parameterized2.0/Makefile b/junit/parameterized2.0/Makefile new file mode 100644 index 0000000..ecebbea --- /dev/null +++ b/junit/parameterized2.0/Makefile @@ -0,0 +1,15 @@ +all: compile compileTest + +compile: + mkdir -p bin + javac src/Min.java -d bin + +compileTest: + mkdir -p bin + javac -classpath /usr/share/java/junit4.jar src/Min.java testcase/TestParameterized.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/parameterized2.0/src/Min.java b/junit/parameterized2.0/src/Min.java new file mode 100644 index 0000000..b860a73 --- /dev/null +++ b/junit/parameterized2.0/src/Min.java @@ -0,0 +1,22 @@ +import java.util.*; + +public class Min { + public static > T min(List list) { + if (list.size() == 0) { + throw new IllegalArgumentException("Min.min"); + } + Iterator itr = list.iterator(); + T result = itr.next(); + + if (result == null) + throw new NullPointerException("Min.min"); + + while (itr.hasNext()) { // throws NPE, CCE as needed + T comp = itr.next(); + if (comp.compareTo(result) < 0) { + result = comp; + } + } + return result; + } +} diff --git a/junit/parameterized2.0/testcase/TestParameterized.java b/junit/parameterized2.0/testcase/TestParameterized.java new file mode 100644 index 0000000..a2c8f97 --- /dev/null +++ b/junit/parameterized2.0/testcase/TestParameterized.java @@ -0,0 +1,30 @@ +import java.util.*; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(value = Parameterized.class) +public class TestParameterized> { + List list; + T expected; + + public TestParameterized(List list, T expected) { + this.list = list; + this.expected = expected; + } + + @Parameters(name = "{index}: Min.min({0})={1}") + public static Iterable data1() { + return Arrays.asList(new Object[][] { { Arrays.asList(1, 2, 3, 4, 5, 6, 7), 1 }, + { Arrays.asList("a", "b", "c", "d", "e", "f"), "a" }, { Arrays.asList("aba", "acb", "abc"), "aba" } }); + } + + @Test + public void testDistant() { + T result = Min.min(list); + assertEquals(expected, result); + } +} diff --git a/junit/parameterized2.0/testcase/TestRunner.java b/junit/parameterized2.0/testcase/TestRunner.java new file mode 100644 index 0000000..4fb2d7b --- /dev/null +++ b/junit/parameterized2.0/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(TestParameterized.class); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/junit/stub/Makefile b/junit/stub/Makefile new file mode 100644 index 0000000..4bd3f28 --- /dev/null +++ b/junit/stub/Makefile @@ -0,0 +1,49 @@ +all: compile compileTest + +SimpleSocket: compileTest runSocketCov calCov showCov + +Simple: compileTest runCov calCov showCov + +compile: + mkdir -p bin + javac src/TcpExample.java src/TcpClient.java src/TcpServer.java -d bin + +runServer: + cd bin ; java TcpExample server + +runClient: + cd bin ; java TcpExample client + +compileTest: + mkdir -p bin + javac -classpath /usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar \ + src/TcpClient.java \ + testcase/TestTcpClient.java testcase/TestTcpSocket.java testcase/TestRunner.java -d bin + +testSocket: + cd bin ; java -classpath .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunner Socket + +test: + cd bin ; java -classpath .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunner + +runSocketCov: + rm bin/*.exec 2> /dev/null || true + cd bin ; java -javaagent:../lib/jacocoagent.jar -cp .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunner Socket + +runCov: + rm bin/*.exec 2> /dev/null || true + cd bin ; java -javaagent:../lib/jacocoagent.jar -cp .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunner + +calCov: + cd bin ; java -jar ../lib/jacococli.jar report jacoco.exec --classfiles ../bin \ + --sourcefiles ../src --sourcefiles ../testcase --html reportDir + +showCov: + RED="\033[0;31m" ; NC='\033[0m' ; printf "$${RED}http://" ; curl -s https://api.ipify.org/ ; printf ":8000/$${NC}\n" + cd bin/reportDir ; python3 -m http.server 8000 + +kill: + killall python3 + +clean: + rm -r bin/*.class bin/*.exec bin/reportDir 2> /dev/null || true diff --git a/junit/stub/README.md b/junit/stub/README.md new file mode 100644 index 0000000..6cb7275 --- /dev/null +++ b/junit/stub/README.md @@ -0,0 +1,24 @@ +## Makefile + +### Run Java program + +`make [all]` - Compile JAVA class + +`make runServer` - Run TCP server example + +`make runClient` - Run TCP client example + +### Run unittest +Test client only + +`make compileTest` - Compile Junit testing class + +`make testSocket` - Do unittest with Socket + +`make test` - Do unittest with stub + +### Show Jacoco coverage Web page + +`make SimpleSocket` - make {clean, compileTest, runSocketCov, calCov, showCov} + +`make Simple` - make {clean, compileTest, runCov, calCov, showCov} \ No newline at end of file diff --git a/junit/stub/lib/META-INF/MANIFEST.MF b/junit/stub/lib/META-INF/MANIFEST.MF new file mode 100644 index 0000000..8870b89 --- /dev/null +++ b/junit/stub/lib/META-INF/MANIFEST.MF @@ -0,0 +1,11 @@ +Manifest-Version: 1.0 +Implementation-Title: JaCoCo Java Agent +Automatic-Module-Name: org.jacoco.agent.rt +Premain-Class: org.jacoco.agent.rt.internal_1f1cc91.PreMain +Implementation-Version: 0.8.3 +Archiver-Version: Plexus Archiver +Built-By: godin +Created-By: Apache Maven +Build-Jdk: 1.8.0_152 +Implementation-Vendor: Mountainminds GmbH & Co. KG + diff --git a/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties b/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties new file mode 100644 index 0000000..d77a8ec --- /dev/null +++ b/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Jan 23 02:22:02 CET 2019 +version=0.8.3 +groupId=org.jacoco +artifactId=org.jacoco.agent.rt diff --git a/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml b/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml new file mode 100644 index 0000000..9e05cc1 --- /dev/null +++ b/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml @@ -0,0 +1,96 @@ + + + + 4.0.0 + + + org.jacoco + org.jacoco.build + 0.8.3 + ../org.jacoco.build + + + org.jacoco.agent.rt + + + JaCoCo :: Agent RT + JaCoCo Java Agent + + + true + true + + + + + ${project.groupId} + org.jacoco.core + + + + + src + + + + org.apache.maven.plugins + maven-shade-plugin + + + package + + shade + + + true + all + true + + + org.jacoco.agent.rt.internal + ${jacoco.runtime.package.name} + + + org.jacoco.core + ${jacoco.runtime.package.name}.core + + + org.objectweb.asm + ${jacoco.runtime.package.name}.asm + + + + + org.ow2.asm:* + + module-info.class + + + + + + + ${jacoco.runtime.package.name}.PreMain + ${project.artifactId} + ${project.description} + ${project.organization.name} + ${project.version} + + + + + + + + + + diff --git a/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties b/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties new file mode 100644 index 0000000..8e440db --- /dev/null +++ b/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Jan 23 02:21:51 CET 2019 +version=0.8.3 +groupId=org.jacoco +artifactId=org.jacoco.core diff --git a/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml b/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml new file mode 100644 index 0000000..d1b6886 --- /dev/null +++ b/junit/stub/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml @@ -0,0 +1,69 @@ + + + + 4.0.0 + + + org.jacoco + org.jacoco.build + 0.8.3 + ../org.jacoco.build + + + org.jacoco.core + + JaCoCo :: Core + JaCoCo Core + + + + org.ow2.asm + asm + + + org.ow2.asm + asm-commons + + + org.ow2.asm + asm-tree + + + + + src + + + + org.apache.felix + maven-bundle-plugin + + + process-classes + + manifest + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + + + diff --git a/junit/stub/lib/about.html b/junit/stub/lib/about.html new file mode 100644 index 0000000..766ff54 --- /dev/null +++ b/junit/stub/lib/about.html @@ -0,0 +1,73 @@ + + + + +About + + + +

About This Content

+ +

+ 2019/01/23 +

+ +

License

+ +

+ All Content in this plug-in is made available by Mountainminds GmbH & Co. + KG, Munich. Unless otherwise indicated below, the Content is provided to you + under the terms and conditions of the Eclipse Public License Version 1.0 + ("EPL"). A copy of the EPL is available at + http://www.eclipse.org/legal/epl-v10.html. + For purposes of the EPL, "Program" will mean the Content. +

+ +

Third Party Content

+ +

+ The Content includes items that have been sourced from third parties as set + out below. +

+ +

ASM 3.x

+ +

+ This plug-in contains the ASM library + which is subject to the terms and conditions of the following license: +

+ +
+Copyright (c) 2000-2005 INRIA, France Telecom
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holders nor the names of its
+   contributors may be used to endorse or promote products derived from
+   this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+THE POSSIBILITY OF SUCH DAMAGE.
+
+ + + \ No newline at end of file diff --git a/junit/stub/lib/jacocoagent.jar b/junit/stub/lib/jacocoagent.jar new file mode 100644 index 0000000..0b25a00 Binary files /dev/null and b/junit/stub/lib/jacocoagent.jar differ diff --git a/junit/stub/lib/jacocoant.jar b/junit/stub/lib/jacocoant.jar new file mode 100644 index 0000000..09ac4a8 Binary files /dev/null and b/junit/stub/lib/jacocoant.jar differ diff --git a/junit/stub/lib/jacococli.jar b/junit/stub/lib/jacococli.jar new file mode 100644 index 0000000..fea7a1c Binary files /dev/null and b/junit/stub/lib/jacococli.jar differ diff --git a/junit/stub/lib/org.jacoco.agent-0.8.3.201901230119.jar b/junit/stub/lib/org.jacoco.agent-0.8.3.201901230119.jar new file mode 100644 index 0000000..1ca448b Binary files /dev/null and b/junit/stub/lib/org.jacoco.agent-0.8.3.201901230119.jar differ diff --git a/junit/stub/lib/org.jacoco.ant-0.8.3.201901230119.jar b/junit/stub/lib/org.jacoco.ant-0.8.3.201901230119.jar new file mode 100644 index 0000000..6cb0624 Binary files /dev/null and b/junit/stub/lib/org.jacoco.ant-0.8.3.201901230119.jar differ diff --git a/junit/stub/lib/org.jacoco.core-0.8.3.201901230119.jar b/junit/stub/lib/org.jacoco.core-0.8.3.201901230119.jar new file mode 100644 index 0000000..22f5594 Binary files /dev/null and b/junit/stub/lib/org.jacoco.core-0.8.3.201901230119.jar differ diff --git a/junit/stub/lib/org.jacoco.report-0.8.3.201901230119.jar b/junit/stub/lib/org.jacoco.report-0.8.3.201901230119.jar new file mode 100644 index 0000000..8b36f81 Binary files /dev/null and b/junit/stub/lib/org.jacoco.report-0.8.3.201901230119.jar differ diff --git a/junit/stub/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties b/junit/stub/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties new file mode 100644 index 0000000..f585044 --- /dev/null +++ b/junit/stub/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties @@ -0,0 +1,3 @@ +VERSION=0.8.3.201901230119 +HOMEURL=http://www.jacoco.org/jacoco +RUNTIMEPACKAGE=org.jacoco.agent.rt.internal_1f1cc91 \ No newline at end of file diff --git a/junit/stub/src/TcpClient.java b/junit/stub/src/TcpClient.java new file mode 100644 index 0000000..4dc6d5d --- /dev/null +++ b/junit/stub/src/TcpClient.java @@ -0,0 +1,61 @@ +import java.net.*; +import java.io.*; + +// 1. 本程式必須與 TcpServer.java 程式搭配執行,先執行 TcpServer 再執行本程式。 +// 2. 本程式必須有一個參數,指定伺服器的 IP。 +// 用法範例: java TcpClient + +class TcpClientParseCommunicate { + private Socket client; + private InputStream inputStream; + private StringBuffer buf; + + public TcpClientParseCommunicate(Socket client) { + this.client = client; + } + + public StringBuffer getBuf() { + return buf; + } + + public void communicate() throws Exception { + this.inputStream = client.getInputStream(); // 取得輸入訊息的串流 + } + + public void parseInput() throws Exception { + try { + buf = new StringBuffer(); // 建立讀取字串。 + while (true) { // 不斷讀取。 + int x = inputStream.read(); // 讀取一個 byte。(read 傳回 -1 代表串流結束) + if (x == -1) + break; // x = -1 代表串流結束,讀取完畢,用 break 跳開。 + byte b = (byte) x; // 將 x 轉為 byte,放入變數 b. + buf.append((char) b);// 假設傳送ASCII字元都是 ASCII。 + } + } catch (Exception e) { + inputStream.close(); // 關閉輸入串流。 + } + } + +} + +public class TcpClient { + private Socket client; + private TcpClientParseCommunicate tcpClientParseCommunicate; + + public TcpClient(String ip, int port) throws Exception { + client = new Socket(ip, port); // 連接至遠端機器 + } + + public void read() throws Exception { + tcpClientParseCommunicate = new TcpClientParseCommunicate(client); + tcpClientParseCommunicate.communicate(); + tcpClientParseCommunicate.parseInput(); + } + + public void output() throws Exception { + StringBuffer buf = tcpClientParseCommunicate.getBuf(); + System.out.println(buf); // 印出接收到的訊息。 + client.close(); + } +} diff --git a/junit/stub/src/TcpExample.java b/junit/stub/src/TcpExample.java new file mode 100644 index 0000000..199a855 --- /dev/null +++ b/junit/stub/src/TcpExample.java @@ -0,0 +1,19 @@ +//import TcpServer; +//import TcpClient; + +public class TcpExample { + public static int port = 6666; // 設定傳送埠為 6666。 + + public static void main(String args[]) throws Exception { + System.out.println("===== " + args[0] + " Mode ====="); // 印出接收到的訊息。 + if ("server".equals(args[0])) { + TcpServer tcpServer = new TcpServer(port); + tcpServer.tcpServer(); + } else if ("client".equals(args[0])) { + TcpClient tcpClient = new TcpClient("127.0.0.1", port); + tcpClient.read(); + tcpClient.output(); + } + } + +} \ No newline at end of file diff --git a/junit/stub/src/TcpServer.java b/junit/stub/src/TcpServer.java new file mode 100644 index 0000000..b4daa50 --- /dev/null +++ b/junit/stub/src/TcpServer.java @@ -0,0 +1,24 @@ +import java.net.*; +import java.io.*; + +// 1. 本程式必須與 TcpClient.java 程式搭配執行,先執行本程式再執行 TcpClient。 +// 2. 執行方法 : java TcpServer + +public class TcpServer { + private int port; + + public TcpServer(int port) { + this.port = port; + } + + public void tcpServer() throws Exception { + ServerSocket ss = new ServerSocket(port); // 建立 TCP 伺服器。 + while (true) { // 不斷的接收處理輸入訊息。 + Socket sc = ss.accept(); // 接收輸入訊息。 + OutputStream os = sc.getOutputStream(); // 取得輸出串流。 + os.write("From Server : Hi !".getBytes("UTF-8"));// 送訊息到 Client 端。 + os.close(); // 關閉輸出串流。 + sc.close(); // 關閉 TCP 伺服器。 + } + } +} \ No newline at end of file diff --git a/junit/stub/testcase/TestRunner.java b/junit/stub/testcase/TestRunner.java new file mode 100644 index 0000000..9796795 --- /dev/null +++ b/junit/stub/testcase/TestRunner.java @@ -0,0 +1,19 @@ +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; + if (args.length >= 1 && "Socket".equals(args[0])) { + result = JUnitCore.runClasses(TestTcpSocket.class); + result = JUnitCore.runClasses(TestTcpClient.class); + } else { + result = JUnitCore.runClasses(TestTcpClient.class); + } + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/junit/stub/testcase/TestTcpClient.java b/junit/stub/testcase/TestTcpClient.java new file mode 100644 index 0000000..d42438f --- /dev/null +++ b/junit/stub/testcase/TestTcpClient.java @@ -0,0 +1,56 @@ +import org.junit.Test; +import org.junit.*; // before after ... + +import static org.junit.Assert.assertEquals; + +import java.io.InputStream; +import java.io.ByteArrayInputStream; + +import java.net.*; // Socket + +import org.mockito.Mockito.*; // java mock: Mockito +import static org.mockito.Mockito.*; + +public class TestTcpClient { + + @Test + public void testTcpClientWithStub() throws Exception { + final String initialString = "testTcpClientWithStub"; + final InputStream targetStream = new ByteArrayInputStream(initialString.getBytes()); + + class SocketStub extends Socket { + SocketStub(String host, int port) { + // Without connect with remote + } + + public InputStream getInputStream() { + return targetStream; + } + } + + final Socket socket = new SocketStub(null, -1); + + TcpClientParseCommunicate tcpClientParseCommunicate = new TcpClientParseCommunicate(socket); + tcpClientParseCommunicate.communicate(); + tcpClientParseCommunicate.parseInput(); + StringBuffer sb = tcpClientParseCommunicate.getBuf(); + + assertEquals(initialString, sb.toString()); + } + + @Test + public void testTcpClientWithStubMockito() throws Exception { + final String initialString = "testTcpClientWithStubMockito"; + final InputStream targetStream = new ByteArrayInputStream(initialString.getBytes()); + + Socket clientStub = mock(Socket.class); + when(clientStub.getInputStream()).thenReturn(targetStream); + + TcpClientParseCommunicate tcpClientParseCommunicate = new TcpClientParseCommunicate(clientStub); + tcpClientParseCommunicate.communicate(); + tcpClientParseCommunicate.parseInput(); + StringBuffer sb = tcpClientParseCommunicate.getBuf(); + + assertEquals(initialString, sb.toString()); + } +} diff --git a/junit/stub/testcase/TestTcpSocket.java b/junit/stub/testcase/TestTcpSocket.java new file mode 100644 index 0000000..415555a --- /dev/null +++ b/junit/stub/testcase/TestTcpSocket.java @@ -0,0 +1,24 @@ +import org.junit.Test; +import org.junit.*; // before after ... + +import static org.junit.Assert.assertEquals; + +import java.net.*; // Socket + +public class TestTcpSocket { + @Test + public void testTcpClientWithServer() throws Exception { + final String initialString = "From Server : Hi !"; // Guess what server response // Not good + + System.out.println("Try to connect with remote server"); + + final Socket socket = new Socket("127.0.0.1", 6666); + + TcpClientParseCommunicate tcpClientParseCommunicate = new TcpClientParseCommunicate(socket); + tcpClientParseCommunicate.communicate(); + tcpClientParseCommunicate.parseInput(); + StringBuffer sb = tcpClientParseCommunicate.getBuf(); + + assertEquals(initialString, sb.toString()); + } +} diff --git a/junit/stubLW/Makefile b/junit/stubLW/Makefile new file mode 100644 index 0000000..304ad04 --- /dev/null +++ b/junit/stubLW/Makefile @@ -0,0 +1,45 @@ +all: compileTest + +Simple: compileTest runCov calCov showCov + +SimpleANS: compileTestANS runCovANS calCov showCov + +compileTest: + mkdir -p bin + javac -classpath /usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar \ + src/Factorization.java src/Multiplication.java \ + testcase/TestMultiplication.java testcase/TestRunner.java -d bin + +compileTestANS: + mkdir -p bin + javac -classpath /usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar \ + src/Factorization.java src/Multiplication.java \ + testcase/TestMultiplicationANS.java testcase/TestRunnerANS.java -d bin + +test: + cd bin ; java -classpath .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunner + +testANS: + cd bin ; java -classpath .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunnerANS + +runCov: + rm bin/*.exec 2> /dev/null || true + cd bin ; java -javaagent:../lib/jacocoagent.jar -cp .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunner + +runCovANS: + rm bin/*.exec 2> /dev/null || true + cd bin ; java -javaagent:../lib/jacocoagent.jar -cp .:/usr/share/java/junit4.jar:/usr/share/java/mockito-core.jar TestRunnerANS + +calCov: + cd bin ; java -jar ../lib/jacococli.jar report jacoco.exec --classfiles ../bin \ + --sourcefiles ../src --sourcefiles ../testcase --html reportDir + +showCov: + RED="\033[0;31m" ; NC='\033[0m' ; printf "$${RED}http://" ; curl -s https://api.ipify.org/ ; printf ":8000/$${NC}\n" + cd bin/reportDir ; python3 -m http.server 8000 + +kill: + killall python3 + +clean: + rm -r bin/*.class bin/*.exec bin/reportDir 2> /dev/null || true diff --git a/junit/stubLW/lib/META-INF/MANIFEST.MF b/junit/stubLW/lib/META-INF/MANIFEST.MF new file mode 100644 index 0000000..8870b89 --- /dev/null +++ b/junit/stubLW/lib/META-INF/MANIFEST.MF @@ -0,0 +1,11 @@ +Manifest-Version: 1.0 +Implementation-Title: JaCoCo Java Agent +Automatic-Module-Name: org.jacoco.agent.rt +Premain-Class: org.jacoco.agent.rt.internal_1f1cc91.PreMain +Implementation-Version: 0.8.3 +Archiver-Version: Plexus Archiver +Built-By: godin +Created-By: Apache Maven +Build-Jdk: 1.8.0_152 +Implementation-Vendor: Mountainminds GmbH & Co. KG + diff --git a/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties b/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties new file mode 100644 index 0000000..d77a8ec --- /dev/null +++ b/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Jan 23 02:22:02 CET 2019 +version=0.8.3 +groupId=org.jacoco +artifactId=org.jacoco.agent.rt diff --git a/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml b/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml new file mode 100644 index 0000000..9e05cc1 --- /dev/null +++ b/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.agent.rt/pom.xml @@ -0,0 +1,96 @@ + + + + 4.0.0 + + + org.jacoco + org.jacoco.build + 0.8.3 + ../org.jacoco.build + + + org.jacoco.agent.rt + + + JaCoCo :: Agent RT + JaCoCo Java Agent + + + true + true + + + + + ${project.groupId} + org.jacoco.core + + + + + src + + + + org.apache.maven.plugins + maven-shade-plugin + + + package + + shade + + + true + all + true + + + org.jacoco.agent.rt.internal + ${jacoco.runtime.package.name} + + + org.jacoco.core + ${jacoco.runtime.package.name}.core + + + org.objectweb.asm + ${jacoco.runtime.package.name}.asm + + + + + org.ow2.asm:* + + module-info.class + + + + + + + ${jacoco.runtime.package.name}.PreMain + ${project.artifactId} + ${project.description} + ${project.organization.name} + ${project.version} + + + + + + + + + + diff --git a/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties b/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties new file mode 100644 index 0000000..8e440db --- /dev/null +++ b/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Wed Jan 23 02:21:51 CET 2019 +version=0.8.3 +groupId=org.jacoco +artifactId=org.jacoco.core diff --git a/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml b/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml new file mode 100644 index 0000000..d1b6886 --- /dev/null +++ b/junit/stubLW/lib/META-INF/maven/org.jacoco/org.jacoco.core/pom.xml @@ -0,0 +1,69 @@ + + + + 4.0.0 + + + org.jacoco + org.jacoco.build + 0.8.3 + ../org.jacoco.build + + + org.jacoco.core + + JaCoCo :: Core + JaCoCo Core + + + + org.ow2.asm + asm + + + org.ow2.asm + asm-commons + + + org.ow2.asm + asm-tree + + + + + src + + + + org.apache.felix + maven-bundle-plugin + + + process-classes + + manifest + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + + + diff --git a/junit/stubLW/lib/about.html b/junit/stubLW/lib/about.html new file mode 100644 index 0000000..766ff54 --- /dev/null +++ b/junit/stubLW/lib/about.html @@ -0,0 +1,73 @@ + + + + +About + + + +

About This Content

+ +

+ 2019/01/23 +

+ +

License

+ +

+ All Content in this plug-in is made available by Mountainminds GmbH & Co. + KG, Munich. Unless otherwise indicated below, the Content is provided to you + under the terms and conditions of the Eclipse Public License Version 1.0 + ("EPL"). A copy of the EPL is available at + http://www.eclipse.org/legal/epl-v10.html. + For purposes of the EPL, "Program" will mean the Content. +

+ +

Third Party Content

+ +

+ The Content includes items that have been sourced from third parties as set + out below. +

+ +

ASM 3.x

+ +

+ This plug-in contains the ASM library + which is subject to the terms and conditions of the following license: +

+ +
+Copyright (c) 2000-2005 INRIA, France Telecom
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holders nor the names of its
+   contributors may be used to endorse or promote products derived from
+   this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+THE POSSIBILITY OF SUCH DAMAGE.
+
+ + + \ No newline at end of file diff --git a/junit/stubLW/lib/jacocoagent.jar b/junit/stubLW/lib/jacocoagent.jar new file mode 100644 index 0000000..0b25a00 Binary files /dev/null and b/junit/stubLW/lib/jacocoagent.jar differ diff --git a/junit/stubLW/lib/jacocoant.jar b/junit/stubLW/lib/jacocoant.jar new file mode 100644 index 0000000..09ac4a8 Binary files /dev/null and b/junit/stubLW/lib/jacocoant.jar differ diff --git a/junit/stubLW/lib/jacococli.jar b/junit/stubLW/lib/jacococli.jar new file mode 100644 index 0000000..fea7a1c Binary files /dev/null and b/junit/stubLW/lib/jacococli.jar differ diff --git a/junit/stubLW/lib/org.jacoco.agent-0.8.3.201901230119.jar b/junit/stubLW/lib/org.jacoco.agent-0.8.3.201901230119.jar new file mode 100644 index 0000000..1ca448b Binary files /dev/null and b/junit/stubLW/lib/org.jacoco.agent-0.8.3.201901230119.jar differ diff --git a/junit/stubLW/lib/org.jacoco.ant-0.8.3.201901230119.jar b/junit/stubLW/lib/org.jacoco.ant-0.8.3.201901230119.jar new file mode 100644 index 0000000..6cb0624 Binary files /dev/null and b/junit/stubLW/lib/org.jacoco.ant-0.8.3.201901230119.jar differ diff --git a/junit/stubLW/lib/org.jacoco.core-0.8.3.201901230119.jar b/junit/stubLW/lib/org.jacoco.core-0.8.3.201901230119.jar new file mode 100644 index 0000000..22f5594 Binary files /dev/null and b/junit/stubLW/lib/org.jacoco.core-0.8.3.201901230119.jar differ diff --git a/junit/stubLW/lib/org.jacoco.report-0.8.3.201901230119.jar b/junit/stubLW/lib/org.jacoco.report-0.8.3.201901230119.jar new file mode 100644 index 0000000..8b36f81 Binary files /dev/null and b/junit/stubLW/lib/org.jacoco.report-0.8.3.201901230119.jar differ diff --git a/junit/stubLW/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties b/junit/stubLW/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties new file mode 100644 index 0000000..f585044 --- /dev/null +++ b/junit/stubLW/lib/org/jacoco/agent/rt/internal_1f1cc91/core/jacoco.properties @@ -0,0 +1,3 @@ +VERSION=0.8.3.201901230119 +HOMEURL=http://www.jacoco.org/jacoco +RUNTIMEPACKAGE=org.jacoco.agent.rt.internal_1f1cc91 \ No newline at end of file diff --git a/junit/stubLW/src/Factorization.java b/junit/stubLW/src/Factorization.java new file mode 100644 index 0000000..d7f15b1 --- /dev/null +++ b/junit/stubLW/src/Factorization.java @@ -0,0 +1,11 @@ +import java.util.ArrayList; + +public interface Factorization { + + // factorization(15) = 3 * 5 + public ArrayList factorization(int total); + + // rootValue is negative && ifAbsolute is false, return -1 + public int factorizationRoot(int rootValue, boolean ifAbsolute); + +} \ No newline at end of file diff --git a/junit/stubLW/src/Multiplication.java b/junit/stubLW/src/Multiplication.java new file mode 100644 index 0000000..c93f000 --- /dev/null +++ b/junit/stubLW/src/Multiplication.java @@ -0,0 +1,45 @@ +import java.util.ArrayList; +import java.util.Iterator; + +public class Multiplication { + + public Multiplication() { + + } + + public int multiplication(int a, int b) { + return a * b; + } + + public int multiplicationWithFactorizationAbsolute(Factorization factorizationStub, int total) { + // Multiply everything in ArrayList + ArrayList array = factorizationStub.factorization(total); + if (array.size() < 2) { + // Won't multiply single number + return -1; + } + + int ans = 1; + Iterator it = array.iterator(); + while (it.hasNext()) { + ans *= it.next(); + } + return ans; + } + + public int multiplicationWithFactorization(Factorization factorizationStub, int total, boolean ifAbsolute) { + // Multiply everything in ArrayList + ArrayList array = factorizationStub.factorization(total); + if (array.size() < 2) { + // Won't multiply single number + return -1; + } + + int ans = factorizationStub.factorizationRoot(total, ifAbsolute); + Iterator it = array.iterator(); + while (it.hasNext()) { + ans *= it.next(); + } + return ans; + } +} \ No newline at end of file diff --git a/junit/stubLW/testcase/TestMultiplication.java b/junit/stubLW/testcase/TestMultiplication.java new file mode 100644 index 0000000..4b68e51 --- /dev/null +++ b/junit/stubLW/testcase/TestMultiplication.java @@ -0,0 +1,104 @@ +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +import java.util.ArrayList; +import java.util.Arrays; + +public class TestMultiplication { + + @Test + public void testMultiplication() { + // Test simple multiplications + int a = 3; + int b = 5; + int expectedANS = 15; + + Multiplication multiplication = new Multiplication(); + int actualANS = multiplication.multiplication(a, b); + + assertEquals(expectedANS, actualANS); + } + + @Test + public void testMultiplicationWithStubMockitoAbsolute() { + // Test two number multiplications + int a = 3; + int b = 5; + int expectedANS = 15; + ArrayList array = new ArrayList(Arrays.asList(a, b)); + + Factorization factorizationStub = mock(Factorization.class); + when(factorizationStub.factorization(expectedANS)).thenReturn(array); + + Multiplication multiplication = new Multiplication(); + int actualANS = multiplication.multiplicationWithFactorizationAbsolute(factorizationStub, expectedANS); + + assertEquals(expectedANS, actualANS); + } + + @Test + public void testMultiplicationWithStubMockito() { + // Test two number multiplications + int a = -7; + int b = 5; + int expectedANS = -35; + ArrayList array = new ArrayList(Arrays.asList(a, b)); + + Factorization factorizationStub = mock(Factorization.class); + when(factorizationStub.factorization(expectedANS)).thenReturn(array); + when(factorizationStub.factorizationRoot(-35, true)).thenReturn(1); + + Multiplication multiplication = new Multiplication(); + int actualANS = multiplication.multiplicationWithFactorization(factorizationStub, expectedANS, true); + + assertEquals(expectedANS, actualANS); + } + + @Test + public void testMultiplicationWithStubMockitoAny() { + // Test two number multiplications + int a = 7; + int b = 5; + int expectedANS = -35; + ArrayList array = new ArrayList(Arrays.asList(a, b)); + + Factorization factorizationStub = mock(Factorization.class); + when(factorizationStub.factorization(expectedANS)).thenReturn(array); + when(factorizationStub.factorizationRoot(anyInt(), anyBoolean())).thenReturn(-1); + + Multiplication multiplication = new Multiplication(); + int actualANS = multiplication.multiplicationWithFactorization(factorizationStub, expectedANS, false); + + assertEquals(expectedANS, actualANS); + } + + //@Test + public void testMultiplicationThreeNumberArrayList() { + // Test three number multiplications + // Here should init prepared answer + + // Here should new Factorization object with Mockito stub + // Here should ask stub object to act whatever we want + + Multiplication multiplication = new Multiplication(); + int actualANS = multiplication.multiplicationWithFactorizationAbsolute(factorizationStub, expectedANS); + + assertEquals(expectedANS, actualANS); + } + + //@Test + public void testMultiplicationOneNumberArrayList() { + // Test one number multiplications, complete test coverage to 100% + // Here should init prepared answer + + // Here should new Factorization object with Mockito stub + // Here should ask stub object to act whatever we want + + Multiplication multiplication = new Multiplication(); + int actualANS = multiplication.multiplicationWithFactorizationAbsolute(factorizationStub, expectedANS); + + assertEquals(expectedANS, actualANS); + } +} diff --git a/junit/stubLW/testcase/TestRunner.java b/junit/stubLW/testcase/TestRunner.java new file mode 100644 index 0000000..85d0143 --- /dev/null +++ b/junit/stubLW/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(TestMultiplication.class); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/selenium/facebook.py b/selenium/facebook.py new file mode 100644 index 0000000..8f2b61e --- /dev/null +++ b/selenium/facebook.py @@ -0,0 +1,68 @@ +from selenium import webdriver +from selenium.webdriver.common.action_chains import ActionChains +from selenium.common.exceptions import NoSuchElementException +import time +import base64 + +url_list = ["https://www.facebook.com/DoctorKoWJ/", + "https://www.facebook.com/twherohan/", "https://www.facebook.com/TerryGou1018/", + "https://www.facebook.com/chingte/", "https://www.facebook.com/tsaiingwen/"] + + +def main(): + options = webdriver.ChromeOptions() + options.add_argument("--disable-notifications") + + # Start new web browser + global driver + driver = webdriver.Chrome(options=options) + facebook_login() + # unlike() + # like() + driver.close() + + +def facebook_login(): + driver.get('https://www.facebook.com') + email_box = driver.find_element_by_xpath('//*[@id="email"]') + pass_box = driver.find_element_by_xpath('//*[@id="pass"]') + email_box.send_keys('user@dns.com') + file = open("password.txt", "r") + password = file.read() + # print(password) + pass_box.send_keys(password) + login_box = driver.find_element_by_xpath( + '//*[@id="login_form"]/table/tbody/tr[2]/td[3]/label') + login_box.click() + + +def unlike(): + for url in url_list: + driver.get(url) + time.sleep(2) # Let the user actually see something! + try: + like_box = driver.find_element_by_xpath( + '//*[@id="u_0_z"]/div[2]/div/div/div/div/div/div/a') + # like_box.click() + ac = ActionChains(driver) + ac.move_to_element(like_box).move_by_offset( + 0, 30).click().perform() + time.sleep(1) # Let the user actually see something! + except NoSuchElementException: + print("already unlike") + + +def like(): + for url in url_list: + driver.get(url) + time.sleep(2) # Let the user actually see something! + try: + like_box = driver.find_element_by_xpath( + '//*[@id="u_0_10"]/div/div/div[1]/div/span/button') + like_box.click() + except NoSuchElementException: + print("already like") + + +if __name__ == '__main__': + main() diff --git a/selenium/google.py b/selenium/google.py new file mode 100644 index 0000000..7869c87 --- /dev/null +++ b/selenium/google.py @@ -0,0 +1,14 @@ +from selenium import webdriver +import time + +# start web driver, open a browser +driver = webdriver.Chrome() +# open web page +driver.get('http://www.google.com/xhtml') +# find element +search_box = driver.find_element_by_name('q') +search_box.send_keys('NCTU') +time.sleep(2) # Let the user actually see something! +search_box.submit() +time.sleep(5) # Let the user actually see something! +driver.close() # close browser \ No newline at end of file diff --git a/selenium/teiba_member_number.py b/selenium/teiba_member_number.py new file mode 100644 index 0000000..0b6d7ef --- /dev/null +++ b/selenium/teiba_member_number.py @@ -0,0 +1,67 @@ +#!python +import unittest +from selenium import webdriver +from selenium.common.exceptions import NoSuchElementException +import time + + +class TiebaSlogan(unittest.TestCase): + @classmethod + def setUpClass(self): + # start web driver, open a browser + self.driver = webdriver.Chrome() + + def setUp(self): + # open web page + self.driver.get('https://tieba.baidu.com/index.html') + + def test_windows(self): + # find element + search_box = self.driver.find_element_by_xpath('//*[@id="wd1"]') + search_box.send_keys('windows') + search_box.submit() + memNum = self.driver.find_element_by_class_name('card_menNum') + self.assertGreaterEqual(int(memNum.text.replace(',', '')), 106700) + + def test_linux(self): + # find element + search_box = self.driver.find_element_by_xpath('//*[@id="wd1"]') + search_box.send_keys('linux') + search_box.submit() + memNum = self.driver.find_element_by_class_name('card_menNum') + self.assertGreaterEqual(int(memNum.text.replace(',', '')), 203900) + + def test_taiwan(self): + with self.assertRaises(NoSuchElementException): + # find element + search_box = self.driver.find_element_by_xpath('//*[@id="wd1"]') + search_box.send_keys('taiwan') + search_box.submit() + self.driver.find_element_by_class_name('card_menNum') + self.assertGreaterEqual(int(memNum.text.replace(',', '')), 371700) + + def test_taiwan_warning(self): + # find element + search_box = self.driver.find_element_by_xpath('//*[@id="wd1"]') + search_box.send_keys('taiwan') + search_box.submit() + slogan = self.driver.find_element_by_xpath( + '/html/body/div[2]/div/div[2]/div[1]/h2') + self.assertEqual(slogan.text, "抱歉,根据相关法律法规和政策,本吧暂不开放。") + + def test_number(self): + # for loop to find the integral multiples of eight + pass + + def tearDown(self): + # print("exit") + pass + + @classmethod + def tearDownClass(self): + # close browser + self.driver.close() + + +if __name__ == "__main__": + unittest.main() diff --git a/selenium/teiba_slogan.py b/selenium/teiba_slogan.py new file mode 100644 index 0000000..f0cc5d3 --- /dev/null +++ b/selenium/teiba_slogan.py @@ -0,0 +1,75 @@ +#!python +import unittest +from selenium import webdriver +from selenium.common.exceptions import NoSuchElementException +import time + + +class TiebaSlogan(unittest.TestCase): + @classmethod + def setUpClass(self): + # start web driver, open a browser + self.driver = webdriver.Chrome() + + def setUp(self): + # open web page + self.driver.get('https://tieba.baidu.com/index.html') + + def test_windows(self): + # find element + search_box = self.driver.find_element_by_xpath('//*[@id="wd1"]') + search_box.send_keys('windows') + search_box.submit() + slogan = self.driver.find_element_by_class_name('card_slogan') + self.assertEqual(slogan.text, "不仅仅是一个Windows系统交流平台") + + def test_linux(self): + # find element + search_box = self.driver.find_element_by_xpath('//*[@id="wd1"]') + search_box.send_keys('linux') + search_box.submit() + slogan = self.driver.find_element_by_class_name('card_slogan') + self.assertEqual(slogan.text, "Just 4 Fun") + + def test_taiwan(self): + with self.assertRaises(NoSuchElementException): + # find element + search_box = self.driver.find_element_by_xpath('//*[@id="wd1"]') + search_box.send_keys('taiwan') + search_box.submit() + slogan = self.driver.find_element_by_class_name('card_slogan') + self.assertEqual(slogan.text, "百度台湾吧,我在这里,你在哪里?") + + def test_taiwan_warning(self): + # find element + search_box = self.driver.find_element_by_xpath('//*[@id="wd1"]') + search_box.send_keys('taiwan') + search_box.submit() + slogan = self.driver.find_element_by_xpath( + '/html/body/div[2]/div/div[2]/div[1]/h2') + self.assertEqual(slogan.text, "抱歉,根据相关法律法规和政策,本吧暂不开放。") + + def test_number(self): + for i in range(1, 10): + # reopen web page + self.driver.get('https://tieba.baidu.com/index.html') + search_box = self.driver.find_element_by_xpath( + '//*[@id="wd1"]') + print(5*i) + search_box.send_keys(5*i) + search_box.submit() + self.driver.find_element_by_class_name( + 'card_slogan') + + def tearDown(self): + # print("exit") + pass + + @classmethod + def tearDownClass(self): + # close browser + self.driver.close() + + +if __name__ == "__main__": + unittest.main()