private void moveContainerItems(final Node from, final Node to) throws RepositoryException { Session session = from.getSession(); for (Node fromChild : new NodeIterable(from.getNodes())) { String newName = fromChild.getName(); int counter = 0; while (to.hasNode(newName)) { newName = fromChild.getName() + ++counter; } session.move(fromChild.getPath(), to.getPath() + "/" + newName); } }
protected void addContentIfEmpty( Session session ) throws Exception { if (session.getRootNode().getNodes().getSize() == 1L) { if (addedContent) { fail("Already added content and should see some existing content"); } session.getRootNode().addNode("topLevel", "nt:unstructured"); session.save(); addedContent = true; } }
/** * Orders the node first among its siblings. */ public static void orderFirst(Node node) throws RepositoryException { Node parent = node.getParent(); NodeIterator siblings = parent.getNodes(); Node firstSibling = siblings.nextNode(); if (!firstSibling.isSame(node)) { parent.orderBefore(node.getName(), firstSibling.getName()); } }
/** * Tests the <code>first()</code> function. * <p> * For configuration description see {@link XPathDocOrderTest}. */ public void testDocOrderFirstFunction() throws Exception { String xpath = xpathRoot + "/*[first()]"; String resultPath = testRootNode.getNodes().nextNode().getPath(); docOrderTest(new Statement(xpath, qsXPATH), resultPath); }
public void setMetrics(Set<Metric> metrics) { try { NodeIterator nodes = getNode().getNodes(METRICS); while (nodes.hasNext()) { Node metricNode = (Node) nodes.next(); metricNode.remove(); } for (Metric metric : metrics) { Node metricNode = getNode().addNode(METRICS, METRIC_TYPE); JcrPropertyUtil.setProperty(metricNode, NAME, metric.getClass().getSimpleName()); JcrPropertyUtil.setProperty(metricNode, DESCRIPTION, metric.getDescription()); JcrUtil.addGenericJson(metricNode, JSON, metric); } } catch (RepositoryException e) { throw new MetadataRepositoryException("Failed to retrieve the metric nodes", e); } }
@Test public void testImportCommandDuplicateName() throws Exception { // GIVEN doReturn(true).when(importCommand).checkPermissions(RepositoryConstants.WEBSITE, targetNode.getPath(), Permission.WRITE); targetNode.addNode("about", NodeTypes.Page.NAME); targetNode.getSession().save(); // WHEN importCommand.execute(context); // THEN assertTrue(targetNode.hasNodes()); assertEquals(2, targetNode.getNodes("about").getSize()); assertTrue(targetNode.hasNode("about[2]/extras")); assertTrue(targetNode.hasNode("about[2]/extras/extras1")); }
private static void dumpChanges(Node node, PrintWriter out) throws RepositoryException { if (node.isModified()) { out.println(node.getPath() + " is modified"); } else if (node.isNew()) { out.println(node.getPath() + " is new"); } for (Iterator iter = node.getNodes(); iter.hasNext(); ) { Node child = (Node) iter.next(); dumpChanges(child, out); } }
@Test public void testOrdering() throws RepositoryException, TaskExecutionException { // GIVEN final Session session = new MockSession(RepositoryConstants.CONFIG); InstallContext ctx = mock(InstallContext.class); when(ctx.getJCRSession(RepositoryConstants.CONFIG)).thenReturn(session); Node parent = session.getRootNode(); parent.addNode("A", NodeTypes.ContentNode.NAME); parent.addNode("B", NodeTypes.ContentNode.NAME); Node childC = parent.addNode("C", NodeTypes.ContentNode.NAME); final OrderNodeToFirstPositionTask nodeto1stPosTask = new OrderNodeToFirstPositionTask("order-C-tp1st-Pos", "orders the node 'C' to 1st position", RepositoryConstants.CONFIG, "C"); // WHEN nodeto1stPosTask.doExecute(ctx); // THEN assertTrue(NodeUtil.isSame(childC, parent.getNodes().nextNode())); }
private Node getLastChild(Node parent, Collection<String> childNames) throws RepositoryException { Node lastMatch = null; NodeIterator nodes = parent.getNodes(); while (nodes.hasNext()) { Node child = nodes.nextNode(); if (childNames.contains(child.getName())) { lastMatch = child; } } return lastMatch; }
@Test public void testDoesNothingGivenProperty() throws Exception { // GIVEN Node root = session.getRootNode(); Node node = root.addNode("nodeName"); node.setProperty("propertyName", "propertyValue"); long nodeCountBefore = node.getNodes().getSize(); DuplicateNodeAction action = new DuplicateNodeAction(definition, new JcrPropertyAdapter(node.getProperty("propertyName")), eventBus); // WHEN action.execute(); // THEN assertEquals(nodeCountBefore, node.getNodes().getSize()); assertFalse(node.hasNode("nodeName0")); assertTrue(eventBus.isEmpty()); }
private void assertNodeOrder(Node node, String[] names) throws RepositoryException { NodeIterator nodes = node.getNodes(); for (String name : names) { Node childNode = nodes.nextNode(); if (!childNode.getName().equals(name)) { fail("Expected [" + name + "] was [" + childNode.getName() + "]"); } } } }
private void removeContainerItems(final Node page) throws RepositoryException { final List<Node> containers = findContainers(page); for (Node container : containers) { for (Node child : new NodeIterable(container.getNodes())) { log.debug("Remove container item '{}' for container '{}'.", child.getName(), container.getPath()); child.remove(); } } }
/** * Tests if <somepath>/% returns no nodes if node at <somepath> * is a leaf. * @throws NotExecutableException */ public void testDescendantLeaf() throws RepositoryException, NotExecutableException { // find leaf Node leaf = testRootNode; while (leaf.hasNodes()) { leaf = leaf.getNodes().nextNode(); } String sql = getStatement(leaf.getPath() + "/%"); executeSqlQuery(session, sql, new Node[0]); }
@Test public void shouldReturnEmptyIterator() throws RepositoryException { Node jcrRootNode = session.getRootNode(); Node rootNode = jcrRootNode.addNode("mapSuperclassTest"); Node newNode = rootNode.addNode("newNode"); assertNotNull(newNode); NodeIterator nodeIterator = rootNode.getNodes("myMap"); assertFalse(nodeIterator.hasNext()); session.save(); nodeIterator = rootNode.getNodes("myMap"); assertFalse(nodeIterator.hasNext()); }
protected void exportNode(final Node sourceNode, final DefinitionNodeImpl parentNode, final Set<String> excludedPaths) throws RepositoryException { final String path = sourceNode.getPath(); if (!isVirtual(sourceNode) && !shouldExcludeNode(path) && !excludedPaths.contains(path)) { final DefinitionNodeImpl definitionNode = parentNode.addNode(createNodeName(sourceNode)); exportProperties(sourceNode, definitionNode); for (final Node childNode : new NodeIterable(sourceNode.getNodes())) { exportNode(childNode, definitionNode, excludedPaths); } } }
@Test public void doesNothingGivenProperty() throws Exception { // GIVEN Node node = root.addNode(NODE_NAME); node.setProperty("propertyName", "propertyValue"); long nodeCountBefore = node.getNodes().getSize(); AddNodeAction action = new AddNodeAction(definition, new JcrPropertyAdapter(node.getProperty("propertyName")), eventBus); // WHEN action.execute(); // THEN assertEquals(nodeCountBefore, node.getNodes().getSize()); assertFalse(node.hasNode(NEW_NODE_NAME)); assertTrue(eventBus.isEmpty()); }
private void assertNodeOrder(Node node, String[] names) throws RepositoryException { NodeIterator nodes = node.getNodes(); for (String name : names) { Node childNode = nodes.nextNode(); if (!childNode.getName().equals(name)) { fail("Expected [" + name + "] was [" + childNode.getName() + "]"); } } } }