sebastiandaschner news
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=";
assertThat(encoded)
.startsWith("aGV")
.doesNotContain("!")
.endsWith("=");
The output for failing assertions can be enriched with descriptions to show the intentions of the test:
String separator = ",";
String list = "value1,value2";
assertThat(list)
.describedAs("The configuration list '%s' is empty")
.isNotEmpty()
.describedAs("The list '%s' is not separated by '%s'", list, separator)
.contains(separator);
AssertJ especially shines when used with collections and Java 8 lambdas.
List<String> coffeeOrigins = asList("Colombia", "Ethiopia", "El Salvador");
assertThat(coffeeOrigins)
.isSubsetOf(coffeeProducers)
.doesNotContain("Kenya", "Brazil")
.hasSize(3)
.filteredOn(s -> s.startsWith("E"))
.hasSize(2);
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:
All opinions are my own and do not reflect those of my employer or colleagues.