View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.cache;
28  
29  import java.util.Arrays;
30  import java.util.Date;
31  
32  import org.apache.hc.client5.http.cache.HttpCacheEntry;
33  import org.apache.hc.client5.http.cache.Resource;
34  import org.apache.hc.client5.http.cache.ResourceIOException;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.util.LangUtils;
37  import org.hamcrest.BaseMatcher;
38  import org.hamcrest.Description;
39  import org.hamcrest.Factory;
40  import org.hamcrest.Matcher;
41  
42  public class HttpCacheEntryMatcher extends BaseMatcher<HttpCacheEntry> {
43  
44      private final HttpCacheEntry expectedValue;
45  
46      public HttpCacheEntryMatcher(final HttpCacheEntry expectedValue) {
47          this.expectedValue = expectedValue;
48      }
49  
50      @Override
51      public boolean matches(final Object item) {
52          if (item instanceof HttpCacheEntry) {
53              try {
54                  final HttpCacheEntry otherValue = (HttpCacheEntry) item;
55  
56                  final int expectedStatus = expectedValue.getStatus();
57                  final int otherStatus = otherValue.getStatus();
58                  if (expectedStatus != otherStatus) {
59                      return false;
60                  }
61                  final Date expectedRequestDate = expectedValue.getRequestDate();
62                  final Date otherRequestDate = otherValue.getRequestDate();
63                  if (!LangUtils.equals(expectedRequestDate, otherRequestDate)) {
64                      return false;
65                  }
66                  final Date expectedResponseDate = expectedValue.getResponseDate();
67                  final Date otherResponseDate = otherValue.getResponseDate();
68                  if (!LangUtils.equals(expectedResponseDate, otherResponseDate)) {
69                      return false;
70                  }
71                  final Header[] expectedHeaders = expectedValue.getHeaders();
72                  final Header[] otherHeaders = otherValue.getHeaders();
73                  if (expectedHeaders.length != otherHeaders.length) {
74                      return false;
75                  }
76                  for (int i = 0; i < expectedHeaders.length; i++) {
77                      final Header h1 = expectedHeaders[i];
78                      final Header h2 = otherHeaders[i];
79                      if (!h1.getName().equals(h2.getName()) || !LangUtils.equals(h1.getValue(), h2.getValue())) {
80                          return false;
81                      }
82                  }
83                  final Resource expectedResource = expectedValue.getResource();
84                  final byte[] expectedContent = expectedResource != null ? expectedResource.get() : null;
85                  final Resource otherResource = otherValue.getResource();
86                  final byte[] otherContent = otherResource != null ? otherResource.get() : null;
87                  if (!Arrays.equals(expectedContent, otherContent)) {
88                      return false;
89                  }
90                  return true;
91              } catch (final ResourceIOException ex) {
92                  throw new RuntimeException(ex);
93              }
94          }
95          return false;
96      }
97  
98      @Override
99      public void describeTo(final Description description) {
100         description.appendValue(expectedValue);
101     }
102 
103     @Factory
104     public static Matcher<HttpCacheEntry> equivalent(final HttpCacheEntry target) {
105         return new HttpCacheEntryMatcher(target);
106     }
107 
108 }