<commit_before>/*
 * 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.query.qom;

import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.Node;
import javax.jcr.PropertyType;
import javax.jcr.query.Query;
import javax.jcr.query.qom.QueryObjectModelConstants;

import java.util.Calendar;

/**
 * <code>NodeLocalNameTest</code> checks if conversion of literals is correctly
 * performed and operators work as specified.
 * TODO: assumes https://jsr-283.dev.java.net/issues/show_bug.cgi?id=483 gets resolved as initially proposed
 */
public class NodeLocalNameTest extends AbstractQOMTest {

    private Node node1;

    private String nodeLocalName;

    protected void setUp() throws Exception {
        super.setUp();
        node1 = testRootNode.addNode(nodeName1, testNodeType);
        testRootNode.save();
        int colon = nodeName1.indexOf(':');
        if (colon != -1) {
            nodeLocalName = nodeName1.substring(colon + 1);
        } else {
            nodeLocalName = nodeName1;
        }
    }

    protected void tearDown() throws Exception {
        node1 = null;
        super.tearDown();
    }

    public void testStringLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(nodeLocalName);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{node1});
    }

    public void testStringLiteralInvalidName() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue("[" + nodeLocalName);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testBinaryLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(
                nodeLocalName, PropertyType.BINARY);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{node1});
    }

    public void testDateLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(Calendar.getInstance());
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testDoubleLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(Math.PI);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testDecimalLiteral() throws RepositoryException {
        // TODO must not match node
    }

    public void testLongLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(283);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testBooleanLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(true);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testNameLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(
                nodeLocalName, PropertyType.NAME);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{node1});
    }

    public void testPathLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(
                nodeLocalName, PropertyType.PATH);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{node1});

        literal = superuser.getValueFactory().createValue(
                node1.getPath(), PropertyType.PATH);
        q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});

        literal = superuser.getValueFactory().createValue(
                nodeName1 + "/" + nodeName1, PropertyType.PATH);
        q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testReferenceLiteral() throws RepositoryException {
        if (!node1.isNodeType(mixReferenceable)) {
            node1.addMixin(mixReferenceable);
        }
        node1.save();
        Value literal = superuser.getValueFactory().createValue(node1);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testWeakReferenceLiteral() throws RepositoryException {
        // TODO must not match node
    }

    public void testURILiteral() throws RepositoryException {
        // TODO must not match node
    }

    public void testEqualTo() throws RepositoryException {
        checkOperator(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, false, true, false);
    }

    public void testGreaterThan() throws RepositoryException {
        checkOperator(QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN, true, false, false);
    }

    public void testGreaterThanOrEqualTo() throws RepositoryException {
        checkOperator(QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO, true, true, false);
    }

    public void testLessThan() throws RepositoryException {
        checkOperator(QueryObjectModelConstants.JCR_OPERATOR_LESS_THAN, false, false, true);
    }
<commit_msg>JCR-2085: test case (TCK) maintenance for JCR 2.0 - tests related to new property types DECIMAL, URI and WEAKREFERENCE<commit_after>/*
 * 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.query.qom;

import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.Node;
import javax.jcr.PropertyType;
import javax.jcr.query.Query;
import javax.jcr.query.qom.QueryObjectModelConstants;

import java.util.Calendar;
import java.math.BigDecimal;

/**
 * <code>NodeLocalNameTest</code> checks if conversion of literals is correctly
 * performed and operators work as specified.
 */
public class NodeLocalNameTest extends AbstractQOMTest {

    private Node node1;

    private String nodeLocalName;

    protected void setUp() throws Exception {
        super.setUp();
        node1 = testRootNode.addNode(nodeName1, testNodeType);
        superuser.save();
        int colon = nodeName1.indexOf(':');
        if (colon != -1) {
            nodeLocalName = nodeName1.substring(colon + 1);
        } else {
            nodeLocalName = nodeName1;
        }
    }

    protected void tearDown() throws Exception {
        node1 = null;
        super.tearDown();
    }

    public void testStringLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(nodeLocalName);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{node1});
    }

    public void testStringLiteralInvalidName() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue("[" + nodeLocalName);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testBinaryLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(
                nodeLocalName, PropertyType.BINARY);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{node1});
    }

    public void testDateLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(Calendar.getInstance());
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testDoubleLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(Math.PI);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testDecimalLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(new BigDecimal(283));
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testLongLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(283);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testBooleanLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(true);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testNameLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(
                nodeLocalName, PropertyType.NAME);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{node1});
    }

    public void testPathLiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue(
                nodeLocalName, PropertyType.PATH);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{node1});

        literal = superuser.getValueFactory().createValue(
                node1.getPath(), PropertyType.PATH);
        q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});

        literal = superuser.getValueFactory().createValue(
                nodeName1 + "/" + nodeName1, PropertyType.PATH);
        q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testReferenceLiteral() throws RepositoryException {
        if (!node1.isNodeType(mixReferenceable)) {
            node1.addMixin(mixReferenceable);
        }
        superuser.save();
        Value literal = superuser.getValueFactory().createValue(node1);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testWeakReferenceLiteral() throws RepositoryException {
        if (!node1.isNodeType(mixReferenceable)) {
            node1.addMixin(mixReferenceable);
        }
        superuser.save();
        Value literal = superuser.getValueFactory().createValue(node1, true);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testURILiteral() throws RepositoryException {
        Value literal = superuser.getValueFactory().createValue("http://example.com", PropertyType.URI);
        Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
        checkResult(q.execute(), new Node[]{});
    }

    public void testEqualTo() throws RepositoryException {
        checkOperator(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, false, true, false);
    }

    public void testGreaterThan() throws RepositoryException {
        checkOperator(QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN, true, false, false);
    }

    public void testGreaterThanOrEqualTo() throws RepositoryException {
        checkOperator(QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO, true, true, false);
    }

    public void testLessThan() throws RepositoryException {
        checkOperator(QueryObjectModelConstants.JCR_OPERATOR_LESS_THAN, false, false, true);
    }

{'fraction_non_alphanumeric': 0.09407188647986746, 'fraction_numerical': 0.00511416840740474, 'mean_word_length': 3.6110926602457654, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7665620', 'n_tokens_mistral': 3667, 'n_tokens_neox': 3604, 'n_words': 913}