Thursday, June 27, 2013

How Does Synchronized Work With Thread


There are 2 threads:
With Synchronized:

   Thread A and Thread B are doing one job at the same time. With synchronized Thread A does first until it is released.
Without Synchronized:   

   Thread A and Thread B are doing one job. It causes racing condition

Spring Testing 3.1: Active Enviroment Profile For Annotation

Step 01:

package com.config.enviroment.profile.annotation;

public class User {
    private String userName;
    private String email;
    private Long salary;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Long getSalary() {
        return salary;
    }

    public void setSalary(Long salary) {
        this.salary = salary;
    }
}



Step 02:

package com.config.enviroment.profile.annotation;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * For Development Environment
 * @author hieunt29
 *
 */

@Configuration
@Profile(\"DEV\")
public class UserDevEnviroment {
    /*
     * Injected bean
     */

    @Bean(name = \"userBean\")
    public User addUser() {
        User user = new User();
        user.setUserName(\"Nguyen Trung Hieu DEV\");
        user.setSalary(123456789l);
        user.setEmail(\"DEV-hieunt29@fsoft.com.vn\");
        return user;
    }
}




Step 03:
package com.config.enviroment.profile.annotation;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * For Production Environment
 * 
 * @author hieunt29
 * 
 */

@Configuration
@Profile(\"PROD\")
public class UserProdEnviroment {
    /*
     * Injected bean
     */

    @Bean(name = \"userBean\")
    public User addUser() {
        User user = new User();
        user.setUserName(\"Nguyen Trung Hieu PROD\");
        user.setSalary(999999999l);
        user.setEmail(\"PROD-hieunt29@fsoft.com.vn\");
        return user;
    }
}

Step 04: src/test/java
package com.config.enviroment.profile.annotation.TEST;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.config.enviroment.profile.annotation.User;
import com.config.enviroment.profile.annotation.UserDevEnviroment;
import com.config.enviroment.profile.annotation.UserProdEnviroment;

/**
 * For development enviroment
 * 
 * @author hieunt29
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from \"UserDevEnviroment.class, UserProdEnviroment.class\"
@ContextConfiguration(classes = { UserDevEnviroment.class,
        UserProdEnviroment.class })
//DEV environment will be actived
@ActiveProfiles(\"DEV\")
public class UserBeanDevTest {
    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(\"Nguyen Trung Hieu DEV\"));
        assertThat(userBean.getSalary(), equalTo(123456789L));
        assertThat(userBean.getEmail(), equalTo(\"DEV-hieunt29@fsoft.com.vn\"));
    }
    
}

Step 05:src/test/java

package com.config.enviroment.profile.annotation.TEST;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.config.enviroment.profile.annotation.User;
import com.config.enviroment.profile.annotation.UserDevEnviroment;
import com.config.enviroment.profile.annotation.UserProdEnviroment;

/**
 * For production enviroment
 * 
 * @author hieunt29
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
//ApplicationContext will be loaded from \"UserDevEnviroment.class, UserProdEnviroment.class\"
@ContextConfiguration(classes = { UserDevEnviroment.class,
        UserProdEnviroment.class })
//PROD environment will be actived
@ActiveProfiles(\"PROD\")
public class UserBeanProdTest {
    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(\"Nguyen Trung Hieu PROD\"));
        assertThat(userBean.getSalary(), equalTo(999999999l));
        assertThat(userBean.getEmail(), equalTo(\"PROD-hieunt29@fsoft.com.vn\"));
    }
}


Spring Testing 3.1: Active Enviroment Profile For XML

Step 01:

package com.config.enviroment.profile.xml;

public class User {
    private String userName;
    private String email;
    private Long salary;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Long getSalary() {
        return salary;
    }

    public void setSalary(Long salary) {
        this.salary = salary;
    }
}



Step 02:src/test/java

package com.config.enviroment.profile.xml.TEST;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.config.enviroment.profile.xml.User;

/**
 * For development enviroment
 * 
 * @author hieunt29
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
//ApplicationContext will be loaded from applicationContext.xml\"
@ContextConfiguration(locations = \"classpath:applicationContext.xml\")
//DEV environment will be actived
@ActiveProfiles(\"DEV\")
public class UserBeanDevTest {
    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(\"Nguyen Trung Hieu DEV\"));
        assertThat(userBean.getSalary(), equalTo(15000000L));
        assertThat(userBean.getEmail(), equalTo(\"DEV-hieunt29@fsoft.com.vn\"));
    }
}



Step 03:src/test/java

package com.config.enviroment.profile.xml.TEST;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.config.enviroment.profile.xml.User;
/**
 * For production enviroment
 * @author hieunt29
 *
 */

@RunWith(SpringJUnit4ClassRunner.class)
//ApplicationContext will be loaded from applicationContext.xml\"
@ContextConfiguration(locations = \"classpath:applicationContext.xml\")
//PROD environment will be actived
@ActiveProfiles(\"PROD\")
public class UserBeanProdTest {
    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(\"Nguyen Trung Hieu PROD\"));
        assertThat(userBean.getSalary(), equalTo(9999999l));
        assertThat(userBean.getEmail(), equalTo(\"PRODhieunt29@fsoft.com.vn\"));
    }
}

Lesson 01: How To Solve Problem!!!

Saturday, June 22, 2013

Spring Testing 3.1: Context Configuration Inheritance For Annotation

Step 01:
package com.config.inheritance.annotation;

public class Department {
    private String departmentId;
    private String departmentName;

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}
Step 02:
package com.config.inheritance.annotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
// spring config that loads the properties file
@ImportResource(\\\"classpath:/properties-config.xml\\\")
public class DepartmentBeanApp {
    /**
     * Using property \\\'EL\\\' syntax to load values from the department.properties value
     */

    private @Value(\\\"#{departmentProperties[\\\'departmentBean.id\\\']}\\\")
    String departmentId;
    private @Value(\\\"#{departmentProperties[\\\'departmentBean.departmentname\\\']}\\\")
    String departmentName;

    @Bean(name = \\\"departmentBean\\\")
    public Department addDepartment() {
        Department department = new Department();
        department.setDepartmentId(departmentId);
        department.setDepartmentName(departmentName);
        return department;
    }
}
Step 03:
package com.config.inheritance.annotation;

public class User {
    private String userName;
    private Long salary;

    public Long getSalary() {
        return salary;
    }

    public void setSalary(Long salary) {
        this.salary = salary;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
Step 04 
package com.config.inheritance.annotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
// spring config that loads the properties file
@ImportResource(\"classpath:/properties-config.xml\")
public class UserBeanApp {
    /**
     * Using property \'EL\' syntax to load values from the user.properties value
     */

    private @Value(\"#{userProperties[\'userBean.username\']}\")
    String userName;
    private @Value(\"#{userProperties[\'userBean.salary\']}\")
    Long salary;

    /*
     * Injected bean
     */

    @Bean(name = \"userBean\")
    public User addUser() {
        User user = new User();
        user.setUserName(userName);
        user.setSalary(salary);
        return user;
    }
}
Step 05:
package com.config.inheritance.annotation.TEST;


import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.config.inheritance.annotation.User;
import com.config.inheritance.annotation.UserBeanApp;

@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from UserBeanApp
@ContextConfiguration(classes = { UserBeanApp.class })
// spring config that loads the properties file
@ImportResource(\"classpath:/properties-config.xml\")
public class UserBeanTest {
    /**
     * Using property \'EL\' syntax to load values from the user.properties value
     */

    private @Value(\"#{userProperties[\'userBean.username\']}\")
    String userName;
    private @Value(\"#{userProperties[\'userBean.salary\']}\")
    Long salary;

    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(userName));
        assertThat(userBean.getSalary(), equalTo(salary));
    }
}
Step 06:
package com.config.inheritance.annotation.TEST;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;

import com.config.inheritance.annotation.Department;
import com.config.inheritance.annotation.DepartmentBeanApp;

@ContextConfiguration(classes = { DepartmentBeanApp.class })
@ImportResource(\"classpath:/properties-config.xml\")
public class DepartmentBeanTest extends UserBeanTest {

    /**
     * Using property \'EL\' syntax to load values from the department.properties
     * value
     */

    private @Value(\"#{departmentProperties[\'departmentBean.id\']}\")
    String departmentId;
    private @Value(\"#{departmentProperties[\'departmentBean.departmentname\']}\")
    String departmentName;

    @Autowired
    Department departmentBean;

    @Test
    public void testAddDepartment() {
        assertThat(departmentBean.getDepartmentId(), equalTo(departmentId));
        assertThat(departmentBean.getDepartmentName(), equalTo(departmentName));
    }
}

Spring Testing 3.1: Mixing XML resources and @Configuration classes

Step 01
package com.resource.config;

public class User {
    private String userName;
    private Long salary;

    public Long getSalary() {
        return salary;
    }

    public void setSalary(Long salary) {
        this.salary = salary;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Step 02

package com.resource.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
// spring config that loads the properties file
@ImportResource(\"classpath:/properties-config.xml\")
public class UserBeanApp {
    /**
     * Using property \'EL\' syntax to load values from the user.properties value
     */

    private @Value(\"#{userProperties[\'userBean.username\']}\")
    String userName;
    private @Value(\"#{userProperties[\'userBean.salary\']}\")
    Long salary;

    /*
     * Injected bean
     */

    @Bean(name = \"userBean\")
    public User addUser() {
        User user = new User();
        user.setUserName(userName);
        user.setSalary(salary);
        return user;
    }
}

Step 03

package com.resource.config;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class UserBeanMain {

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(
                UserBeanApp.class);
        User jetBean = ctx.getBean(User.class);
        System.out.println(\"User Name=\" + jetBean.getUserName());
        System.out.println(\"Salary=\" + jetBean.getSalary());
    }

}

Step 04 

package com.resource.config.TEST;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.resource.config.User;
import com.resource.config.UserBeanApp;

@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from UserBeanApp
@ContextConfiguration(classes = { UserBeanApp.class })
// spring config that loads the properties file
@ImportResource(\"classpath:/properties-config.xml\")
public class UserBeanTest {
    /**
     * Using property \'EL\' syntax to load values from the user.properties value
     */

    private @Value(\"#{userProperties[\'userBean.username\']}\")
    String userName;
    private @Value(\"#{userProperties[\'userBean.salary\']}\")
    Long salary;

    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(userName));
        assertThat(userBean.getSalary(), equalTo(salary));
    }
}
Dowload Source Code: spring-resource-config