Posts

Showing posts from April, 2014

Parameterized JUnit Tests by Example

public class ReverseString { public static void main(String[] args) { String str = "abracadabra0"; System.out.println(reverseStr(str)); } public static String reverseStr(String str) { StringBuilder sb = new StringBuilder(); for (int i = str.length() - 1; i >= 0; i--) { sb.append(str.charAt(i)); } return sb.toString(); } } import static org.junit.Assert.assertEquals; import java.util.Arrays; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class ReverseStringTest { String testStr = ""; String expectedStr = ""; @Rule public ExpectedException exception = ExpectedException.none(); public ReverseStringTest(String ts, String es) { this.testStr = ts; this.expectedStr = es; } @Parameters public static Iterable data() { …

Java Annotations by Example

import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Annotations are defined via the @interface annotation before the class name. * Via @Retention you define if the annotation should be retained at runtime or * not. The @Target annotation lets you define where this annotation can be * used, e.g. the class, fields, methods, etc. * * @author Watsh * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface CanRun { } public class AnnotatedMethodsClass { public void method1() { System.out.println("method1"); } @CanRun public void method2() { System.out.println("method2"); } @CanRun public void method3() { System.out.println("method3"); } public void method4() { System.out.println("method4"); } public void method5() {…

Map Reduce - Overview

MapReduce is a parallel and distributed solution approach developed by Google for processing large datasets. Described in this paper - http://research.google.com/archive/mapreduce.html Map transforms a set of data into key value pairs and Reduce aggregates this data into a scalar. A reducer receives all the data for an individual "key" from all the mappers. The approach assumes that there are no dependencies between the input data. This make it easy to parallelize the problem. The number of parallel reduce task is limited by the number of distinct "key" values which are emitted by the map function. MapReduce incorporates usually also a framework which supports MapReduce operations. A master controls the whole MapReduce process. The MapReduce framework is responsible for load balancing, re-issuing task if a worker as failed or is to slow, etc. The master divides the input data into separate units, send individual chunks of data to the mapper machines and collects the i…

REST: JAX-RS by Example

RESTAnnotation Description @PATH(your_path) Sets the path to base URL + /your_path. The base URL is based on your application name, the servlet and the URL pattern from theweb.xmlconfiguration file. @POST, @GET, @PUT, @DELETE Indicates that the following method will answer to an HTTP POST/GET/PUT/DELETE request, respectively. @Produces(MediaType.TEXT_PLAIN[, more-types]) @Produces defines which MIME type is delivered by a method annotated with @GET. @Consumes(type[, more-types]) @Consumes defines which MIME type is consumed by this method. @PathParam Used to inject values from the URL into a method parameter. This way you inject, for example, the ID of a resource into the method to get the correct object.
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

//Plain old Java Object it does not extend as class or implements
//an interface

//The class registers its methods for the HTTP GET request using the @GET annotation.

Java Messaging Service 1.1 - Overview

Image
JMS  Administrative tools allow you to bind destinations and connection factories into a JNDI namespace. A JMS client can then use resource injection to access the administered objects in the namespace and then establish a logical connection to the same objects through the JMS provider.
Point to point messaging ·Each message has only one consumer. ·A sender and a receiver of a message have no timing dependencies. The receiver can fetch the message whether or not it was running when the client sent the message. ·The receiver acknowledges the successful processing of a message. Use PTP messaging when every message you send must be processed successfully by one consumer.

Publish/subscribe messaging
·Each message can have multiple consumers. ·Publishers and subscribers have a timing dependency. A client that subscribes to a topic can consume only messages published after the client has created a subscription, and the subscriber must continue to be active in order for it to consume messages.
The …

CAP Theorem

Proposed by Eric Brewer. States that it is impossible for adistributed computer systemto simultaneously provide all three of the following guarantees:
Consistency – all nodes see the same data at the same time
Availability – a guarantee that every request receives a response about whether it was successful or failed
Partition tolerance – the system continues to operate despite arbitrary message loss when network is partitioned.
CA = If network is partitioned, the nodes continue to be available by themselves and data continues to be consistent on the same node even when there is no partition tolerance. AP = Nodes may still be available even when we ensure partition tolerance by disallowing further writes but data will not remain consistent between the nodes anymore as nodes cannot exchange message due to network partitioning. PC = Nodes may be consistent but in order to ensure partition tolerance they may not be available to write.
http://www.julianbrowne.com/article/viewer/brewers-cap-the…

Object Oriented Programming Principles - SOLID

SOLID 1. Single Responsibility Principle – there should not be more than one reason for a class to change, or a class should always handle single functionality. If you put more than one functionality in oneClass in Java it introducecouplingbetween two functionality and even if you change one functionality there is chance you broke coupled functionality,  which require another round of testing to avoid any surprise on production environment.

2. Open/Closed principle – Classes, methods or functions should be Open for extension (new functionality) and Closed for modification. The idea was that once completed, the implementation of a class could only be modified to correct errors; new or changed features would require that a different class be created. That class could reuse coding from the original class throughinheritance. The derived subclass might or might not have the sameinterfaceas the original class. Meyer's definition advocatesimplementation inheritance. Implementation can be r…

Design Patterns - Behavioral

Image
Behavioral1.Chain of Responsibility - delegates commands to a chain of processing objects. interface Chain {
publicabstractvoid setNext(Chain nextInChain);
publicabstractvoid process(Number request); }
class Number { privateintnumber;
public Number(int number) { this.number = number;        }
publicint getNumber() { returnnumber;        }
}
class NegativeProcessor implements Chain {
private Chain nextInChain;