Skip to content

Instantly share code, notes, and snippets.

@gjb2048
Created February 19, 2015 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gjb2048/d9fcb4e33fddb6a60531 to your computer and use it in GitHub Desktop.
Save gjb2048/d9fcb4e33fddb6a60531 to your computer and use it in GitHub Desktop.
Nibble Of A Byte
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-armhf/
export GRADLE_HOME=~/gradle-2.3
export PATH=$PATH:/usr/lib/jvm/java-7-openjdk-armhf/bin:$GRADLE_HOME/bin
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
/*
* NibbleOfAByte.
* Copyright (C) 2015 Gareth J Barnard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Also see: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-->
<project default="jar" name="NibbleOfAByte" basedir="." xmlns:ivy="antlib:org.apache.ivy.ant">
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<property name="version.num" value="1" />
<buildnumber file="build.num" />
<!-- Standard directory identifiers -->
<property name="java.src.dir" value="${basedir}/src/main/java/" />
<property name="java.test.dir" value="${basedir}/src/test/java/" />
<property name="java.dest.dir" value="${basedir}/build/" />
<property name="java.dist.dir" value="${basedir}/target/" />
<!-- Test -->
<property name="test.lib.dir" value="${basedir}/testlib/" />
<path id="test.class.path">
<pathelement location="${test.lib.dir}/junit-4.12.jar"/> <!-- http://search.maven.org/remotecontent?filepath=junit/junit/4.12/junit-4.12.jar -->
<pathelement location="${test.lib.dir}/JUnitParams-1.0.3.jar"/> <!-- http://search.maven.org/remotecontent?filepath=pl/pragmatists/JUnitParams/1.0.3/JUnitParams-1.0.3.jar -->
<pathelement location="${test.lib.dir}/hamcrest-core-1.3.jar"/> <!-- http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar -->
</path>
<!-- Ivy for test dependencies. -->
<property name="ivy.install.version" value="2.4.0" />
<condition property="ivy.home" value="${env.IVY_HOME}">
<isset property="env.IVY_HOME" />
</condition>
<property name="ivy.home" value="${user.home}/.ant" />
<property name="ivy.jar.dir" value="${ivy.home}/lib" />
<property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />
<property name="ivy.lib.dir" location="${test.lib.dir}/" />
<property name="ivy.dep.file" value="${basedir}/ivy_depend.xml" />
<target name="download-ivy" unless="offline">
<mkdir dir="${ivy.jar.dir}"/>
<get src="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
dest="${ivy.jar.file}" usetimestamp="true"/>
</target>
<target name="init-ivy" depends="download-ivy">
<path id="ivy.lib.path">
<fileset dir="${ivy.jar.dir}" includes="*.jar"/>
</path>
<taskdef resource="org/apache/ivy/ant/antlib.xml"
uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
</target>
<target name="lib-resolve" description="Retrieve dependencies with ivy" depends="init-ivy">
<ivy:retrieve />
</target>
<!-- Build and test targets. -->
<target name="init" depends="lib-resolve">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile. -->
<mkdir dir="${java.dest.dir}"/>
</target>
<!-- Compile. -->
<target name="compile" depends="init"
description="Compile the source." >
<!-- Compile the java code from ${java.src.dir} into ${java.dest.dir} -->
<javac srcdir="${java.src.dir}" destdir="${java.dest.dir}" includeantruntime="false"/>
</target>
<target name="test-compile" depends="compile"
description="Compile the test." >
<!-- Compile the java code from ${java.test.dir} into ${java.dest.dir} -->
<javac srcdir="${java.test.dir}" destdir="${java.dest.dir}" includeantruntime="false" classpathref="test.class.path"/>
</target>
<!-- Test. -->
<target name="test" depends="test-compile">
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${java.test.dir}"/>
<pathelement path="${java.dest.dir}/"/>
<path refid="test.class.path" />
</classpath>
<formatter type="plain"/>
<test name="nibbleofabyte.NibbleOfAByteJUnitParamsTest"/>
<test name="nibbleofabyte.NibbleOfAByteNibbleParameterizedTest"/>
</junit>
</target>
<!-- Package. -->
<target name="jar" depends="compile, test">
<jar destfile="${java.dist.dir}NibbleOfAByte-${version.num}-Ant.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="nibbleofabyte.NibbleOfAByteMain" />
<attribute name="Class-Path" value="." />
<attribute name="Built-Date" value="${TODAY}" />
<attribute name="Implementation-Title" value="Nibble of a byte" />
<attribute name="Implementation-Version" value="${version.num}-b${build.number}" />
<attribute name="Built-By" value="G J Barnard" />
</manifest>
<fileset dir="${java.dest.dir}" />
</jar>
</target>
<!-- Execute. -->
<target name="run" depends="jar">
<java jar="${java.dist.dir}NibbleOfAByte-${version.num}-Ant.jar" fork="true" />
</target>
<!-- Clean. -->
<target name="clean"
description="clean up" >
<delete dir="${java.dest.dir}"/>
<delete dir="${java.dist.dir}"/>
<delete dir="${test.lib.dir}"/>
</target>
</project>
/*
* NibbleOfAByte.
* Copyright (C) 2015 Gareth J Barnard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Also see: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// https://gradle.org/docs/current/userguide/tutorial_java_projects.html
apply plugin: 'java'
// http://gradle.org/docs/current/userguide/application_plugin.html
apply plugin:'application'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'pl.pragmatists', name: 'JUnitParams', version: '1.0.3'
}
sourceCompatibility = '1.7'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
version = '1-Gradle'
mainClassName = "nibbleofabyte.NibbleOfAByteMain"
def builtDate = new Date().format("yyyy-MM-dd HH:mm:ss")
println 'Built date: ' + builtDate
jar {
manifest {
attributes 'Implementation-Title': 'Nibble of a byte',
'Implementation-Version': version,
'Main-Class': mainClassName,
'Class-Path': '.',
'Built-Date': builtDate,
'Built-By': 'G J Barnard'
}
}
uploadArchives {
repositories {
flatDir {
dirs 'target'
}
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
/*
* NibbleOfAByte.
* Copyright (C) 2015 Gareth J Barnard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Also see: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-->
<ivy-module version="2.0">
<info organisation="gjbarnardtube" module="NibbleOfAByte"/>
<dependencies>
<dependency org="junit" name="junit" rev="4.12"/>
<dependency org="pl.pragmatists" name="JUnitParams" rev="1.0.3"/>
</dependencies>
</ivy-module>
/*
* NibbleOfAByte.
* Copyright (C) 2015 Gareth J Barnard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Also see: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
package nibbleofabyte;
import javax.xml.bind.DatatypeConverter;
public class NibbleOfAByte
{
private final Byte aByte;
public NibbleOfAByte(byte number)
{
aByte = number;
}
public String getByte()
{
String retr;
if (aByte < 0)
{
retr = String.valueOf(aByte & 0xFF);
}
else
{
retr = aByte.toString();
}
return retr;
}
public String getNibbles()
{
byte[] byteIn = {aByte};
String hex = DatatypeConverter.printHexBinary(byteIn);
return "Higher nibble: " + hex.charAt(0) + ", lower nibble: " + hex.charAt(1);
}
}
/*
* NibbleOfAByte.
* Copyright (C) 2015 Gareth J Barnard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Also see: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
package nibbleofabyte;
import junitparams.JUnitParamsRunner;
import static junitparams.JUnitParamsRunner.$;
import junitparams.Parameters;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
/**
* Ref: http://www.javacodegeeks.com/2013/12/parameterized-junit-tests-with-junitparams.html
*/
@RunWith(JUnitParamsRunner.class)
public class NibbleOfAByteJUnitParamsTest {
public NibbleOfAByteJUnitParamsTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
@Parameters(method = "nibbleOfAByteTestValues")
public void nibbleOfAByteTest(byte theByte, String theString) {
NibbleOfAByte us = new NibbleOfAByte(theByte);
String usByte = us.getByte();
assertTrue (theByte + " - " + usByte + " - " + theString, usByte.compareTo(theString) == 0);
}
private Object nibbleOfAByteTestValues() {
return $($(value(20), "20"),
$(value(88), "88"),
$(value(109), "109"),
$(value(127), "127"),
$(value(180), "180"),
$(value(200), "200"));
}
@Test
@Parameters(method = "nibbleOfAByteNibbleTestValues")
public void nibbleOfAByteNibbleTest(byte theByte, String theString) {
NibbleOfAByte us = new NibbleOfAByte(theByte);
String usNibbles = us.getNibbles();
assertTrue (theByte + " - " + usNibbles + " - " + theString, usNibbles.compareTo(theString) == 0);
}
private Object nibbleOfAByteNibbleTestValues() {
return $($(value(180), "Higher nibble: B, lower nibble: 4"),
$(value(200), "Higher nibble: C, lower nibble: 8"));
}
private static byte value(int value) {
return (byte) value;
}
}
/*
* NibbleOfAByte.
* Copyright (C) 2015 Gareth J Barnard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Also see: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
package nibbleofabyte;
public class NibbleOfAByteMain
{
private final String name = "Java program on a Raspberry Pi in a Jar - the byte is: ";
private final NibbleOfAByte theByte;
public static void main(String args[])
{
NibbleOfAByteMain us = new NibbleOfAByteMain();
System.out.println(us.getName());
System.out.println(us.getNibbles());
}
public NibbleOfAByteMain()
{
theByte = new NibbleOfAByte((byte) 180);
}
public String getName()
{
return name + theByte.getByte() + '.';
}
public String getNibbles()
{
return theByte.getNibbles() + '.';
}
}
/*
* NibbleOfAByte.
* Copyright (C) 2015 Gareth J Barnard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Also see: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
package nibbleofabyte;
import java.util.Arrays;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
/**
*
* @author Gareth
*/
@RunWith(Parameterized.class)
public class NibbleOfAByteNibbleParameterizedTest {
private final byte theByte;
private final String theString;
public NibbleOfAByteNibbleParameterizedTest(byte theByte, String theString) {
this.theByte = theByte;
this.theString = theString;
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void nibbleOfAByteNibbleTest() {
NibbleOfAByte us = new NibbleOfAByte(theByte);
String usNibbles = us.getNibbles();
assertTrue (theByte + " - " + usNibbles + " - " + theString, usNibbles.compareTo(theString) == 0);
}
@Parameterized.Parameters
public static Iterable data() {
return Arrays.asList(
new Object[][]{
{value(180), "Higher nibble: B, lower nibble: 4"},
{value(200), "Higher nibble: C, lower nibble: 8"}
}
);
}
private static byte value(int value) {
return (byte) value;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
/*
* NibbleOfAByte.
* Copyright (C) 2015 Gareth J Barnard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Also see: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-->
<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>gjbarnardtube</groupId>
<artifactId>NibbleOfAByte</artifactId>
<version>1-Maven</version>
<packaging>jar</packaging>
<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.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>package-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>nibbleofabyte.NibbleOfAByteMain</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>target/NibbleOfAByte-1-Maven.jar</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
rootProject.name = 'NibbleOfAByte'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment