Featured FREE Whitepapers

What's New Here?

career-logo

The Full-Time Developer ‘Curse’

Some time ago I wrote an article with my advice to (junior) developers about their career.  One of the things I explained was that by the word “young” I meant all developer who feel young so it doesn’t matter how many years of experience you’re carrying on your back.  You’re still “young”! A few days ago, I came back from two-weeks of vacations and I realized that during this period I had plenty of time to apply at least once all these little advice (learn new things, promote yourself, be part of an opensource project, etc). That was really cool. And the funniest of all is that I spent no more than 2-3 hours  a day. Amazing! But very quickly I realized also, that the only reason I managed to do it, was the fact that I was in vacations. Why I can’t do all these nice things every week, every month? The answer is really simple. I work as a full-time developer : This practically means that I spend 9 hours daily working with legacy code, existing or new products of a specific domain. The technologies could be considered modern but they’re not state-of-the art so my learning curve is dropping down. My main responsibility is to delivery product value based on customer requirements and occasionally trouble-shot software issues. I love my job and I feel very creative with what I do but I still feel that something’s missing. I don’t have spare time (in my working hours) to  learn new languages or new tools / frameworks etc. so I try to fill the blanks in my free time. However, do I have any free time? Let me explain my thoughts : Add to the 9 hours I commit to work one more hour to get prepared in the morning and rest for a while when I get back. So far 10 hours of the day are out. Add 8 hours of sleep needed and what’s left? 6 hours / weekday. One could argue that this is fair enough for all the other activities I’m suggesting. Well, I partially agree but… You need some time to hangout with your friends, some time to relax by yourself, to do some housekeeping tasks. Now imagine you’re married with kids. Kids need (at least) your attention so 1-2 hours are clearly dedicated to them, and trust me this is the happiest time of the day, no matter how tired you are or how many problems jump in your mind. Now do the maths and you’ll see that nothing’s left during the week days. And my calculations were very rough. Some people are committing  more than 10 hours / day and spend more than 2 hours for preparation and going to work. So the root cause of the problem is that I’m a full-time developer!! Right, yeah! Wouldn’t be great if I had to work only 4 hours/day? Then I would have the required free time for the rest of my tasks and activities.  But what about my salary? Probably if I work less I will be paid less but then I will have difficulties with my loan, my every day expenses. So, thank God, I’m a full-time developer. Or not? For me the ideal scenario would be the following :Part time developer ( 4 hours/day ) Enough free time for my other activities that generate some income to cover the missing salary.Am I asking too much? I don’t know but what I know for sure is that I’m cursed and blessed for being a full-time developer. You?Reference: The Full-Time Developer ‘Curse’ from our JCG partner Patroklos Papapetrou at the Only Software matters blog....
junit-logo

This is Stuff: jUnit: Dynamic Tests Generation

Dynamic tests generation is useful when you need to run the same set of tests on many different input values or configurations. It can be achieved either using parametrized tests or using theories. Theories are valuable when you have a bunch of data to be used as parameters and want to run tests on all their combinations. You get less control, but you do not have to write combining and iterating code by yourself. Basics about how theories work are explained on java code geeks (original at java advent calendar), so this post focus on parametrized tests. Parametrized tests are better when you need to have good control over the input values, e.g. directory with files that are served as an input or a list of meaningful parameters combinations. Parametrized Tests Parametrized test is a test case able to accept parameters and a list of all parameter combinations you want it to run at. JUnit goes through the list of parameters, initializes the test case with each of them and then runs all its test methods. Both GUI and Maven runners then interpret each parametrized test run as a separate test. If some of them fails, it is immediately clear which did failed and how many of them failed. Example Use Case Less4j is less to css compiler, so each of its tests is defined by an input less file and an expected css file. The compiler is run on input file and its output is compared to the expected css. If they match, test is passed. All .less files are stored in a directory. Parametrized test case reads that directory and creates one jUnit test for each file. Therefore we can add new tests just by creating new .less and .css, run tests via “run all” button and see new test in all reports. How to Use It Parametrized test case must have following things:a @RunWith(Parameterized.class) class annotation, a constructor that accepts test case parameters, a static method annotated with @Parameters to generate parameters, test methods that runs on parameters supplied in constructor.Constructor Parametrized constructor must have at least one parameter. For example, the compiler test case can take input less as a first argument and expected compiled css as second argument. The third argument name is ignored and will be explained later: @RunWith(Parameterized.class) public class ParametrizedTest {public ParametrizedTest(String less, String expectedCss, String name) { this.less = less; this.expectedCss = expectedCss; }} Parameters The static method generating parameters must return an implementation of the Iterable interface. The iterator returns arrays containing sets of parameters. Each array is used to create one test case instance and objects in it are used as constructor parameters. For example, following method returns two arrays and thus leads to two test case instances: @Parameters(name="Name: {2}") public static Iterable<Object[]> generateParameters() { List<Object[]> result = new ArrayList<Object[]>(); result.add(new Object[] {"less", "css", "pass"}); result.add(new Object[] {"less", "error", "fail"}); return result; } The name annotation parameter is optional. Its value will be shown in GUI or maven report as the test case name. The {n} is placeholder for n-th array value. They are indexed from 0, so the first test case will be named Name: pass and second test case will be named Name: fail. Test Methods Parametrized test case can have any number of tests and they must be annotated with @Test annotation: @Test public void testCss() { //dummy test method String actualCss = compile(less); assertEquals(expectedCss, actualCss); }@Test public void testSourceMap() { //another test method String actualCss = compile(less); assertEquals(expectedCss, actualCss); }private String compile(String less) { //dummy compile method return "css"; } Output If you run the above test class, the JUnit view will show following structure: [F] com.github.sommeri.jUnit4Examples.ParametrizedTest [ ] |-- [Name: pass] [ ] |---------------- testCss[Name: pass] [ ] |---------------- testSourceMap[Name: pass] [F] |-- [Name: fail] [F] |---------------- testCss[Name: fail] [F] |---------------- testSourceMap[Name: fail] Full Test Case @RunWith(Parameterized.class) public class ParametrizedTest { private String less; private String expectedCss;public ParametrizedTest(String less, String expectedCss, String name) { this.less = less; this.expectedCss = expectedCss; } @Parameters(name="Name: {2}") public static Iterable<Object[]> generateParameters() { List<Object[]> result = new ArrayList<Object[]>(); result.add(new Object[] {"less", "css", "pass"}); result.add(new Object[] {"less", "error", "fail"}); return result; } @Test public void testCss() { String actualCss = compile(less); assertEquals(expectedCss, actualCss); }@Test public void testSourceMap() { String actualCss = compile(less); assertEquals(expectedCss, actualCss); }//dummy compile method private String compile(String less) { return "css"; } }Reference: This is Stuff: jUnit: Dynamic Tests Generation from our JCG partner Maria Jurcovicova at the This is Stuff blog....
redis-logo

URL shortener service in 42 lines of code in… Java (?!) Spring Boot + Redis

Apparently writing a URL shortener service is the new “Hello, world!” in the IoT/microservice/era world. It all started with A URL shortener service in 45 lines of Scala – neat piece of Scala, flavoured with Spray and Redis for storage. This was quickly followed with A url shortener service in 35 lines of Clojure and even URL Shortener in 43 lines of Haskell. So my inner anti-hipster asked: how long would it be in Java? But not plain Java, for goodness’ sake. Spring Boot with Spring Data Redis are a good starting point. All we need is a simple controller handling GET and POST:         import com.google.common.hash.Hashing; import org.apache.commons.validator.routines.UrlValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.http.*; import org.springframework.web.bind.annotation.*; import javax.servlet.http.*; import java.nio.charset.StandardCharsets; @org.springframework.boot.autoconfigure.EnableAutoConfiguration @org.springframework.stereotype.Controller public class UrlShortener { public static void main(String[] args) { SpringApplication.run(UrlShortener.class, args); } @Autowired private StringRedisTemplate redis; @RequestMapping(value = "/{id}", method = RequestMethod.GET) public void redirect(@PathVariable String id, HttpServletResponse resp) throws Exception { final String url = redis.opsForValue().get(id); if (url != null) resp.sendRedirect(url); else resp.sendError(HttpServletResponse.SC_NOT_FOUND); } @RequestMapping(method = RequestMethod.POST) public ResponseEntity<String> save(HttpServletRequest req) { final String queryParams = (req.getQueryString() != null) ? "?" + req.getQueryString() : ""; final String url = (req.getRequestURI() + queryParams).substring(1); final UrlValidator urlValidator = new UrlValidator(new String[]{"http", "https"}); if (urlValidator.isValid(url)) { final String id = Hashing.murmur3_32().hashString(url, StandardCharsets.UTF_8).toString(); redis.opsForValue().set(id, url); return new ResponseEntity<>("http://mydomain.com/" + id, HttpStatus.OK); } else return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } The code is nicely self-descriptive and is functionally equivalent to a version in Scala. I didn’t try to it squeeze too much to keep line count as short as possible, code above is quite typical with few details:I don’t normally use wildcard imports I don’t use fully qualified class names (I wanted to save one import line, I admit) I surround if/else blocks with braces I almost never use field injection, ugliest brother in inversion of control family. Instead I would go for constructor to allow testing with mocked Redis:@Autowired private final StringRedisTemplate redis; public UrlShortener(StringRedisTemplate redis) { this.redis = redis; } The thing I struggled the most was… obtaining the original, full URL. Basically I needed everything after .com or port. No bloody way (neither servlets, nor Spring MVC), hence the awkward getQueryString() fiddling. You can use the service as follows – creating shorter URL: $ curl -vX POST localhost:8080/https://www.google.pl/search?q=tomasz+nurkiewicz > POST /https://www.google.pl/search?q=tomasz+nurkiewicz HTTP/1.1 > User-Agent: curl/7.30.0 > Host: localhost:8080 > Accept: */* > < HTTP/1.1 200 OK < Server: Apache-Coyote/1.1 < Content-Type: text/plain;charset=ISO-8859-1 < Content-Length: 28 < Date: Sat, 23 Aug 2014 20:47:40 GMT GET /50784f51 HTTP/1.1 > User-Agent: curl/7.30.0 > Host: localhost:8080 > Accept: */* > < HTTP/1.1 302 Found < Server: Apache-Coyote/1.1 < Location: https://www.google.pl/search?q=tomasz+nurkiewicz < Content-Length: 0 < Date: Sat, 23 Aug 2014 20:48:00 GMT ...
java-logo

Java 8 : Functional VS Traditional

The business logic is the same : Given a String expression composed of visits / time like : “1/24h,1..3/3h,5/*” Then the result should be the following list of Strings: “1/24h”, “1/3h”,”2/3h”,”3/3h”, “5/1h”,”5/2h”,”5/3h”,”5/4h”,”5/5h”,until ,”24/1h” So, 2 things need to be solved, the dots and the stars for the visits and for the time. I will use Java 8 , but  I’ll show you the difference between implementing this logic using Functional  and implementing it in a traditional way with loops and ifs. package com.marco; import java.util.ArrayList; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; public class ExpressionConverter {         private static final String COMMA = ",";         private static final String SEPARATOR = "/";        private final ImmutableList<Function<String, List<String>>> visitFunctions;         private final ImmutableList<Function<String, List<String>>> timeFunctions;        public ExpressionConverter(DotsVisitFunction dotsVisitFunction, StarVisitFunction starVisitFunction, StandardFunction standardFunction,                         StarTimeFunction starTimeFunction) {                 this.visitFunctions = ImmutableList.of(dotsVisitFunction, starVisitFunction, standardFunction);                 this.timeFunctions = ImmutableList.of(starTimeFunction, standardFunction);         }        public List<String> convertVisitTimeExpressionFunctional(String visitTimeExpression) {                 return Lists.newArrayList(visitTimeExpression.split(COMMA)).parallelStream().filter(it -> !it.isEmpty())                                 .map(it -> interpretSingleExpressionFunctional(it)).flatMap(it -> it.parallelStream()).collect(Collectors.toList());         }        public List<String> convertVisitTimeExpressionTraditional(String visitTimeExpression) {                 List<String> result = new ArrayList<String>();                for (String singleVisitTime : visitTimeExpression.split(COMMA)) {                         if (!singleVisitTime.isEmpty()) {                                 result.addAll(interpretSingleVisitTimeExpressionTraditional(singleVisitTime));                         }                 }                return result;        }        private List<String> interpretSingleExpressionFunctional(String singleExpression) {                 String visit = singleExpression.split(SEPARATOR)[0];                 String time = singleExpression.split(SEPARATOR)[1];                 List<String> result = Lists.newArrayList();                visitFunctions.stream().map(it -> it.apply(visit)).flatMap(it -> it.stream()).forEach(visitIt -> {                         timeFunctions.stream().map(it -> it.apply(time)).flatMap(it -> it.stream()).forEach(timeIt -> {                                 result.add(visitIt + SEPARATOR + timeIt);                         });                 });                 return result;         }        private List<String> interpretSingleVisitTimeExpressionTraditional(String singleExpression) {                String visit = singleExpression.split(SEPARATOR)[0];                 String time = singleExpression.split(SEPARATOR)[1];                List<String> result = Lists.newArrayList();                 List<String> visists = Lists.newArrayList();                 List<String> times = Lists.newArrayList();                for (Function<String, List<String>> visitFunction : visitFunctions) {                         visists.addAll(visitFunction.apply(visit));                 }                 for (Function<String, List<String>> timeFunction : timeFunctions) {                         times.addAll(timeFunction.apply(time));                 }                 for (String visitIt : visists) {                         for (String timeIt : times) {                                 result.add(visitIt + SEPARATOR + timeIt);                         }                 }                 return result;         } } As you can see we have 2 public and 2 private methods, functional and traditional. Here is a simple test focusing on the performance of the two styles : package com.marco; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyLong; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.math.BigDecimal; import java.util.Set; import org.junit.Before; import org.junit.Test; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; public class ExpressionConverterTest {        private static final String EXPRESSIONS = "6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,55..57/14400";        private Cache cache;        private ExpressionConverter expressionConverter;        @Before         public void init() {                 cache = mock(Cache.class);                 expressionConverter = new ExpressionConverter(new DotsVisitFunction(cache), new StarVisitFunction(cache), new StandardFunction(),                                 new StarTimeFunction(cache));                Set<Integer> totVisists = Sets.newHashSet();                 for (int i = 1; i <= 100; i++) {                         totVisists.add(i);                 }                when(cache.getVisits(anyLong(), (BigDecimal) any(), anyInt())).thenReturn(ImmutableSet.copyOf(totVisists));         }        @Test         public void testPerf() {                long averageTraditional = 0l;                 long averageFuntional = 0l;                for (int a = 0; a < 10; a++) {                         long start = System.currentTimeMillis();                        for (int i = 0; i < 1000; i++) {                                 expressionConverter.convertVisitTimeExpressionTraditional(EXPRESSIONS);                        }                         System.out.println("Traditional java " + (System.currentTimeMillis() - start) + " ms");                         averageTraditional += (System.currentTimeMillis() - start);                        long start2 = System.currentTimeMillis();                         for (int i = 0; i < 1000; i++) {                                 expressionConverter.convertVisitTimeExpressionFunctional(EXPRESSIONS);                         }                         System.out.println("Functional java " + (System.currentTimeMillis() - start2) + " ms");                         averageFuntional += (System.currentTimeMillis() - start2);                 }                System.out.println("Average Traditional java : " + (averageTraditional / 10) + " ms");                 System.out.println("Average Functional java : " + (averageFuntional / 10) + " ms");        } } And this is the output : Traditional java 1274 msFunctional java 773 msTraditional java 1054 msFunctional java 531 msTraditional java 961 msFunctional java 493 msTraditional java 948 msFunctional java 492 msTraditional java 949 msFunctional java 491 msTraditional java 958 msFunctional java 481 msTraditional java 1004 msFunctional java 474 msTraditional java 949 msFunctional java 471 msTraditional java 947 msFunctional java 475 msTraditional java 942 msFunctional java 472 msAverage Traditional java : 998 msAverage Functional java : 515 ms Functional is 2 times faster than Traditional. Why? Because of this : parallelStream() Parallel Stream will split the job in several tasks leveraging your multicore system. So can you put parallelStream() everywhere and replace all the stream() methods with parallelStream() ?? No! The parallelStream()can in fact cause a big degradation of performance if used in the wrong place. There are loads of posts out there on this argument that should be read before using this feature, but just remember, if you want to use parallelStream() make sure to measure the performance before and after!!!!!!!!!!Reference: Java 8 : Functional VS Traditional from our JCG partner Marco Castigliego at the Remove duplication and fix bad names blog....
software-development-2-logo

Why Testers Are Losing The ISO 29119 Battle

The ISO 29119 is making waves in the testing communities, regarding its content and necessity. Its focus on test planning and documentation gets modern testers to ask why. I’ve worked in an ISO certified company, so maybe I can shed light from my past experience.          ISO standards are written by committees, which are made of people. We’re assuming that these people are experts, but mainly they are not. Some of them are academics who didn’t experience real life work. There’s a big hidden assumption, that adhering to the standards ensures quality and stakeholder requirements. This is why management gives them the go ahead, apart from the certificate on the wall. ISO certification are for organizations, not people. Most of the work in handling the acceptance, checks and certification are not by done by the practitioners. For example, in my former company, the person in charge of adhering to the ISO standards in R&D was not a developer or tester, and was outside on R&D. She was very good at what she did, but did not have any product development experience. There’s a great fear of not failing to meet the ISO inspections, and therefore they rather go specifically according to a standard, which is specific about what they need to follow, if there is one. ISO is really simple: Define your process, prove you adhere to it, get certified. In my former life, all development procedures were NOT dictated by ISO, but by the organization internally. They were derived indirectly from ISO 9000. To get the annual certificate we needed to show proof that we actually did what the procedures said. As non-practitioners lead the process, there’s a need to create a common ground in terms of process and language. How would a general QA manager (non-tester) know that the development team are doing code reviews? The practitioners need to document it.  In the ISO 29119 case, the documentation is of test planning and documentation. These are the easiest to put in process form, as real testing skills are hard to put into process, it’s an easy way out. Don’t worry, it’s not just testing, but every high-skill suffers the same fate. Of course, test plans and test result documentations do not guarantee any quality, or guide people to test better. Finally, the environment is there too: Even if you’re not in a regulated industry, which is required to meet standards, you’ll find a competitor who does get the certificate, and will push your organization for taking this on too. The ISO institute gets money from the inspection so they have an interest to push the standards to be adapted by many.Let’s summarize:   An organization wants the certification because they need it, or believe it will help their quality. They are looking for the best, simplest and less risky way to get it. The ISO organization gives the simplest common ground that works with “documentation as proof” concept. Everybody’s happy. Except the practitioners. The uproar against traditional ISO standards is not new. When we decided that code reviews is needed, we had to document them, just so we can meet the standard. I needed to sneak in “documentable” features and tweak the form to pass an inspection. And all I wanted is an abstract framework for doing code reviews, where people pair and inspect the code for problems, not the location of braces. But that’s life. I had to compromise. There’s a whole system out there that works fine for most people. A vocal minority won’t change that. The antidote does not come from regulation and standards. It comes from the responsibility of the organization to learn and improve. Do you really want to be just a standard organization, or a bit more?Reference: Why Testers Are Losing The ISO 29119 Battle from our JCG partner Gil Zilberfeld at the Geek Out of Water blog....
jboss-infinispan-logo

Clustered Idempotent Consumer Pattern with Infinispan

I’ve created a small project that shows how to use JBoss Infinispan with Apache Camel and the Idempotent Consumer Pattern to guarantee a message will not be processed twice in a clustered environment. Imagine you have an application that has to scale out easily by deploying it on multiple containers. But the application has to process each unique request only once across the cluster. The solution is simple: use Idempotent Consumer Pattern in Camel with a repository that can scale out easily. This is where Infinispan comes into play. Infinispan is extremely scalable, highly available key/value store and data grid. If you use InfinispanIdempotentRepository with an idempotent consumer, it will create an in-memory cache to store the requests, and the moment you start another instance of the application, the cache instances will sync and the idempotent consumers in all applications will not process existing requests any longer.With this project (idempotent consumer demo5) you can start as many containers as you want, each container will start a rest endpoint on a new port starting from 8080 (http://localhost:8080/idempotent/KEY), and if you perform a GET request with a key, the subsequent requests with the same key to any other container will be rejected. Behind the scene Infinispan is replicating all the processed keys across the cluster of Camel applications and ensuring consistency. The core of the application is the following route definition that finds a new free port number for each instance of the application: public class IdempotentRoute extends RouteBuilder { private static final transient Logger LOGGER = LoggerFactory.getLogger(IdempotentRoute.class);private InfinispanIdempotentRepository infinispanRepo; private int port;@Override public void configure() throws Exception { from("restlet:http://localhost:" + port + "/idempotent/{key}?restletMethods=GET").idempotentConsumer(header("key"), infinispanRepo) .setBody(simple("UNIQUE REQUEST ACCEPTED: ${header.key}")) .stop() .end().setBody(simple("REQUEST REJECTED: ${header.key}")); }public InfinispanIdempotentRepository getInfinispanRepo() { return infinispanRepo; }public void setInfinispanRepo(InfinispanIdempotentRepository infinispanRepo) { this.infinispanRepo = infinispanRepo; }public void start() { port = AvailablePortFinder.getNextAvailable(8080); LOGGER.info("Using port: " + port); } } Simple, isn’t it.Reference: Clustered Idempotent Consumer Pattern with Infinispan from our JCG partner Bilgin Ibryam at the OFBIZian blog....
software-development-2-logo

Unit Testing – Cost vs. Benefit

I have been a big fan of unit testing for a very long time; my blog is ridden with posts about it. If I look at how automated unit testing has progressed in my projects over the last decade I am noticing that the inclination to do unit testing is just not there. Recently, I came across this Podcast series between Martin Fowler, Kent Beck and DHH which was in response to DHH’s post TDD is dead, long live Testing. The Podcast did’t reveal something new to me but it helped enlighten me. I spent the last evening introspecting and thinking how I have progressed TDD/Unit Testing in general over my projects and the trend I see is that the ones where I have been more involved as a developer or a senior developer or even an architect the effort to make unit testing has been there, BUT unit testing (not TDD) didnt work very well in last big project. And since I have taken up the responsibilities of a Solution Architect and as with my responsibilities I have moved away from Code a bit, I see the next layer not doing unit testing very much. As I spent the time thinking what could be the causes, this is where the Podcast series helped a lot. The reasons that I can attribute to Unit Testing (not TDD) just not happening on projects recentlyWriting Automated Unit Tests is just not as fun as Coding is Writing Automated Unit Tests is just not as fun as Coding isYou get the point! I compare the cases when I first did unit testing back in 2004 (my first project with an IT Consultancy Organization) and then the last one in 2012 and I see why is that was the case. The environment in which I was doing unit testing back in 2004 and then in 2012 (8 years apart) were very different. Back in the days, I was an Enterprise/Core Java/.Net developer, and my life was to deal with things like making Database calls, writing services – business logic, validations, yada yada yada. The part which involved writing automated test cases for Web layer was minimal because the code that went into Web Controller (MVC frameworks we used varied) was only view. And we used Functional/System testing approach to test the UI. The responsibility of creating those selinium test cases were not with the QA Function but still with me as the developer. So the whole testing was “what makes sense” – we did TDD on services code and then functional testing (post coding) on Front-end. But the most important part was that as a developer I was supposed to make sure that nothing breaks when I committed the code. The CI tools were not that advanced back in the days, but they were there. We used Ant for build and there was a task in there which was responsible for running test cases else failed the build (I even coded the ant build script). We were not able to integrate Selenium test runner ant script so that was a manual step, but we had to run a whole suite to ensure everything works. The phenomena “Self Testing Code” existed and we followed it to the core. The were build break fines place – 50 INR as fine of we failed a break; 3 consecutive breaks and it went upto 500 INR; 20 INR for any regression defects introduced (QA had their own suits to keep us in control). When I coded and when I committed the code, I knew that at night no one is going to call me to tell me that other members of my team is stuck. I was working in India and I had a team working from Boston and i knew that when I left and when they came in they wont be gated because i fucked up a compilation or broke something – they can just go about doing their work and be productive. Everyone did so and we all were happy bunch of developers/team. We did not have Continuous Delivery, but once story was done, a couple of rounds of QA testing and it was demo able to the clients. Out test coverage was 80%+ and we had a solid test suite. The suite however took 2 hours to run (I will talk about is very soon as that was the part which make Unit Testing a pleasure). That was the last time, I saw the workflow work so very well. Surprisingly, that was also the first time I was doing it. Fast forward 2007-08; I was the architect on a 40 people team (Developers, QA, Site Developers) and we were working on a web development – some heavy web development. It was using Backbase, JSF on JBoss, talking to MySQL via hibernate, everything wrapped around Spring. We started with the project and like before I wanted to do full blown TDD (not just Unit Testing). We started, but very soon (Sprint 1) there was feedback from developers that unit testing was too tough, it takes more time than it takes time to code. I was not the architect and I had produced a Golden Copy as a reference point which the team was using; so the feedback team was giving me made no sense. I let the team struggle on it for another sprint thinking they just need time to come around it, but when it started to hit our velocity points, I was forced to see what was going on in that Unit Testing Departments and what I saw shocked the soul out of me. The automated tests were there for a formality – the tests did test something, they provided some coverage but they were monstrous. They were ridden with mocks (almost) everywhere. No wonder it took so much time for the team to write test cases. For everything that needed to test they had to create a mock. A bit of it was lack of experience as well where team did not create helpers classes to provide for those common mocks, but trying to mock things like HTTPSession, HTTPRequest, Data Adapters for MySQL logic – aka every collaborator was in there. Well they did the right thing (per Unit Testing definition) – test the unit itself, so they mocked even the classes making calls to databases. It make so much harder to test a functionality. We quickly turned it around and set some ground rulesWe will not use mocks If we find a scenario where mocks could not be avoided we will not write automated unit tests for itWhat this meant was a) this was not Unit Testing in it’s purest form (I don’t even know what Unit Testing is in it’s purest form, but what definitions and my seniors told me) and b) we had a bit of less coverage. But, I was okay with those consequences. The outcome was that team was not needed to write stuff that was used for just testing; it made our whole test suite a bit slow because it now made calls to the DB, but DB was local so it was not so slow – the team started to spend less time on unit testing, we wrote lesser tests and achieved better coverage to code. This wasn’t unit testing, but we did achieve Self Testing Code. Our CI build was now once again telling us if a commit broke something else. The fun was coming back into the game and we started to make progress. Then in Jan 2008, we decided to switch out Backbase and move over to Flex and this needed a redesign not just in our front-end but also in our back-end services. When we put together a team to redesign and reimplement the services they were given a solid test suite of 300 odd tests. The implementation was thrown away a few interfaces were changed. We fixed the test suite only (We made a decision to let our QA team fix the test suite – which need a QA person to code/script in Java which caused a lot of pushback, but I won that battle) the interface part and then started writing those services again and all we had to do was to make those 300 test cases pass. It was a breeze; we didn’t even need a QA team to test our services. Our test suite was good enough to do it. We never achieved perfection, but then I was able to meet the goals. Fast Forward 2012-2013; I was the architect on Adobe CQ 5.5 project and the frameworks had changed. There were no services we were writing. The traditional MVC framework was all not there at the developers disposal. I saw the code and there was monstrosity of JSP having business logic in scriplets, single tiered Java classes which did everything from getting data, manipulating them, validating inputs, sending outputs in very fluid formats like Maps (ValueMaps to be specific to Sling implementation). Amongst all this we made some hard design decisions like no scriptlets or business logic in JSPs. Hence there was now going to be Java Code, but there were numerous sling and JCR dependencies that we had to deal with. I once again wanted to do what I had done before – No MOCKS/STUBS. But, the frameworks to be able to do that were’t at my disposal back then. They existed but the community around then was thin, support was less and whatever existed was not going to work. And I ended up deciding to do Mocks. 3 months and it was exactly what was in 2008 – it wasn’t working. The mocks were so bad that we could change the logic and tests would still pass. What had happened that developers just mangle everything and tests were not even testing state or behavior. They just went for coverage. The tools were better this time and we had sonar. The idea was to write a test case and make it pass and show an increased covered in sonar. We had used mockit and it seemed like a good idea at the time, but just didn’t work. I understand mocks but i just don’t get them. We setup some state and we check for that state – all makes sense. But when everything is coming from Collaborators and the object you are testing is just aggregating and assembling data into a ValueObject (Map in our case), testing that assembly makes no sense. It’s like saying a developer can code the following wrong. outputData.put("athletes", athleteList); Well, if a developer can’t get a bunch of basic java statements right, well they have no business coding; let’s send them into a training and save ourselves from a world of pain. Fast Forward 2014, I am not a solution architect and not focusing a whole lot of coding per say. I love to code, but it’s not my day job (i would like coding to be my day job some day). But, now I see teams struggle a lot to get unit testing even off of the ground. The environment is similar to what I had back in 2012, but now I do have a solution and I think there is a way of doing it without Mocks. Yet, the struggle is there, developers dont see this as fun any more – it looks like a burden. The Cost vs. Benefit argument is a valid one in every scenario, and in case of our software development lifecycle where getting any sort of confidence is just too tough, as a developer the need to have something to fall back on and knowing that we are not breaking a system with my next commit is huge. In my development lifecycle, we can probably still live with some regression, but as we reach application maturity, as we start to reach stabilization not having a Self Testing Code makes us like Mammoths (not elephants) who move ever so slowly. In the current world of marketing, where we have clients who want to run campaigns in next 2 weeks, we can’t be slow in how soon we release code. You can only be relevant in the industry if you can move quickly, achieve the Continuous Delivery or at least reach a point when you can get releases out in production with a reasonable speed. Those days where releases used to happen once every 6 months are gone; or at least gone in the environment/market I am operating. We have to have Self Testing Code, We have to Unit Test, We have to have feedback loops and We have to have feedback loop as soon as possible. There is no avoidance; let’s understand and embrace Or be extinct in a few years.Reference: Unit Testing – Cost vs. Benefit from our JCG partner Kapil Viren Ahuja at the Scratch Pad blog....
java-logo

Pizza problem – builder vs decorator

Problem Statement We need to build the software for a pizza company who wants to prepare different types of pizzas, e.g Chicken Pizza, Flat Bread, Pepperoni Pizza with Extra Cheese, put add on toppings on it. Lets try to see which design pattern suits this problem statement and under what scenario. Traditionally, for pizza problem, builder pattern is most commonly used. However there are some examples using decorator as well, both the approaches are correct but there is difference in use case. Builder is an object creation pattern whereas decorator is used to change the already built object at runtime. Lets try to understand this by the examples :Builder Pattern : Here the use case is that pizza is prepared in one go with set specifications. Lets see the Pizza Class : public class Pizza {private float totalPrice = 0;private Size size; private Topping topping; private Crust crust; private Cheese cheese;public Size getSize() { return size; }public void setSize(Size size) { this.size = size; }public Topping getTopping() { return topping; }public void setTopping(Topping topping) { this.topping = topping; }public Crust getCrust() { return crust; }public void setCrust(Crust crust) { this.crust = crust; }public Cheese getCheese() { return cheese; }public void setCheese(Cheese cheese) { this.cheese = cheese; }public float getTotalPrice() { return totalPrice; }public void addToPrice(float price) { this.totalPrice = totalPrice + price; } } The 4 enum classes : public enum Cheese { AMERICAN { public float getCost() { return 40; } }, ITALIAN { public float getCost() { return 60; } };public abstract float getCost();}public enum Crust {THIN { public float getCost(){ return 70; } } , STUFFED{ public float getCost(){ return 90; } };public abstract float getCost(); }public enum Size { MEDIUM { public float getCost() { return 100; } }, LARGE { public float getCost() { return 160; } };public abstract float getCost(); }public enum Topping {PEPPERONI { public float getCost(){ return 30; } }, CHICKEN{ public float getCost(){ return 35; } }, MUSHROOM{ public float getCost(){ return 20; } };public abstract float getCost(); } The PizzaBuilder Class : public class PizzaBuilder {Pizza pizza = new Pizza();public PizzaBuilder withTopping(Topping topping) { pizza.setTopping(topping); pizza.addToPrice(topping.getCost()); return this; }public PizzaBuilder withSize(Size size) { pizza.setSize(size); pizza.addToPrice(size.getCost()); return this; }public PizzaBuilder withCrust(Crust crust) { pizza.setCrust(crust); pizza.addToPrice(crust.getCost()); return this; }public Pizza build() { return pizza; }public double calculatePrice() { return pizza.getTotalPrice(); }} The Test Case : public class PizzaBuilderTest {@Test public void shouldBuildThinCrustChickenPizza(){ Pizza pizza = new PizzaBuilder().withCrust(Crust.THIN).withTopping(Topping.CHICKEN).withSize(Size.LARGE).build(); assertEquals(Topping.CHICKEN,pizza.getTopping()); assertEquals(Size.LARGE,pizza.getSize()); assertEquals(Crust.THIN,pizza.getCrust()); assertEquals(265.0,pizza.getTotalPrice(),0); } }Decorator Pattern : Decorator Pattern is used to add or remove additional functionalities or responsibilities from the object dynamically without impacting the original object. The use case would be that some base pizza is first prepared and then different specifications are added to it. Here, we need an interface (Pizza) for the BasicPizza (Concrete Component) that we want to decorate and a class PizzaDecorator that contains reference field of Pizza (decorated) interface. The Pizza Interface: public interface Pizza { public String bakePizza(); public float getCost(); } The Base Pizza Implementation : public class BasePizza implements Pizza{public String bakePizza() { return &amp;quot;Basic Pizza&amp;quot;; } public float getCost(){ return 100; } } PizzaDecorator class : public class PizzaDecorator implements Pizza { Pizza pizza; public PizzaDecorator(Pizza newPizza) { this.pizza = newPizza; }public String bakePizza() { return pizza.bakePizza(); }@Override public float getCost() { return pizza.getCost(); } } The 2 decorators : Mushroom and Pepperoni public class Mushroom extends PizzaDecorator {public Mushroom(Pizza newPizza) { super(newPizza); }@Override public String bakePizza() { return super.bakePizza() + &amp;quot; with Mashroom Topings&amp;quot;; }@Override public float getCost() { return super.getCost()+80; } } public class Pepperoni extends PizzaDecorator {public Pepperoni(Pizza newPizza) { super(newPizza); }@Override public String bakePizza() { return super.bakePizza() + &amp;quot; with Pepperoni Toppings&amp;quot;; }@Override public float getCost() { return super.getCost()+110; } } The test Case: public class PizzaDecoratorTest {@Test public void shouldMakePepperoniPizza(){ Pizza pizza = new Pepperoni(new BasePizza()); assertEquals(&amp;quot;Basic Pizza with Pepperoni Toppings&amp;quot;,pizza.bakePizza()); assertEquals(210.0,pizza.getCost(),0); } }The difference Patterns like builder and factory (and abstract factory) are used in creation of objects. And the patterns like decorator (also called as structural design patterns) are used for extensibility or to provide structural changes to already created objects. Both types of patterns largely favour composition over inheritance, so giving this as a differentiator for using builder instead of decorator will not make any sense. Both give behaviour upon runtime rather than inheriting it. So, one should use builder if he wants to limit the object creation with certain properties/features. For example there are 4-5 attributes which are mandatory to be set before the object is created or we want to freeze object creation until certain attributes are not set yet. Basically, use it instead of constructor – as Joshua Bloch states in Effective Java, 2nd Edition. The builder exposes the attributes the generated object should have, but hides how to set them. Decorator is used to add new features of an existing object to create a new object. There is no restriction of freezing the object until all its features are added. Both are using composition so they might look similar but they differ largely in their use case and intention. Another approach could be to use Factory Pattern; if we do not want to expose the attributes and want the creation of a certain pizza “magically” inside then based on some attributes. We will explore this implementation using Factory Pattern in the later post.Reference: Pizza problem – builder vs decorator from our JCG partner Anirudh Bhatnagar at the anirudh bhatnagar blog....
android-logo

Resolving the dual ItemClick conundrum for the Android Gallery View

Yes, the Gallery View is deprecated, however currently there really isn’t an out of the box solution to what the gallery view provides, which is a center locked horizontal scrolling list of items. The Twoway-view project has some potential in addressing this need, but it needs time to mature a bit. There are many issues with the Gallery view, but one of the most significant is if you need the ability to show context menus for an item. If you are using it with a D-Pad (say in an Android TV application), and you long press on an item using D-PAD OK, or BUTTON-A, you’ll get both ItemLongClick and ItemClick events fired by the gallery. This has been a long standing bug within the app, an has plagued me for years. I finally tracked it down and created the following gist that can be applied to a custom version of the Gallery. /** * This change should work in both the original Gallery code as well as in the vairous open source * extensions and variations. * * Find the displatchLongPress method in the Gallery class file. and update it to the following. * * Make sure that longPressHandled is a field available to all the other methods. * */ boolean longPressHandled = false; private boolean dispatchLongPress(View view, int position, long id) { longPressHandled = false;if (mOnItemLongClickListener != null) { longPressHandled = mOnItemLongClickListener.onItemLongClick(this, mDownTouchView, mDownTouchPosition, id); }if (!longPressHandled) { mContextMenuInfo = new AdapterContextMenuInfo(view, position, id); longPressHandled = super.showContextMenuForChild(this); }if (longPressHandled) { performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); }return longPressHandled; }/** * Find the onKeyUp method and change it to the following. * * Since dispatchLongPress is fired before the onKeyUp event, we have * to check to see if the onItemLongClickListener handled the event. * If so do not performItemClick, and always reset the longPressHandled to false. * */ @Override public boolean onKeyUp(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: case KeyEvent.KEYCODE_ENTER: {if (mReceivedInvokeKeyDown) { if (mItemCount > 0) {dispatchPress(mSelectedChild); postDelayed(new Runnable() { @Override public void run() { dispatchUnpress(); } }, ViewConfiguration.getPressedStateDuration());int selectedIndex = mSelectedPosition - mFirstPosition;if (!longPressHandled) { performItemClick(getChildAt(selectedIndex), mSelectedPosition, mAdapter.getItemId(mSelectedPosition)); } } }// Clear the flag mReceivedInvokeKeyDown = false; longPressHandled = false;return true; } }return super.onKeyUp(keyCode, event); } Basically, what was happening is that the onKeyUp event that fired the ItemClick event never checked to see if the dispatchLongPress event had already handled the event. So it was always firing the ItemClick event. The gist makes this small change, and actually eliminates numerous work arounds that had to be done to get context menus to show with the gallery. This bug only affects you if you needed to use a D-PAD, Remote Control, or Game Controller as an input device. Normal touch screen events are handled correctly.Reference: Resolving the dual ItemClick conundrum for the Android Gallery View from our JCG partner David Carver at the Intellectual Cramps blog....
software-development-2-logo

15 Java Socket Programming, Networking Interview Questions and Answers

Networking and Socket Programming is one of the most important areas of Java programming language, especially for those programmers, who are working in client/server based applications. Knowledge of important protocols e.g. TCP and UDP in detail is very important, especially if you are, for example, in the business of writing high frequency trading applications, which communicate via FIX Protocol or native exchange protocol. In this article, we will discuss some of the frequently asked questions on networking and socket programming, mostly based around TCP IP protocol. This article is kinda light on NIO though, as it doesn’t include questions regarding multiplexing, selectors, ByteBuffer and FileChannel etc., but it does include classic questions like difference between IO and NIO. Main focus of this post is to make a Java developer familiar with the low level parts e.g. how TCP and UDP protocol works, socket options and writing multi-threaded servers in Java. Questions discussed here are not really tied up to the Java programming language, and can be used in any programming language, which allows programmers to write client-server applications. By the way, If you are going for an interview in an Investment bank for a core Java developer role, you better prepare well on Java NIO, Socket Programming, TCP, UDP and Networking along with other popular topics e.g. multi-threading, Collections API and Garbage Collection tuning. You can also contribute any question, which is asked to you or related to socket programming and networking and can be useful for Java interviews. Java Networking and Socket Programming Questions Answers Here is my list of 15 interview questions related to networking basics, internet protocol and socket programming in Java. Though it doesn’t contain basic questions form API e.g. Server, ServerSocket, it focus on the high level concept of writing scalable server in Java using NIO selectors and how to implement that using threads, the limitations and issues etc. I will probably add few more questions based on some best practices while writing socket based application in Java. If you know a good question on this topic, feel free to suggest. 1. Difference between TCP and UDP protocol? There are many differences between TCP (Transmission control Protocol) and UDP (User Datagram Protocol), but the main is that TCP is connection oriented, while UDP is connection less. This means TCP provides a guaranteed delivery of messages in the order they were sent, while UDP doesn’t provide any delivery guarantee. Because of this guarantee, TCP is slower than UDP, as it needs to perform more work. TCP is best suited for messages, which you can’t afford to loss, e.g. order and trade messages in electronic trading, wire transfer in banking and finance etc. UDP is more suited for media transmission, where loss of one packet, known as datagrams is affordable and doesn’t affect quality of service. This answer is enough for most of the interviews, but you need to be more detailed when you are interviewing as Java developer for high frequency trading desk. Some of the points which many candidates forget to mention is about order and data boundary. In TCP, messages are guaranteed to be delivered in the same order as they are sent but data boundary is not preserved, which means multiple messages can be combined and sent together, or the receiver may receive one part of the message in one packet and other part of the message in next packet. Though application will receive full message and in the same order. TCP protocol will do the assembling of the message for you. On the other hand, UDP sends full messages in a datagram packet, if a client receives the packet it is guaranteed that it will get the full message, but there is no guarantee that packets will come in same order they are sent. In short, you must mention the following differences between TCP and UDP protocol while answering during interview :TCP is guaranteed delivery, UDP is not guaranteed. TCP guarantees order of messages, UDP doesn’t. Data boundary is not preserved in TCP, but UDP preserves it. TCP is slower compared to UDP.For a more detailed answer, see my post 9 differences between TCP and UDP protocol. 2. How does TCP handshake works? Three messages are exchanged as part of a TCP head-shake e.g. Initiator sends SYN, upon receiving this Listener sends SYN-ACK, and finally initiator replied with ACK, at this point TCP connection is moved to ESTABLISHED state. This process is easily understandable by looking at the following diagram.3. How do you implement reliable transmission in UDP protocol? This is usually a follow-up of a previous interview question. Though UDP doesn’t provide delivery guarantee at protocol level, you can introduce your own logic to maintain reliable messaging e.g. by introducing sequence numbers and retransmission. If the receiver finds that it has missed a sequence number, it can ask for a replay of that message from the Server. TRDP protocol, which is used in Tibco Rendezvous (a popular high speed messaging middle-ware) uses UDP for faster messaging and provides reliability guarantee by using sequence number and retransmission. 4. What is Network Byte Order? How does two host communicate if they have different byte-ordering? There are two ways to store two bytes in memory, little endian (least significant byte at the starting address) and big endian (most significant byte at the starting address). They are collectively known as host byte order. For example, an Intel processor stores the 32-bit integer as four consecutive bytes in memory in the order 1-2-3-4, where 1 is the most significant byte. IBM PowerPC processors would store the integer in the byte order 4-3-2-1. Networking protocols such as TCP are based on a specific network byte order, which uses big-endian byte ordering. If two machines are communicating with each other and they have different byte ordering, they are converted to network byte order before sending or after receiving. Therefore, a little endian micro-controller sending to a UDP/IP network must swap the order in which bytes appear within multi byte values before the values are sent onto the network, and must swap the order in which bytes appear in multi byte values received from the network before the values are used. In short, you can also say network byte order is standard of storing byte during transmission, and it uses big endian byte ordering mechanism. 5. What is Nagle’s algorithm? If an interviewer is testing your knowledge of TCP/IP protocol then it’s very rare for him not to ask this question. Nagle’s algorithm is way of improving performance of TCP/IP protocol and networks by reducing the number of TCP packets that need to be sent over network. It works by buffering small packets until the buffer reaches Maximum Segment Size. This is because small packets, which contain only 1 or 2 bytes of data, have more overhead in terms of a TCP header, which is of 40 bytes. These small packets can also lead to congestion in a slow network. Nagle’s algorithm tries to improve the efficiency of the TCP protocol by buffering them, in order to send a larger packet. Also Nagle’s algorithm has a negative effect on non small writes, so if you are writing large data on packets than it’s better to disable Nagle’s algorithm. In general, Nagle’s algorithm is a defense against a careless application, which sends lots of small packets to network, but it will not benefit or have a negative effect on a well written application, which properly takes care of buffering. 6. What is TCP_NODELAY? TCP_NODELAY is an option to disable Nagle’s algorithm, provided by various TCP implementations. Since Nagle’s algorithm performs badly with TCP delayed acknowledgement algorithm, it’s better to disable Nagle’s when you are doing write-write-read operation. Where a read after two successive write on socket may get delayed up-to 500 millisecond, until the second write has reached the destination. If latency is more of concern compared to bandwidth usage, e.g. in a network based multi-player game where users want to see action from other players immediately, it’s better to bypass Nagle’s delay by using TCP_NODELAY flag. 7. What is multicasting or multicast transmission? Which Protocol is generally used for multicast? TCP or UDP? Multi-casting or multicast transmission is one to many distribution, where a message is delivered to a group of subscribers simultaneously in a single transmission from publisher. Copies of messages are automatically created in other network elements e.g. Routers, but only when the topology of network requires it. Tibco Rendezvous supports multicast transmission. Multi-casting can only be implemented using UDP, because it sends full data as datagram package, which can be replicated and delivered to other subscribers. Since TCP is a point-to-point protocol, it can not deliver messages to multiple subscriber, until it has link between each of them. Though, UDP is not reliable, and messages may be lost or delivered out of order. Reliable multicast protocols such as Pragmatic General Multicast (PGM) have been developed to add loss detection and retransmission on top of IP multicast. IP multicast is widely deployed in enterprises, commercial stock exchanges, and multimedia content delivery networks. A common enterprise use of IP multicast is for IPTV applications. 8. What is the difference between Topic and Queue in JMS? The main difference between Topic and Queue in Java Messaging Service comes when we have multiple consumers to consumer messages. If we set-up multiple listener thread to consume messages from Queue, each messages will be dispatched to only one thread and not all thread. On the other hand in case of Topic each subscriber gets it’s own copy of message. 9. What is the difference between IO and NIO? The main difference between NIO and IO is that NIO provides asynchronous, non blocking IO, which is critical to write faster and scalable networking systems. On the other hand, most of the utilities from the IO classes are blocking and slow. NIO takes advantage of asynchronous system calls in UNIX systems such as the select() system call for network sockets. Using select(), an application can monitor several resources at the same time and can also poll for network activity without blocking. The select() system call identifies if data is pending or not, then read() or write() may be used knowing that they will complete immediately. 10. How do you write a multi-threaded server in Java? A multi-threaded server is the one which can serve multiple clients without blocking. Java provides excellent support to develop such a server. Prior to Java 1.4, you could write multi-threaded server using traditional socket IO and threads. This had a severe limitation on scalability, because it creates a new thread for each connection and you can only create a fixed number of threads, depending upon machine’s and platform’s capability. Though this design can be improved by using thread pools and worker threads, it still a resource intensive design. After JDK 1.4 and NIO’s introduction, writing scalable and multi-threaded server become bit easier. You can easily create it in single thread by using Selector, which takes advantage of asynchronous and non-blocking IO model of Java NIO. 11. What is an ephemeral port? In TCP/IP, a connection usually contains four things, Server IP, Server port, Client IP and Client Port. Out of these four, 3 are well known in most of the time, what is not known is client port, this is where ephemeral ports come into picture. Ephemeral ports are dynamic ports assigned by your machine’s IP stack, from a specified range, known as ephemeral port range, when a client connection explicitly doesn’t specify a port number. These are short lived, temporary ports, which can be reused once connection is closed. However, most of IP software, doesn’t reuse an ephemeral port, until the whole range is exhausted. Similar to TCP, UDP protocol also uses an ephemeral port, while sending datagrams . In Linux, the ephemeral port range is from 32768 to 61000, while in windows the default ephemeral port range is 1025 to 5000. Similarly, different operating systems have different ephemeral port ranges. 11. What is sliding window protocol? Sliding window protocol is a technique for controlling transmitted data packets between two network computers where reliable and sequential delivery of data packets is required, such as provided by Transmission Control Protocol (TCP). In the sliding window technique, each packet includes a unique consecutive sequence number, which is used by the receiving computer to place data in the correct order. The objective of the sliding window technique is to use the sequence numbers to avoid duplicate data and to request missing data. 12. When do you get “too many files open” error? Just like a File connection, a Socket Connection also needs file descriptors. Since every machine has limited number of file descriptors, it’s possible that they may run out of file descriptors. When this happens, you will see the “too many files open“ error. You can check how many file descriptors per process are allowed on a UNIX based system by executing the ulimit -n command or simply count entries on /proc/ /fd/. 14) What is TIME_WAIT state in TCP protocol? When does a socket connection goes to TIME_WAIT state? When one end of a TCP Connection closes it by making system call, it goes into TIME_WAIT state. Since TCP packets can arrive in the wrong order, the port must not be closed immediately in order to allow late packets to arrive. That’s why that end of the TCP connection goes into TIME_WAIT state. For example, if a client closes a socket connection, then it will go to TIME_WAIT state, similarly if server closes connection then you will see TIME_WAIT there. You can check the status of your TCP and UDP sockets by using these networking commands in UNIX. 15. What will happen if you have too many socket connections in TIME_WAIT state on Server? When a socket connection or port goes into TIME_WAIT state, it doesn’t release the file descriptor associated with it. The file descriptor is only released when TIME_WAIT state is gone, i.e. after some specified configured time. If too many connections are in TIME_WAIT state than your Server may ran out of file descriptors and start throwing “too many files open” error, and stop accepting new connections. That’s all about in this list of networking and socket programming interview questions and answers. Though I have originally intended this list for Java programmers it is equally useful for any programmer. In fact, this is bare minimum knowledge of sockets and protocols every programmer should have. I have found that C and C++ programmers are better answering these questions than an average Java programmer. One reason for this may be because Java programmers have got so many useful libraries, e.g. Apache MINA, which handle all the low level work for them. Anyway, knowledge of fundamentals is very important and everything else is just an excuse, but at the same time I also recommend using tried and tested libraries like Apache MINA for production code.Reference: 15 Java Socket Programming, Networking Interview Questions and Answers from our JCG partner Javin Paul at the Javarevisited blog....
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close