We can use the flatten
method in Groovy to flatten a collection that contains other collections into a single collection with all elements. We can pass a closure as extra argument to the flatten
method to transform each element that is flattened. The argument of the closure is the element from the original collection.
In the following example we first use the flatten
method without a closure argument. Then we pass a closure argument and transform the element:
Continue reading →
In a previous post we learned about callouts in Asciidoctor to add explanation to source code. While surfing the Internet I came upon the following blog post by Alex Soto: Auto-numbered Callouts in Asciidoctor. I turns out that since Asciidoctor 1.5.8 we can use a dot (.
) instead of explicit numbers to have automatic increasing numbering for the callouts.
Let’s take our example from the earlier blog post and now use auto numbered callouts:
Continue reading →
Using the Stream API and the map
method we can transform elements in a stream to another object. Instead of using the map
method we can also write a custom Collector
and transform the elements when we use the collect
method as terminal operation of the stream.
First we have an example where we transform String values using the map
method:
Continue reading →
If we want to include Asciidoc markup as source language and show the markup without transforming it we can use a listing or literal block. For example we are using Asciidoc markup to write a document about Asciidoctor and want to include some Asciidoc markup examples. If the markup contains sections like a listing or literal block and it is enclosed in a listing or literal block, the tranformation goes wrong. Because the beginning of the included listing or literal block is seen as the ending of the enclosing listing or literal block. Let’s see what goes wrong with an example where we have the following Asciidoc markup:
When we transform this to HTML we get the following output:
Continue reading →
Normally when we run tests in our Gradle build, all our tests are executed and at the end we can see which tests are failing. But what if we want to let the build fail at the first failing test? Especially for a large test suite this can save a lot of time, because we don’t have to run all (failing) tests, we immediately get informed that at least one test is failing.
We can do this by passing the command-line option --fail-fast
when we run the test
task in Gradle. With this option Gradle will stop the build and report a failure at the first failing test. Instead of passing the command-line option --fail-fast
we can set the property failFast
of the test
task to true
. Using the property failFast
allows to still fail the build on the first failing test even if we for example run a build
task that depends on the test
task. The command-line option --fail-fast
only works if we run the test
task directly, not if it is part of the task graph for our build when we run another task.
Continue reading →
In Java we can use a Predicate
to test if something is true
or false
. This is especially useful when we use the filter
method of the Java Stream API. We can use lambda expressions to define our Predicate
or implement the Predicate
interface. If we want to combine different Predicate
objects we can use the or
, and
and negate
methods of the Predicate
interfaces. These are default methods of the interface and will return a new Predicate
.
Let’s start with an example where we have a list of String
values. We want to filter all values that start with Gr or with M. In our first implementation we use a lambda expression as Predicate
and implements both tests in this expression:
Continue reading →
Sometimes when we are developing we might to need to lookup the unicode value for a character. If we are using macOS we can use the Character Viewer to lookup the unicode. We can open the Character Viewer using the key combination ⌃+⌘+Space (Ctrl+Cmd+Space) or open the Edit menu in our application and select Emoji & Symbols. We can type the character we want to unicode value for in the Search box or look it up in the lists. When we select the character we can see at the right the Unicode for that character:
Continue reading →
When we write tests or specifications using Spock for our Spring Boot application, we might want to replace some Spring components with a stub or mock version. With the stub or mock version we can write expected outcomes and behaviour in our specifications. Since Spock 1.2 and the Spock Spring extension we can use the @SpringBean
annotation to replace a Spring component with a stub or mock version. (This is quite similar as the @MockBean
for Mockito mocks that is supported by Spring Boot). We only have to declare a variable in our specification of the type of the Spring component we want to replace. We directly use the Stub()
or Mock()
methods to create the stub or mock version when we define the variable. From now on we can describe expected output values or behaviour just like any Spock stub or mock implementation.
Continue reading →
In a previous post we learned about setting the value of options
attribute to nowrap
on listing and literal blocks, so the lines in the block are not wrapped.
In the comments a user suggested another option to disable line wrapping for all listing and literal blocks in the document by using the document attribute prewrap
.
We must negate the document attribute, :prewrap!:
, to disable all wrapping.
If we place this document attribute at the top of our Asciidoctor document it is applied for the whole document.
We can also place it at other places in the document to change the setting for all listing and literal blocks following the prewrap
document attribute.
To enable wrapping again we set :prewrap:
(leaving out the exclamation mark).
In the following example we have markup with a listing, literal and example block and we use the document attribute :prewrap!:
to disable the wrapping for the listing and literal blocks:
Continue reading →
Since Gradle 5 we can easily use a bill of materials (BOM) in our build file to get recommended dependency versions. The dependency versions defined in the BOM are dependency constraints in Gradle. This means the dependencies we define in our build that are part of the BOM don’t need a version, because the version is resolved via the dependency constraint that is defined in the BOM. Also transitive dependency versions are resolved using the BOM if applicable. We use the dependency handler method platform
to define the BOM we want to import. The versions in the BOM are recommendations. We can override the recommendation by specifying the version for a dependency found in the BOM with an explicit version.
Continue reading →
From Maven builds we know the dependencyManagement
section in our POM file. In the section we can describe dependencies with their version and later in the dependencies
section we can refer to the dependency without the version. We can use dependency constraints in Gradle to do the same thing. A dependency constraint can be used to define the version or version range for a dependency defined in our scripts or a transitive dependency. Just like a dependency the dependency constraint is defined for a configuration, so we can fine tune the constraints to the correct configuration.
Using dependency constraints in a multi-project build allows us to define the dependency versions in the root build file and define project dependencies per project without a version. The version will then be used from the dependency constraint we defined in the root build file.
Continue reading →
Since Asciidoctor 2.0.0 we can add the collapsible
option to an example block. When the markup is generated to HTML we get a HTML details
and summary
section. The content of the example block is collapsed (default behaviour because it is in a details
section) and a clickable text is available to open the collapsed block (the summary
section), so we can see the actual content. The text we can click on is by default Details, but we can change that by setting the title of the example block. Then the title is used as the text to click on to open the collapsed content.
Continue reading →