/** * Create a context with the provided optional id. * The provided id must be higher than the current mark. * @param id optional id to use * @return Created ImportContext * @throws IOException * @throws IllegalArgumentException if the provided id is invalid */ public ImportContext createContext(Long id) throws IOException, IllegalArgumentException { ImportContext context = new ImportContext(); if (id != null) { Long retval = contextStore.advanceId(id); assert retval >= id; context.setId(retval); contextStore.save(context); } else { contextStore.add(context); } return context; }
/** * Create a context with the provided optional id. The provided id must be higher than the * current mark. * * @param id optional id to use * @return Created ImportContext * @throws IOException * @throws IllegalArgumentException if the provided id is invalid */ public ImportContext createContext(Long id) throws IOException, IllegalArgumentException { ImportContext context = new ImportContext(); if (id != null) { Long retval = contextStore.advanceId(id); assert retval >= id; context.setId(retval); contextStore.save(context); } else { contextStore.add(context); } return context; }
@Test public void testIDManagement() throws Exception { ImportStore store = new MemoryImportStore(); // verify base - first one is zero ImportContext zero = new ImportContext(); store.add(zero); assertEquals(new Long(0), zero.getId()); // try for zero again (less than current case - client out of sync) Long advanceId = store.advanceId(0L); assertEquals(new Long(1), advanceId); // and again for current (equals current case - normal mode) advanceId = store.advanceId(2L); assertEquals(new Long(2), advanceId); // now jump ahead (client advances case - server out of sync) advanceId = store.advanceId(666L); assertEquals(new Long(666), advanceId); // the next created import should be one higher ImportContext dumby = new ImportContext(); store.add(dumby); assertEquals(new Long(667), dumby.getId()); } }