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.web.jcr;
017
018import static org.hamcrest.CoreMatchers.is;
019import static org.hamcrest.core.IsNull.notNullValue;
020import static org.junit.Assert.assertThat;
021import static org.junit.Assert.fail;
022import static org.mockito.Mockito.mock;
023import static org.mockito.Mockito.when;
024import java.util.Collections;
025import java.util.Enumeration;
026import java.util.List;
027import java.util.Set;
028import java.util.Vector;
029import javax.jcr.Session;
030import javax.servlet.ServletContext;
031import org.junit.Before;
032import org.junit.Test;
033import org.mockito.invocation.InvocationOnMock;
034import org.mockito.stubbing.Answer;
035import org.modeshape.jcr.api.RepositoryFactory;
036
037public class RepositoryManagerTest {
038
039    private final String VALID_JCR_URL = "file:src/test/resources/repo-config.json";
040    protected final List<String> PARAM_NAMES = Collections.singletonList(RepositoryFactory.URL);
041
042    private ServletContext context;
043
044    @Before
045    public void beforeEach() throws Exception {
046        context = mock(ServletContext.class);
047        when(context.getInitParameterNames()).thenAnswer(new Answer<Enumeration<?>>() {
048            @SuppressWarnings( {"unchecked", "rawtypes"} )
049            @Override
050            public Enumeration<?> answer( InvocationOnMock invocation ) throws Throwable {
051                return new Vector(PARAM_NAMES).elements();
052            }
053        });
054        when(context.getInitParameter(RepositoryFactory.URL)).thenReturn(VALID_JCR_URL);
055    }
056
057    @Test
058    public void shouldLoadFileBasedModeShapeEngineFromClassPath() throws Exception {
059        // This will start up the repository ...
060        RepositoryManager.initialize(context);
061
062        Set<String> repoNames = RepositoryManager.getJcrRepositoryNames();
063        assertThat(repoNames, is(Collections.singleton("Test Repository")));
064
065        Session session = RepositoryManager.getSession(null, "Test Repository", "default");
066        assertThat(session, is(notNullValue()));
067
068        try {
069            String repoName = "xxxyyyzzz";
070            RepositoryManager.getSession(null, repoName, "default");
071            fail("Should not have found repository \"" + repoName + "\"");
072        } catch (NoSuchRepositoryException e) {
073            // expected
074        }
075
076        RepositoryManager.shutdown();
077    }
078}