Showing posts with label Eclipse. Show all posts

Running JUnit 5 in Gradle Project

Follow the given step to setup JUnit 5 in Gradle project. Newbies wanna read Setup first gradle project in eclipse.

Step 1. Prepare build.gradle for JUnit 5

Step 1.1. Attach JUnit 5 gradle plugin to project.
apply plugin: 'org.junit.platform.gradle.plugin'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
    	// Gradle plugin
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3'
    }
}
Step 1.2. Add JUnit 5 dependencies.
dependencies {
    // download library for JUnit
    compile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M3'
    compile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M3'
    compile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-M3'
    
    // compile project for testing using Junit Jupiter Api
    testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3")
}
Step 1.3. JUnit 5 configuration.
Properties are self-explanatory.
junitPlatform {
    platformVersion "1.0.0-M3"
    filters {
        engines {
            include 'junit-jupiter'
            // exclude 'junit-vintage'
        }
        tags {
            // include 'fast', 'smoke'
            // exclude 'slow', 'ci'
        }
        packages {
            // include 'com.sample.included1', 'com.sample.included2'
            // exclude 'com.sample.excluded1', 'com.sample.excluded2'
        }
        //includeClassNamePattern '.*Spec'
        includeClassNamePatterns '.*Test', '.*Tests'
    }
}

Complete build.gradle
This is how your build.gradle file will look like after following the above steps.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.junit.platform.gradle.plugin'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

buildscript {
	repositories {
        mavenCentral()
    }
    dependencies {
    	// Gradle plugin
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3'
    }
}

dependencies {
    // download library for JUnit
    compile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M3'
    compile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M3'
    compile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-M3'
    
    // compile project for test using Junit Jupiter Api
    testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3")
}

junitPlatform {
    platformVersion "1.0.0-M3"
    filters {
        engines {
            include 'junit-jupiter'
            // exclude 'junit-vintage'
        }
        tags {
            // include 'fast', 'smoke'
            // exclude 'slow', 'ci'
        }
        packages {
            // include 'com.sample.included1', 'com.sample.included2'
            // exclude 'com.sample.excluded1', 'com.sample.excluded2'
        }
        //includeClassNamePattern '.*Spec'
        includeClassNamePatterns '.*Test', '.*Tests'
    }
}

Step 2. Refresh dependencies
Refresh Gradle project so it'll download required libraries.

Step 3. Project structure.
Create test source folder parallel to main source folder (i.e src/test/java and src/test/resources). Follow the image.

JUnit Test + Gradle project structure

Step 4. Create first unit test.

Source code (Addition.java)
package com.javaquery.math;

public class Addition {
	/**
	 * Add given number 'a' and 'b'.
	 * @param a
	 * @param b
	 * @return
	 */
	public int add(int a, int b){
	    return a + b;
	}
}
Source code (AdditionTest.java)
I created two test cases. Out of two case one is success and other is fail.
package com.javaquery.math;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import static org.junit.jupiter.api.Assertions.assertEquals;

@RunWith(JUnitPlatform.class)
public class AdditionTest {

	@Test
	public void add(){
	    int x = new Addition().add(12, 3);
	    assertEquals(x, 15);
	}
	
	@Test
	public void failTest(){
	    int y = new Addition().add(20, 30);
	    assertEquals(y, 20);
	}
}

Step 5. Clean and Test
Perform clean and test task on your gradle project.
clean
test

Output
[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks: 
[sts]      clean
[sts]      test
[sts] -----------------------------------------------------
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:junitPlatformTestJan 01, 2017 11:48:00 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
INFO: Discovered TestEngines with IDs: [junit-jupiter]


Failures (1):
  JUnit Jupiter:AdditionTest:failTest()
    MethodSource [className = 'com.javaquery.math.AdditionTest', methodName = 'failTest', methodParameterTypes = '']
    => org.opentest4j.AssertionFailedError: expected: <50> but was: <20>

Test run finished after 105 ms
[         2 containers found      ]
[         0 containers skipped    ]
[         2 containers started    ]
[         0 containers aborted    ]
[         2 containers successful ]
[         0 containers failed     ]
[         2 tests found           ]
[         0 tests skipped         ]
[         2 tests started         ]
[         0 tests aborted         ]
[         1 tests successful      ]
[         1 tests failed          ]

:junitPlatformTest FAILED

Setup first gradle project in eclipse


Gradle (Modern Open Source Build Tool for Continuous Delivery)
Built over the concepts of Apache Ant and Apache Maven. It helps you with build process of project. Using Gradle you can perform tasks like

  • Build project and its sub projects.
  • Test the build.
  • Execute customized task like you do with any other programming language using.
Gradle uses Groovy Programming language for project configuration.

Step 1. Download Gradle
Download latest binary distribution from https://www.gradle.org/gradle-download/

Step 2. Extract the binary.
Extract the downloaded binary in say C:\gradle-3.2.1.

Step 3. Setup Gradle eclipse preference.
In Eclipse go to Window > Preferences > Gradle (STS) > Folder.


Step 4. Download eclipse gradle plugin.
In Eclipse go to Help > Eclipse Marketplace and search for gradle. There is official eclipse plugin from gradle however I prefer the gradle plugin developed by Spring as a part of Sprint Tool Suits (STS).


Step 5. Create new Gradle project.
Go to File > New > Other > Gradle (STS) > Gradle (STS) Project.


Step 6. Specify your first gradle project name.


Step 7. Upgrade Java version
Sample gradle project is configured to use Java 1.5 so change it to your system's Java version. Open build.gradle and change sourceCompatibility = 1.5 to sourceCompatibility = 1.8

Step 8. Refresh gradle project.

Right click on Project and go to Gradle (STS) > Refresh All.



Steps to setup DB2 Data Studio Client for Visual Explain of queries.

As a beginner, It was hard for me to figure out how to prepare visual execution plan in DB2 Data Studio Client. I was ending up with the following error.
com.ibm.datatools.dsoe.common.da.exception.OSCSQLException: com.ibm.db2.jcc.am.SqlSyntaxErrorException: The required Explain table "VICKY.EXPLAIN_INSTANCE" does not exist.. SQLCODE=-219, SQLSTATE=42704, DRIVER=3.68.61
     at com.ibm.datatools.dsoe.explain.luw.impl.ExplainThread.explain(Unknown Source)
     at com.ibm.datatools.dsoe.explain.luw.impl.ExplainThread.buildLUWExplainModelNeedSync(Unknown Source)
     at com.ibm.datatools.dsoe.explain.luw.impl.ExplainThread.commonProcess(Unknown Source)
     at com.ibm.datatools.dsoe.explain.luw.impl.ExplainThread.process(Unknown Source)
     at com.ibm.datatools.dsoe.explain.luw.Explainer.process(Unknown Source)
     at com.ibm.datatools.dsoe.ape.core.LUWAPEModelGenerator.generate(Unknown Source)
     at com.ibm.datatools.dsoe.ape.core.APEModelGenerator.generate(Unknown Source)
     at com.ibm.datatools.visualexplain.common.viewer.popup.LaunchVisualExplainFromEditorAction$1.run(Unknown Source)
     at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(Unknown Source)
Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: The required Explain table "VICKY.EXPLAIN_INSTANCE" does not exist.. SQLCODE=-219, SQLSTATE=42704, DRIVER=3.68.61
     at com.ibm.db2.jcc.am.gd.a(Unknown Source)
     at com.ibm.db2.jcc.am.gd.a(Unknown Source)
     at com.ibm.db2.jcc.am.gd.a(Unknown Source)
     at com.ibm.db2.jcc.am.uo.c(Unknown Source)
     at com.ibm.db2.jcc.am.uo.d(Unknown Source)
     at com.ibm.db2.jcc.am.uo.a(Unknown Source)
     at com.ibm.db2.jcc.t4.bb.h(Unknown Source)
     at com.ibm.db2.jcc.t4.bb.b(Unknown Source)
     at com.ibm.db2.jcc.t4.p.a(Unknown Source)
     at com.ibm.db2.jcc.t4.vb.i(Unknown Source)
     at com.ibm.db2.jcc.am.uo.ib(Unknown Source)
     at com.ibm.db2.jcc.am.uo.a(Unknown Source)
     at com.ibm.db2.jcc.am.uo.e(Unknown Source)
     at com.ibm.db2.jcc.am.uo.execute(Unknown Source)
     ... 9 more


Steps to setup Visual Explain

Step 1: Switch Data Studio Activity. Administer Databases to Tune Queries (As shown in Image)

Administer Databases to Tune Queries+ DB2 Data Studio Client

Step 2: Right click Data Source. Analyze and Tune > Configure and Tuning > Guided Configuration (As shown in Image)


Now you can generate visual explain on your queries.

How to configure SQL Explorer in Eclipse?

Today I came to know one cool plugin of Eclipse. Its SQL Explorer, some of you may aware of this awesome plugin. This article for those who not heard about this plugin ever.

Features: 
  • Connect remote databases.
  • Able to show Tables, Function, View, Stored Procedure, etc...
  • Quick preview of table contains (Table Structure, Foreign keys, Indexes, etc...)
  • Query execution history.

Drawback: (Or may be I'm wrong)
  • Can't execute Stored Procedure, Function, etc... from SQL Explorer

Prerequisite:
Note: Download from original site in-case you face some issue for your eclipse environment.

Step 1: Download and extract sqlexplorer_plugin-3.6.1_SR6.zip file. Merge it with eclipse `features` and `plugins` folder.

Step 2: Restart eclipse.

Step 3: Go to Window > Preferences > SQL Explorer and follow step as below image and click Edit.

SQL Explorer

Step 4: Click on Extra Class Path > Add JARs and select `sqljdbc-1.2.0.jar` from location you downloaded at starting of this article. Then click on List Drivers. Click OK on both popup window.

SQL Explorer

Step 5: Now you are good to go. All You need to open perspective window in eclipse. Go to Window > Open Perspective > Other > SQL Explorer.

Step 6: You'll have SQL Explorer window open in your eclipse. In left pan you'll have connection tab. Right click in that tab area create new connection.

Step 7: Provide your connection details in popup window and click ok.

SQL Explorer

That's it. We are done with SQL Explorer setup. Enjoy your new SQL editor. Open below image in new tab to check out SQL Explorer in Eclipse.

SQL Explorer

How to attach MANIFEST.MF file in jar in/using eclipse?

Every jar file you create using editor it explicitly attach MANIFEST.MF file in it. That contains basic information of that jar file. I was also generating jar file using editor regardless its MANIFEST.MF file. After major security changes in Java 1.7.0_45 its mandatory to specify some attribute in MANIFEST.MF in Applet jar. But its good practice to add this MANIFEST.MF file with proper information in it, whether its applet or simple jar file.

Step 1: Open up simple notepad or other text editor. Add your required attribute in it and save as "MANIFEST.MF" . Here is all attribute information of MANIFEST.MF file. This is my sample MANIFEST.MF file.
Manifest-Version: 1.0
Codebase: *
Permissions: all-permissions
Application-Library-Allowable-Codebase: *
Caller-Allowable-Codebase: *
Application-Name: Digital Certificate
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_25-b06 (Sun Microsystems Inc.)

Step 2: Copy MANIFEST.MF file in your project's folder.

jar, applet, manifest, eclipse

Step 3: Right click on project / file and click on Export.

jar, applet, manifest, eclipse

Step 4: Select JAR file in dialog box and click Next.

jar, applet, manifest, eclipse

Step 5: Select your output file and its location. And click Next.

jar, applet, manifest, eclipse

Step 6: Click Next in JAR Packaging option.
Step 7: Select "Use existing manifest from workspace" and browse your MANIFEST.MF file and click on finish. 

jar, applet, manifest, eclipse

Other Resources:
How to attach MANIFEST.MF file in jar in/using Netbeans?

How to solve PermGen space Error in eclipse?

eclipse
Unhandled event loop exception (PermGen space)
For all those developers,beginners who are facing this kind of problem in Eclipse IDE running in windows with JVM update java 1.6 update 21 or later. The permGen space problem is common with the Eclipse IDE. The one working solution is to increase the space in eclipse.ini or the configuration file following the given below steps.

Open the Configuration file in the eclipse directory forlder. for eg. C:\eclipse-jee-galileo-SR2-win32\eclipse
just add -XX:MaxPermSize=256m command below the -vmargs line. Change value of -XX:MaxPermSize as per your requirement. If your project is big you should use 512m or 1024m. I'm using 256m in my project.

Now your eclipse.ini file should look like this

-startup
plugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.0.200.v20090519
-product
org.eclipse.epp.package.jee.product
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256M
-vmargs
-XX:MaxPermSize=512m 
-Dosgi.requiredJavaVersion=1.5
-Xms512m
-Xmx1024m