Java

Spock Framework with Groovy Example

Posted on

Spock is a testing and specification framework for Java and Groovy application.

http://spockframework.org/

 

Spock Framework with Groovy Example:-

Spock Framework gives you a highly expressive test cases in you test module. It is inspired from several existing testing framework like JUnit, RSpec, jMock, Mockito, Groovy, Scala, Vulcans.

Before starting Spock Framework in your project, you should have at least basic knowledge of Groovy, It’s not very hard to learn even it similar to Java – [ What is Groovy ].

Let start with Spock Specification which describes expected features ( Properties, Aspect ) . A Specification represented by Groovy Class that extends from import spock.lang.Specification. 

Class Specification contains a number of useful methods for writing specifications.

As JUnit, Spock also provides some predefined method for setup and cleanup method as given below:-

def setup() {}          // run before every feature method
def cleanup() {}        // run after every feature method
def setupSpec() {}     // run before the first feature method
def cleanupSpec() {}   // run after the last feature method

@shared:

Sometimes an object needs to be shared between features methods rather than creating it every time in features method level which may be expensive to recreate every time. so we need to share that object between features therefore, @Shared  annotation helps us to do this job.

Objects stored into instance fields are not shared between feature methods. Instead, every feature method gets its own object. This helps to isolate feature methods from each other, which is often a desirable goal.

@shared
def connection = new DBConnection();

Block : –

Block is a predefined set of a label that supported by Spock Framework. Usually, Block is an area where a setup, stimulus, response, condition, expectation and cleanup is described.

Blocks2Phases

below are the complete example of Spock specification that would help us to understand.

@Unroll :-

As you can see in below Spock Test example, @Unroll annotation are used in some features, By putting @Unroll annotation on the feature. it means an iteration is required on feature and on each iteration values given in where block get substituted by its value and leading hash sign (#), to refer to data variables.

@Unroll
def "Power(#base,#exponent) == #result1 and Power(#base,2) == #result2"() {
    expect:
    Math.pow(base, exponent) == result1
    and:
    Math.pow(base, 2) == result2
    where:
    base | exponent || result1 | result2
    1    | 2        || 1 | 1
    2    | 3        || 8 | 4
    3    | 4        || 81 | 9
    4    | 5        || 1024 | 16
}

Prerequisites

  • JDK 5 or higher
  • Ant 1.7 or higher (for Ant build)
  • Maven 2.x (for Maven build)

Creating an IDEA Project:-

  • Create a Java project in IntellJ and set up project source folder and testing source folder under the project.

 

 

 

 

 

 

 

  • Create a pom.xml file under root folder and copy and paste below contents and build this project by command line or IntelliJ.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spock.example</groupId>
    <artifactId>GroovySpockExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>spring_boot</name>
    <description>Demo Example of Groovy with Spock</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.0-groovy-2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/*Spec.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

  • Create a Groovy Test file under test folder
import spock.lang.Specification
import spock.lang.Unroll

/**
 * Created by Ajay Kumar on 4/7/2017.
 */
class GroovyTest extends Specification {

    def "Testing:- Null is not allowed in Synchronized Hashtable Collection"() {
        given: "Initialize a Hashtable Connection "
        def table = new Hashtable()
        when: " When an element insert into it"
        table.put("One", "One")
        then: "Size is One"
        table.size() == 1
        when: " When insert a null value"
        table.put(null, null)
        then: "NullPointerException thrown"
        thrown(NullPointerException)
    }

    def "Testing:- Only One null value is allowed in Set Collection"() {
        given: "Initialize a HashSet Connection "
        def set = new HashSet()
        when: " When an element insert into it"
        set.add("One")
        and: " And add a Null element into it"
        set.add(null)
        and: "And add another null value"
        set.add(null)
        then: "Size is One"
        set.size() == 2
    }

    def "Testing ArrayList"() {
        given:
        def array = new ArrayList();
        when: "Push an element into Array"
        array.add("Hello")
        then: "One element is expected in Array"
        array.size() == 1
        array.isEmpty() == false
        when: "Pop out element from Array"
        array.remove(0)
        then:
        array.size() == 0
        when: "When we get elements from Array"
        array.get(0)
        then:
        thrown(IndexOutOfBoundsException)
    }

    @Unroll
    def "Power of #val is #result"() {
        expect:
        Math.pow(val, 2) == result
        where:
        val || result
        1   || 1
        2   || 4
        3   || 9
        4   || 16
    }

    @Unroll
    def "Power(#base,#exponent) is #result"() {
        expect:
        Math.pow(base, exponent) == result
        where:
        base | exponent || result
        1    | 2        || 1
        2    | 3        || 8
        3    | 4        || 81
        4    | 5        || 1024
    }

    @Unroll
    def "Power(#base,#exponent) == #result1 and Power(#base,2) == #result2"() {
        expect:
        Math.pow(base, exponent) == result1
        and:
        Math.pow(base, 2) == result2
        where:
        base | exponent || result1 | result2
        1    | 2        || 1 | 1
        2    | 3        || 8 | 4
        3    | 4        || 81 | 9
        4    | 5        || 1024 | 16
    }

    @Unroll
    def "Computing the maximum of (#a,#b) is #c"() {
        expect:
        Math.max(a, b) == c

        where:
        a << [5, 3]
        b << [1, 9]
        c << [5, 9]
    }

    @Unroll
    def " #name should have length #length "() {
        expect:
        name.length() == length
        where:
        name << ["Computing", "Should", "Have", "Name"]
        length << [9, 6, 4, 4]
    }
}

Here is Test output:-

 

Thanks

Reading/Writing Files using FileChannel

Posted on

6db3a-download

There are multiple ways to reading/writing a file in Java. Usually, we used java.lang.IO package to read/write a file but as my last experience with copying a large file size (>256 MB ) into some other file location or on a network.

 

As we also tried to used java.lang.io package to get this job done but we were unable to copy the file in a Network location, there were performance issues.

http://stackoverflow.com/questions/1605332/java-nio-filechannel-versus-fileoutputstream-performance-usefulness

And finally, I got an alternative way to copy a file into a Network location  i.e FileChannel . its provide a better performance than InputStream/OutputStream.

Some interesting points about java.nio or FileChannel:-

  1. it’s a buffer-oriented, means- same Buffer is used to reading / writing so you can move back or forth in the Buffer as you need to.  usee flip() method to get the pointer to next position.
  2. It’s a non-blocking, i.e means a Thread request to reading data from Channel then only get what is currently available in a buffer, if data is not available then rather than blocking until data get available in buffer, Thread goes on and do something else in meantime. The same is true for non-blocking writing. A thread can request that some data be written to a channel, but not wait for it to be fully written. The thread can then go on and do something else in the mean time.
  3. Concurrent processing can be done in FileChannel which is safe for use.
  4. Channels can be read and written asynchronously.

Below example demonstrated how to write into a file using FileChannel.


 import java.io.File;
        import java.io.FileNotFoundException;
        import java.io.IOException;
        import java.io.RandomAccessFile;
        import java.nio.ByteBuffer;
        import java.nio.channels.FileChannel;
        import java.nio.channels.WritableByteChannel;
        
        /**
         * Created by kumajye on 10/01/2017.
         */
        public class FileChannelTest {
            // This is a Filer location where write operation to be done.
            private static final String FILER_LOCATION = "C:\\documents\\test";
            // This is a text message that to be written in filer location file.
            private static final String MESSAGE_WRITE_ON_FILER = "Operation has been committed.";
        
            public static void main(String[] args) throws FileNotFoundException {
                // Initialized the File and File Channel
                RandomAccessFile randomAccessFileOutputFile = null;
                FileChannel outputFileChannel = null;
                try {
                    // Create a random access file with 'rw' permission..
                    randomAccessFileOutputFile = new RandomAccessFile(FILER_LOCATION + File.separator + "readme.txt", "rw");
                    outputFileChannel = randomAccessFileOutputFile.getChannel();
                    //Read line of code one by one and converted it into byte array to write into FileChannel.
                    final byte[] bytes = (MESSAGE_WRITE_ON_FILER + System.lineSeparator()).getBytes();
                    // Defined a new buffer capacity.
                    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
                    // Put byte array into butter array.
                    buffer.put(bytes);
                    // its flip the buffer and set the position to zero for next write operation.
                    buffer.flip();
                    /**
                     * Writes a sequence of bytes to this channel from the given buffer.
                     */
                    outputFileChannel.write(buffer);
                    System.out.println("File Write Operation is done!!");
        
                } catch (IOException ex) {
                    System.out.println("Oops Unable to proceed file write Operation due to ->" + ex.getMessage());
                } finally {
                    try {
                        outputFileChannel.close();
                        randomAccessFileOutputFile.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
        
                }
        
            }
        
        }


How to Schedule Future Task Using java.util.Timer

Posted on Updated on

java.util.Timer

6db3a-downloadA java.util.Timer class provides a facility to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals. Timer class uses java.util.TaskQueue to add tasks at given regular interval and at any time there can be only one thread running the TimerTask.

TimerTask :-

java.util.TimerTask is an abstract class that implements Runnable interface and we need to extend this class to create our own TimerTask that can be scheduled using java Timer class.

Key points :-

  • java.util.Timer is a thread safe – means, multiple threads can share a single Timer object without the need for external synchronization.
  • This class schedules tasks for one-time execution, or for repeated execution at regular intervals.
  • All constructors start a timer thread.
  • This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method.

Class Constructors :-

  • Timer() – Create a new Timer
  • Timer(boolean isDaemon) – Creates a new timer whose associated thread may be specified to run as a daemon.
  • Timer(String name) – Creates a new timer whose associated thread has the specified name.
  • Timer(String name, boolean isDaemon) – Creates a new timer whose associated thread has the specified name, and may be specified to run as a daemon.

Class Methods :-

  • cancel() – Terminates this timer, discarding any currently scheduled tasks.
  • purge() – Removes all cancelled tasks from this timer’s task queue.
  • schedule(TimerTask task,Date time) –  Schedules the specified task for execution at the specified time.
  • schedule(TimerTask task,Date firstTime,long period) – Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
  • schedule(TimerTask task,long delay) – Schedules the specified task for execution after the specified delay.
  • schedule(TimerTask task,long delay,long period) – Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
  • scheduleAtFixedRate(TimerTask task, Date firstTime, long period) – Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
  • scheduleAtFixedRate(TimerTask task, long delay, long period) – Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.

Example :-

Here, we scheduled a non-repeated future task that will be print on a Console after the delay of 10 min. TimerTask is a task that can be scheduled for one-time or repeated execution by a Timer and it is a sub-class of Runnable Interface.


import java.util.Timer;
import java.util.TimerTask;
/**
 * Created by MYPC on 10/8/2016.
 */
public class FutureExecutionExample {
    public static void main(String[] args) {
        Timer t = new Timer();
        t.schedule(new TimerTask() {
                  @Override
                  public void run() {
                  // Execute given scheduled task after 10 min.
                  System.out.println(" Run specific task at given time." + System.currentTimeMillis());
               }
             }
         , 10 * 60 * 1000);  // 10 mins
    }
}


Let assume, we create a Timer to run every 2 seconds interval but the execution of Thread takes 3 seconds then Timer keeping adding a schedule task in queue and as soon as Thread execution get finished then, it’s notify to queue to execute next one.


Timer t = new Timer();
t.schedule(new TimerTask() {
       @Override
       public void run() {
           System.out.println(" Run specific task at given time." + System.currentTimeMillis());
           try {
               Thread.sleep(3000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
   }

, 1000,2000);  // After 1 second given task will be executed and every 2 second given task execution repeat.
}


Java 8 – Consumer Example

Posted on Updated on

Java 8 – Consumer Example 

Consumjava-8-lambdaer<T> introduced in Java 8.  Although it is a Functional Interface that  accepts only one argument and  operation to be performed on that argument but do not return anything.

Since It has only one method i.e

accept( T t);  //Performs this operation on the given argument.

and another method which is a default method– As you know that since Java 8, An Interface can be  a default method implementation which is denoted as default keyword.

default Consumer<T> andThen(Consumer<? super T> after)

Basically, this default method performs a sequence of composed consumers operation that followed by the after operation.

Below is some example that’s build up you understanding of Consumer<T> concept :-

 


package com.example1;

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by MYPC on 9/19/2016.
 */
public class ConsumerTest {

    public static void main(String[] args) {

        /**
         * Example 1
         */
        List integerList = Stream.of(new Integer(1), new Integer(10), new Integer(200), new Integer(101), new Integer(-10), new Integer(0))
                .collect(Collectors.toList());
        forEach(integerList, (number) -> {
            System.out.println(" Number is " + number);
        });

        /**
         * Example 2
         */
        Consumer consumer = (number) -> {
            number += number;
            System.out.println(number);
        };

        /**
         * Example 3
         */
        consumer.accept(2000);
        getConsumer().accept(1000);

        /**
         * Example 4
         */
        integerList.forEach((num -> {
            System.out.println("Java 8 forEach Loop" + num);
        }));


    }

    /**
     * Return consumer that to be applied on accept(T t) parameter.
     *
     * @return
     */
    public static Consumer getConsumer() {
        return (number) -> System.out.println("Method Invoked here " + number);
    }

    /**
     * Iterate over the Collection and to be consumed by console or other system.
     *
     * @param collections
     * @param consumer
     * @param 
     */
    public static  void forEach(Collection collections, Consumer consumer) {
        collections.forEach(consumer);
    }
}


Let’s move to some other example:-

In the example, we have tried to create a generic forEach loop that’s iterating over the generated Query result and consume by some other object.

ExecutorContext.Java – Object consume data producted by getQuery().


import java.util.List;
import java.util.function.Consumer;

interface ExecutorContext {
    List getQuery();
    Consumer getConsumer();
}


StubsUtils.Java : – Utility class


class StubsUtils {

    public static void forEach(ExecutorContext executorContext) {
        executorContext.getQuery().forEach(executorContext.getConsumer());
    }
}
 

Java8ConsumerExample.Java :- This is  a Consumer client that invoked forEach loop and override  ExecutorConext interface to produce & consume the query set elements.

Here, getQuery() method returns a Collection  but it could be any Result set query that will be produced a result set by JdbcTemplate. Other method  getConsumer() will be applied the desired operation over the each element of Collection returned by getQuery() method means printing on consoles and adding into a list.

Inside the getConsumer(), you can probably apply your logic to perform the operation over the Input argument.


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Java8ConsumerExample {

    private static List QUERY_RESULTSET_INTEGER = Arrays.asList(new Integer(1), new Integer(10), new Integer(200), new Integer(101), new Integer(-10), new Integer(0));
    private static List QUERY_RESULTSET_STRING = Stream.of("A", "B", "C", "D", "E", "F").collect(Collectors.toList());

    public static void main(String[] args) {

        // EXAMPLE : 1

        /**
         * Iterator over the Query generated integer list and print on console.
         */
        StubsUtils.forEach(new ExecutorContext() {
            List consumerList = new ArrayList();

            /**
             * Let's assume that our query returns set of Integer that would be consume by some caller class or print on console.
             * @return
             */
            @Override
            public List getQuery() {
                return QUERY_RESULTSET_INTEGER;
            }

            @Override
            public Consumer getConsumer() {
                return x -> {
                    System.out.println(x);
                    consumerList.add(x);
                };
            }
        });

        // EXAMPLE : 2
        /**
         * Iterator over the Query generated String list and print on console.
         */
        StubsUtils.forEach(new ExecutorContext() {
            List consumerList = new ArrayList();

            /**
             * Let's assume that our query returns set of Integer that would be consume by some caller class or print on console.
             * @return
             */
            @Override
            public List getQuery() {
                return QUERY_RESULTSET_STRING;
            }

            @Override
            public Consumer getConsumer() {
                return x -> {
                    System.out.println(x);
                    consumerList.add(x);
                };
            }
        });

    }
}


Output:-

1 10 200 101 -10 0
A B C D E F

How to read/write CSV file to Map in java

Posted on Updated on

As you see in last article, how to read a CSV file into Java object with the help of Jackson API. In this article, we will see how to write a list of Map ( List<Map<String,String>> ) into a CSV file in your machine and again read it through with Jackson API.

Add this dependency in your pom.xml file to used Jackson API in your application.

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-csv</artifactId>
  <version>2.7.0</version>
</dependency>

 

Below are a sample example of reading or writing a CSV file with the help of Jackson API, which makes it very easy to write or read the CSV file into Java Object.

 


package com.example;

import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.io.*;
import java.util.*;

/**
 * Created by MYPC on 8/1/2016.
 */
public class ConvertMapToCSVTest {
    public static void main(String[] args) throws IOException {

        List<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        /*** Rows 1 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C001");
        map.put("Name", "Ajay Kumar");
        map.put("Email", "ajay@gmail.com");
        map.put("CountryCode", "TH");
        map.put("Budget", "1000000");
        map.put("Used", "600000");
        myArrList.add(map);

        /*** Rows 2 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C002");
        map.put("Name", "Rahul Kumar");
        map.put("Email", "Rahul.kumar@gmail.com");
        map.put("CountryCode", "UK");
        map.put("Budget", "2000000");
        map.put("Used", "800000");
        myArrList.add(map);

        /*** Rows 3 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C003");
        map.put("Name", "Wasim Akram");
        map.put("Email", "waism.atnet@rediff.com");
        map.put("CountryCode", "US");
        map.put("Budget", "3000000");
        map.put("Used", "600000");
        myArrList.add(map);

        /*** Rows 4 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C004");
        map.put("Name", "Santosh Kumar");
        map.put("Email", "santosh.ku@yahoo.com");
        map.put("CountryCode", "US");
        map.put("Budget", "4000000");
        map.put("Used", "100000");
        myArrList.add(map);

        File file = new File("d:\\sample.csv");
        // Create a File and append if it already exists.
        Writer writer = new FileWriter(file, true);
        Reader reader = new FileReader(file);
        //Copy List of Map Object into CSV format at specified File location.
        csvWriter(myArrList, writer);
        //Read CSV format from specified File location and print on console..
        csvReader(reader);
    }

    /**
     * @param listOfMap
     * @param writer
     * @throws IOException
     */
    public static void csvWriter(List<HashMap<String, String>> listOfMap, Writer writer) throws IOException {
        CsvSchema schema = null;
        CsvSchema.Builder schemaBuilder = CsvSchema.builder();
        if (listOfMap != null && !listOfMap.isEmpty()) {
            for (String col : listOfMap.get(0).keySet()) {
                schemaBuilder.addColumn(col);
            }
            schema = schemaBuilder.build().withLineSeparator("\r").withHeader();
        }
        CsvMapper mapper = new CsvMapper();
        mapper.writer(schema).writeValues(writer).writeAll(listOfMap);
        writer.flush();
    }

    /**
     * 
     * @param collection
     * @param writer
     * @param 
     * @throws IOException
     */
    public static  void csvWriter(Collection collection, Writer writer) throws IOException {
        if (collection != null && collection.size() > 0) {
            CsvMapper mapper = new CsvMapper();
            Object[] objects = collection.toArray();
            Class type = objects[0].getClass();
            CsvSchema schema = mapper.schemaFor(type).withHeader();
            mapper.writer(schema).writeValues(writer).write(objects);
        } else {
            writer.write("No Data");
        }
        writer.flush();
    }


    /**
     * @param reader
     * @throws IOException
     */

    public static void csvReader(Reader reader) throws IOException {
        Iterator<Map<String, String>> iterator = new CsvMapper()
                .readerFor(Map.class)
                .with(CsvSchema.emptySchema().withHeader())
                .readValues(reader);
        while (iterator.hasNext()) {
            Map<String, String> keyVals = iterator.next();
            System.out.println(keyVals);
        }
    }
}


A CSV file is created at specified location i.e d:\\sample.csv in file system.

Email,Used,Budget,CustomerID,CountryCode,Name
ajay@gmail.com,600000,1000000,C001,TH,"Ajay Kumar"
Rahul.kumar@gmail.com,800000,2000000,C002,UK,"Rahul Kumar"
waism.atnet@rediff.com,600000,3000000,C003,US,"Wasim Akram"
santosh.ku@yahoo.com,100000,4000000,C004,US,"Santosh Kumar"

Reading sample.csv file from file system.

{Email=ajay@gmail.com, Used=600000, Budget=1000000, CustomerID=C001, CountryCode=TH, Name=Ajay Kumar}
{Email=Rahul.kumar@gmail.com, Used=800000, Budget=2000000, CustomerID=C002, CountryCode=UK, Name=Rahul Kumar}
{Email=waism.atnet@rediff.com, Used=600000, Budget=3000000, CustomerID=C003, CountryCode=US, Name=Wasim Akram}
{Email=santosh.ku@yahoo.com, Used=100000, Budget=4000000, CustomerID=C004, CountryCode=US, Name=Santosh Kumar}

Hoping, It would be helpful !! Thanks

Jackson JSON Java Parser Example

Posted on

Jackson JSON Parser API provides easy way to convert JSON to POJO Object and supports easy conversion to Map from JSON data. Jackson supports generics too and directly converts them from JSON to object.

we can add below dependency in your pom.xml.

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.0</version>
</dependency>

jackson-databind jar depends on jackson-core and jackson-annotations libraries, so if you are adding them directly to build path, make sure you add all three otherwise you will get a runtime error.

Below is the Utilities class that uses Jackson API to transform String to Json to Map object or any Java type object or vice-versa.   ThreadLocal is used here because ObjectMapper is not Thread’s safe in Multithreading app otherwise simply create a static variable of ObjectMapper.


package com.example.utilities;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.Map;

/**
 * Created by MYPC on 7/18/2016.
 */
public class JSONConverter {

    protected static final ThreadLocal OBJECT_MAPPER_THREAD_LOCAL = ThreadLocal.withInitial(() -> new ObjectMapper());

    /**
     * Convert String as JSON to JsonNode.
     *
     * @param payload
     * @return
     */
    public static JsonNode getJsonFromString(String payload) {
        try {
            return OBJECT_MAPPER_THREAD_LOCAL.get().readTree(payload);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Convert JsonNode Object to Map
     *
     * @param payload
     * @return
     */
    public static Map getMapFromJson(JsonNode payload) {
        try {
            return OBJECT_MAPPER_THREAD_LOCAL.get().readerFor(Map.class).readValue(payload);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Convert Map Object to JsonNode.
     *
     * @param payload
     * @return
     */
    public static JsonNode getJsonFromMap(Map payload) {
        try {
            return getJsonFromString(OBJECT_MAPPER_THREAD_LOCAL.get().writeValueAsString(payload));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Create a Empty Object Node. i.e {}
     *
     * @return
     */
    public static ObjectNode createObjectNode() {
        return OBJECT_MAPPER_THREAD_LOCAL.get().createObjectNode();
    }

    /**
     * Create a Empty Array Json Object i.e [{},{}]
     *
     * @return
     */
    public static ArrayNode createArrayNode() {
        return OBJECT_MAPPER_THREAD_LOCAL.get().createArrayNode();
    }

    /**
     * Remove particular key elements from JsonNode.
     *
     * @param payload
     * @param key
     * @return
     */
    public static JsonNode remove(JsonNode payload, String key) {
        ((ObjectNode) payload).remove(key);
        return payload;
    }

    public static void main(String[] args) {
        String payload = "{\"id\":\"1001\", \"amount\":\"10000045.999\", \"currency\":\"EUR\"}";
        System.out.println(" Payload String  >> " + payload);
        JsonNode jsonNode = getJsonFromString(payload);
        System.out.println(" Convert String Payload to JsonNode  >> " + payload);
        Map map = getMapFromJson(getJsonFromString(payload));
        System.out.println(" GET Map Object from Payload String >> " + map);
        System.out.println(" Before Removing 'amount' from payload >> " + jsonNode);
        remove(jsonNode, "amount");
        System.out.println(" After Removing 'amount' from payload >> " + jsonNode);
        System.out.println(" Convert Map to JSON Node Object  >>" + getJsonFromMap(map));

        System.out.println(" Employee JsonNode >>" + createEmployeeJson());
        System.out.println(" Convert Employee Json to Map Object >>" + getMapFromJson(createEmployeeJson()));
    }

    /**
     * Testing on some real data.
     * employee.json
     * {
     * "id":"123",
     * "name":"Rahul Kumar",
     * "address":{
     *      "street":"Kalkaji",
     *      "city":"Delhi",
     *      "zipcode":"110020"
     * },
     * "phoneNumbers":[
     *      "9923456469",
     *      "9886786786"
     * ],
     * "role":"Developer"
     * }
     *
     * @return
     */
    private static JsonNode createEmployeeJson() {
        ObjectNode employeeJsonNode = createObjectNode();
        ObjectNode addressNode = createObjectNode();
        ArrayNode phoneList = createArrayNode();
        addressNode.put("street", "Kalkaji");
        addressNode.put("city", "Delhi");
        addressNode.put("zipcode", "110020");

        phoneList.add("9923456469");
        phoneList.add("9886786786");

        employeeJsonNode.put("id", "123");
        employeeJsonNode.put("name", "Rahul Kumar");
        employeeJsonNode.set("address", addressNode);
        employeeJsonNode.set("phoneNumbers", phoneList);
        employeeJsonNode.put("role", "Developer");
        return employeeJsonNode;
    }
}


output:-

Payload String >> {"id":"1001", "amount":"10000045.999", "currency":"EUR"}
 Convert String Payload to JsonNode >> {"id":"1001", "amount":"10000045.999", "currency":"EUR"}
 GET Map Object from Payload String >> {id=1001, amount=10000045.999, currency=EUR}
 Before Removing 'amount' from payload >> {"id":"1001","amount":"10000045.999","currency":"EUR"}
 After Removing 'amount' from payload >> {"id":"1001","currency":"EUR"}
 Convert Map to JSON Node Object >>{"id":"1001","amount":"10000045.999","currency":"EUR"}
 Employee JsonNode >>{"id":"123","name":"Rahul Kumar","address":{"street":"Kalkaji","city":"Delhi","zipcode":"110020"},"phoneNumbers":["9923456469","9886786786"],"role":"Developer"}
 Convert Employee Json to Map Object >>{id=123, name=Rahul Kumar, address={street=Kalkaji, city=Delhi, zipcode=110020}, phoneNumbers=[9923456469, 9886786786], role=Developer}

How to read CSV file in Java

Posted on

Jackson data format module for reading and writing CSV-encoded data, either as “raw” data (sequence of String arrays ) , or via data binding to/from Java Objects (POJOs).

To use this extension on Maven-based projects, use following dependency:

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-csv</artifactId>
  <version>2.7.0</version>
</dependency>

Read CSV file based on Java Class.

AS below schema Java Object have explicitly defined Order and properties type to ensured that reading or writing the data in given order.


package com.example.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonRootName;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * Created by MYPC on 7/18/2016.
 */
@JsonPropertyOrder(value = {"id", "amount", "currency"})
@JsonRootName("transaction")
public class Transaction implements Serializable {
    public enum Currency {
        INR, HKD, EUR, USD
    }
    @JsonProperty
    private String id;
    @JsonProperty
    @JsonFormat(shape = JsonFormat.Shape.NUMBER)
    private BigDecimal amount;
    @JsonProperty
    private Currency currency;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public double getAmount() {
        return amount.doubleValue();
    }
    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }
    public Currency getCurrency() {
        return currency;
    }
    public void setCurrency(Currency currency) {
        this.currency = currency;
    }
    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Transaction{");
        sb.append("id='").append(id).append('\'');
        sb.append(", amount=").append(amount);
        sb.append(", currency=").append(currency);
        sb.append('}');
        return sb.toString();
    }
}


Trade.csv  file reside in project “/src/main/resources” folder.

id,amount,currency
"1001",10000045.999,"EUR"
"1002",17777045.999,"HKD"
"1003",10000045.999,"USD"
"1004",1234045.999,"EUR"
"1005",100875.999,"HKD"
"1006",165780045.999,"EUR"
"1007",32452345.999,"INR"
"1008",7777777.999,"EUR"

 

Reading CSV file to Java Object.


package com.example;

import com.example.model.Transaction;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.io.IOException;
import java.util.List;
/**
 * Created by MYPC on 7/18/2016.
 */
public class JSONSchemaReaderApp {

    public static void main(String[] args) throws IOException {

        CsvMapper csvMapper = new CsvMapper();
        CsvSchema csvSchema = csvMapper.typedSchemaFor(Transaction.class).withHeader();
        List list = new CsvMapper().readerFor(Transaction.class)
                .with(csvSchema.withColumnSeparator(CsvSchema.DEFAULT_COLUMN_SEPARATOR))
                .readValues(JSONSchemaReaderApp.class.getClassLoader().getResource("trade.csv"))
                .readAll();
        for (int i = 0; i < list.size(); i++) {
            System.out.printf(" Transaction Row [%d] : %s \n", i + 1, list.get(i));
        }
    }
}


Output:-

Transaction Row [1] : Transaction{id='1001', amount=10000045.999, currency=EUR}
Transaction Row [2] : Transaction{id='1002', amount=17777045.999, currency=HKD}
Transaction Row [3] : Transaction{id='1003', amount=10000045.999, currency=USD}
Transaction Row [4] : Transaction{id='1004', amount=1234045.999, currency=EUR}
Transaction Row [5] : Transaction{id='1005', amount=100875.999, currency=HKD}
Transaction Row [6] : Transaction{id='1006', amount=165780045.999, currency=EUR}
Transaction Row [7] : Transaction{id='1007', amount=32452345.999, currency=INR}
Transaction Row [8] : Transaction{id='1008', amount=7777777.999, currency=EUR}

Reading CSV file into Map


public static void main(String[] args) throws IOException {
        CsvMapper csvMapper = new CsvMapper();

        CsvSchema csvSchema = csvMapper.typedSchemaFor(Map.class).withHeader();
        MappingIterator<Map<String, String>> it = csvMapper.readerFor(Map.class).with(csvSchema.withColumnSeparator(CsvSchema.DEFAULT_COLUMN_SEPARATOR))
                .readValues(JSONSchemaReaderApp.class.getClassLoader().getResource("trade.csv"));
        List<Map<String, String>> listOfMapSchema = it.readAll();

        for (Map<String, String> map : listOfMapSchema) {
            System.out.println(map);
        }

    }

output:-

{id=1001, amount=10000045.999, currency=EUR}
{id=1002, amount=17777045.999, currency=HKD}
{id=1003, amount=10000045.999, currency=USD}
{id=1004, amount=1234045.999, currency=EUR}
{id=1005, amount=100875.999, currency=HKD}
{id=1006, amount=165780045.999, currency=EUR}
{id=1007, amount=32452345.999, currency=INR}
{id=1008, amount=7777777.999, currency=EUR}