All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jackrabbit.test.api.SetPropertyStringTest Maven / Gradle / Ivy

There is a newer version: 2.23.1-beta
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.test.api;

import org.apache.jackrabbit.test.AbstractJCRTest;

import javax.jcr.Value;
import javax.jcr.Node;

import javax.jcr.ValueFormatException;
import javax.jcr.PropertyType;

import java.util.Arrays;


/**
 * SetPropertyStringTest tests the methods
 * Node.setProperty(String, String), Node.setProperty(String,
 * String[]) and Node.setProperty(String, String[], int)
 *
 */
public class SetPropertyStringTest extends AbstractJCRTest {

    private Node testNode;

    private String s1 = "abc";
    private String s2 = "xyz";

    private String[] sArray1 = new String[3];
    private String[] sArray2 = new String[3];
    private String[] sArrayNull = new String[3];

    private Value[] vArray1 = new Value[3];
    private Value[] vArray2 = new Value[3];


    protected void setUp() throws Exception {
        super.setUp();
        testNode = testRootNode.addNode(nodeName1, testNodeType);

        sArray1[0] = "a";
        sArray1[1] = "b";
        sArray1[2] = "c";
        sArray2[0] = "x";
        sArray2[1] = "y";
        sArray2[2] = "z";
        sArrayNull[0] = null;
        sArrayNull[1] = null;
        sArrayNull[2] = null;

        vArray1[0] = superuser.getValueFactory().createValue("a");
        vArray1[1] = superuser.getValueFactory().createValue("b");
        vArray1[2] = superuser.getValueFactory().createValue("c");

        vArray2[0] = superuser.getValueFactory().createValue("x");
        vArray2[1] = superuser.getValueFactory().createValue("y");
        vArray2[2] = superuser.getValueFactory().createValue("z");
    }

    protected void tearDown() throws Exception {
        testNode = null;
        for (int i = 0; i < vArray1.length; i++) {
            vArray1[i] = null;
        }
        for (int i = 0; i < vArray2.length; i++) {
            vArray2[i] = null;
        }
        super.tearDown();
    }

    // String

    /**
     * Tests if adding a property with Node.setProperty(String,
     * String) works with Session.save()
     */
    public void testNewStringPropertySession() throws Exception {
        testNode.setProperty(propertyName1, s1);
        superuser.save();
        assertEquals("Setting property with Node.setProperty(String, String) and Session.save() not working",
                s1,
                testNode.getProperty(propertyName1).getString());
    }

    /**
     * Tests if modifying a property with Node.setProperty(String,
     * String) works with Session.save()
     */
    public void testModifyStringPropertySession() throws Exception {
        testNode.setProperty(propertyName1, s1);
        superuser.save();
        testNode.setProperty(propertyName1, s2);
        superuser.save();
        assertEquals("Modifying property with Node.setProperty(String, String) and Session.save() not working",
                s2,
                testNode.getProperty(propertyName1).getString());
    }

    /**
     * Tests if adding a property with Node.setProperty(String,
     * String) works with parentNode.save()
     */
    public void testNewStringPropertyParent() throws Exception {
        testNode.setProperty(propertyName1, s1);
        testRootNode.getSession().save();
        assertEquals("Setting property with Node.setProperty(String, String) and parentNode.save() not working",
                s1,
                testNode.getProperty(propertyName1).getString());
    }

    /**
     * Tests if modifying a property with Node.setProperty(String,
     * String) works with parentNode.save()
     */
    public void testModifyStringPropertyParent() throws Exception {
        testNode.setProperty(propertyName1, s1);
        testRootNode.getSession().save();
        testNode.setProperty(propertyName1, s2);
        testRootNode.getSession().save();
        assertEquals("Modifying property with Node.setProperty(String, String) and parentNode.save() not working",
                s2,
                testNode.getProperty(propertyName1).getString());
    }

    /**
     * Tests if removing a String property with
     * Node.setProperty(String, null) works with
     * Session.save()
     */
    public void testRemoveStringPropertySession() throws Exception {
        testNode.setProperty(propertyName1, s1);
        superuser.save();
        testNode.setProperty(propertyName1, (String) null);
        superuser.save();
        assertFalse("Removing property with Node.setProperty(String, (String)null) and Session.save() not working",
                testNode.hasProperty(propertyName1));
    }

    /**
     * Tests if removing a String property with
     * Node.setProperty(String, null) works with
     * parentNode.save()
     */
    public void testRemoveStringPropertyParent() throws Exception {
        testNode.setProperty(propertyName1, s1);
        testRootNode.getSession().save();
        testNode.setProperty(propertyName1, (String) null);
        testRootNode.getSession().save();
        assertFalse("Removing property with Node.setProperty(String, (String)null) and parentNode.save() not working",
                testNode.hasProperty(propertyName1));
    }


    // String with PropertyType

    /**
     * Tests if adding a property with Node.setProperty(String,
     * String, int) works with Session.save()
     */
    public void testNewStringPropertySessionWithPropertyType() throws Exception {
        testNode.setProperty(propertyName1, s1, PropertyType.STRING);
        superuser.save();
        assertEquals("Setting property with Node.setProperty(String, String, int) and Session.save() not working",
                s1,
                testNode.getProperty(propertyName1).getString());
    }

    /**
     * Tests if modifying a property with Node.setProperty(String,
     * String, int) works with Session.save()
     */
    public void testModifyStringPropertySessionWithPropertyType() throws Exception {
        testNode.setProperty(propertyName1, s1, PropertyType.STRING);
        superuser.save();
        testNode.setProperty(propertyName1, s2, PropertyType.STRING);
        superuser.save();
        assertEquals("Modifying property with Node.setProperty(String, String, int) and Session.save() not working",
                s2,
                testNode.getProperty(propertyName1).getString());
    }

    /**
     * Tests if adding a property with Node.setProperty(String,
     * String, int) works with parentNode.save()
     */
    public void testNewStringPropertyParentWithPropertyType() throws Exception {
        testNode.setProperty(propertyName1, s1, PropertyType.STRING);
        testRootNode.getSession().save();
        assertEquals("Setting property with Node.setProperty(String, String, int) and parentNode.save() not working",
                s1,
                testNode.getProperty(propertyName1).getString());
    }

    /**
     * Tests if modifying a property with Node.setProperty(String,
     * String, int) works with parentNode.save()
     */
    public void testModifyStringPropertyParentWithPropertyType() throws Exception {
        testNode.setProperty(propertyName1, s1, PropertyType.STRING);
        testRootNode.getSession().save();
        testNode.setProperty(propertyName1, s2, PropertyType.STRING);
        testRootNode.getSession().save();
        assertEquals("Modifying property with Node.setProperty(String, String, int) and parentNode.save() not working",
                s2,
                testNode.getProperty(propertyName1).getString());
    }

    /**
     * Tests if removing a String property with
     * Node.setProperty(String, null, int) works with
     * Session.save()
     */
    public void testRemoveStringPropertySessionWithPropertyType() throws Exception {
        testNode.setProperty(propertyName1, s1, PropertyType.STRING);
        superuser.save();
        testNode.setProperty(propertyName1, (String) null, PropertyType.STRING);
        superuser.save();
        assertFalse("Removing property with Node.setProperty(String, (String)null, int) and Session.save() not working",
                testNode.hasProperty(propertyName1));
    }

    /**
     * Tests if removing a String property with
     * Node.setProperty(String, null, int) works with
     * parentNode.save()
     */
    public void testRemoveStringPropertyParentWithPropertyType() throws Exception {
        testNode.setProperty(propertyName1, s1, PropertyType.STRING);
        testRootNode.getSession().save();
        testNode.setProperty(propertyName1, (String) null, PropertyType.STRING);
        testRootNode.getSession().save();
        assertFalse("Removing property with Node.setProperty(String, (String)null, int) and parentNode.save() not working",
                testNode.hasProperty(propertyName1));
    }


    // String[]

    /**
     * Tests if adding properties with Node.setProperty(String,
     * String[]) works with Session.save()
     */
    public void testNewStringArrayPropertySession() throws Exception {
        testNode.setProperty(propertyName2, sArray1);
        superuser.save();
        assertEquals("Setting properties with Node.setProperty(String, String[]) and Session.save() not working",
                Arrays.asList(vArray1),
                Arrays.asList(testNode.getProperty(propertyName2).getValues()));
    }

    /**
     * Tests if modifying properties with Node.setProperty(String,
     * String[]) works with Session.save()
     */
    public void testModifyStringArrayPropertySession() throws Exception {
        testNode.setProperty(propertyName2, sArray1);
        superuser.save();
        testNode.setProperty(propertyName2, sArray2);
        superuser.save();
        assertEquals("Modifying properties with Node.setProperty(String, String[]) and Session.save() not working",
                Arrays.asList(vArray2),
                Arrays.asList(testNode.getProperty(propertyName2).getValues()));
    }

    /**
     * Tests if adding properties with Node.setProperty(String,
     * String[]) works with parentNode.save()
     */
    public void testNewStringArrayPropertyParent() throws Exception {
        testNode.setProperty(propertyName2, sArray1);
        testRootNode.getSession().save();
        assertEquals("Setting properties with Node.setProperty(String, String[]) and parentNode.save() not working",
                Arrays.asList(vArray1),
                Arrays.asList(testNode.getProperty(propertyName2).getValues()));
    }

    /**
     * Tests if modifying properties with Node.setProperty(String,
     * String[]) works with parentNode.save()
     */
    public void testModifyStringArrayPropertyParent() throws Exception {
        testNode.setProperty(propertyName2, sArray1);
        testRootNode.getSession().save();
        testNode.setProperty(propertyName2, sArray2);
        testRootNode.getSession().save();
        assertEquals("Modifying properties with Node.setProperty(String, String[]) and parentNode.save() not working",
                Arrays.asList(vArray2),
                Arrays.asList(testNode.getProperty(propertyName2).getValues()));
    }

    /**
     * Tests if removing a String[] property with
     * Node.setProperty(String, null) works with
     * Session.save()
     */
    public void testRemoveStringArrayPropertySession() throws Exception {
        testNode.setProperty(propertyName2, sArray1);
        superuser.save();
        testNode.setProperty(propertyName2, (String[]) null);
        superuser.save();
        assertFalse("Removing property with Node.setProperty(String, (String[])null) and Session.save() not working",
                testNode.hasProperty(propertyName2));
    }

    /**
     * Tests if removing a String[] property with
     * Node.setProperty(String, null) works with
     * parentNode.save()
     */
    public void testRemoveStringArrayPropertyParent() throws Exception {
        testNode.setProperty(propertyName2, sArray1);
        testRootNode.getSession().save();
        testNode.setProperty(propertyName2, (String[]) null);
        testRootNode.getSession().save();
        assertFalse("Removing property with Node.setProperty(String, (String[])null) and parentNode.save() not working",
                testNode.hasProperty(propertyName2));
    }

    /**
     * Tests if Node.setProperty(String, String[]) saves an array
     * of null values as an empty String[]
     */
    public void testSetNullStringArray() throws Exception {
        testNode.setProperty(propertyName2, sArrayNull);
        superuser.save();
        assertEquals("Node.setProperty(String, nullStringArray[]) did not set the property to an empty String[]",
                Arrays.asList(new Value[0]),
                Arrays.asList(testNode.getProperty(propertyName2).getValues()));
    }


    // String[] with PropertyType

    /**
     * Tests if adding properties with Node.setProperty(String, String[],
     * int) works with Session.save()
     */
    public void testNewStringArrayPropertySessionWithPropertyType() throws Exception {
        testNode.setProperty(propertyName2, sArray1, PropertyType.STRING);
        superuser.save();
        assertEquals("Setting properties with Node.setProperty(String, String[], int) and Session.save() not working",
                Arrays.asList(vArray1),
                Arrays.asList(testNode.getProperty(propertyName2).getValues()));
    }

    /**
     * Tests if modifying properties with Node.setProperty(String,
     * String[], int) works with Session.save()
     */
    public void testModifyStringArrayPropertySessionWithPropertyType() throws Exception {
        testNode.setProperty(propertyName2, sArray1, PropertyType.STRING);
        superuser.save();
        testNode.setProperty(propertyName2, sArray2, PropertyType.STRING);
        superuser.save();
        assertEquals("Modifying properties with Node.setProperty(String, String[], int) and Session.save() not working",
                Arrays.asList(vArray2),
                Arrays.asList(testNode.getProperty(propertyName2).getValues()));
    }

    /**
     * Tests if adding properties with Node.setProperty(String, String[],
     * int) works with parentNode.save()
     */
    public void testNewStringArrayPropertyParentWithPropertyType() throws Exception {
        testNode.setProperty(propertyName2, sArray1, PropertyType.STRING);
        testRootNode.getSession().save();
        assertEquals("Setting properties with Node.setProperty(String, String[], int) and parentNode.save() not working",
                Arrays.asList(vArray1),
                Arrays.asList(testNode.getProperty(propertyName2).getValues()));
    }

    /**
     * Tests if modifying properties with Node.setProperty(String,
     * String[], int) works with parentNode.save()
     */
    public void testModifyStringArrayPropertyParentWithPropertyType() throws Exception {
        testNode.setProperty(propertyName2, sArray1, PropertyType.STRING);
        testRootNode.getSession().save();
        testNode.setProperty(propertyName2, sArray2, PropertyType.STRING);
        testRootNode.getSession().save();
        assertEquals("Modifying properties with Node.setProperty(String, String[], int) and parentNode.save() not working",
                Arrays.asList(vArray2),
                Arrays.asList(testNode.getProperty(propertyName2).getValues()));
    }

    /**
     * Tests if removing a String[] property with
     * Node.setProperty(String, null, int) works with
     * Session.save()
     */
    public void testRemoveStringArrayPropertySessionWithPropertyType() throws Exception {
        testNode.setProperty(propertyName2, sArray1, PropertyType.STRING);
        superuser.save();
        testNode.setProperty(propertyName2, (String[]) null, PropertyType.STRING);
        superuser.save();
        assertFalse("Removing property with Node.setProperty(String, (String[])null, int) and Session.save() not working",
                testNode.hasProperty(propertyName2));
    }

    /**
     * Tests if removing a String[] property with
     * Node.setProperty(String, null, int) works with
     * parentNode.save()
     */
    public void testRemoveStringArrayPropertyParentWithPropertyType() throws Exception {
        testNode.setProperty(propertyName2, sArray1, PropertyType.STRING);
        testRootNode.getSession().save();
        testNode.setProperty(propertyName2, (String[]) null, PropertyType.STRING);
        testRootNode.getSession().save();
        assertFalse("Removing property with Node.setProperty(String, (String[])null, int) and parentNode.save() not working",
                testNode.hasProperty(propertyName2));
    }

    /**
     * Tests if Node.setProperty(String, String[], int) saves an
     * array of null values as an empty String[]
     */
    public void testSetNullStringArrayWithPropertyType() throws Exception {
        testNode.setProperty(propertyName2, sArrayNull, PropertyType.STRING);
        superuser.save();
        assertEquals("Node.setProperty(String, nullStringArray[], int) did not set the property to an empty Value[]",
                Arrays.asList(new Value[0]),
                Arrays.asList(testNode.getProperty(propertyName2).getValues()));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy