001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.jdbc;
017
018import static org.mockito.Matchers.anyString;
019import static org.mockito.Mockito.mock;
020import static org.mockito.Mockito.when;
021import java.sql.Driver;
022import java.util.Properties;
023import javax.naming.Context;
024import org.modeshape.jcr.JcrRepository;
025import org.modeshape.jcr.ModeShapeEngine;
026
027/**
028 * This is a test suite that operates against a complete JcrRepository instance created and managed using the
029 * {@link ModeShapeEngine}. Essentially this is an integration test, but it does test lower-level functionality of the
030 * implementation of the JCR interfaces related to querying. (It is simply more difficult to unit test these implementations
031 * because of the difficulty in mocking the many other components to replicate the same functionality.)
032 * <p>
033 * Also, because queries are read-only, the engine is set up once and used for the entire set of test methods.
034 * </p>
035 * <p>
036 * The following are the SQL semantics that the tests will be covering:
037 * <li>variations of simple SELECT * FROM</li>
038 * <li>JOIN
039 * </p>
040 * <p>
041 * To create the expected results to be used to run a test, use the test and print method: example:
042 * DriverTestUtil.executeTestAndPrint(this.connection, "SELECT * FROM [nt:base]"); This will print the expected results like this:
043 * String[] expected = { "jcr:primaryType[STRING]", "mode:root", "car:Car", "car:Car", "nt:unstructured" } Now copy the expected
044 * results to the test method. Then change the test to run the executeTest method passing in the <code>expected</code> results:
045 * example: DriverTestUtil.executeTest(this.connection, "SELECT * FROM [nt:base]", expected);
046 * </p>
047 */
048public class JcrDriverIntegrationTest extends AbstractJdbcDriverIntegrationTest {
049
050    @Override
051    protected Driver createDriver( JcrRepository repository ) throws Exception {
052        // Create a JcrDriver instance that uses JNDI ...
053        final Context jndi = mock(Context.class);
054        when(jndi.lookup(anyString())).thenReturn(repository);
055        JcrDriver.JcrContextFactory contextFactory = new JcrDriver.JcrContextFactory() {
056            @Override
057            public Context createContext( Properties properties ) {
058                return jndi;
059            }
060        };
061
062        return new JcrDriver(contextFactory);
063    }
064
065    @Override
066    protected String createConnectionUrl( JcrRepository repository ) throws Exception {
067        return LocalJcrDriver.JNDI_URL_PREFIX + "jcr/local?repositoryName=" + repository.getName();
068    }
069}