Search in sources :

Example 16 with VersionIterator

use of javax.jcr.version.VersionIterator in project jackrabbit-oak by apache.

the class CopyTest method testCopyVersionableNodeClearsVersions.

@Test
public void testCopyVersionableNodeClearsVersions() throws Exception {
    Session session = getAdminSession();
    Node toCopy = session.getNode(TEST_PATH + "/source/node");
    toCopy.addMixin(JcrConstants.MIX_VERSIONABLE);
    session.save();
    Version v1 = toCopy.checkin();
    toCopy.checkout();
    Version v2 = toCopy.checkin();
    toCopy.checkout();
    session.getWorkspace().copy(TEST_PATH + "/source/node", TEST_PATH + "/target/copied");
    Node copy = testNode.getNode("target/copied");
    VersionHistory vh = copy.getVersionHistory();
    Version rootV = vh.getRootVersion();
    assertEquals(0, rootV.getSuccessors().length);
    VersionIterator vItr = vh.getAllVersions();
    while (vItr.hasNext()) {
        if (!rootV.isSame(vItr.nextVersion())) {
            fail("Unexpected version in version history of copied node.");
        }
    }
    try {
        vh.getVersion(v1.getName());
        fail("Unexpected version in version history of copied node.");
    } catch (VersionException e) {
    // success
    }
    try {
        vh.getVersion(v2.getName());
        fail("Unexpected version in version history of copied node.");
    } catch (VersionException e) {
    // success
    }
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionIterator(javax.jcr.version.VersionIterator) VersionHistory(javax.jcr.version.VersionHistory) Session(javax.jcr.Session) VersionException(javax.jcr.version.VersionException) Test(org.junit.Test)

Example 17 with VersionIterator

use of javax.jcr.version.VersionIterator in project jackrabbit-oak by apache.

the class VersionCopyTestUtils method assertLabeledVersions.

public static void assertLabeledVersions(VersionHistory history) throws RepositoryException {
    final VersionIterator versions = history.getAllVersions();
    // root
    assertFalse(versions.nextVersion().getFrozenNode().hasProperty("version"));
    for (final String label : LABELS) {
        assertEquals(label, versions.nextVersion().getFrozenNode().getProperty("version").getString());
    }
}
Also used : VersionIterator(javax.jcr.version.VersionIterator)

Example 18 with VersionIterator

use of javax.jcr.version.VersionIterator in project jackrabbit by apache.

the class VersionHistoryResourceImpl method initProperties.

//--------------------------------------------------------------------------
/**
     * Fill the property set for this resource.
     */
@Override
protected void initProperties() {
    if (!propsInitialized) {
        super.initProperties();
        // change resource type defined by default item collection
        properties.add(new ResourceType(new int[] { ResourceType.COLLECTION, ResourceType.VERSION_HISTORY }));
        // required root-version property for version-history resource
        try {
            String rootVersionHref = getLocatorFromNode(((VersionHistory) getNode()).getRootVersion()).getHref(false);
            properties.add(new HrefProperty(VersionHistoryResource.ROOT_VERSION, rootVersionHref, false));
        } catch (RepositoryException e) {
            log.error(e.getMessage());
        }
        // required, protected version-set property for version-history resource
        try {
            VersionIterator vIter = ((VersionHistory) getNode()).getAllVersions();
            ArrayList<Version> l = new ArrayList<Version>();
            while (vIter.hasNext()) {
                l.add(vIter.nextVersion());
            }
            properties.add(getHrefProperty(VersionHistoryResource.VERSION_SET, l.toArray(new Version[l.size()]), true, false));
        } catch (RepositoryException e) {
            log.error(e.getMessage());
        }
    }
}
Also used : HrefProperty(org.apache.jackrabbit.webdav.property.HrefProperty) Version(javax.jcr.version.Version) ArrayList(java.util.ArrayList) VersionIterator(javax.jcr.version.VersionIterator) ResourceType(org.apache.jackrabbit.webdav.property.ResourceType) RepositoryException(javax.jcr.RepositoryException) VersionHistory(javax.jcr.version.VersionHistory)

Example 19 with VersionIterator

use of javax.jcr.version.VersionIterator in project jackrabbit by apache.

the class RemoveVersionTest method testRemoveAllVersions.

/**
     * Creates 3 versions and removes them afterwards. Checks if version history
     * was purged, too.
     *
     * Tests error reported in JCR-2601
     *
     * @throws Exception if an error occurs
     */
public void testRemoveAllVersions() throws Exception {
    Node n = testRootNode.addNode(nodeName1);
    n.addMixin(mixVersionable);
    superuser.save();
    String path = n.getPath();
    // create some versions
    VersionManager mgr = superuser.getWorkspace().getVersionManager();
    // v1.0
    mgr.checkpoint(path);
    // v1.1
    mgr.checkpoint(path);
    // v1.2
    mgr.checkpoint(path);
    // get version history
    VersionHistory vh = mgr.getVersionHistory(path);
    String id = vh.getIdentifier();
    // remove versionable node
    n.remove();
    superuser.save();
    // get the names of the versions
    List<String> names = new LinkedList<String>();
    VersionIterator vit = vh.getAllVersions();
    while (vit.hasNext()) {
        Version v = vit.nextVersion();
        if (!v.getName().equals("jcr:rootVersion")) {
            names.add(v.getName());
        }
    }
    assertEquals("Number of versions", 3, names.size());
    // remove all versions
    for (String name : names) {
        vh.removeVersion(name);
    }
    // assert that version history is gone
    try {
        superuser.getNodeByIdentifier(id);
        fail("Version history not removed after last version was removed.");
    } catch (RepositoryException e) {
    // ok
    }
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionIterator(javax.jcr.version.VersionIterator) RepositoryException(javax.jcr.RepositoryException) VersionManager(javax.jcr.version.VersionManager) VersionHistory(javax.jcr.version.VersionHistory) LinkedList(java.util.LinkedList)

Example 20 with VersionIterator

use of javax.jcr.version.VersionIterator in project jackrabbit by apache.

the class RestoreTest method testRestoreInvalidVersion2.

/**
     * VersionException expected on Node.restore(String, boolean) if the specified version is not part of this node's version history.
     *
     * @throws RepositoryException
     */
@SuppressWarnings("deprecation")
public void testRestoreInvalidVersion2() throws RepositoryException {
    String invalidName;
    do {
        invalidName = createRandomString(3);
        for (VersionIterator it = versionableNode.getVersionHistory().getAllVersions(); it.hasNext(); ) {
            Version v = it.nextVersion();
            if (invalidName.equals(v.getName())) {
                invalidName = null;
                break;
            }
        }
    } while (invalidName == null);
    try {
        versionableNode.restore(invalidName, true);
        fail("VersionException expected on Node.restore(String, boolean) if the specified version is not part of this node's version history.");
    } catch (VersionException e) {
    // ok
    }
}
Also used : Version(javax.jcr.version.Version) VersionIterator(javax.jcr.version.VersionIterator) VersionException(javax.jcr.version.VersionException)

Aggregations

VersionIterator (javax.jcr.version.VersionIterator)29 Version (javax.jcr.version.Version)16 VersionHistory (javax.jcr.version.VersionHistory)12 Node (javax.jcr.Node)9 ArrayList (java.util.ArrayList)8 RepositoryException (javax.jcr.RepositoryException)6 VersionException (javax.jcr.version.VersionException)5 VersionManager (javax.jcr.version.VersionManager)4 Test (org.junit.Test)4 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)3 JcrDavException (org.apache.jackrabbit.webdav.jcr.JcrDavException)3 HashMap (java.util.HashMap)2 Session (javax.jcr.Session)2 DavException (org.apache.jackrabbit.webdav.DavException)2 DavResource (org.apache.jackrabbit.webdav.DavResource)2 HrefProperty (org.apache.jackrabbit.webdav.property.HrefProperty)2 VersionResource (org.apache.jackrabbit.webdav.version.VersionResource)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1