[size=1em]01 [size=1em]02 [size=1em]03 [size=1em]04 [size=1em]05 [size=1em]06 [size=1em]07 [size=1em]08 [size=1em]09 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 [size=1em]16 [size=1em]17 [size=1em]18 | [size=1em][size=1em]private final static SimpleDateFormat SDF [size=1em] = new SimpleDateFormat("yyyy-MM-dd HH:mm"); [size=1em]@Test [size=1em]public void shouldReturnHourlyRanges() throws ParseException { [size=1em] // given [size=1em] Date dateFrom = SDF.parse("2012-07-23 12:00"); [size=1em] Date dateTo = SDF.parse("2012-07-23 15:00"); [size=1em] // when [size=1em] final List<range> ranges = HourlyRange.getRanges(dateFrom, dateTo); [size=1em] // then [size=1em] assertEquals(3, ranges.size()); [size=1em] assertEquals(SDF.parse("2012-07-23 12:00").getTime(), ranges.get(0).getStart()); [size=1em] assertEquals(SDF.parse("2012-07-23 13:00").getTime(), ranges.get(0).getEnd()); [size=1em] assertEquals(SDF.parse("2012-07-23 13:00").getTime(), ranges.get(1).getStart()); [size=1em] assertEquals(SDF.parse("2012-07-23 14:00").getTime(), ranges.get(1).getEnd()); [size=1em] assertEquals(SDF.parse("2012-07-23 14:00").getTime(), ranges.get(2).getStart()); [size=1em] assertEquals(SDF.parse("2012-07-23 15:00").getTime(), ranges.get(2).getEnd()); [size=1em]} |
[size=1em]1 [size=1em]2 [size=1em]3 | [size=1em][size=1em]org.junit.ComparisonFailure: [size=1em]Expected :1343044800000 [size=1em]Actual :1343041200000 |
[size=1em]01 [size=1em]02 [size=1em]03 [size=1em]04 [size=1em]05 [size=1em]06 [size=1em]07 [size=1em]08 [size=1em]09 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 [size=1em]16 [size=1em]17 [size=1em]18 | [size=1em][size=1em]private void assertThatRangeExists(List<Range> ranges, int rangeNb, [size=1em] String start, String stop) throws ParseException { [size=1em] assertEquals(ranges.get(rangeNb).getStart(), SDF.parse(start).getTime()); [size=1em] assertEquals(ranges.get(rangeNb).getEnd(), SDF.parse(stop).getTime()); [size=1em]} [size=1em]@Test [size=1em]public void shouldReturnHourlyRanges() throws ParseException { [size=1em] // given [size=1em] Date dateFrom = SDF.parse("2012-07-23 12:00"); [size=1em] Date dateTo = SDF.parse("2012-07-23 15:00"); [size=1em] // when [size=1em] final List<Range> ranges = HourlyRange.getRanges(dateFrom, dateTo); [size=1em] // then [size=1em] assertEquals(ranges.size(), 3); [size=1em] assertThatRangeExists(ranges, 0, "2012-07-23 12:00", "2012-07-23 13:00"); [size=1em] assertThatRangeExists(ranges, 1, "2012-07-23 13:00", "2012-07-23 14:00"); [size=1em] assertThatRangeExists(ranges, 2, "2012-07-23 14:00", "2012-07-23 15:00"); [size=1em]} |
[size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 | [size=1em][size=1em]assertThat(myDouble).isLessThanOrEqualTo(2.0d); [size=1em]assertThat(myListOfStrings).contains("a"); [size=1em]assertThat("some text") [size=1em] .isNotEmpty() [size=1em] .startsWith("some") [size=1em] .hasLength(9); |
[size=1em]01 [size=1em]02 [size=1em]03 [size=1em]04 [size=1em]05 [size=1em]06 [size=1em]07 [size=1em]08 [size=1em]09 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 | [size=1em][size=1em]@Test [size=1em]public void shouldReturnHourlyRanges() throws ParseException { [size=1em] // given [size=1em] Date dateFrom = SDF.parse("2012-07-23 12:00"); [size=1em] Date dateTo = SDF.parse("2012-07-23 15:00"); [size=1em] // when [size=1em] List<Range> ranges = HourlyRange.getRanges(dateFrom, dateTo); [size=1em] // then [size=1em] RangeAssert.assertThat(ranges) [size=1em] .hasSize(3) [size=1em] .isSortedAscending() [size=1em] .hasRange("2012-07-23 12:00", "2012-07-23 13:00") [size=1em] .hasRange("2012-07-23 13:00", "2012-07-23 14:00") [size=1em] .hasRange("2012-07-23 14:00", "2012-07-23 15:00"); [size=1em]} |
[size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 [size=1em]9 | [size=1em][size=1em]public class RangeAssert extends ListAssert<Range> { [size=1em] protected RangeAssert(List<Range> ranges) { [size=1em] super(ranges); [size=1em] } [size=1em] public static RangeAssert assertThat(List<Range> ranges) { [size=1em] return new RangeAssert(ranges); [size=1em] } |
[size=1em]01 [size=1em]02 [size=1em]03 [size=1em]04 [size=1em]05 [size=1em]06 [size=1em]07 [size=1em]08 [size=1em]09 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 [size=1em]16 [size=1em]17 [size=1em]18 [size=1em]19 [size=1em]20 [size=1em]21 [size=1em]22 [size=1em]23 [size=1em]24 [size=1em]25 [size=1em]26 [size=1em]27 [size=1em]28 [size=1em]29 [size=1em]30 [size=1em]31 [size=1em]32 [size=1em]33 | [size=1em][size=1em]private final static SimpleDateFormat SDF [size=1em] = new SimpleDateFormat("yyyy-MM-dd HH:mm"); [size=1em] public RangeAssert isSortedAscending() { [size=1em] isNotNull(); [size=1em] long start = 0; [size=1em] for (int i = 0; i < actual.size(); i++) { [size=1em] Assertions.assertThat(start) [size=1em] .isLessThan(actual.get(i).getStart()); [size=1em] start = actual.get(i).getStart(); [size=1em] } [size=1em] return this; [size=1em] } [size=1em] public RangeAssert hasRange(String from, String to) throws ParseException { [size=1em] isNotNull(); [size=1em] Long dateFrom = SDF.parse(from).getTime(); [size=1em] Long dateTo = SDF.parse(to).getTime(); [size=1em] boolean found = false; [size=1em] for (Range range : actual) { [size=1em] if (range.getStart() == dateFrom && range.getEnd() == dateTo) { [size=1em] found = true; [size=1em] } [size=1em] } [size=1em] Assertions [size=1em] .assertThat(found) [size=1em] .isTrue(); [size=1em] return this; [size=1em] } [size=1em]} |
[size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 | [size=1em][size=1em]Assertions [size=1em] .assertThat(actual.size()) [size=1em] .as("number of ranges") [size=1em] .isEqualTo(expectedSize); |
[size=1em]1 [size=1em]2 [size=1em]3 | [size=1em][size=1em]org.junit.ComparisonFailure: [number of ranges] [size=1em] Expected :4 [size=1em] Actual :3 |
[size=1em]01 [size=1em]02 [size=1em]03 [size=1em]04 [size=1em]05 [size=1em]06 [size=1em]07 [size=1em]08 [size=1em]09 [size=1em]10 [size=1em]11 | [size=1em][size=1em]public RangeAssert hasRange(String from, String to) throws ParseException { [size=1em] ... [size=1em] String errMsg = String.format("ranges\n%s\ndo not contain %s-%s", [size=1em] actual ,from, to); [size=1em] ... [size=1em] Assertions.assertThat(found) [size=1em] .overridingErrorMessage(errMsg) [size=1em] .isTrue(); [size=1em] ... [size=1em]} |
[size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 | [size=1em][size=1em]HourlyRange{Mon Jul 23 12:00:00 CEST 2012 to Mon Jul 23 13:00:00 CEST 2012}, [size=1em]HourlyRange{Mon Jul 23 13:00:00 CEST 2012 to Mon Jul 23 14:00:00 CEST 2012}, [size=1em]HourlyRange{Mon Jul 23 14:00:00 CEST 2012 to Mon Jul 23 15:00:00 CEST 2012}] [size=1em]do not contain 2012-07-23 16:00-2012-07-23 14:00 |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |