Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release-6u1' into v6-master
Browse files Browse the repository at this point in the history
  • Loading branch information
oguchi-shiori03 committed Mar 15, 2024
2 parents 44351b7 + 4992bcc commit 73cc925
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 31 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.nablarch.framework</groupId>
<artifactId>nablarch-core</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>

<scm>
<connection>scm:git:git://github.com/nablarch/${project.artifactId}.git</connection>
Expand All @@ -17,7 +17,7 @@
<parent>
<groupId>com.nablarch</groupId>
<artifactId>nablarch-parent</artifactId>
<version>6</version>
<version>6u1</version>
</parent>

<dependencies>
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/nablarch/core/date/SystemTimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.TimeZone;

import nablarch.core.ThreadContext;
import nablarch.core.repository.SystemRepository;
import nablarch.core.util.annotation.Published;

Expand Down Expand Up @@ -53,6 +56,17 @@ public static Timestamp getTimestamp() {
return getProvider().getTimestamp();
}

/**
* システム日時を取得する。
*
* @return システム日時
*/
public static LocalDateTime getLocalDateTime(){
TimeZone tz = getTimeZone();

return getTimestamp().toInstant().atZone(tz.toZoneId()).toLocalDateTime();
}

/**
* システム日付を yyyyMMdd 形式の文字列で取得する。
*
Expand Down Expand Up @@ -99,4 +113,19 @@ private static SystemTimeProvider getProvider() {
}
return provider;
}

/**
* タイムゾーンを取得する。
*
* <p>設定されているタイムゾーンを取得する。
*
* @return スレッドコンテキストに設定されているタイムゾーン。スレッドコンテキストにタイムゾーンが設定されていない場合は、システムデフォルトのタイムゾーン。
*/
private static TimeZone getTimeZone(){
TimeZone timeZone = ThreadContext.getTimeZone();
if (timeZone != null) {
return timeZone;
}
return TimeZone.getDefault();
}
}
102 changes: 73 additions & 29 deletions src/test/java/nablarch/core/date/SystemTimeUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,61 @@
package nablarch.core.date;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThrows;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import nablarch.core.repository.ObjectLoader;
import nablarch.core.ThreadContext;
import nablarch.core.repository.SystemRepository;
import nablarch.util.FixedSystemTimeProvider;

import org.junit.Assert;
import org.junit.After;
import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
* {@link SystemTimeUtil}のテストクラス
* @author Miki Habu
*/
public class SystemTimeUtilTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

private TimeZone defaultTimeZone;

/**
* テスト実施前準備
*
*/
@Before
public void setUp() {
defaultTimeZone = TimeZone.getDefault();
// リポジトリの初期化
SystemRepository.load(new ObjectLoader() {
@Override
public Map<String, Object> load() {
HashMap<String, Object> result = new HashMap<String, Object>();
FixedSystemTimeProvider provider = new FixedSystemTimeProvider();
provider.setFixedDate("20110107123456");
result.put("systemTimeProvider", provider);
return result;
}
SystemRepository.load(() -> {
HashMap<String, Object> result = new HashMap<>();
FixedSystemTimeProvider provider = new FixedSystemTimeProvider();
provider.setFixedDate("20110107123456");
result.put("systemTimeProvider", provider);
return result;
});
}


/**
* テスト実施後処理
*
*/
@After
public void tearDown() {
TimeZone.setDefault(defaultTimeZone);
}

/**
* {@link SystemTimeUtil#getDate()}のテスト
*
Expand All @@ -55,22 +65,19 @@ public Map<String, Object> load() {
public void testGetDate() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date expected = sdf.parse("20110107123456000");
Assert.assertThat(SystemTimeUtil.getDate(), is(expected));
MatcherAssert.assertThat(SystemTimeUtil.getDate(), is(expected));
}

/**
* {@link SystemTimeUtil#getDate()} のテスト。
* <p/>
* リポジトリに値がない場合、例外を送出するかどうか。
* @throws Exception
*/
@Test
public void testGetDateErr() throws Exception {
public void testGetDateErr() {
SystemRepository.clear();
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("specified systemTimeProvider is not registered in SystemRepository.");

SystemTimeUtil.getDate();
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, SystemTimeUtil::getDate);
MatcherAssert.assertThat(exception.getMessage(), is("specified systemTimeProvider is not registered in SystemRepository."));
}

/**
Expand All @@ -79,31 +86,68 @@ public void testGetDateErr() throws Exception {
@Test
public void testGetTimestamp() {
Timestamp expected = Timestamp.valueOf("2011-01-07 12:34:56.000000000");
Assert.assertThat(SystemTimeUtil.getTimestamp(), is(expected));
MatcherAssert.assertThat(SystemTimeUtil.getTimestamp(), is(expected));
}

/**
* {@link SystemTimeUtil#getLocalDateTime()}のテスト
* <p/>
* スレッドコンテキストにタイムゾーンが設定されていない場合、システムデフォルトのタイムゾーンを使用して{@link LocalDateTime}を取得できること。
*/
@Test
public void testGetLocalDateTimeDefault() {
ThreadContext.setTimeZone(null);
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));

LocalDateTime expected = ZonedDateTime
.parse(
"2011-01-07T12:34:56",
DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(defaultTimeZone.toZoneId())
)// 文字列を、システムデフォルトのタイムゾーンの日時と考えるようにしたうえで変換
.withZoneSameInstant(ZoneId.of("America/Los_Angeles"))// タイムゾーンをアメリカに変更
.toLocalDateTime();

MatcherAssert.assertThat(SystemTimeUtil.getLocalDateTime(), is(expected));
}

/**
* {@link SystemTimeUtil#getLocalDateTime()} のテスト。
* <p/>
* スレッドコンテキストにタイムゾーンが設定されている場合、スレッドコンテキストのタイムゾーンを使用する。
*/
@Test
public void testGetLocalDateTime() {
ThreadContext.setTimeZone(defaultTimeZone);
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMddHHmmss");
LocalDateTime expected = LocalDateTime.parse("20110107123456", dtf);

MatcherAssert.assertThat(SystemTimeUtil.getLocalDateTime(), is(expected));
}

/**
* {@link SystemTimeUtil#getDateString()}のテスト。
*/
@Test
public void testGetCurrentDateString() {
Assert.assertThat(SystemTimeUtil.getDateString(), is("20110107"));
MatcherAssert.assertThat(SystemTimeUtil.getDateString(), is("20110107"));
}

/**
* {@link SystemTimeUtil#getDateTimeString()}のテスト
*/
@Test
public void testGetDateTimeString() {
Assert.assertThat(SystemTimeUtil.getDateTimeString(), is("20110107123456"));
MatcherAssert.assertThat(SystemTimeUtil.getDateTimeString(), is("20110107123456"));
}

/**
* {@link SystemTimeUtil#getDateTimeMillisString()}のテスト
*/
@Test
public void testGetDateTimeMillisString() {
Assert.assertThat(SystemTimeUtil.getDateTimeMillisString(), is("20110107123456000"));
MatcherAssert.assertThat(SystemTimeUtil.getDateTimeMillisString(), is("20110107123456000"));
}

}

0 comments on commit 73cc925

Please sign in to comment.