Search in sources :

Example 1 with ContentImportListener

use of org.apache.sling.jcr.contentloader.ContentImportListener in project sling by apache.

the class DefaultContentCreatorTest method createReferenceProperty.

@Test
public void createReferenceProperty() throws RepositoryException, NoSuchFieldException {
    final String propName = uniqueId();
    final String[] propValues = { uniqueId(), uniqueId() };
    final ContentImportListener listener = mockery.mock(ContentImportListener.class);
    final Map<String, String[]> delayedMultipleReferences = (Map<String, String[]>) PrivateAccessor.getField(contentCreator, "delayedMultipleReferences");
    this.mockery.checking(new Expectations() {

        {
            oneOf(listener).onCreate(with(any(String.class)));
        }
    });
    contentCreator.init(ImportOptionsFactory.createImportOptions(false, false, false, false, false), new HashMap<String, ContentReader>(), null, listener);
    contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
    contentCreator.createProperty(propName, PropertyType.REFERENCE, propValues);
    assertTrue(parentNode.hasProperty(propName));
    assertTrue(parentNode.getProperty(propName).isNew());
    String referencesKey = parentNode.getPath() + "/" + propName;
    String[] uuidsOrPaths = delayedMultipleReferences.get(referencesKey);
    assertNotNull(uuidsOrPaths);
    assertEquals(propValues.length, uuidsOrPaths.length);
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) Test(org.junit.Test)

Example 2 with ContentImportListener

use of org.apache.sling.jcr.contentloader.ContentImportListener in project sling by apache.

the class DefaultContentCreatorTest method testCreateReferenceProperty.

@Test
public void testCreateReferenceProperty() throws RepositoryException {
    final String propertyName = "foo";
    final String propertyValue = "bar";
    final String rootNodeName = uniqueId();
    final String uuid = "1b8c88d37f0000020084433d3af4941f";
    final Session session = mockery.mock(Session.class);
    final ContentImportListener listener = mockery.mock(ContentImportListener.class);
    parentNode = mockery.mock(Node.class);
    prop = mockery.mock(Property.class);
    this.mockery.checking(new Expectations() {

        {
            oneOf(session).itemExists(with(any(String.class)));
            will(returnValue(true));
            oneOf(session).getItem(with(any(String.class)));
            will(returnValue(parentNode));
            exactly(2).of(parentNode).getPath();
            will(returnValue("/" + rootNodeName));
            oneOf(parentNode).isNode();
            will(returnValue(true));
            oneOf(parentNode).isNodeType("mix:referenceable");
            will(returnValue(true));
            oneOf(parentNode).getUUID();
            will(returnValue(uuid));
            oneOf(parentNode).getSession();
            will(returnValue(session));
            oneOf(parentNode).hasProperty(with(any(String.class)));
            oneOf(parentNode).setProperty(propertyName, uuid, PropertyType.REFERENCE);
            oneOf(parentNode).getProperty(with(any(String.class)));
            oneOf(listener).onCreate(with(any(String.class)));
        }
    });
    contentCreator.init(ImportOptionsFactory.createImportOptions(false, false, false, false, false), new HashMap<String, ContentReader>(), null, listener);
    contentCreator.prepareParsing(parentNode, null);
    contentCreator.createProperty(propertyName, PropertyType.REFERENCE, propertyValue);
    //The only way I found how to test this method is to check numbers of methods calls
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) Test(org.junit.Test)

Example 3 with ContentImportListener

use of org.apache.sling.jcr.contentloader.ContentImportListener in project sling by apache.

the class DefaultContentCreatorTest method createNodeWithOverwrite.

@Test
public void createNodeWithOverwrite() throws RepositoryException {
    final String newNodeName = uniqueId();
    final String propertyName = uniqueId();
    final String propertyValue = uniqueId();
    final ContentImportListener listener = mockery.mock(ContentImportListener.class);
    this.mockery.checking(new Expectations() {

        {
            oneOf(listener).onCreate(with(any(String.class)));
        }
    });
    contentCreator.init(ImportOptionsFactory.createImportOptions(true, false, true, false, false), new HashMap<String, ContentReader>(), null, listener);
    contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
    Node nodeToOverwrite = parentNode.addNode(newNodeName);
    nodeToOverwrite.setProperty(propertyName, propertyValue);
    assertTrue(parentNode.getNode(newNodeName).hasProperty(propertyName));
    contentCreator.createNode(newNodeName, null, null);
    //If node was overwritten(as we expect) it will not contain this property
    assertFalse(parentNode.getNode(newNodeName).hasProperty(propertyName));
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) Test(org.junit.Test)

Example 4 with ContentImportListener

use of org.apache.sling.jcr.contentloader.ContentImportListener in project sling by apache.

the class DefaultContentCreatorTest method finishNodeWithMultipleProperty.

//------DefaultContentCreator#finishNode()------//
@Test
public void finishNodeWithMultipleProperty() throws RepositoryException, NoSuchFieldException {
    final String propName = uniqueId();
    final String underTestNodeName = uniqueId();
    final Map<String, List<String>> delayedMultipleReferences = (Map<String, List<String>>) PrivateAccessor.getField(contentCreator, "delayedMultipleReferences");
    final ContentImportListener listener = mockery.mock(ContentImportListener.class);
    this.mockery.checking(new Expectations() {

        {
            exactly(3).of(listener).onCreate(with(any(String.class)));
        }
    });
    contentCreator.init(ImportOptionsFactory.createImportOptions(false, false, false, false, false), new HashMap<String, ContentReader>(), null, listener);
    contentCreator.prepareParsing(parentNode, null);
    contentCreator.createProperty(propName, PropertyType.REFERENCE, new String[] { underTestNodeName });
    contentCreator.createNode(underTestNodeName, null, null);
    assertEquals(1, delayedMultipleReferences.size());
    Node underTest = parentNode.getNode(underTestNodeName);
    underTest.addMixin("mix:referenceable");
    contentCreator.finishNode();
    assertEquals(0, delayedMultipleReferences.size());
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) Test(org.junit.Test)

Example 5 with ContentImportListener

use of org.apache.sling.jcr.contentloader.ContentImportListener in project sling by apache.

the class DefaultContentCreatorTest method createUndefinedProperty.

@Test
public void createUndefinedProperty() throws RepositoryException {
    final String propName = uniqueId();
    final ContentImportListener listener = mockery.mock(ContentImportListener.class);
    this.mockery.checking(new Expectations() {

        {
            oneOf(listener).onCreate(with(any(String.class)));
        }
    });
    contentCreator.init(ImportOptionsFactory.createImportOptions(false, false, false, false, false), new HashMap<String, ContentReader>(), null, listener);
    contentCreator.prepareParsing(parentNode, null);
    assertFalse(parentNode.hasProperty(propName));
    contentCreator.createProperty(propName, PropertyType.UNDEFINED, new String[] {});
    assertTrue(parentNode.hasProperty(propName));
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) Test(org.junit.Test)

Aggregations

ContentImportListener (org.apache.sling.jcr.contentloader.ContentImportListener)11 ContentReader (org.apache.sling.jcr.contentloader.ContentReader)10 Expectations (org.jmock.Expectations)10 Test (org.junit.Test)10 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 RequestParameter (org.apache.sling.api.request.RequestParameter)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 ContentImporter (org.apache.sling.jcr.contentloader.ContentImporter)1 ImportOptions (org.apache.sling.jcr.contentloader.ImportOptions)1 Modification (org.apache.sling.servlets.post.Modification)1 VersioningConfiguration (org.apache.sling.servlets.post.VersioningConfiguration)1 RequestProperty (org.apache.sling.servlets.post.impl.helper.RequestProperty)1