@Test
  public void supportsPosixlyCorrectBehavior() {
    OptionParser parser = new OptionParser("i:j::k");
    String[] arguments = {
      "-ibar", "-i", "junk", "xyz", "-jixnay", "foo", "-k", "blah", "--", "bah"
    };

    OptionSet options = parser.parse(arguments);

    assertTrue(options.has("i"));
    assertTrue(options.has("j"));
    assertTrue(options.has("k"));
    assertEquals(asList("bar", "junk"), options.valuesOf("i"));
    assertEquals(singletonList("ixnay"), options.valuesOf("j"));
    assertEquals(asList("xyz", "foo", "blah", "bah"), options.nonOptionArguments());

    parser.posixlyCorrect(true);
    options = parser.parse(arguments);

    assertTrue(options.has("i"));
    assertFalse(options.has("j"));
    assertFalse(options.has("k"));
    assertEquals(asList("bar", "junk"), options.valuesOf("i"));
    assertEquals(emptyList(), options.valuesOf("j"));
    assertEquals(
        asList("xyz", "-jixnay", "foo", "-k", "blah", "--", "bah"), options.nonOptionArguments());
  }
  public void testConnectionFailure() throws Exception {
    // create mock objects that will simulate a connection failure
    GSSClient client = createMock(GSSClient.class);
    expect(client.getCentralRevision((QName) anyObject()))
        .andThrow(new IOException("Host unreachable"));
    replay(client);
    GSSClientFactory factory = createMock(GSSClientFactory.class);
    expect(factory.createClient(new URL("http://localhost:8081/geoserver/ows"), null, null))
        .andReturn(client);
    replay(factory);

    synch.clientFactory = factory;

    // perform synch
    Date start = new Date();
    synch.synchronizeOustandlingLayers();
    Date end = new Date();

    // check we stored the last failure marker
    SimpleFeature f =
        getSingleFeature(fsUnitTables, ff.equal(ff.property("table_id"), ff.literal(1), false));
    Date lastFailure = (Date) f.getAttribute("last_failure");
    assertNotNull(lastFailure);
    assertTrue(lastFailure.compareTo(start) >= 0 && lastFailure.compareTo(end) <= 0);

    // check we marked the unit as failed
    f = getSingleFeature(fsUnits, ff.equal(ff.property("unit_name"), ff.literal("unit1"), false));
    assertTrue((Boolean) f.getAttribute("errors"));
  }
 @Test
 public void resolvesToShortOptionsWithArgumentFollowingX() {
   OptionSet options = parser.parse("-foxbar");
   assertFalse(options.has("fox"));
   assertTrue(options.has("f"));
   assertTrue(options.has("o"));
   assertTrue(options.has("x"));
   assertEquals(singletonList("bar"), options.valuesOf("x"));
 }
 @Test
 public void shortOptionsInDifferentOrder() {
   OptionSet options = parser.parse("-fxo");
   assertFalse(options.has("fox"));
   assertTrue(options.has("f"));
   assertFalse(options.has("o"));
   assertTrue(options.has("x"));
   assertEquals(singletonList("o"), options.valuesOf("x"));
 }
 private static void delete(File directory) {
   for (File file : directory.listFiles()) {
     if (file.isDirectory()) {
       delete(file);
     } else {
       assertTrue(file.delete());
     }
   }
   assertTrue(directory.delete());
 }
 @Test
 public void shouldFindChangedFiles() throws IOException {
   Set<File> files = detector.findChangedFiles();
   assertTrue("Should have found changed files on first run", files.size() > 0);
   assertThat(
       "TestFakeProduct should be in changed list",
       files,
       hasItem(getFileForClass(TestFakeProduct.class)));
   assertTrue(
       "FakeProduct should be in changed list",
       files.contains(getFileForClass(FakeProduct.class)));
   assertTrue("Should have no changed files now", detector.findChangedFiles().isEmpty());
 }
 @Test
 public void resolvesAbbreviationToLongOption() {
   OptionSet options = parser.parse("-fo");
   assertTrue(options.has("fox"));
   assertFalse(options.has("f"));
   assertFalse(options.has("o"));
 }
  @Test
  public void singleLongOption() {
    OptionSet options = parser.parse("--verbose");

    assertTrue(options.has("verbose"));
    assertEquals(emptyList(), options.valuesOf("verbose"));
    assertEquals(emptyList(), options.nonOptionArguments());
  }
 @Test
 public void resolvesToLongOptionWithSingleDashEvenWithMatchingShortOptions() {
   OptionSet options = parser.parse("-fox");
   assertTrue(options.has("fox"));
   assertFalse(options.has("f"));
   assertFalse(options.has("o"));
   assertFalse(options.has("x"));
 }
  @Test
  public void abbreviationIsALegalOption() {
    OptionSet options = parser.parse("--verb");

    assertFalse(options.has("verbose"));
    assertTrue(options.has("verb"));
    assertEquals(emptyList(), options.valuesOf("verb"));
    assertEquals(emptyList(), options.nonOptionArguments());
  }
  @Test
  public void shouldFindRemovedFiles() throws Exception {
    File newFile = createFileForClass(TestFakeProduct.class);
    assertThat(detector.findChangedFiles(), hasItem(newFile));

    FileUtils.forceDelete(newFile);

    assertTrue(detector.filesWereRemoved());
    assertThat(detector.findChangedFiles(), not(hasItem(newFile)));
  }
  @Test
  public void withArgumentComingAfterCluster() {
    OptionSet options = parser.parse("-fox", "bar");

    assertTrue(options.has("fox"));
    assertFalse(options.has("f"));
    assertFalse(options.has("o"));
    assertFalse(options.has("x"));
    assertEquals(singletonList("bar"), options.nonOptionArguments());
  }
  public void testLocalChanges() throws Exception {
    // apply a local change on Central so that we'll get a non empty transaction sent to the client
    VersioningFeatureStore restricted =
        (VersioningFeatureStore) synchStore.getFeatureSource("restricted");
    SimpleFeatureType schema = restricted.getSchema();
    // remove the third feature
    Id removeFilter =
        ff.id(singleton(ff.featureId("restricted.c15e76ab-e44b-423e-8f85-f6d9927b878a")));
    restricted.removeFeatures(removeFilter);
    assertEquals(3, restricted.getCount(Query.ALL));

    // build the expected PostDiff request
    QName typeName = new QName("http://www.openplans.org/spearfish", "restricted");
    PostDiffType postDiff = new PostDiffType();
    postDiff.setFromVersion(-1);
    postDiff.setToVersion(3);
    postDiff.setTypeName(typeName);
    TransactionType changes = WfsFactory.eINSTANCE.createTransactionType();
    DeleteElementType delete = WfsFactory.eINSTANCE.createDeleteElementType();
    delete.setTypeName(typeName);
    delete.setFilter(removeFilter);
    changes.getDelete().add(delete);
    postDiff.setTransaction(changes);

    // create mock objects that will check the calls are flowing as expected
    GSSClient client = createMock(GSSClient.class);
    expect(client.getCentralRevision((QName) anyObject())).andReturn(new Long(-1));
    client.postDiff(postDiff);
    expect(client.getDiff((GetDiffType) anyObject())).andReturn(new GetDiffResponseType());
    replay(client);
    GSSClientFactory factory = createMock(GSSClientFactory.class);
    expect(factory.createClient(new URL("http://localhost:8081/geoserver/ows"), null, null))
        .andReturn(client);
    replay(factory);

    synch.clientFactory = factory;

    // perform synch
    Date start = new Date();
    synch.synchronizeOustandlingLayers();
    Date end = new Date();

    // check we stored the last synch marker
    SimpleFeature f =
        getSingleFeature(fsUnitTables, ff.equal(ff.property("table_id"), ff.literal(1), false));
    Date lastSynch = (Date) f.getAttribute("last_synchronization");
    assertNotNull(lastSynch);
    assertTrue(lastSynch.compareTo(start) >= 0 && lastSynch.compareTo(end) <= 0);
    assertNull(f.getAttribute("last_failure"));

    // check we marked the unit as succeded
    f = getSingleFeature(fsUnits, ff.equal(ff.property("unit_name"), ff.literal("unit1"), false));
    assertFalse((Boolean) f.getAttribute("errors"));
  }
 @Test
 public void shouldDetectChangedFilesByTimeStamp() throws Exception {
   detector =
       new FileChangeDetector() {
         @Override
         protected long getModificationTimestamp(File classFile) {
           return timestamp;
         }
       };
   detector.setClasspathProvider(classpath);
   assertFalse(
       "Should have found changed files on first run", detector.findChangedFiles().isEmpty());
   assertTrue("Timestamp is unchanged", detector.findChangedFiles().isEmpty());
   timestamp += 100;
   assertFalse("Timestamp changed", detector.findChangedFiles().isEmpty());
 }
  public void testEmptyUpdates() throws Exception {
    QName typeName = new QName("http://www.openplans.org/spearfish", "restricted");

    // build a "no loca changes" post diff
    PostDiffType postDiff = new PostDiffType();
    postDiff.setFromVersion(-1);
    postDiff.setToVersion(-1);
    postDiff.setTypeName(typeName);
    postDiff.setTransaction(WfsFactory.eINSTANCE.createTransactionType());

    // create mock objects that will simulate a connection failure
    GSSClient client = createMock(GSSClient.class);
    expect(client.getCentralRevision((QName) anyObject())).andReturn(new Long(-1));
    client.postDiff(postDiff);
    expect(client.getDiff((GetDiffType) anyObject())).andReturn(new GetDiffResponseType());
    replay(client);
    GSSClientFactory factory = createMock(GSSClientFactory.class);
    expect(factory.createClient(new URL("http://localhost:8081/geoserver/ows"), null, null))
        .andReturn(client);
    replay(factory);

    synch.clientFactory = factory;

    // perform synch
    Date start = new Date();
    synch.synchronizeOustandlingLayers();
    Date end = new Date();

    // check we stored the last synch marker
    SimpleFeature f =
        getSingleFeature(fsUnitTables, ff.equal(ff.property("table_id"), ff.literal(1), false));
    Date lastSynch = (Date) f.getAttribute("last_synchronization");
    assertNotNull(lastSynch);
    assertTrue(lastSynch.compareTo(start) >= 0 && lastSynch.compareTo(end) <= 0);
    assertNull(f.getAttribute("last_failure"));

    // check we marked the unit as succeded
    f = getSingleFeature(fsUnits, ff.equal(ff.property("unit_name"), ff.literal("unit1"), false));
    assertFalse((Boolean) f.getAttribute("errors"));
  }
 @Test
 public void shouldHandleStrangeClasspaths() throws Exception {
   ChangeDetector changeDetector = new FileChangeDetector();
   changeDetector.setClasspathProvider(new StandaloneClasspath(new ArrayList<File>(), ""));
   assertTrue(changeDetector.findChangedFiles().isEmpty());
 }
 private File createFileForClass(Class<TestFakeProduct> clazz) throws IOException {
   File destFile = InfinitestTestUtils.getFileForClass(altClassDir, clazz.getName());
   assertTrue(destFile.getParentFile().mkdirs());
   assertTrue(destFile.createNewFile());
   return destFile;
 }
 @Test
 public void resolvesShortOptionToShortOption() {
   OptionSet options = parser.parse("-f");
   assertFalse(options.has("fox"));
   assertTrue(options.has("f"));
 }
 @Test
 public void resolvesShortOptionToShortOptionEvenWithDoubleHyphen() {
   OptionSet options = parser.parse("--f");
   assertFalse(options.has("fox"));
   assertTrue(options.has("f"));
 }