Sunday, 12 April 2015

Playing with Java 9 and REPL (LJC & Oracle Hackday)

Playing with Java 9 and REPL  (LJC & Oracle Hackday)

Java REPL  => Read-Evaluate-Print-Loop

JDK 9 - https://jdk9.java.net/download/ - b57
Kulla - https://java.net/projects/adoptopenjdk/downloads/download/kulla.jar


/ /Tells you which JDKs you have

% /usr/libexec/java_home -verbose

// You can set that export command in your ~/.profile(or .zshrc )
// and/or ~/.bashrc to permanently set JAVA_HOME
// for all shells/terminals/applications.

% export JAVA_HOME="`/usr/libexec/java_home -v '1.8*'`"

// Set JAVA_HOME to be Java 9 for the terminal
// window you are in

% export JAVA_HOME="`/usr/libexec/java_home -v '1.9*'`"

open up you .profile(mac users or .zshrc if you use zsh) and add 

export JAVA_HOME="`/usr/libexec/java_home -v '1.8*'`"

PS :: Here after when you open a new terminal jdk 1.8 is going to be your primary JDK.


% java -jar kulla.jar
|  Welcome to the Java REPL -- Version 0.401
|  Type /help for help

-> /help
Type a Java language expression, statement, or declaration.
Or type one of the following commands:

/l  or /list [all]                -- list the source you have typed
       /seteditor <executable>    -- set the external editor command to use
/e  or /edit <name or id>         -- edit a source entry referenced by name or id
/s  or /save [all|history] <file> -- save the source you have typed
/o  or /open <file>               -- open a file as source input
/v  or /vars                      -- list the declared variables and their values
/m  or /methods                   -- list the declared methods and their signatures
/c  or /classes                   -- list the declared classes
/x  or /exit                      -- exit the REPL
/r  or /reset                     -- reset everything in the REPL
/f  or /feedback <level>          -- feedback information: off, concise, normal, verbose, default, or ?
/p  or /prompt                    -- toggle display of a prompt
/cp or /classpath <path>          -- add a path to the classpath
/h  or /history                   -- history of what you have typed
       /setstart <file>           -- read file and set as the new start-up definitions
       /savestart <file>          -- save the default start-up definitions to the file
/?  or /help                      -- this help message

Supported shortcuts include:
<tab>       -- show possible completions for the current text
Shift-<tab> -- for current method or constructor invocation, show a synopsis of the method/constructor


 -> String myFoo = "Hello World";
|  Added variable myFoo of type String with initial value "Hello World"

-> /e 11
|  No id 11 found.  See /list

 -> /e add
|  No definition named add found.  See /classes /methods or /vars


-> /reset
|  Resetting state.


Resetting the repl editor

-> /exit
|  Goodbye


The earlier problem of was of using the version Version 0.401.

Download the latest version

https://java.net/projects/adoptopenjdk/downloads/download/kulla.jar.

% java -jar kulla.jar
|  Welcome to the Java REPL -- Version 0.411
|  Type /help for help


-> String foo = "Hello World";
|  Added variable foo of type String with initial value "Hello World"

-> int add(int a, int b) {
>> return a+b;
>> }


-> String add(String a, String b) {
>> return a+b;
>> }
|  Added method add


Method overloading in REPL

  10 : int add(int a, int b) {
           return a+b;
       }
  11 : String add(String a, String b) {
       return a+b;
       }
  12 : add(10,100);
  13 : add ("Hello", "world");




-> /vars
|    String foo = "Hello World"
|    int $1 = 110
|    String $2 = "Helloworld"

-> /v
|    String foo = "Hello World"
|    int $1 = 110
|    String $2 = "Helloworld"


-> /methods
|    printf (Ljava/lang/String;[Ljava/lang/Object;)V
|    add (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
|    add (II)I

-> /m
|    printf (Ljava/lang/String;[Ljava/lang/Object;)V
|    add (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
|    add (II)I


-> /list

   9 : String foo = "Hello World";
  10 : int add(int a, int b) {
           return a+b;
       }
  11 : String add(String a, String b) {
       return a+b;
       }
  12 : add(10,100);
  13 : add ("Hello", "world");

-> /list all

   1 : import java.util.*;
   2 : import java.io.*;
   3 : import java.math.*;
   4 : import java.net.*;
   5 : import java.util.concurrent.*;
   6 : import java.util.prefs.*;
   7 : import java.util.regex.*;
   8 : void printf(String format, Object... args) { System.out.printf(format, args); }
   9 : String foo = "Hello World";
  10 : int add(int a, int b) {
           return a+b;
       }
  11 : String add(String a, String b) {
       return a+b;
       }
  12 : add(10,100);
  13 : add ("Hello", "world");


-> /history

does print or lists out all the commands we have typed in till now.










Saturday, 23 November 2013

Sample Spring TTD BDD :: Revisiting

Planning to revisit my old blog.

Going to explain Step by Step

> mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.geethsangam.music -DartifactId=Geethsangam


I am going to use eclipse

>  mvn eclipse:eclipse


You can change the pom.xml


<packaging>jar</packaging> to <packaging>pom</packaging>

This is going to be the parent pom for us

You can create the mvn module from the command prompt or from eclipse

From terminal


>cd Geethsangam

> mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.geethsangam.music -DartifactId=geethsangam-core


From Eclipse 


File > New > Maven > Maven module 

or

Right click on the project 

Maven > New Maven Module Project



Adding cucumber-jvm to projects

Add these 2 dependencies in the pom.xml

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.1.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.1.2</version>
        <scope>test</scope>

    </dependency>

Create Folders for cucumber BDD test


> cd Geethsangam

> mkdir cucumber

> cd cucumber

> mkdir features

 encies in the pom.xml

In the cucumber folder create test acceptance folder.

> mkdir test

Then Add the following class

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty""html:target/cucumber-html-report""json-pretty:target/cucumber-json-report.json"}, tags = "~@wip")
public class RunCukesTest {
}

Wednesday, 18 September 2013

Adding cucumber-jvm to projects

Add these 2 dependencies in the pom.xml

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.1.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.1.2</version>
        <scope>test</scope>

    </dependency>


In the Test add a acceptance folder


Then Add the following class

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber-html-report", "json-pretty:target/cucumber-json-report.json"}, tags = "~@wip")
public class RunCukesTest {
}


Now when you run 

>mvn test


It will complain that there are no feature file

Now in the resources folder based on your package structure create acceptance folder and then add a sample feature

Feature: Testing for sample core

Scenario Outline: Successful login

        Given I want to use the browser [browser]
        When I type testuser1 in the username input field
        When I type testuser1 in the password input field
        And I click the login button
        Then I am on the launcher page

        Examples:
                |     browser            |
                |     Firefox            |
                |     Chrome             |
                |     InternetExplorer   |

Now when you run >mvn test

it will ask us to implement the feature


You can implement missing steps with the snippets below:

@Given("^I want to use the browser \\[browser\\]$")
public void I_want_to_use_the_browser_browser() throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

@When("^I type testuser(\\d+) in the username input field$")
public void I_type_testuser_in_the_username_input_field(int arg1) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

you have configured cucumber-jvm to your projects


Adding Apache log4j

in pom.xml add the log4j dependency

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>

    </dependency>

in resources add a new file log4j.properties

# Root logger option
log4j.rootLogger=debug, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


Now you should be able to log in messages


For mockito add this dependency

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>

        </dependency>


We can have exception rules as

    @Rule

    public ExpectedException expectedEx = ExpectedException.none();

    @Test
    public void shouldAcceptOnlyIncrementOf100Messages() {
        expectedEx.expect(IllegalArgumentException.class);
        expectedEx.expectMessage("The number of messages per seconds must be multiple of 100 )");
        new FixedRateMessageInjector(queueDriver, 108, 2);
    }


In case if you want to use AWS add

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.4.1</version>

        </dependency>

Tuesday, 11 June 2013

UK Oracle Developer Day


Oracle Developer Day 2013

http://www.oracle.com/us/dm/h2fy11/180676-emeafm13047156mpp001c003-oem-1940507.html

Friday, 7 June 2013

One question by a user on Java pass by reference

Hi

Was just going through one of the blogs where there was confusion on pass by value and pass by reference, i found a interesting problem posted by one of the user.

I guess he wanted to interchange values of 2 Objects using a temp Object.

This was his program

http://javapapers.com/core-java/java-pass-by-value-and-pass-by-reference/

hello guys, i am above Anonymous
i hope this will help more for your confusion.
just a simple example
public class point
{
int x,y;
public void tricky(Point arg1, Point arg2)
{
arg1.x = 100;
arg1.y = 100;
Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}
public static void main(String [] args)
{
Point pnt1 = new Point(0,0);
Point pnt2 = new Point(0,0);
line 1:System.out.println(“X: ” + pnt1.x + ” Y: ” +pnt1.y);
line 2:System.out.println(“X: ” + pnt2.x + ” Y: ” +pnt2.y);
System.out.println(” “);
tricky(pnt1,pnt2);
line 3:System.out.println(“X: ” + pnt1.x + ” Y:” + pnt1.y);
line 4:System.out.println(“X: ” + pnt2.x + ” Y: ” +pnt2.y);
}
}
just think there is a pass by reference in java. the output at line 4 will be 100 and 100 but it gives 0 and 0. so there is no pass by reference in java.

X: 0 Y: 0
X: 0 Y: 0

X: 100 Y: 100
X: 0 Y: 0



import java.awt.Point;

public class point {

    int x;
    int y;

    public static void tricky(Point arg1, Point arg2) {
        arg1.x = 100;
        arg1.y = 100;
        Point temp = arg1;
        arg1 = arg2;
        arg2 = temp;
    }

    public static void tricky1(Point arg1, Point arg2) {
        arg1.x = 100;
        arg1.y = 100;
        Point temp = new Point(arg1.x, arg1.y);
        System.out.println("temp :: X: "+ temp.x + " Y: " +temp.y);
        arg1.x = arg2.x;
        arg1.y = arg2.y;
        arg2.x = temp.x;
        arg2.y = temp.y;
    }

    public static void main(String [] args) {
        Point pnt1 = new Point(0,0);
        Point pnt2 = new Point(0,0);
        System.out.println("X: "+ pnt1.x + " Y: " +pnt1.y);
        System.out.println("X: "+ pnt2.x + " Y: " +pnt2.y);
        System.out.println(" The users wrong way of pass by reference");
        tricky(pnt1,pnt2);
        System.out.println("X: "+ pnt1.x + " Y: " +pnt1.y);
        System.out.println("X: "+ pnt2.x + " Y: " +pnt2.y);
        System.out.println(" My solution to the problem pass by reference");
        tricky1(pnt1,pnt2);
        System.out.println("X: "+ pnt1.x + " Y: " +pnt1.y);
        System.out.println("X: "+ pnt2.x + " Y: " +pnt2.y);
    }
}

> java point

X: 0 Y: 0
X: 0 Y: 0
 The users wrong way of pass by reference
X: 100 Y: 100
X: 0 Y: 0
 My solution to the problem pass by reference
temp :: X: 100 Y: 100
X: 0 Y: 0
X: 100 Y: 100

Sunday, 2 June 2013

Virtual Developer Day: Java

Virtual Developer Day: Java

Click on the image or link to register for Java Virtual Developer Day 2013



Join this FREE virtual event where you will learn about:
•    Improved developer productivity and HTML5 applications
•    What's new in Java that helps you begin programming on a wide range of embedded devices
•    Language improvements in Java SE to accelerate application development
Watch informative tutorials that you can repeat at your own pace to improve your Java programming expertise, as well as engage in live chat sessions with technical experts.
Americas/Canada - Wednesday June 19, 2013 
9:00 a.m. - 1:00 p.m. PDT / 12:00 p.m. - 4:00 p.m. EDT / 1:00 p.m. - 5:00 p.m. BRT
EMEA/Africa/Middle East - Tuesday June 25th, 2013 
9:00:00 a.m.  - 1:00pm BST / 10:00:00 a.m. -  2:00 p.m.CEST /
1:30:00 p.m.  - 5:30:00 p.m. IST / 12:00:00 p.m. - 4:00 p.m. MSK /
08:00:00 a.m.  - 12:00 p.m. Corresponding UTC (GMT)