001/* 002 * Copyright 2020 Global Biodiversity Information Facility (GBIF) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.gbif.api.util.comparators; 017 018import org.gbif.api.model.registry.Endpoint; 019 020import java.util.Date; 021 022import org.junit.jupiter.api.Test; 023 024import static org.junit.jupiter.api.Assertions.assertEquals; 025import static org.junit.jupiter.api.Assertions.assertTrue; 026 027public class EndpointCreatedComparatorTest { 028 029 private static final EndpointCreatedComparator COMP = EndpointCreatedComparator.INSTANCE; 030 private final long now = System.currentTimeMillis(); 031 032 @Test 033 public void testSuccessfulSimpleComparisons() { 034 Endpoint e1 = new Endpoint(); 035 Endpoint e2 = new Endpoint(); 036 037 e1.setCreated(new Date(now)); 038 e2.setCreated(new Date(now)); 039 assertEquals(0, COMP.compare(e1, e2)); 040 041 e2.setCreated(new Date(now-10000)); 042 assertTrue(COMP.compare(e1, e2) > 0); 043 044 e2.setCreated(new Date(now+10000)); 045 assertTrue(COMP.compare(e1, e2) < 0); 046 } 047 048 @Test 049 public void testNullComparisons() { 050 Endpoint e1 = new Endpoint(); 051 Endpoint e2 = new Endpoint(); 052 053 assertEquals(0, COMP.compare(e1, e2)); 054 055 e1.setCreated(new Date(now)); 056 assertTrue(COMP.compare(e1, e2) < 0); 057 058 e2.setCreated(new Date(now-10000)); 059 assertTrue(COMP.compare(e1, e2) > 0); 060 061 e2.setCreated(new Date(now+10000)); 062 assertTrue(COMP.compare(e1, e2) < 0); 063 } 064}