sebastiandaschner news

saturday, april 29, 2017

Welcome to the fifth newsletter edition!

I’m writing this just after coming back from Oracle Code Prague. The last days were pretty full of exciting conferences. I spoke about the topic of CQRS (Command Query Responsibility Segregation) with Java EE at Oracle Code Toronto, London and Prague. I think the Oracle organizers are doing a great job at these one-day events all across the world — the attendees seem to enjoy it. If you’re curious what CQRS is about you can watch the free video course Event Sourcing, Distributed Systems & CQRS.

Next week I’ll be speaking at Java Day Istanbul and I’ll present the keynote at Oracle Code New Delhi in India on May 10th. Both events will be the first time for me that I’ll visit these countries — so the traveling continues.

In terms of Java content these days are also quite packed.

The next days will bring some Java and Java EE quizzes as part of #100DaysOfJava — stay tuned and follow the @Java twitter handle.


What’s new


Base64-encode data using plain Java 8

Since JDK 1.8 the useful class java.util.Base64 is included that de- and encodes data from and to the Base64 format. That means if your codebase still uses Apache Commons and similar libraries for this purposes you can get rid of them.

Encoding and decoding respectively is done as follows:

byte[] data = "hello".getBytes(StandardCharsets.UTF_8);
String encoded = Base64.getEncoder().encodeToString(data);
assertThat(encoded, is("aGVsbG8="));

byte[] decoded = Base64.getDecoder().decode(encoded);
assertThat(new String(decoded, StandardCharsets.UTF_8), is("hello"));

Always good to see often needed features being added to the JDK!


AssertJ matchers vs. Hamcrest

What you just saw in the Base64 example was a typical usage of Hamcrest matchers which is — at least in my experience — the widest used test matching library. Another great library that comes with convenient fluid interfaces is AssertJ. See the following examples for some inspiration:

String encoded = "aGVsbG8=";

The output for failing assertions can be enriched with descriptions to show the intentions of the test:

String separator = ",";
String list = "value1,value2";
        .describedAs("The configuration list '%s' is empty")
        .describedAs("The list '%s' is not separated by '%s'", list, separator)

AssertJ especially shines when used with collections and Java 8 lambdas.

List<String> coffeeOrigins = asList("Colombia", "Ethiopia", "El Salvador");

        .doesNotContain("Kenya", "Brazil")
        .filteredOn(s -> s.startsWith("E"))

After using both matching libraries — in the past it was Hamcrest in probably 99% of the cases — I like the AssertJ’s API even better. I encourage you to give it a try.


Continuously watch Kubernetes resources

With the Kubernetes command kubectl get …​ you can see the available resources of a specific type, e. g. kubectl get pods. By adding the argument --watch or -w the output is updated continuously: kubectl get pods --watch


Thanks a lot for reading and see you next time — somewhere else on this planet!


Did you like the content? You can subscribe to the newsletter for free: