Beispiel #1
0
  @Test
  public void testDeleteArtifact() throws Exception {
    String artifactFileName = "s-ramp-press-release.pdf";
    InputStream pdf = this.getClass().getResourceAsStream("/sample-files/core/" + artifactFileName);
    Document document = new Document();
    document.setName(artifactFileName);
    document.setArtifactType(BaseArtifactEnum.DOCUMENT);

    // Add an artifact
    BaseArtifactType artifact = persistenceManager.persistArtifact(document, pdf);
    Assert.assertNotNull(artifact);
    Assert.assertEquals(Document.class, artifact.getClass());
    Assert.assertEquals(new Long(18873l), ((DocumentArtifactType) artifact).getContentSize());
    log.info(
        "persisted s-ramp-press-release.pdf to JCR, returned artifact uuid=" + artifact.getUuid());

    // Now delete that artifact
    ArtifactType at = ArtifactType.valueOf(artifact);
    persistenceManager.deleteArtifact(document.getUuid(), at);

    // Now make sure we can't load it back up
    BaseArtifactType deleted = persistenceManager.getArtifact(document.getUuid(), at);
    Assert.assertNull(deleted);

    SrampQuery query = queryManager.createQuery("/s-ramp[@uuid = ?]");
    query.setString(document.getUuid());
    ArtifactSet artifactSet = query.executeQuery();
    Assert.assertEquals(0, artifactSet.size());
  }
  private ArtifactSet query(List<String> criteria, List<Object> parameters)
      throws SrampAtomException {
    ArtifactSet artifactSet = null;
    /* Query */

    StringBuilder queryBuilder = new StringBuilder();
    // Initial query

    queryBuilder.append("/s-ramp"); // $NON-NLS-1$

    // Now create the query predicate from the generated criteria
    if (criteria.size() > 0) {
      queryBuilder.append("["); // $NON-NLS-1$
      queryBuilder.append(StringUtils.join(criteria, " and ")); // $NON-NLS-1$
      queryBuilder.append("]"); // $NON-NLS-1$
    }

    try {
      QueryManager queryManager = QueryManagerFactory.newInstance();
      SrampQuery srampQuery =
          queryManager.createQuery(
              queryBuilder.toString(), "lastModifiedTimestamp", false); // $NON-NLS-1$

      for (Object parameter : parameters) {
        if (parameter instanceof String) {
          srampQuery.setString((String) parameter);
        } else if (parameter instanceof Date) {
          srampQuery.setDate((Date) parameter);
        }
      }
      artifactSet = srampQuery.executeQuery();
    } catch (Throwable e) {
      logger.error(
          Messages.i18n.format("maven.resource.query.error", queryBuilder.toString()),
          e); //$NON-NLS-1$
      throw new SrampAtomException(e);
    }
    return artifactSet;
  }
  @Test
  public void testDerivedRelationshipQueries() throws Exception {
    createOntology();

    addDocument("no-classifications"); // $NON-NLS-1$
    addDocument("one-classification: china", "China"); // $NON-NLS-1$ //$NON-NLS-2$
    addDocument("one-classification: japan", "Japan"); // $NON-NLS-1$ //$NON-NLS-2$
    addDocument("one-classification: germany", "Germany"); // $NON-NLS-1$ //$NON-NLS-2$

    // Verify that both docs are available
    SrampQuery query = queryManager.createQuery("/s-ramp/core/Document"); // $NON-NLS-1$
    ArtifactSet artifactSet = query.executeQuery();
    Assert.assertNotNull(artifactSet);
    Assert.assertEquals(4, artifactSet.size());

    // Make sure there's only one with the given name
    query = queryManager.createQuery("/s-ramp/core/Document[@name = ?]"); // $NON-NLS-1$
    query.setString("no-classifications"); // $NON-NLS-1$
    artifactSet = query.executeQuery();
    Assert.assertNotNull(artifactSet);
    Assert.assertEquals(1, artifactSet.size());

    // Should get just the one classified by China
    query =
        queryManager.createQuery(
            "/s-ramp/core/Document[s-ramp:exactlyClassifiedByAllOf(., 'China')]"); //$NON-NLS-1$
    artifactSet = query.executeQuery();
    Assert.assertNotNull(artifactSet);
    Assert.assertEquals(1, artifactSet.size());

    // Should get zero artifacts
    query =
        queryManager.createQuery(
            "/s-ramp/core/Document[s-ramp:exactlyClassifiedByAllOf(., 'Asia')]"); //$NON-NLS-1$
    artifactSet = query.executeQuery();
    Assert.assertNotNull(artifactSet);
    Assert.assertEquals(0, artifactSet.size());

    // Should get just the one classified by Germany
    query =
        queryManager.createQuery(
            "/s-ramp/core/Document[s-ramp:exactlyClassifiedByAllOf(., 'Germany')]"); //$NON-NLS-1$
    artifactSet = query.executeQuery();
    Assert.assertNotNull(artifactSet);
    Assert.assertEquals(1, artifactSet.size());

    // Should get zero artifacts
    query =
        queryManager.createQuery(
            "/s-ramp/core/Document[s-ramp:exactlyClassifiedByAllOf(., 'China', 'Germany')]"); //$NON-NLS-1$
    artifactSet = query.executeQuery();
    Assert.assertNotNull(artifactSet);
    Assert.assertEquals(0, artifactSet.size());

    // Should get all classified artifacts
    query =
        queryManager.createQuery(
            "/s-ramp/core/Document[s-ramp:classifiedByAllOf(., 'World')]"); //$NON-NLS-1$
    artifactSet = query.executeQuery();
    Assert.assertNotNull(artifactSet);
    Assert.assertEquals(3, artifactSet.size());

    // Should get two artifacts - japan and china
    query =
        queryManager.createQuery(
            "/s-ramp/core/Document[s-ramp:classifiedByAllOf(., 'Asia')]"); //$NON-NLS-1$
    artifactSet = query.executeQuery();
    Assert.assertNotNull(artifactSet);
    Assert.assertEquals(2, artifactSet.size());

    // Should get two artifacts - japan and china
    query =
        queryManager.createQuery(
            "/s-ramp/core/Document[s-ramp:classifiedByAnyOf(., 'Japan', 'China')]"); //$NON-NLS-1$
    artifactSet = query.executeQuery();
    Assert.assertNotNull(artifactSet);
    Assert.assertEquals(2, artifactSet.size());
  }