package votorola.a.diff.harvest; // Copyright 2012. Christian Weilbach. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import static org.junit.Assert.assertEquals; import java.io.IOException; import java.net.URISyntaxException; import java.sql.SQLException; import java.util.Date; import javax.script.ScriptException; import org.junit.Before; import org.junit.Test; import votorola.a.VoteServer; import votorola.a.diff.harvest.StateTable; /** * Unit test for URLTable. */ public class StateTableTest { private volatile StateTable stateTable; @Before public void setup() throws IOException, ScriptException, SQLException, URISyntaxException { final VoteServer.Run vsr = new VoteServer( System.getProperty("user.name")).new Run(false); assert (vsr.database() != null); stateTable = new StateTable(vsr.database()); if(stateTable.exists()) { stateTable.drop(); } stateTable.create(); } final private String baseUrl = "http://some.base.url/path/"; // oldest archive post (first post in archive) final private Marker startM = Marker.create( "00000/abcd.html", new Date(1)); // some old post (stale temp marker) final private Marker oldM = Marker.create( "01234/abcd.html", new Date(50000000)); // new post final private Marker newM = Marker.create( "09999/abcd.html", new Date(100000000)); /** * Test basic operations for one URL. * @throws SQLException */ @Test public void testPutAndGetNewest() throws SQLException { stateTable.put(baseUrl, oldM); assert(stateTable.getNewest(baseUrl).equals(oldM)); assert(stateTable.getNewest(baseUrl, StateTable.Type.TEMP).equals(oldM)); } /** * Test range removal of temporary * @throws SQLException */ @Test public void testFinishAndUpdate() throws SQLException { stateTable.put(baseUrl, newM); stateTable.put(baseUrl, oldM); stateTable.put(baseUrl, startM); // mark start finished stateTable.finish(baseUrl, startM, startM); // only newM is temporary assert(stateTable.getNewest(baseUrl, StateTable.Type.TEMP).equals(newM)); // check that newest is final now assert(stateTable.getNewest(baseUrl, StateTable.Type.FIN).equals(startM)); // no PERM marker assert(stateTable.getNewest(baseUrl, StateTable.Type.PERM).path().equals("")); stateTable.update(baseUrl); assert(stateTable.getNewest(baseUrl, StateTable.Type.PERM).equals(startM)); assert(stateTable.getNewest(baseUrl, StateTable.Type.TEMP).equals(newM)); stateTable.finish(baseUrl, newM, startM); // oldM is removed and no temporary markers are left assert(stateTable.getNewest(baseUrl, StateTable.Type.TEMP).path().equals("")); assert(stateTable.getNewest(baseUrl, StateTable.Type.FIN).equals(newM)); assert(stateTable.getNewest(baseUrl, StateTable.Type.PERM).equals(startM)); stateTable.update(baseUrl); // check that newM is permanent now assert(stateTable.getNewest(baseUrl, StateTable.Type.PERM).equals(newM)); // no FIN marker anymore assertEquals(stateTable.getNewest(baseUrl, StateTable.Type.FIN).path(), ""); } }