/** * Copyright 2009, 2010 The Regents of the University of California * Licensed under the Educational Community License, Version 2.0 * (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.osedu.org/licenses/ECL-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an "AS IS" * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing * permissions and limitations under the License. * */ package ch.entwine.weblounge.bridge.oaipmh.harvester; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.opencastproject.util.data.Option.some; import ch.entwine.weblounge.bridge.oaipmh.TestUtil; import ch.entwine.weblounge.bridge.oaipmh.util.PersistenceEnv; import ch.entwine.weblounge.bridge.oaipmh.util.PersistenceUtil; import org.joda.time.DateTime; import org.junit.Test; import org.opencastproject.util.data.Option; import java.util.Date; import javax.persistence.EntityManagerFactory; /** * Test persistence of {@link LastHarvested}. */ public class LastHarvestedTest { @Test public void testLastHarvested() { PersistenceEnv penv = newPenv(); assertEquals(Option.none(), LastHarvested.getLastHarvestDate(penv, "bla")); DateTime now = new DateTime(); Date a = now.toDate(); // save LastHarvested.update(penv, new LastHarvested("url-1", a)); assertEquals(some(a), LastHarvested.getLastHarvestDate(penv, "url-1")); // now update Date b = now.plusMinutes(1).toDate(); LastHarvested.update(penv, new LastHarvested("url-1", b)); assertEquals(some(b), LastHarvested.getLastHarvestDate(penv, "url-1")); // save another LastHarvested.update(penv, new LastHarvested("url-2", a)); assertEquals(some(a), LastHarvested.getLastHarvestDate(penv, "url-2")); // cleanup 1 LastHarvested.cleanup(penv, "url-1 url-2".split(" ")); assertTrue(LastHarvested.getLastHarvestDate(penv, "url-1").isSome()); assertTrue(LastHarvested.getLastHarvestDate(penv, "url-2").isSome()); // cleanup 2 LastHarvested.cleanup(penv, "url-2".split(" ")); assertTrue(LastHarvested.getLastHarvestDate(penv, "url-1").isNone()); assertTrue(LastHarvested.getLastHarvestDate(penv, "url-2").isSome()); } private PersistenceEnv newPenv() { EntityManagerFactory emf = TestUtil.newTestEntityManagerFactory("ch.entwine.weblounge.bridge.oaipmh.harvester"); return PersistenceUtil.newPersistenceEnvironment(emf); } }