package com.mycompany.sandbox; public class App { public static void main(String[] args) { App thisObj = new App(); System.out.println(thisObj.factorial(Integer.parseInt(args[0]))); } /** * x! を求めます. * * @param x 数値 * @return x! */ public int factorial(final int x) { return recursive(x); } /** * 再帰的に x * recursive(x-1) を実行します. * * @param x 数値 * @return x! */ private int recursive(final int x) { if (x == 1) { return x; } return x * recursive(x - 1); } }
package com.mycompany.sandbox; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { @Test public void test() { App app = new App(); assertThat(app.factorial(10), is(3628800)); } }
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany</groupId> <artifactId>Sandbox</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>Sandbox</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <version>3.2</version> <configuration> <locales>ja</locales> <inputEncoding>UTF-8</inputEncoding> <outputEncoding>UTF-8</outputEncoding> <reportPlugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.6.0.201210061924</version> </plugin> </reportPlugins> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> <scope>test</scope> </dependency> </dependencies> </project>
package com.mycompany.sandbox; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; public class IsMultiple extends BaseMatcher<Integer> { private int expectNum; private int actualNum; public static IsMultiple multipleOf(int num) { return new IsMultiple(num); } public IsMultiple(int num) { expectNum = num; } @Override public boolean matches(Object actual) { if (actual == null || !(actual instanceof Integer)) { return false; } actualNum = ((Integer) actual).intValue(); return (actualNum % expectNum == 0); } @Override public void describeTo(Description desc) { desc.appendText(" multiple of "); desc.appendValue(expectNum); } }
package com.mycompany.sandbox; import static com.mycompany.sandbox.IsMultiple.multipleOf; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import org.junit.Test; public class AppTest { @Test public void test() { assertThat(100, is(multipleOf(3))); } }