@Test
  public void testRead() throws DataStoreException, NoSuchAuthorityCodeException, FactoryException {

    final ExtendedDataStore store = new ExtendedDataStore(dataStore);
    final Name name = DefaultName.valueOf("{http://www.geotoolkit.org/test}extsql");
    assertFalse(store.getNames().contains(name));

    // add a new query
    final Query query =
        QueryBuilder.language(
            JDBCDataStore.CUSTOM_SQL,
            "SELECT geometry as geo ,\"intProperty\" as it FROM custom",
            name);
    store.addQuery(query);
    assertTrue(store.getNames().contains(name));

    final FeatureType ft = store.getFeatureType(name);

    final FeatureCollection col =
        store.createSession(true).getFeatureCollection(QueryBuilder.all(name));
    assertEquals(ft, col.getFeatureType());
    assertEquals(3, col.size());

    final FeatureIterator ite = col.iterator();
    try {
      while (ite.hasNext()) {
        final Feature f = ite.next();
        assertEquals(f.getType(), ft);
      }
    } finally {
      ite.close();
    }
  }
    /** Find the next feature */
    private void findNext() {
      if (nextFeature != null) {
        return;
      }

      while (nextFeature == null) {

        if (nextFC != null) {
          if (ite.hasNext()) {
            nextFeature = ite.next();
            continue;
          } else {
            nextFC = null;
            ite = null;
          }
        } else {
          if (firstPass) { // first pass iterate on the original FeatureCollection
            if (originalFI.hasNext()) {
              nextFC = modify2(originalFI.next(), firstPass, featureList);
              ite = nextFC.iterator();
            } else {
              firstPass = false;
            }
          } else {
            if (unionFI.hasNext()) { // second pass iterate on the union FeatureCollection
              nextFC = modify2(unionFI.next(), firstPass, featureList);
              ite = nextFC.iterator();
            } else {
              break;
            }
          }
        }
      }
    }
 public void testReadFeatures() throws Exception {
   FeatureCollection fc =
       dataStore.createSession(false).getFeatureCollection(QueryBuilder.all(nsname(PERSON)));
   assertEquals(2, fc.size());
   FeatureIterator<SimpleFeature> fr = fc.iterator();
   assertTrue(fr.hasNext());
   SimpleFeature f = fr.next();
   assertTrue(fr.hasNext());
   f = fr.next();
   assertFalse(fr.hasNext());
   fr.close();
 }
 /** Close the original FeatureIterator */
 @Override
 public void close() {
   originalFI.close();
 }
  /**
   * @param featureCollection
   * @param writer
   * @param fragment : true if we write in a stream, dont write start and end elements
   * @throws DataStoreException
   */
  public Element writeFeatureCollection(
      final FeatureCollection featureCollection, final boolean fragment, final boolean wfs)
      throws DataStoreException, ParserConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // then we have to create document-loader:
    factory.setNamespaceAware(false);
    DocumentBuilder loader = factory.newDocumentBuilder();

    // creating a new DOM-document...
    Document document = loader.newDocument();

    // the XML header
    if (!fragment) {
      document.setXmlVersion("1.0");
      // writer.writeStartDocument("UTF-8", "1.0");
    }

    // the root Element
    final Element rootElement;
    if (wfs) {
      rootElement = document.createElementNS("http://www.opengis.net/wfs", "FeatureCollection");
      rootElement.setPrefix("wfs");
    } else {
      rootElement = document.createElementNS("http://www.opengis.net/gml", "FeatureCollection");
      rootElement.setPrefix("gml");
    }

    document.appendChild(rootElement);

    String collectionID = "";
    if (featureCollection.getID() != null) {
      collectionID = featureCollection.getID();
    }
    final Attr idAttribute = document.createAttributeNS(Namespaces.GML, "id");
    idAttribute.setValue(collectionID);
    idAttribute.setPrefix("gml");
    rootElement.setAttributeNodeNS(idAttribute);

    if (schemaLocation != null && !schemaLocation.equals("")) {
      rootElement.setAttributeNS(
          "http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", schemaLocation);
    }

    /*FeatureType type = featureCollection.getFeatureType();
    if (type != null && type.getName() != null) {
        String namespace = type.getName().getNamespaceURI();
        if (namespace != null && !namespace.equals(Namespaces.GML)) {
            Prefix prefix    = getPrefix(namespace);
            writer.writeNamespace(prefix.prefix, namespace);
        }
    }*/
    /*
     * The boundedby part
     */
    final Element boundElement = writeBounds(featureCollection.getEnvelope(), document);
    if (boundElement != null) {
      rootElement.appendChild(boundElement);
    }

    // we write each feature member of the collection
    FeatureIterator iterator = featureCollection.iterator();
    try {
      while (iterator.hasNext()) {
        final Feature f = iterator.next();
        final Element memberElement = document.createElementNS(Namespaces.GML, "featureMember");
        memberElement.setPrefix("gml");
        memberElement.appendChild(writeFeature(f, document, true));
        rootElement.appendChild(memberElement);
      }

    } finally {
      // we close the stream
      iterator.close();
    }
    return rootElement;
  }