Exemple #1
0
  /**
   * This function gets all EMF objects in the specified collection from MongoDB. This function
   * assumes that MongoDB is running on localhost.
   *
   * @param <T> The type of object to return.
   * @param resourceSet The resource set to use when loading the objects.
   * @param db The name of the MongoDB database containing the objects.
   * @param collection The name of the MongoDB collection containing the objects.
   * @return The EMF objects in the collection.
   */
  @SuppressWarnings("unchecked")
  public static <T extends EObject> Collection<T> getObjects(
      ResourceSet resourceSet, String db, String collection) {
    Resource resource =
        resourceSet.getResource(
            URI.createURI("mongodb://localhost/" + db + "/" + collection + "/?"), true);
    assertThat(resource, is(notNullValue()));

    ECollection eCollection = (ECollection) resource.getContents().get(0);
    ArrayList<T> objects = new ArrayList<T>();

    for (EObject object : eCollection.getValues()) objects.add((T) object);

    return objects;
  }
Exemple #2
0
  /**
   * This function gets the specified EMF object from MongoDB. The object is located by querying all
   * of the objects in the collection. If more than one object is found in the collection, the unit
   * test will be failed. This function is useful when you don't have full visibility to a process
   * that stores a single object to MongoDB. This function assumes that MongoDB is running on
   * localhost.
   *
   * @param <T> The type of object to return.
   * @param resourceSet The resource set to use when loading the object.
   * @param db The name of the MongoDB database containing the object.
   * @param collection The name of the MongoDB collection containing the object.
   * @return The EMF object or null if it was not found.
   */
  @SuppressWarnings("unchecked")
  public static <T extends EObject> T getObject(
      ResourceSet resourceSet, String db, String collection) {
    Resource resource =
        resourceSet.getResource(
            URI.createURI("mongodb://localhost/" + db + "/" + collection + "/?"), true);
    assertThat(resource, is(notNullValue()));

    ECollection eCollection = (ECollection) resource.getContents().get(0);

    if (eCollection.getValues().isEmpty()) return null;

    assertThat(eCollection.getValues().size(), is(1));
    return (T) eCollection.getValues().get(0);
  }