Search This Blog

Sunday 25 March 2012

Running JUnit 4 from command line

1.) Download Junit 4.

2.) Write a test class :

import org.junit.Test;
import junit.framework.TestCase;
    
public class TestJUnit4 extends TestCase
{
    int value1 = 2, value2 = 3, expectedResult = 5;
    
    @Test
    public void testSuccess()
    {
        assertTrue(value1 + value2 == expectedResult);
    }
    
    @Test
    public void testFail()
    {
        assertTrue(value1 - value2 == expectedResult);
    }
}

3.) Compile it at from command line :

javac -cp .;c:\junit\junit4.jar TestJUnit4.java

4.) Run it :

C:\JavaProgs>java -cp .;c:\junit\junit4.jar org.junit.runner.JUnitCore TestJUnit4

This gives below result :

Time: 0.01
There was 1 failure:
1) testFail(TestJUnit4)
junit.framework.AssertionFailedError
at junit.framework.Assert.fail(Assert.java:48)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertTrue(Assert.java:27)
at TestJUnit4.testFail(TestJUnit4.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:98)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:53)
at org.junit.runner.JUnitCore.main(JUnitCore.java:45)

FAILURES!!!
Tests run: 2, Failures: 1


Running JUnit 3 from command line

1.) Download Junit 3.

2.) Write a test class :

import junit.framework.TestCase;


public class TestJUnit3 extends TestCase
{
int value1 = 2, value2 = 3, expectedResult = 5;


public void testSuccess()
{
assertTrue(value1 + value2 == expectedResult);
}


public void testFail()
{
assertTrue(value1 - value2 == expectedResult);
}
}


3.) Compile it at from command line :

C:\JavaProgs>javac -cp .;c:\junit\junit3.jar TestJUnit3.java

4.) Run it :

C:\JavaProgs>java -cp .;c:\junit\junit3.jar junit.textui.TestRunner TestJUnit3

This gives below result :

Time: 0
There was 1 failure:
1) testFail(TestJUnit3)junit.framework.AssertionFailedError
at TestJUnit3.testFail(TestJUnit3.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

FAILURES!!!
Tests run: 2, Failures: 1, Errors: 0

Thursday 22 March 2012

Installing ANT

1. Download ANT

2. Extract the downloaded zip file.

3. Set ANT_HOME environment variable pointing to your Ant location(like,C:/ant)

4. Now set Path variable pointing to your ANT bin directory(%ANT_HOME%\bin)

5. Test ANT at command prompt :
echo %ANT_HOME% //your current ANT directory
ant -version //version of the ANT

If you don't want to follow these steps, you can simply download and run the windows installer for ANT

Wednesday 8 June 2011

Custom Roles in Spring Security

By default, Spring Security accepts roles like 'ROLE_ADMIN', 'ROLE_USER'.
We can change this default behavior by changing the Role Prefix from "Role_" to "".
In order to achieve this, we will add following code in the applicationContext-security.xml of our Login Example.
    <beans:bean id="roleVoter"
        class="org.springframework.security.vote.RoleVoter ">
        <beans:property name="rolePrefix" value="" />
    </beans:bean>
    <beans:bean id="authenticatedVoter"  class="org.springframework.security.vote.AuthenticatedVoter" />
    <beans:bean id="accessDecisionManager"  class="org.springframework.security.vote.AffirmativeBased">
    <beans:property name="decisionVoters">
        <beans:list>
            <beans:ref bean="roleVoter" />
            <beans:ref bean="authenticatedVoter" />
        </beans:list>
    </beans:property>
    </beans:bean>
After this, we can use our custom roles like 'admin' and 'user' instead of 'ROLE_ADMIN' and 'ROLE_USER'.
<authentication-provider>
        <password-encoder hash="md5"/>
        <user-service>
            <user name="sandeep" password="00f1de4e151ccfc1fc9ff735a5efc479" authorities="admin,user" />
            <user name="vijay" password="e555f863fb09593119fe2f3459e9783a" authorities="user" />
        </user-service>
 </authentication-provider> 

Configuring Logout in Spring Security

We will change the applicationContext-security.xml of our login example, to configure logout.
we will add logout tag in the http Section.
<http auto-config="true" access-denied-page="/deniedPage.jsp">
    <intercept-url pattern="/securePage**" access="ROLE_ADMIN" />
    <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <form-login login-processing-url="/j_spring_security_check"
                login-page="/login.jsp"
                default-target-url="/securePage.jsp"
                authentication-failure-url="/login.jsp" />
    <logout    logout-url="/j_spring_security_logout" invalidate-session="true" logout-success-url="/login.jsp" />
</http>
It is obvious from the configuration that session will be invalidated and login page will be displayed after logout.    

Using Spring Security with JDBC

First we need two tables.We will execute the following queries to create the tables.
create table users (username varchar(50) not null primary key,password varchar(50) not null,enabled boolean not null);

create table authorities (
    username varchar(50) not null,
    authority varchar(50) not null,
    foreign key (username) references users (username),
    unique index ix_auth_username (username, authority)
);
Then, we will insert some data into the tables.
INSERT INTO users VALUES ('sandeep', '00f1de4e151ccfc1fc9ff735a5efc479', true);
INSERT INTO users VALUES ('vijay',   'e555f863fb09593119fe2f3459e9783a', true);
INSERT INTO authorities VALUES ('sandeep', 'ROLE_ADMIN');
INSERT INTO authorities VALUES ('sandeep', 'ROLE_USER');
INSERT INTO authorities VALUES ('vijay',   'ROLE_USER');
Now we will change the applicationContext-security.xml of our login example, to configure JDBC.

    
        
         
         
    
Finally,we need the 'datasource.xml'. This is based on MySQL.


    
        
            com.mysql.jdbc.Driver
        
        
            jdbc:mysql://localhost:3306/test
        
        
            root
        
        
            sandeep
        
    

Using MD5 Encrypted Password in Spring Security

We need following changes in the applicationContext-security.xml of our login example,
<authentication-provider>
<password-encoder hash="md5"/>
        <user-service>
            <user name="sandeep" password="00f1de4e151ccfc1fc9ff735a5efc479" authorities="ROLE_ADMIN, ROLE_USER"/>
            <user name="vijay" password="e555f863fb09593119fe2f3459e9783a" authorities="ROLE_USER"/>
        </user-service>
</authentication-provider>
Here,"00f1de4e151ccfc1fc9ff735a5efc479" is the MD5 encryption for "swastik"
We can use http://md5encrypter.com/ to encrypt the password.