Package org.apache.jackrabbit.oak.jcr.query

Source Code of org.apache.jackrabbit.oak.jcr.query.QueryTest

/*
* 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.oak.jcr.query;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
import org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest;
import org.junit.After;
import org.junit.Test;

/**
* Tests the query feature.
*/
public class QueryTest extends AbstractRepositoryTest {

    @After
    public void tearDown() throws RepositoryException {
        logout();
    }

    @Test
    public void simple() throws RepositoryException {
        Repository repository = getRepository();
        Session session = repository.login();
        try {
            Node hello = session.getRootNode().addNode("hello");
            hello.setProperty("id""1");
            hello.setProperty("text""hello world");
            session.save();

            ValueFactory vf = session.getValueFactory();

            QueryManager qm = session.getWorkspace().getQueryManager();
            Query q = qm.createQuery("select text from [nt:base] where id = $id", Query.JCR_SQL2);
            q.bindValue("id", vf.createValue("1"));
            QueryResult r = q.execute();
            RowIterator it = r.getRows();
            assertTrue(it.hasNext());
            Row row = it.nextRow();
            assertEquals("hello world", row.getValue("text").getString());
            assertFalse(it.hasNext());

        } finally {
            session.logout();
        }
    }

}
TOP

Related Classes of org.apache.jackrabbit.oak.jcr.query.QueryTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.