001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.assertions; 014 015import static org.apache.juneau.internal.ObjectUtils.*; 016 017import java.time.temporal.*; 018import java.util.*; 019 020import org.apache.juneau.internal.*; 021 022/** 023 * Used for fluent assertion calls against dates. 024 * 025 * <h5 class='section'>Example:</h5> 026 * <p class='bcode w800'> 027 * <jc>// Validates the response expiration is after the current date.</jc> 028 * <jv>client</jv> 029 * .get(<jsf>URL</jsf>) 030 * .run() 031 * .assertDateHeader(<js>"Expires"</js>).isAfterNow(); 032 * </p> 033 * 034 * @param <R> The return type. 035 */ 036@FluentSetters(returns="FluentDateAssertion<R>") 037public class FluentDateAssertion<R> extends FluentComparableAssertion<R> { 038 039 private final Date value; 040 041 /** 042 * Constructor. 043 * 044 * @param value The value being tested. 045 * @param returns The object to return after the test. 046 */ 047 public FluentDateAssertion(Date value, R returns) { 048 this(null, value, returns); 049 } 050 051 /** 052 * Constructor. 053 * 054 * @param creator The assertion that created this assertion. 055 * @param value The value being tested. 056 * @param returns The object to return after the test. 057 */ 058 public FluentDateAssertion(Assertion creator, Date value, R returns) { 059 super(creator, value, returns); 060 this.value = value; 061 } 062 063 /** 064 * Asserts that the value equals the specified value at the specified precision. 065 * 066 * @param value The value to check against. 067 * @param precision The precision (e.g. {@link ChronoUnit#SECONDS}. 068 * @return The response object (for method chaining). 069 * @throws AssertionError If assertion failed. 070 */ 071 public R isEqual(Date value, ChronoUnit precision) throws AssertionError { 072 if (ne(this.value, value, (x,y)->x.toInstant().truncatedTo(precision).equals(y.toInstant().truncatedTo(precision)))) 073 throw error("Unexpected value.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); 074 return returns(); 075 } 076 077 /** 078 * Asserts that the value is after the specified value. 079 * 080 * @param value The values to check against. 081 * @return The response object (for method chaining). 082 * @throws AssertionError If assertion failed. 083 */ 084 public R isAfter(Date value) throws AssertionError { 085 exists(); 086 assertNotNull("value", value); 087 if (! (this.value.after(value))) 088 throw error("Value was not after expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); 089 return returns(); 090 } 091 092 /** 093 * Asserts that the value is after the current date. 094 * 095 * @return The response object (for method chaining). 096 * @throws AssertionError If assertion failed. 097 */ 098 public R isAfterNow() throws AssertionError { 099 return isAfter(new Date()); 100 } 101 102 /** 103 * Asserts that the value is before the specified value. 104 * 105 * @param value The values to check against. 106 * @return The response object (for method chaining). 107 * @throws AssertionError If assertion failed. 108 */ 109 public R isBefore(Date value) throws AssertionError { 110 exists(); 111 assertNotNull("value", value); 112 if (! (this.value.before(value))) 113 throw error("Value was not before expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); 114 return returns(); 115 } 116 117 /** 118 * Asserts that the value is before the current date. 119 * 120 * @return The response object (for method chaining). 121 * @throws AssertionError If assertion failed. 122 */ 123 public R isBeforeNow() throws AssertionError { 124 return isBefore(new Date()); 125 } 126 127 /** 128 * Asserts that the value is between (exclusive) the specified upper and lower values. 129 * 130 * @param lower The lower value to check against. 131 * @param upper The upper value to check against. 132 * @return The response object (for method chaining). 133 * @throws AssertionError If assertion failed. 134 */ 135 public R isBetween(Date lower, Date upper) throws AssertionError { 136 exists(); 137 assertNotNull("lower", lower); 138 assertNotNull("upper", upper); 139 isBefore(upper); 140 isAfter(lower); 141 return returns(); 142 } 143 144 // <FluentSetters> 145 146 @Override /* GENERATED - Assertion */ 147 public FluentDateAssertion<R> msg(String msg, Object...args) { 148 super.msg(msg, args); 149 return this; 150 } 151 152 @Override /* GENERATED - Assertion */ 153 public FluentDateAssertion<R> stderr() { 154 super.stderr(); 155 return this; 156 } 157 158 @Override /* GENERATED - Assertion */ 159 public FluentDateAssertion<R> stdout() { 160 super.stdout(); 161 return this; 162 } 163 164 // </FluentSetters> 165}