Tuesday, December 01, 2020

Parameterized Tests in JUnit 5

A parameterized test allows you to run a test against a varying set of data. If you find yourself calling the same test but with different inputs, over and over again, a parameterized test would help make your code cleaner. To create one in JUnit 5 you need to:

  • Annotate the test method with @ParameterizedTest
  • Annotate the test method with at lease one source e.g. @ValueSource
  • Consume the arguments in the test method

The sections below describe some of the commonly used source annotations you can use to provide inputs to your test methods.

@ValueSource
This annotation lets you specify a single array of literal values that will be passed to your test method one by one, as shown in the example below:

@ParameterizedTest
@ValueSource(ints = {2, 4, 6})
void testIsEven(final int i) {
  assertTrue(i % 2 == 0);
}

@CsvSource
This annotation allows you to specify an array of comma-separated values, which is useful if your test method takes multiple arguments. If you have a large number of arguments, you can use an ArgumentsAccessor to extract the arguments as opposed to creating a method with a long parameter list. For example:

@ParameterizedTest(name = "Person with name {0} and age {1}")
@CsvSource({ "Alice, 28",
             "Bob, 30" })
void testPerson(final String name, final int age) {
  final Person p = new Person(name, age);
  assertThat(p.getName(), is(name));
  assertThat(p.getAge(), is(age));
}

@ParameterizedTest(name = "Person with name {0} and age {1}")
@CsvSource({ "Alice, 28",
             "Bob, 30" })
void testPersonWithArgumentAccessor(final ArgumentsAccessor arguments) {
  final String name = arguments.getString(0);
  final int age = arguments.getInteger(1);
  final Person p = new Person(name, age);
  assertThat(p.getName(), is(name));
  assertThat(p.getAge(), is(age));
}

By the way, note how I have also customised the display name of the test using the {0} and {1} argument placeholders.

@CsvFileSource
This annotation is similar to CsvSource but allows you to load your test inputs from a CSV file on the classpath. For example:

@ParameterizedTest(name = "Person with name {0} and age {1}")
@CsvFileSource(resources = { "data.csv" })
void testPerson(final String name, final int age) {
  final Person p = new Person(name, age);
  assertThat(p.getName(), is(name));
  assertThat(p.getAge(), is(age));
}

@MethodSource
This annotation allows you to specify a factory method which returns a stream of objects to be passed to your test method. If your test method has multiple arguments, your factory method should return a stream of Arguments instances as shown in the example below:

import static org.junit.jupiter.params.provider.Arguments.*;

@ParameterizedTest(name = "{0} is sorted to {1}")
@MethodSource("dataProvider")
void testSort(final int[] input, final int[] expected) {
  Arrays.sort(input);
  assertArrayEquals(expected, input);
}

static Stream<Arguments> dataProvider() {
  return Stream.of(
      arguments(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 }),
      arguments(new int[] { 3, 2, 1 }, new int[] { 1, 2, 3 }),
      arguments(new int[] { 5, 5, 5 }, new int[] { 5, 5, 5 }));
}

For more information, see the JUnit 5 User Guide on Parameterized Tests.

If you're still on JUnit 4 (why?!), check out my previous post on Parameterized Tests in JUnit 4.

Friday, November 20, 2020

Kdb+/q - File Compression

Large tables can be compressed in Kdb+ by setting .z.zd. Compression of data can reduce disk cost and in some cases even improve performance for applications that have fast CPUs but slow disks.

.z.zd is a list of three integers consisting of logical block size, algorithm (0=none, 1=q, 2=gzip, 3=snappy, 4=lz4hc) and compression level.

Here is an example showing how to compress a table:

// Helper function that sets .z.zd and
// returns the previous value of .z.zd
.util.setZzd:{
  origZzd:$[count key `.z.zd;.z.zd;()];
  if[x~();
    system"x .z.zd";
    :origZzd;
  ];
  .z.zd:x;
  origZzd}

// create a table
td:([]a:1000000?10; b:1000000?10; c:1000000?10);

// save the table to disk without compression
`:uncompressed set td;

// save the table to disk using q IPC compression
origZzd:.util.setZzd[(17;1;0)];
`:compressed set td;
.util.setZzd[origZzd];

You can check compression stats by using the -21! function:

q)-21!`:compressed
compressedLength  | 5747890
uncompressedLength| 24000041
algorithm         | 1i
logicalBlockSize  | 17i
zipLevel          | 0i

The size of the file on disk is reduced from 22.8 MB to 5.5 MB after using q IPC compression.

Thursday, November 19, 2020

Testing Expected Exceptions with JUnit 5

This post shows how to test for expected exceptions using JUnit 5. If you're still on JUnit 4, please check out my previous post.

Let's start with the following class that we wish to test:

public class Person {
  private final String name;
  private final int age;
    
  /**
   * Creates a person with the specified name and age.
   *
   * @param name the name
   * @param age the age
   * @throws IllegalArgumentException if the age is not greater than zero
   */
  public Person(String name, int age) {
    this.name = name;
    this.age = age;
    if (age <= 0) {
      throw new IllegalArgumentException("Invalid age:" + age);
    }
  }
}

To test that an IllegalArgumentException is thrown if the age of the person is less than zero, you should use JUnit 5's assertThrows as shown below:

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class PersonTest {

  @Test
  void testExpectedException() {
    assertThrows(IllegalArgumentException.class, () -> {
      new Person("Joe", -1);
    });
  }

  @Test
  void testExpectedExceptionMessage() {
    final Exception e = assertThrows(IllegalArgumentException.class, () -> {
      new Person("Joe", -1);
    });
    assertThat(e.getMessage(), containsString("Invalid age"));
  }
}

Related post: Testing Expected Exceptions with JUnit 4 Rules

Tuesday, October 13, 2020

Java 15: Sealed Classes

Java 15 introduces Sealed Classes, a preview language feature, that allows classes/interfaces to restrict which other classes/interfaces may extend or implement them. Here is an example:

public sealed class Vehicle permits Car, Truck, Motorcycle { ... }

final class Car extends Vehicle { ... }
final class Truck extends Vehicle { ... }
final class Motorcycle extends Vehicle { ... }

In the example above, Vehicle is a sealed class, which specifies three permitted subclasses; Car, Truck and Motorcycle.

The subclasses must be:

  • in the same package or module as the superclass. You can even define them in the same source file as the superclass (if they are small in size), in which case the permits clause is not required because the compiler will infer them from the declarations in the file.
  • declared either final (i.e. cannot be extended further), sealed (i.e. permit further subclasses in a restricted fashion) or non-sealed (i.e. open for extension by any class).

Sealing serves two main purposes:

  1. It restricts which classes or interfaces can be a subtype of a class or interface and thus preserves the integrity of your API.
  2. It allows the compiler to list all the permitted subtypes of a sealed type (exhaustiveness analysis), which will (in a future Java release) enable switching over type patterns in a sealed type (and other features). For example, given the following switch statement, the compiler will detect that there is a case statement for every permitted subclass of Vehicle (so no default clause is needed) and it will also give an error if any of them are missing:
    int doSomething(Vehicle v) {
      return switch (v) {
          case Car c -> ...
          case Truck t -> ...
          case Motorcycle m -> ...
      };
    }

Monday, July 20, 2020

kdb+/q - Try Catch

Programming languages typically have a try-catch mechanism for dealing with exceptions. The try block contains the code you want to execute and the catch block contains the code that will be executed if an error occurs in the try block.

Here is an example of a simple try-catch block in Java, which attempts to parse a string into an int and returns -1 if there is an error.

try {
    return Integer.parseInt(x);
} catch (NumberFormatException e) {
    e.printStackTrace();
    return -1;
}

In this post, I will describe the try-catch equivalent for exception handling in the q programming language.

.Q.trp[f;x;g] - for unary functions

For unary functions, you can use .Q.trp (Extend Trap), which takes three arguments:

  1. f - a unary function to execute
  2. x - the argument of f
  3. g - a function to execute if f fails. This function is called with two arguments, the error string x and the backtrace object y

For example:

// Define a function which casts a string to int
parseInt:{[x] "I"$x}

// Define an error function which prints the stack trace and returns -1
// Note: .Q.sbt formats the backtrace object and 2@ prints to stderr
g:{[x;y] 2@"Error: ",x,"\nBacktrace:\n",.Q.sbt y;-1i}

// Try calling the function (wrapped by .Q.trp) with a valid argument
.Q.trp[parseInt;"123";g]
123i

// Try calling the function (wrapped by .Q.trp) with an invalid argument
// The error function is called and the stack trace is printed
.Q.trp[parseInt;`hello;g]
Error: type
Backtrace:
  [2]  parseInt:{[x] "I"$x}
                        ^
  [1]  (.Q.trp)

  [0]  .Q.trp[parseInt;`hello;g]
       ^
-1i

Note: An alternative is to use Trap At which has syntax @[f;x;e] but you won't get the backtrace, so it's better to use .Q.trp.

.[f;args;e] - for n-ary functions

.Q.trp only works for unary functions. For functions with more than one argument, you need to use Trap which has the syntax .[f;args;e]. The error function e does not take any arguments, which means no backtrace available. For example:

// Define a ternary function that sums its arguments
add:{[x;y;z] x+y+z}

.[add;1 2 3;{2@"Failed to perform add";-1}]
6

.[add;(1;2;`foo);{2@"Failed to perform add\n";-1}]
Failed to perform add
-1

Friday, July 10, 2020

Compute MD5 Checksum Hash on Windows and Linux

Use the following commands to print out the MD5 hash for a file.

On Windows:

> CertUtil -hashfile myfile.txt MD5
MD5 hash of file myfile.txt:
76383c2c0bfca944b57a63830c163ad2
CertUtil: -hashfile command completed successfully.

On Linux/Unix:

$ md5sum myfile.txt
76383c2c0bfca944b57a63830c163ad2 *myfile.txt

Sunday, May 17, 2020

Java 14: Helpful NullPointerException Messages

A new JVM option, -XX:+ShowCodeDetailsInExceptionMessages, has been introduced in Java 14, in order to provide helpful NullPointerException messages showing precisely what was null when a NullPointerException occurred. For example, consider the code below:

var name = library.get("My Book").getAuthor().getName();

Before Java 14, the JVM would only print the method, filename, and line number that caused the NPE:

Exception in thread "main" java.lang.NullPointerException
 at Library.main(Library.java:7)

As you can tell, this error message is not very useful because it is impossible to determine which variable was actually null (without using a debugger). Was it the library, the book returned from the library, or the author of the book?

In Java 14, after enabling -XX:+ShowCodeDetailsInExceptionMessages, you will get the following message:

Exception in thread "main" java.lang.NullPointerException: 
Cannot invoke "Author.getName()" because the return value of "Book.getAuthor()" is null
 at Library.main(Library.java:7)

The exception message pinpoints what was null (Book.getAuthor()) and also displays the action that could not be performed as a result of this (Author.getName()).

Monday, April 13, 2020

Java 14: Pattern Matching for instanceof

Java 14 introduces Pattern Matching for instanceof, another preview language feature, that eliminates the need for casts when using instanceof. For example, consider the following code:

if (obj instanceof String) {
    String s = (String) obj;
    System.out.println(s.length());
}

This code can now be rewritten as:

if (obj instanceof String s) {
    System.out.println(s.length());
}

As shown above, the instanceof operator now takes a "binding variable" and the cast to String is no longer required. If obj is an instance of String, then it is cast to String and assigned to the binding variable s. The binding variable is only in scope in the true block of the if-statement.

In particular, this feature makes equals methods a lot more concise as shown in the example below:

@Override
public boolean equals(Object obj) { 
  return this == obj || 
    (obj instanceof Person other) && other.name.equals(name);
}

This feature is an example of pattern matching, which is already available in many other programming languages, and allows us to conditionally extract components from objects. It opens the door for more general pattern matching in the future which I am very excited about!

Saturday, April 11, 2020

Java 14: Records

Java 14 arrived a few weeks ago and introduces the Record type, which is an immutable data carrier class designed to hold a fixed set of fields. Note that this is a preview language feature, which means that it must be explicitly enabled in the Java compiler and runtime using the --enable-preview flag.

I'm going to jump straight in with an example of a Book record designed to hold the title, author, publish date and price of a book. This is how the record class is declared:

public record Book(String title, String author, LocalDate publishDate, double price) {
}

You can use javap to see the code that the compiler has autogenerated:

public final class Book extends java.lang.Record {
  public Book(java.lang.String, java.lang.String, java.time.LocalDate, double);
  public java.lang.String title();
  public java.lang.String author();
  public java.time.LocalDate publishDate();
  public double price();
  public java.lang.String toString();
  public final int hashCode();
  public final boolean equals(java.lang.Object);
}

As shown above, the compiler has automatically generated the constructor, getter methods, hashCode, equals and toString, thus saving us from having to type a lot of boilerplate code.

However, records do not just save on typing. They also make your intent clear that you want to model an immutable data item as a group of related fields.

Compact Constructors for Field Validation

Now let's say that you want to add validation and default values to your record. For example, you might want to validate that Book records are not created with negative prices or future publish dates. This can be done with a compact constructor as shown below:

public record Book(String title, String author, LocalDate publishDate, double price) {

  //compact constructor (no parameter list), used for validation and setting defaults
  public Book {
    if (price < 0.0) {
      throw new IllegalArgumentException("price must be positive");
    }
    if (publishDate != null && publishDate.isAfter(LocalDate.now())) {
      throw new IllegalArgumentException("publishDate cannot be in the future");
    }
    this.author = author == null ? "Unknown" : author;
  }
}

The compact constructor does not have a parameter list. It validates the price and publish date, and also sets a default value for the author. The fields that have not been assigned in this constructor (i.e. title, publishDate and price) are implicitly initialised at the end of this constructor.

Alternative Constructors and Additional Methods

Records allow you to define additional methods, constructors, and static fields, as shown in the code below. However, remember that semantically a record is designed to be a data carrier, so if you feel that are adding extra methods, it might be that you need a class instead of a record.

public record Book(String title, String author, LocalDate publishDate, double price) {

  // static field
  private static final String UNKNOWN_AUTHOR = "UNKNOWN";

  // compact constructor, used for validation and setting defaults
  public Book {
    if (price < 0) {
      throw new IllegalArgumentException("price must be positive");
    }
    if (publishDate != null && publishDate.isAfter(LocalDate.now())) {
      throw new IllegalArgumentException("publishDate cannot be in the future");
    }
    this.author = author == null ? UNKNOWN_AUTHOR : author;
  }

  // static factory constructor
  public static Book freeBook(String title, String author, LocalDate publishDate) {
    return new Book(title, author, publishDate, 0.0);
  }

  // alternative constructor, without an author
  public Book(String title, LocalDate publishDate, double price) {
    this(title, null, publishDate, price);
  }

  // additional method to get the year of publish
  public int publishYear() {
    return publishDate.getYear();
  }

  // override toString to make it more user friendly
  @Override
  public String toString() {
    return String.format("%s (%tY) by %s for £%.2f", title, publishDate, author, price);
  }
}

Saturday, February 29, 2020

FFmpeg Cheatsheet

FFmpeg is a great command-line tool for dealing with audio and video files. Here are some useful commands:

1. Reduce the size of a file

Try passing the file straight through ffmpeg and check if the size reduces:

ffmpeg -i input.mp4 output.mp4

To reduce the size further, scale the video to half the width and height:

ffmpeg -i input.mp4 -vf "scale=iw/2:ih/2" output.mp4
2. Convert a MOV file to MP4
ffmpeg -i input.mov -vcodec h264 -acodec aac -strict -2 out.mp4
3. Create a video from an image by panning across it

If you have a landscape image, first set its height to 1600px, preserving aspect ratio (using an image editor such as IrfanView). Then run the following command to create a video which pans across the image from left to right:

ffmpeg -loop 1 -i input.jpg -vf crop=1200:ih:'min((iw/10)*t,9*iw/10)':0 -t 5 out.mp4

Wednesday, January 01, 2020

fahd.blog in 2019

Happy 2020, everyone!

I'd like to wish everyone a great start to an even greater new year!

In keeping with tradition, here's one last look back at fahd.blog in 2019.

During 2019, I posted 7 new entries on fahd.blog. I am also thrilled that I have more readers from all over the world! Thanks for reading and especially for giving feedback.

Top 3 posts of 2019:

I'm going to be writing a lot more this year, so stay tuned for more great techie tips, tricks and hacks! :)

Related posts: