Pages

Sunday, August 26, 2012

Use a martial arts principle to improve your Test Driven Development skills.


In ancient Japan, martial arts were not just defence techniques, they were also  a way of life(jap. “budo”).
In this way of life they combined physical, moral and spiritual dimensions with the goal of self-improvement.

Since very young, Japanese kids would join a martial arts school, to study: karate, aikido, judo…  
They learned martial arts using a technique called “Kata”.

The “Kata” technique was based in the principle of “learn by repetition”.
By practicing exercises in a repetitive maner the learner  comes to a point where develops the ability to execute those exercises and movements in a natural and reflexive maner(“intuition”).

Today in software development the amount of trainers that recommend to use Test Driven Development Katas, is increasing.  Some would even say that the TDD katas, are the only way to learn TDD.  The original idea of the kata as a maner of learning software development was introduced by Dave Thomas(author of: The pragmatic Programmer: From Journey man to Master ).

Programming principles such as KISS(Keep It Simple and Stupid) or YAGNI(You Ain’t Gonna Need It ) are very common in the TDD philosophy, but the main mantra behind TDD is the RGR principle (Red, Green, Refactor).

From the theoretical point of view, the RGR principle seems often very simple to new TDD developers.
But when it comes to real life practice, in many occasions the complexity of the software can make less experienced developers to panic.

A great practice to avoid this is to practice a TDD kata once in a while.
Peter Norvost(author of the blog: Geek Noise), recommends to new TDD developers to do simple katas of 30 minutes every morning for 2 weeks. 
Now I will show you an example of a very simple TDD kata and then I will perform a demonstration.

KATA #1: “THE STRING CALCULATOR” (Kata description taken from: http://osherove.com/tdd-kata-1/)
1-An empty string returns zero
2-A single number returns the value
3-Two numbers, comma delimited, returns the sum
4-Two numbers, newline delimited, returns the sum
5-Three numbers, delimited either way, returns the sum
6-Negative numbers throw an exception
7-Numbers greater than 1000 are ignored



Note: Don't forget to set the quality to HD in the video player





Monday, August 13, 2012

Subversion Native Library Not Available

Did you ever have to deal with this annoying Subversion update message when Starting your Eclipse IDE?


To avoid it popping up, just go to your preferences and change the SVN interface.


Friday, August 10, 2012

Dynamic initialization of HashMap using double braces

Often we have to work with data structures like: Maps, Arrays,Sets...
And many times it is just more comfortable to initialize them dynamically.

In the following example i use double brace initialization technique to dynamically initialize a map that uses a constant as a key and a List as a value. When we want to initialize this types of structures in a dynamical way, this approach is probably the most comfortable and easy.


1:  new HashMap<ProductCategories, List<Products>>() {  
2:                 {  
3:                      put(ProductCategories.DAIRY_PRODUCTS,  
4:                                new ArrayList<Products>() {  
5:                                     {  
6:                                          add(Products.CHEESE);  
7:                                          add(Products.SOUR_CREAM);  
8:                                          add(Products.YOGURTH);  
9:                                     }  
10:                                 }  
11:                      );  
12:                      put(ProductCategories.MEAT,   
13:                                new ArrayList<Products>() {  
14:                                     {  
15:                                          add(Products.BEEF);  
16:                                          add(Products.CHICKEN);  
17:                                          add(Products.TURKEY);  
18:                                     }  
19:                                }  
20:                      );  
21:                      put(ProductCategories.VEGETABLES,   
22:                                new ArrayList<Products>() {  
23:                                     {  
24:                                          add(Products.TOMATO);  
25:                                          add(Products.ONION);  
26:                                          add(Products.CAGABE);  
27:                                     }  
28:                                }  
29:                      );  
30:                 }  
31:            };  

Sunday, June 10, 2012

Asynchronous communication with JMS(Java Messaging Service) - Part 1

Note: Don't forget to set the quality to HD in the video player




The JMS is a wide topic but that it is very important for a java developer to understand. The use of asynchronous processing in software development is very important and for many systems it is a must.

In this video i wanted to give some brief introduction to JMS and also do some simple to understand practical example that shows how different processes can be performed without the need of having both client and the server waiting for each other until a task is finished.

java, JMS, Queue, Java Messaging System, Asynchronous Processing, Tutorial

Wednesday, May 30, 2012

How many lines of java code does your workspace contain

Did you ever wonder how many lines of java code does your workspace have? Here a little program that can help you find out:

Class 1:

1:  package sample;  
2:  import java.io.File;  
3:  import java.util.Scanner;  
4:  public class LinesOfCodeCounter {  
5:    private long linesOfCode;  
6:    private int scannedProjects;  
7:    public void analizeFolder(File file) {  
8:  //Check if the current file is a file or a folder  
9:      if (file.isDirectory()) {  
10:  //If it is a folder get the inner files  
11:        File[] filesInFolder = file.listFiles();  
12:  //For each of the inner files check if it is a folder or a file(Recursion)  
13:        for (File innerFile : filesInFolder) {  
14:          analizeFolder(innerFile);  
15:        }  
16:      } else if (file.isFile()) {  
17:  //When you find a file that is not a folder, check if it contains java code  
18:        if (file.getName().endsWith(".java")) {  
19:  //Scan the file  
20:          scanFile(file);  
21:        }  
22:      }  
23:    }  
24:    public void scanFile(File file) {  
25:      try {  
26:        Scanner fileScanner = new Scanner(file);  
27:  //Read each line of code  
28:        String tempDataHolder = fileScanner.nextLine();  
29:  //While the file contain lines of code count them  
30:        while (fileScanner.hasNext()) {  
31:          linesOfCode++;  
32:          tempDataHolder = fileScanner.nextLine();  
33:        }  
34:      } catch (Exception ex) {  
35:        ex.printStackTrace();  
36:      }  
37:    }  
38:    public long getLinesOfCode() {  
39:      return linesOfCode;  
40:    }  
41:  }  



Class 2:
1:  package sample;  
2:  import java.io.File;  
3:  public class Main {  
4:    public static void main(String[] args) {  
5:      //The workspace root  
6:      final File workSpaceRoot = new File("D:\\JavaWorkspace");  
7:      //Check if the root contains files inside its self  
8:      LinesOfCodeCounter counter = new LinesOfCodeCounter();  
9:      counter.analizeFolder(workSpaceRoot);  
10:      System.out.print(counter.getLinesOfCode());  
11:    }    
12:  }  

Saturday, May 26, 2012

Clustering with Glassfish 3.1

                  

 

 Here I use the command line to create a cluster in the application server Glassfish 3.1 and add 2 instances to it.
Once the cluster is created I deploy an enterprise application and run it in both instances.

Tuesday, May 1, 2012

File realm & role based security with Glassfish 3.1






Here in this video you can see how i create a simple security realm that uses a file in a safe place in the app server to store the credentials. One thing i forgot was to show you how to logout :)
Don't worry is not very difficult, for that you can implement your own logout servlet to invalidate the session.
Here is some example of code for logging out:


1:  @WebServlet(name = "LogoutServlet", urlPatterns = {"/logout"})  
2:   public class LogoutServlet extends HttpServlet {  
3:   @Override  
4:   protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
5:    //Invalidates the session  
6:    request.getSession(false).invalidate();  
7:    // Redirects back to the initial page.  
8:    response.sendRedirect(request.getContextPath());  
9:   }  
10:   }  

Also here there is an image to show where can you enable HTTPS for the admin panel:


Note: The SSL certificate that comes with glassfish is expired that is the reason why you will see a warning message before you login.

Share with your friends