diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2017-02-20 10:33:44 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2017-02-20 10:33:44 +0100 |
commit | 25bf80f2c6c3c6784f78d9d41b5e38a5273a00fe (patch) | |
tree | e0125ce43205e48c0b14faacca248e58037e6479 /java | |
parent | 348663665082107181968a76fd14c2491b4f51e2 (diff) | |
download | share-25bf80f2c6c3c6784f78d9d41b5e38a5273a00fe.zip share-25bf80f2c6c3c6784f78d9d41b5e38a5273a00fe.tar.gz |
java : add project (ant,ivy,junit,logback)
Diffstat (limited to 'java')
-rw-r--r-- | java/project/build.xml | 170 | ||||
-rw-r--r-- | java/project/ivy.xml | 26 | ||||
-rw-r--r-- | java/project/ivysettings.xml | 18 | ||||
-rw-r--r-- | java/project/src/main/ch/asynk/Main.java | 22 | ||||
-rw-r--r-- | java/project/src/main/ch/asynk/MyClass.java | 25 | ||||
-rw-r--r-- | java/project/src/main/ch/asynk/MyException.java | 11 | ||||
-rw-r--r-- | java/project/src/resources/Application.properties | 3 | ||||
-rw-r--r-- | java/project/src/resources/logback-test.xml | 34 | ||||
-rw-r--r-- | java/project/src/test/java/ch/asynk/Test00.java | 54 | ||||
-rw-r--r-- | java/project/src/test/java/ch/asynk/TestSuite.java | 27 |
10 files changed, 390 insertions, 0 deletions
diff --git a/java/project/build.xml b/java/project/build.xml new file mode 100644 index 0000000..0134266 --- /dev/null +++ b/java/project/build.xml @@ -0,0 +1,170 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns:ivy="antlib:org.apache.ivy.ant" + name="MyProject" + basedir="." + default="test"> + + <target name="configure"> + <property file="${basedir}/src/resources/Application.properties"/> + <property name="src.dir" value="${basedir}/src" /> + <property name="build.dir" value="${basedir}/build"/> + <property name="dist.dir" value="${basedir}/dist"/> + <property name="doc.dir" value="${basedir}/doc"/> + + <property name="main.dir" value="${src.dir}/main" /> + <property name="test.dir" value="${src.dir}/test" /> + <property name="resources.dir" value="${src.dir}/resources"/> + <property name="classes.dir" value="${build.dir}/classes"/> + + <property name="class.main" value="ch.asynk.Main"/> + <property name="jar.name" value="${dist.dir}/myjar-${app.version}"/> + + <property name="compile.debug" value="true"/> + <property name="compile.deprecation" value="false"/> + <property name="compile.optimize" value="true"/> + <property name="signatures.remove" value="true"/> + + <path id="cp.build"> + <dirset dir="${classes.dir}" /> + </path> + <path id="cp.resources"> + <dirset dir="${resources.dir}" /> + </path> + <condition property="filter.sign.out"> + <istrue value="${signatures.remove}"/> + </condition> + + </target> + + <target name="resolve" description="retrieve dependencies with Ivy"> + <ivy:resolve file="ivy.xml" log="download-only"/> + <ivy:cachepath pathid="ivy.deps.compile" conf="compile" /> + <ivy:cachepath pathid="ivy.deps.runtime" conf="runtime" /> + <ivy:cachepath pathid="ivy.deps.test" conf="test" /> + <ivy:cachefileset setid="ivy.deps.runtime.fileset" conf="runtime"/> + </target> + + <target name="clean" depends="configure" description="clean project"> + <delete dir="${build.dir}" /> + <delete dir="${dist.dir}" /> + <delete file="${app.logfile}" /> + <delete file="build.log" /> + <delete file="test.log" /> + </target> + + <target name="ctags"> + <exec executable="ctags" failonerror="true"> + <arg value="-R"/> + <arg value="--language-force=java"/> + <arg value="-f.tags"/> + <arg value="${src.dir}"/> + </exec> + </target> + + <target name="compile" depends="configure,resolve,ctags"> + <record name="build.log" loglevel="verbose" action="start" /> + <mkdir dir="${classes.dir}/main" /> + <javac srcdir="${main.dir}" + destdir="${classes.dir}/main" + debug="${compile.debug}" + deprecation="${compile.deprecation}" + optimize="${compile.optimize}" + includeantruntime="false"> + <classpath refid="ivy.deps.compile"/> + <compilerarg value="-Xlint:all"/> + <compilerarg value="-Xlint:-path"/> + <compilerarg value="-Xlint:-processing"/> + <compilerarg value="-Xmaxerrs"/> + <compilerarg value="10"/> + </javac> + <mkdir dir="${classes.dir}/test" /> + <javac srcdir="${test.dir}" + destdir="${classes.dir}/test" + debug="${compile.debug}" + deprecation="${compile.deprecation}" + optimize="${compile.optimize}" + includeantruntime="false"> + <classpath refid="cp.build"/> + <classpath refid="ivy.deps.compile"/> + <compilerarg value="-Xlint:all"/> + <compilerarg value="-Xlint:-path"/> + <compilerarg value="-Xlint:-processing"/> + <compilerarg value="-Xmaxerrs"/> + <compilerarg value="10"/> + </javac> + </target> + + <target name="test" description="run JUnit tests" depends="compile"> + <junit fork="true" dir="${basedir}" failureProperty="test.failed" printsummary="yes" haltonfailure="no"> + <jvmarg value="-Xmx600M"/> + <classpath refid="cp.build"/> + <classpath refid="ivy.deps.test"/> + <formatter type="plain" usefile="true" extension=".log"/> + <test name = "ch.asynk.TestSuite" todir="." outfile="test"/> + </junit> + <fail message="-- FAILED --" if="test.failed" /> + </target> + + <target name="-jar.deps" if="filter.sign.out"> + <jar jarfile="${jar.name}-deps.jar"> + <zipgroupfileset refid="ivy.deps.runtime.fileset"/> + </jar> + </target> + + <target name="-jar.static" depends="compile"> + <manifestclasspath property="cp.manifest" jarfile="${jar.name}-static-cp.jar" maxParentLevels="10"> + <classpath refid="ivy.deps.runtime"/> + </manifestclasspath> + <jar jarfile="${jar.name}-static-cp.jar" basedir="${classes.dir}/main"> + <manifest> + <attribute name="Main-Class" value="${class.main}"/> + <attribute name="Class-Path" value="${cp.manifest}"/> + </manifest> + <fileset dir="${src.dir}/resources"/> + </jar> + </target> + + <target name="-jar.fat" depends="compile,-jar.deps"> + <jar jarfile="${jar.name}-fat.jar" basedir="${classes.dir}/main"> + <manifest> + <attribute name="Main-Class" value="${class.main}"/> + <attribute name="Class-Path" value="."/> + </manifest> + <fileset dir="${src.dir}/resources"/> + <!-- FIXES Invalid signature file digest for Manifest main attributes --> + <!-- TODO use filter.sign.out --> + <zipfileset src="${jar.name}-deps.jar" excludes="META-INF/*.SF" /> + <!-- <zipgroupfileset refid="ivy.deps.runtime.fileset"/> --> + </jar> + <!-- FIXES Invalid signature file digest for Manifest main attributes --> + <!-- <exec executable="zip"> --> + <!-- <arg value="-d"/> --> + <!-- <arg value="${jar.name}-fat.jar"/> --> + <!-- <arg value="META-INF/*.SF"/> --> + <!-- </exec> --> + </target> + + <target name="-jar.src" depends="compile"> + <jar jarfile="${jar.name}-src.jar" basedir="${basedir}" excludes="build/** dist/** *.log .tags" /> + </target> + + <target name="-jar.dir" depends="configure"> + <mkdir dir="${dist.dir}" /> + </target> + + <target name="dist" description="generate the distribution" depends="-jar.dir,-jar.static,-jar.fat,-jar.src"/> + + <target name="run" description="execute built main Class" depends="compile"> + <java classname="${class.main}" fork="true"> + <classpath refid="cp.build"/> + <classpath refid="cp.resources"/> + <classpath refid="ivy.deps.runtime"/> + </java> + </target> + + <target name="run.jar" description="execute built jars" depends="dist"> + <java jar="${jar.name}-static-cp.jar" fork="true"/> + <java jar="${jar.name}-fat.jar" fork="true"/> + </target> + +</project> diff --git a/java/project/ivy.xml b/java/project/ivy.xml new file mode 100644 index 0000000..08505ed --- /dev/null +++ b/java/project/ivy.xml @@ -0,0 +1,26 @@ +<?xml version="1.0"?> +<!DOCTYPE ivy-module [ + <!ENTITY junit.version "4.12"> +]> +<ivy-module version="2.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd"> + + <info organisation="ch.asynk" module="AntIvyJUnit"/> + + <configurations defaultconf="test" defaultconfmapping="default"> + <conf name="compile" description="Required to compile application"/> + <conf name="runtime" description="Additional run-time dependencies"/> + <conf name="test" description="Required to compile and run tests"/> + </configurations> + + <dependencies defaultconf="compile" defaultconfmapping="compile->default"> + <dependency org="org.projectlombok" name="lombok" rev="1.16.14"/> + <dependency org="junit" name="junit" rev="&junit.version;" conf="compile,test->default"/> + <dependency org="org.hamcrest" name="hamcrest-library" rev="1.3" conf="compile,test->default"/> + <dependency org="org.slf4j" name="slf4j-api" rev="1.7.23" conf="compile,runtime->default"/> + <dependency org="org.codehaus.janino" name="janino" rev="3.0.6" conf="runtime->default"/> + <dependency org="ch.qos.logback" name="logback-classic" rev="1.2.1" conf="runtime->default"/> + </dependencies> + +</ivy-module> diff --git a/java/project/ivysettings.xml b/java/project/ivysettings.xml new file mode 100644 index 0000000..3e44ce3 --- /dev/null +++ b/java/project/ivysettings.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ivysettings> + <settings defaultResolver="default" /> + <!-- <caches defaultCacheDir="${basedir}/lib" /> --> + <!-- <caches default="mine" defaultCacheDir="${basedir}/ivy"> --> + <!-- <cache name="mine" basedir="${basedir}/ivy" /> --> + <!-- </caches> --> + <resolvers> + <chain name="default"> + <ibiblio name="maven2" m2compatible="true"/> + <ibiblio name="maven" m2compatible="true" usepoms="false"/> + <filesystem name="local"> + <ivy pattern="${user.home}/ivy-local/[revision]/[module]/ivy-[revision].xml"/> + <artifact pattern="${user.home}/ivy-local/[revision]/[module]/[module]-[revision](-[classifier]).[ext]"/> + </filesystem> + </chain> + </resolvers> +</ivysettings> diff --git a/java/project/src/main/ch/asynk/Main.java b/java/project/src/main/ch/asynk/Main.java new file mode 100644 index 0000000..bb187bf --- /dev/null +++ b/java/project/src/main/ch/asynk/Main.java @@ -0,0 +1,22 @@ +package ch.asynk; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Main +{ + private static Logger logger = null; + + public static void main(String [] args ) + { + logger = LoggerFactory.getLogger(Main.class); + System.out.println("System.out"); + System.err.println("System.err"); + logger.trace("trace"); + logger.debug("debug"); + logger.info("info"); + logger.warn("warn"); + logger.error("error"); + System.exit(0); + } +} diff --git a/java/project/src/main/ch/asynk/MyClass.java b/java/project/src/main/ch/asynk/MyClass.java new file mode 100644 index 0000000..57ef46f --- /dev/null +++ b/java/project/src/main/ch/asynk/MyClass.java @@ -0,0 +1,25 @@ +package ch.asynk; + +import lombok.Getter; + +public class MyClass +{ + static private String msg = "Hello World"; + + private @Getter int n; + + public MyClass() + { + n = 666; + } + + public void print() + { + System.err.println(msg); + } + + public void exception() throws MyException + { + throw new MyException("it's mine"); + } +} diff --git a/java/project/src/main/ch/asynk/MyException.java b/java/project/src/main/ch/asynk/MyException.java new file mode 100644 index 0000000..06203ae --- /dev/null +++ b/java/project/src/main/ch/asynk/MyException.java @@ -0,0 +1,11 @@ +package ch.asynk; + +public class MyException extends Exception +{ + private static final long serialVersionUID = 01235; + + public MyException(String message) + { + super(message); + } +} diff --git a/java/project/src/resources/Application.properties b/java/project/src/resources/Application.properties new file mode 100644 index 0000000..34430cd --- /dev/null +++ b/java/project/src/resources/Application.properties @@ -0,0 +1,3 @@ +app.logfile=MyApp.log +app.version=0.0.1 +app.env=DEV diff --git a/java/project/src/resources/logback-test.xml b/java/project/src/resources/logback-test.xml new file mode 100644 index 0000000..7f49e52 --- /dev/null +++ b/java/project/src/resources/logback-test.xml @@ -0,0 +1,34 @@ +<configuration debug="true"> + + <contextName>MyApp</contextName> + <property resource="Application.properties" /> + <property name="LOG_LEVEL" value="INFO" /> + <property name="LOG_REF" value="FILE" /> + <property name="LOG_FILE" value="${app.logfile}" /> + <property name="LOG_PATTERN" value="%contextName %date{ISO8601} [%-5level %thread] [%file:%line] - %msg%n" /> + + <if condition='property("app.env").contains("DEV")'> + <then> + <property name="LOG_LEVEL" value="DEBUG" /> + <property name="LOG_REF" value="STDOUT" /> + </then> + </if> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>${LOG_PATTERN}</pattern> + </encoder> + </appender> + + <appender name="FILE" class="ch.qos.logback.core.FileAppender"> + <file>${LOG_FILE}</file> + <encoder> + <pattern>${LOG_PATTERN}</pattern> + </encoder> + </appender> + + <root level="${LOG_LEVEL}"> + <appender-ref ref="${LOG_REF}" /> + </root> + +</configuration> diff --git a/java/project/src/test/java/ch/asynk/Test00.java b/java/project/src/test/java/ch/asynk/Test00.java new file mode 100644 index 0000000..aae83d7 --- /dev/null +++ b/java/project/src/test/java/ch/asynk/Test00.java @@ -0,0 +1,54 @@ +package ch.asynk; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.BeforeClass; +import org.junit.AfterClass; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.hasProperty; + +import ch.asynk.MyClass; + +public class Test00 +{ + private static MyClass my; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @BeforeClass + public static void setUpClass() + { + System.out.println(" BeforeClass : Test00"); + my = new MyClass(); + } + + @AfterClass + public static void tearDownClass() + { + System.out.println(" AfterClass : Test00"); + } + + @Test + public void test_0() + { + assertEquals("n must be equal to 666", my.getN(), 666); + } + + @Test + public void test_1() throws MyException + { + thrown.expect(MyException.class); + thrown.expectMessage("it's mine"); + my.exception(); + } + @Test + public void test_2() throws MyException + { + assertThat(my, hasProperty("n", is(666))); + } +} diff --git a/java/project/src/test/java/ch/asynk/TestSuite.java b/java/project/src/test/java/ch/asynk/TestSuite.java new file mode 100644 index 0000000..3e5d7d8 --- /dev/null +++ b/java/project/src/test/java/ch/asynk/TestSuite.java @@ -0,0 +1,27 @@ +package ch.asynk; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.BeforeClass; +import org.junit.AfterClass; + +@RunWith(Suite.class) + +@Suite.SuiteClasses({ + Test00.class +}) + +public class TestSuite +{ + @BeforeClass + public static void setUpClass() + { + System.out.println("BeforeClass : TestSuite"); + } + + @AfterClass + public static void tearDownClass() + { + System.out.println("AfterClass : TestSuite"); + } +} |