2016년 5월 2일 월요일

jUnit에서 JNDI DataSource을 만들어서 테스트하기


public class OrgServiceTestCase extends AbstractTestCase {

@Inject
@Named("orgService")
private OrgService orgService;

@BeforeClass
public static void bindJNDIIAM() throws NamingException {
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.activate();
JndiTemplate jt = new JndiTemplate();

SimpleDriverDataSource iamDS = new SimpleDriverDataSource();

iamDS.setDriverClass(OracleDriver.class);
iamDS.setUrl("jdbc:oracle:thin:@192.168.0.xxx:1521:ORA11");
iamDS.setUsername("user");
iamDS.setPassword("passwd");
jt.bind("iamDataSource", iamDS);
}

단위테스트케이스 내에서 WAS의 JNDI말고 케이스 내에서 만들어서 사용할때 사용하시면 됩니다.

2016년 4월 28일 목요일

Java 8의 Date/Time API

지금까지의 Date/Time 관련 API는 개발자에게 외면을 받아왔지만, Java 8에서는 "JSR 310: Data and Time API" Spec을 구현하여 획기적으로 변화했다고 한다. 새로운 Date/Time 클래스는 java.time.* 으로 패키징 되어 있다.


System의 Locale에 따른 날짜 및 시간 형식으로 표현된다. 불변성을 가지고 Thread Safe 하다.


java.time.LocalDate
package org.smarttechie;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

/**
* This class demonstrates JAVA 8 data and time API
*
* @author Siva Prasad Rao Janapati
* */
public class LocalDateDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
// Create date
LocalDate localDate = LocalDate.now();
System.out.println("The local date is :: " + localDate);

// Find the length of the month. That is, how many days are there for
// this month.
System.out.println("The number of days available for this month:: "
+ localDate.lengthOfMonth());

// Know the month name
System.out.println("What is the month name? :: "
+ localDate.getMonth().name());

// add 2 days to the today's date.
System.out.println(localDate.plus(2, ChronoUnit.DAYS));

// substract 2 days from today
System.out.println(localDate.minus(2, ChronoUnit.DAYS));

// Convert the string to date
System.out.println(LocalDate.parse("2017-04-07"));
}
}


----------------------------------
The local date is :: 2016-04-28
The number of days available for this month:: 30
What is the month name? :: APRIL
2016-04-30
2016-04-26
2017-04-07


java.time.LocalTime
package org.smarttechie;

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

/**
* This class demonstrates JAVA 8 data and time API
*
* @author Siva Prasad Rao Janapati
* */
public class LocalTimeDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
// Get local time
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
// Get the hour of the day
System.out.println("The hour of the day:: " + localTime.getHour());

// add 2 hours to the time.
System.out.println(localTime.plus(2, ChronoUnit.HOURS));
// add 6 minutes to the time.
System.out.println(localTime.plusMinutes(6));
// substract 2 hours from current time
System.out.println(localTime.minus(2, ChronoUnit.HOURS));
}
}

--------------------------------------
13:33:53.602
The hour of the day:: 13
15:33:53.602
13:39:53.602
11:33:53.602

java.time.LocalDateTime
package org.smarttechie;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
* This class demonstrates JAVA 8 data and time API
*
* @author Siva Prasad Rao Janapati
*
*/
public class LocalDateTimeDemonstration {

/**
* @param args
*/
public static void main(String[] args) {

// Get LocalDateTime object
LocalDateTime localDateTime = LocalDateTime.now();

System.out.println(localDateTime);

// Find the length of month. That is, how many days are there for this
// month.
System.out.println("The number of days available for this month:: "
+ localDateTime.getMonth().length(true));

// Know the month name
System.out.println("What is the month name? :: "
+ localDateTime.getMonth().name());

// add 2 days to today's date.
System.out.println(localDateTime.plus(2, ChronoUnit.DAYS));

// substract 2 days from today
System.out.println(localDateTime.minus(2, ChronoUnit.DAYS));

}
}
------------------------------------
2016-04-28T13:34:58.061
The number of days available for this month:: 30
What is the month name? :: APRIL
2016-04-30T13:34:58.061
2016-04-26T13:34:58.061


java.time.Period
기간을 Day보다 적은 단위로 표현이 가능하다. 시, 분, 초, 밀리, 나노
package org.smarttechie;

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;

/**
* This class demonstrates JAVA 8 data and time API
*
* @author Siva Prasad Rao Janapati
*
*/
public class PeriodDemonstration {

/**
* @param args
*/
public static void main(String[] args) {

LocalDate localDate = LocalDate.now();

Period period = Period.between(localDate,
localDate.plus(2, ChronoUnit.DAYS));
System.out.println(period.getDays());
}
}


java.time.Duration
Day보다 큰 단위로 표현이 가능. 월, 년
package org.smarttechie;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
* This class demonstrates JAVA 8 data and time API
*
* @author Siva Prasad Rao Janapati
*
*/
public class DurationDemonstration {

/**
* @param args
*/
public static void main(String[] args) {

LocalDateTime localDateTime = LocalDateTime.now();

// Get the duration between two dates
Duration duration = Duration.between(localDateTime,
localDateTime.plus(2, ChronoUnit.DAYS));
System.out.println(duration.toDays());
}
}

java.time.Year
윤년 여부도 알수 있는 Year 클래스
package org.smarttechie;
import java.time.Year;
/**
* This class demonstrates JAVA 8 data and time API
* @author Siva Prasad Rao Janapati
*
*/
public class YearDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
  //Get year
  Year year = Year.now();
  System.out.println("Year ::" + year);
  //know the year is leap year or not
  System.out.println("Is year[" +year+"] leap year?"+ year.isLeap());
 }
}



Which Mobile App Development Option is Better?

Different alternatives to native code development have their own advantages and philosophy behind. No one tool or approach can be clearly ma...