Thursday, 29 June 2017

Introduction to Derby

Apache Derby is open source RDBMS (Relational database management system) completely written in Java and maintained by Apache.

Is Apache Derby open source?
Yes

Can I embed derby in my Java application?
Yes, Derby provides an embedded JDBC driver that lets you embed Derby in any Java-based solution.

Is Derby providing transaction support?
Yes

What are the deployment options available?
You can deploy Derby in two ways.
a.   Embedding the derby in your Java application
b.   You can deploy it in client-server model, where derby runs on a server, applications connect to that server to access the database

What are the system requirements to use Derby?
a.   JDK 1.6 or higher (To run the derby jvm is enough, but to develop java applications, we require jdk also).

Where can I download Derby documentation?

What are you going to learn in this tutorial
·       Setting up Apache Derby




Previous                                                 Next                                                 Home

Apache Derby Tutorial

Wednesday, 28 June 2017

EasyMock, PowerMock: Accept any argument of given type to static method

In my previous post, I explained how to pass any object as an argument to mocked static method.  In this post, I am going to explain how to tell the mocked method accept any argument of given type.

EasyMock class provide 'isA()' by using this we can tell the mocked method to accept any object of this type as argument.

Following snippet is used to mock the method 'toUpper' which accepts any argument of type string and return 'HELLO PTR'.

PowerMock.mockStaticPartial(StringUtil.class, "toUpper");
EasyMock.expect(StringUtil.toUpper(EasyMock.isA(String.class))).andReturn("HELLO PTR").times(1);
PowerMock.replayAll();
String result = StringUtil.toUpperAndRepeatStringTwice("hello ptr");

Find below working application.

MethodNotImplementedException.java
package com.sample.model;

public class MethodNotImplementedException extends RuntimeException {
 private static final long serialVersionUID = 1L;

 public MethodNotImplementedException() {
  super();
 }

 public MethodNotImplementedException(String message) {
  super(message);
 }

}

StringUtil.java
package com.sample.util;

import com.sample.model.MethodNotImplementedException;

public class StringUtil {
 public static String toUpper(String str) {
  throw new MethodNotImplementedException();
 }

 public static String toUpperAndRepeatStringTwice(String str) {
  String upperCase = toUpper(str);
  return upperCase + upperCase;
 }
}


StringUtilTest.java
package com.sample.util;

import static org.junit.Assert.assertEquals;

import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ StringUtil.class })
public class StringUtilTest {

 @Test
 public void toUpperAndRepeatStringTwice() {
  PowerMock.mockStaticPartial(StringUtil.class, "toUpper");

  EasyMock.expect(StringUtil.toUpper(EasyMock.isA(String.class))).andReturn("HELLO PTR").times(1);

  PowerMock.replayAll();
  
  String result = StringUtil.toUpperAndRepeatStringTwice("hello ptr");

  assertEquals(result, "HELLO PTRHELLO PTR");

  PowerMock.verifyAll();
 }
}







Previous                                                 Next                                                 Home

Tuesday, 27 June 2017

Check encoding scheme supported or not in java

'java.nio.charset.Charset' class provides 'isSupported' method to check whether given character encoding is supported by your Java implementation or not.
package com.sample;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class Test {

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

    boolean isUTF8Supported = Charset.isSupported("UTF-8");
    boolean isUTF16Supported = Charset.isSupported("UTF-16");
    boolean isUTF32Supported = Charset.isSupported("UTF-32");
    boolean isUTF64Supported = Charset.isSupported("UTF-64");
    boolean isUTF128Supported = Charset.isSupported("UTF-128");
    boolean isUTF256Supported = Charset.isSupported("UTF-256");

    System.out.println("isUTF8Supported : " + isUTF8Supported);
    System.out.println("isUTF16Supported : " + isUTF16Supported);
    System.out.println("isUTF32Supported : " + isUTF32Supported);
    System.out.println("isUTF64Supported : " + isUTF64Supported);
    System.out.println("isUTF128Supported : " + isUTF128Supported);
    System.out.println("isUTF256Supported : " + isUTF256Supported);
  }
}

Output
isUTF8Supported : true
isUTF16Supported : true
isUTF32Supported : true
isUTF64Supported : false
isUTF128Supported : false
isUTF256Supported : false




How to decode url in java?

java.net.URLDecoder class provides 'decode' method to decode the encoded url.

public static String decode(String s, String encodingScheme)
Decodes the string 's' using given encodingScheme.

Every standard Java implementation must support below encoding schemes.

1.   US-ASCII
2.   ISO-8859-1
3.   UTF-8
4.   UTF-16BE
5.   UTF-16LE
6.   UTF-16

'java.nio.charset.Charset' class provides 'isSupported' method to check whether given character encoding is supported by your Java implementation or not.

Below program decodes an encoded url and print to the console.
package com.sample;

import java.io.UnsupportedEncodingException;

public class URLDecoder {

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

  String decodedURL = java.net.URLDecoder.decode("http%3A%2F%2Fself-learning-java-tutorial.blogspot.in%2F2014%2F09%2Fservlets.html", "UTF-8");
  System.out.println("decodedURL : " + decodedURL);
  
 }
}

Output
decodedURL : https://self-learning-java-tutorial.blogspot.com/2014/09/servlets.html


How to encode url in java?

java.net.URLEncoder class provides 'encode' method to encode url.

public static String encode(String s, String encodingScheme)
Encode the url using given encoding scheme.

Every standard Java implementation must support below encoding schemes.

1.   US-ASCII
2.   ISO-8859-1
3.   UTF-8
4.   UTF-16BE
5.   UTF-16LE
6.   UTF-16

'java.nio.charset.Charset' class provides 'isSupported' method to check whether given character encoding is supported by your Java implementation or not.

Below program encodes a url and print the encoded url to the console.
package com.sample;

import java.io.UnsupportedEncodingException;

public class URLEncoder {

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

  String encodedURL = java.net.URLEncoder.encode("https://self-learning-java-tutorial.blogspot.com/2014/09/servlets.html", "UTF-8");
  System.out.println("encodedURL : " + encodedURL);
  
 }
}


Monday, 26 June 2017

Pass any argument to static method

In this post, I am going to explain how to pass any object as an argument to mocked static method. EasyMock class provide 'anyObject()' by using this we can tell the mocked method to accept any object as argument.

Following snippet is used to mock the method 'toUpper' which accepts any argument and return 'HELLO PTR'.

PowerMock.mockStaticPartial(StringUtil.class, "toUpper");
EasyMock.expect(StringUtil.toUpper(EasyMock.anyObject())).andReturn("HELLO PTR").times(1);
PowerMock.replayAll();
String result = StringUtil.toUpperAndRepeatStringTwice("hello ptr");

Find the following working application.

MethodNotImplementedException.java
package com.sample.model;

public class MethodNotImplementedException extends RuntimeException {
 private static final long serialVersionUID = 1L;

 public MethodNotImplementedException() {
  super();
 }

 public MethodNotImplementedException(String message) {
  super(message);
 }

}

StringUtil.java
package com.sample.util;

import com.sample.model.MethodNotImplementedException;

public class StringUtil {
 public static String toUpper(String str) {
  throw new MethodNotImplementedException();
 }

 public static String toUpperAndRepeatStringTwice(String str) {
  String upperCase = toUpper(str);
  return upperCase + upperCase;
 }
}


StringUtilTest.java
package com.sample.util;

import static org.junit.Assert.assertEquals;

import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ StringUtil.class })
public class StringUtilTest {

 @Test
 public void toUpperAndRepeatStringTwice() {
  PowerMock.mockStaticPartial(StringUtil.class, "toUpper");

  EasyMock.expect(StringUtil.toUpper(EasyMock.anyObject())).andReturn("HELLO PTR").times(1);

  PowerMock.replayAll();
  
  String result = StringUtil.toUpperAndRepeatStringTwice("hello ptr");

  assertEquals(result, "HELLO PTRHELLO PTR");

  PowerMock.verifyAll();
 }
}






Previous                                                 Next                                                 Home