Skip to content

Commit

Permalink
Merge branch 'develop' into Issue-#2196
Browse files Browse the repository at this point in the history
  • Loading branch information
01es committed Mar 28, 2024
2 parents 6c344aa + 3aea761 commit 986af29
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public abstract class AbstractDomainDrivenTestCase implements IDomainDrivenData,
private static Function<Class<?>, Object> instantiator;

private static final DateTimeFormatter jodaFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
private static final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final DateFormat DATE_TIME_FORMAT_WITHOUT_SECONDS = new SimpleDateFormat("yyyy-MM-dd HH:mm");
private static final DateFormat DATE_TIME_FORMAT_WITHOUT_MILLIS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final DateFormat DATE_TIME_FORMAT_WITH_MILLIS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");


private Session session;
Expand Down Expand Up @@ -132,10 +135,38 @@ public <C extends IEntityDao<E>, E extends AbstractEntity<?>> C co(final Class<E

}

/**
* Converts string representation {@code dateTime} to an instance of {@link Date}.
* Supported formats:
* <ul>
* <li>yyyy-MM-dd</li>
* <li>yyyy-MM-dd HH:mm</li>
* <li>yyyy-MM-dd HH:mm:ss</li>
* <li>yyyy-MM-dd HH:mm:ss.SSS</li>
* </ul>
*
* @param dateTime
* @return
*/
@Override
public final Date date(final String dateTime) {
try {
return formatter.parse(dateTime);
// has millis part?
if (dateTime.indexOf('.') > 0) {
return DATE_TIME_FORMAT_WITH_MILLIS.parse(dateTime);
}
// has time part without seconds?
else if (dateTime.lastIndexOf(":") == 13) {
return DATE_TIME_FORMAT_WITHOUT_SECONDS.parse(dateTime);
}
// has time part without millis?
else if (dateTime.indexOf(":") > 0) {
return DATE_TIME_FORMAT_WITHOUT_MILLIS.parse(dateTime);
}
// otherwise, assume date without the time part
else {
return DATE_FORMAT.parse(dateTime);
}
} catch (ParseException e) {
throw new DomainDriventTestException(format("Could not parse value [%s].", dateTime));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Optional<Long> execute(Connection connection) throws SQLException {

/**
* Drops and creates the specified sequence with new initial value <code>startWithValue</code>.
* The specified sequence must exists before using this function.
* The specified sequence must exist before using this function.
*
* @param seqName
* @param startWithValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ua.com.fielden.platform.domain.testing;

import org.joda.time.DateTime;
import org.junit.Test;
import ua.com.fielden.platform.test_config.AbstractDaoTestCase;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.from;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.select;

/**
* A test case for date creation using {@link AbstractDaoTestCase#date(String)}.
*
* @author TG Team
*
*/
public class DateCreationTest extends AbstractDaoTestCase {

@Test
public void can_create_dates_from_string_with_time_without_minutes() {
assertEquals(new DateTime("2024-04-27T12:30:00.000").toDate(), date("2024-04-27 12:30"));
}

@Test
public void can_create_dates_from_string_with_time_without_millis() {
assertEquals(new DateTime("2024-04-27T12:30:05.000").toDate(), date("2024-04-27 12:30:05"));
}

@Test
public void can_create_dates_from_string_with_time_with_millis() {
assertEquals(new DateTime("2024-04-27T12:30:05.501").toDate(), date("2024-04-27 12:30:05.501"));
assertEquals(new DateTime("2024-04-27T12:30:05.010").toDate(), date("2024-04-27 12:30:05.010"));
}

@Test
public void can_create_dates_from_string_without_time() {
assertEquals(new DateTime("2024-04-27T00:00:00").toDate(), date("2024-04-27"));
}

@Override
protected void populateDomain() {
// there is nothing to populate
}
}

0 comments on commit 986af29

Please sign in to comment.