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.cmis;
017
018import static org.mockito.Matchers.any;
019import static org.mockito.Matchers.eq;
020import static org.mockito.Mockito.mock;
021import static org.mockito.Mockito.times;
022import static org.mockito.Mockito.verify;
023import java.util.HashMap;
024import java.util.Map;
025import javax.jcr.Credentials;
026import org.apache.chemistry.opencmis.commons.server.CallContext;
027import org.apache.chemistry.opencmis.jcr.JcrRepository;
028import org.junit.Test;
029
030public class JcrServiceTest {
031
032    /**
033     * Ensure that {@link JcrService} returns the workspace for the long form repository ids (e.g. "repositoryName:workspace") and
034     * {@code null} for the short form (e.g. "repositoryName"). For example, "foo" should return {@code null}, and "foo:bar"
035     * should return "bar".
036     */
037    @Test
038    public void testWorkspace() {
039        String[] names = {"foo", "", "baz", "a"};
040        JcrRepository mockRepo = mock(JcrRepository.class);
041        Map<String, JcrRepository> jrs = new HashMap<String, JcrRepository>();
042        for (String name : names) {
043            jrs.put(name, mockRepo);
044        }
045
046        JcrService js = new JcrService(jrs);
047        js.setCallContext(mock(CallContext.class));
048
049        js.login("foo");
050        verify(mockRepo).login(null, null);
051
052        js.login("foo:");
053        verify(mockRepo, times(2)).login(null, null);
054
055        js.login("foo:bar");
056        verify(mockRepo).login(any(Credentials.class), eq("bar"));
057
058        js.login(":quux");
059        verify(mockRepo).login(any(Credentials.class), eq("quux"));
060
061        js.login("a:b:c");
062        verify(mockRepo).login(any(Credentials.class), eq("b"));
063    }
064}