I don't know if it is because I am approaching my 33rd bday and I am starting to show certain sings of aging or why, but sometimes I feel that sometimes we tend to work at super-sonic speed.
The priority is always to fail fast, or the market window, or the mvc, the agile cycle, etc ... When it comes to work we are all somehow indoctrinated to have a results-driven mindset. Everything seem to be results and competition.
Today I had a thought and I wanted to share it here on my blog:
"How would our industry look like if companies replaced their competitive results-driven mindset with a thinking-learning oriented mindset?"
Tuesday, October 9, 2018
Tuesday, September 11, 2018
AspectJ + Custom Annotation + Gradle (Without Spring)
Recently I had to create an aspect to run before and after an annotated method was executed.
The first thing I thought was to just use Spring, but I was told that the devices where this software was going to run have limited resources so Spring would not an option because of it's large memory footprint.
After looking around the internet for a while I decided to do it just by using the aspectJ framework on it's own.
This is how I configured my gradle file
I created a custom annotation which will later allow me to trigger the aspect.
I placed the annotation on the methods I want the aspect to run around
Finally I created the logic of the aspect
In order to see this working in the IntelliJ editor you have to make sure you enable the Gradle Test Runner.

The first thing I thought was to just use Spring, but I was told that the devices where this software was going to run have limited resources so Spring would not an option because of it's large memory footprint.
After looking around the internet for a while I decided to do it just by using the aspectJ framework on it's own.
This is how I configured my gradle file
group 'com.javing.customAnnotations'
version '1.0-SNAPSHOT'
project.ext {
aspectjVersion = '1.8.4'
}
apply plugin: 'java'
apply plugin: 'aspectj.gradle'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
}
}
dependencies {
compile 'org.aspectj:aspectjrt:1.8.4'
compile 'org.aspectj:aspectjweaver:1.8.4'
compile 'org.aspectj:aspectjtools:1.8.4'
compile 'junit:junit:4.12'
}
I created a custom annotation which will later allow me to trigger the aspect.
package spike;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HelloAnnotation {
public boolean isRun() default true;
}
I placed the annotation on the methods I want the aspect to run around
package spike;
public class HelloApp {
public static void main(String[] args) {
HelloApp helloApp = new HelloApp();
helloApp.work();
}
@HelloAnnotation
public void work() {
System.out.println("Hello world!");
}
}
Finally I created the logic of the aspect
package spike;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class HelloAspect {
@Around("execution(* *(..)) && @annotation(spike.HelloAnnotation)")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Before");
Object proceed = pjp.proceed();
System.out.println("After");
return proceed;
}
}
In order to see this working in the IntelliJ editor you have to make sure you enable the Gradle Test Runner.

Friday, August 17, 2018
Refactoring - How do I start?

According with the Software-craftsmanship legend Sandro Mancuso we should start:
- Refactoring from the deepest nested branch of the code and work our way inside out.
- Test from the shortest/less nested branch of the code and work our way from outside in.
Wednesday, August 8, 2018
Using Wiremock to simulate a slow responding server.
Here a little example of a stub created with Wiremock that simulates a slow reply from a server:
Note: This is just for illustration purposes but this same way of stubbing can be added to an acceptance test.
import com.github.tomakehurst.wiremock.WireMockServer;
import java.util.Scanner;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
public class FakeThirdPartySystem {
public static void main(String[] args) {
WireMockServer server = new WireMockServer(8081);
server.start();
server.stubFor(get(urlEqualTo("/someUrl"))
.willReturn(aResponse()
.withStatus(200)
.withFixedDelay(10000)
.withBody("Slow reply!")));
//This is just so that the app doesn't exit straight away
Scanner scanner = new Scanner(System.in);
System.out.println("Press enter to exit");
scanner.nextLine();
server.stop();
}
}
Note: This is just for illustration purposes but this same way of stubbing can be added to an acceptance test.
Labels:
java,
stubbing,
testing,
third party system,
wiremock
Tuesday, April 24, 2018
VAVR - Using map(), flatMap(), Option and Try to get different return types
Simple map() operation in vavr.io takes as argument a function that has as a parameter the type of element contained in the list. The return type of the method in the function can be anything we want. The purpose of map is to transform from one type to another.
flatMap() uses the same mechanics as map but the only difference is that it will remove the duplication by collapsing the duplicates into a single entry. e.g 1,2,2,2,3 flatMapped will become 1,2,3
Sometimes a function can return List<Try<Option<?>>>> that is fine but perhaps Option is sometimes redundant. Notice that this method uses Try<Option>, that looks a bit overkill
To solve the redundancy shown in the example above, we can perform an additional flatMap() so that we get rid of the Option by mapping it to a Try using the toTry() method inside Option. This way we get a List<Try<String>>.
In this final example we map a set of integers to a Try<Option<String>> and then we flatMap the result to Set<Try<String>> in order to transform that Set<Try<String>> into a Try<List<String>> we pass the result to Try.sequence() and we map the outcome to list.
public List<BigDecimal> simpleMap(List<Integer> numbers) {
return numbers.map(n -> m1(n));
}
private BigDecimal m1(Integer i) {
return new BigDecimal(i);
}
flatMap() uses the same mechanics as map but the only difference is that it will remove the duplication by collapsing the duplicates into a single entry. e.g 1,2,2,2,3 flatMapped will become 1,2,3
public List<BigDecimal> flatMapping(List<Integer> numbers) {
return numbers.flatMap(n -> m2(n));
}
private List<BigDecimal> m2(Integer i) {
return List.of(new BigDecimal(i));
}
Sometimes a function can return List<Try<Option<?>>>> that is fine but perhaps Option is sometimes redundant. Notice that this method uses Try<Option>, that looks a bit overkill
public List<Try<Option<String>>> returningARedundantOption(List<Integer> numbers) {
return numbers.map(n -> m3(n));
}
private Try<Option<String>> m3(Integer i) {
//Imagine this option is the result of intereacting with other code
// e.g some dao object
return Try.success(Option.some(""));
}
To solve the redundancy shown in the example above, we can perform an additional flatMap() so that we get rid of the Option by mapping it to a Try using the toTry() method inside Option. This way we get a List<Try<String>>.
public List<Try<String>> removingRedundancy(List<Integer> numbers) {
return numbers.map(n -> {
return m3(n).flatMap(Option::toTry);
});
}
//Same as above
public List<Try<String>> removingRedundancy(List<Integer> numbers) {
return numbers.map(n -> m3(n).flatMap(Option::toTry));
}
private Try<Option<String>> m3(Integer i) {
return Try.success(Option.some(""));
}
In this final example we map a set of integers to a Try<Option<String>> and then we flatMap the result to Set<Try<String>> in order to transform that Set<Try<String>> into a Try<List<String>> we pass the result to Try.sequence() and we map the outcome to list.
public Try<List<String>> usingSequence(Set<Integer> ids) {
Set<Try<String>> result = ids.map(id -> m4(id).flatMap(Option::toTry));
return Try.sequence(result).map(Seq::toList);
}
//Same as above
public Try<List<String>> spike2(Set<Integer> ids) {
return Try.sequence(ids.map(id -> m4(id).flatMap(Option::toTry))).map(Seq::toList);
}
private Try<Option<String>> m4(Integer id) {
Try.success(Option.of("something" + id));
}
For more information about the vavr.io framework: http://www.vavr.io/
Wednesday, August 23, 2017
Dockerizing a modern Java application
In this video I show the structure of a modern Java web application that uses Spring boot, Spring mvc, gradle, angularjs and then I will show how it is commonly prepared to run from within a docker container.
Full Source codes: https://github.com/SFRJ/tictactoe
Thursday, April 20, 2017
Browser automation in Java using Cucumber and Selenium
Subscribe to:
Posts (Atom)