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.hamcrest.core.Is.is;
019import org.junit.After;
020import static org.junit.Assert.assertThat;
021import org.junit.Before;
022import org.junit.Test;
023import org.modeshape.jdbc.delegate.ConnectionInfo;
024import java.sql.DriverManager;
025import java.sql.DriverPropertyInfo;
026import java.sql.SQLException;
027import java.util.Properties;
028
029/**
030 * 
031 */
032public class JcrDriverHttpTest {
033
034    private JcrDriver driver;
035    private String validServerName;
036    private String validRepositoryName;
037    private String validWorkspaceName;
038    private String validUrl;
039    private Properties validProperties;
040
041    @Before
042    public void beforeEach() throws Exception {
043        validRepositoryName = "MyRepository";
044        validServerName = "serverName:8080";
045        validWorkspaceName = "default";
046        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest/" + validRepositoryName + "/"
047                   + validWorkspaceName + "?user=jsmith&password=secret";
048        validProperties = new Properties();
049
050        driver = new JcrDriver(null);
051    }
052
053    @After
054    public void afterEach() throws Exception {
055        try {
056            DriverManager.deregisterDriver(driver);
057        } finally {
058            driver = null;
059        }
060    }
061
062    @Test
063    public void shouldNotBeJdbcCompliant() {
064        assertThat(driver.jdbcCompliant(), is(false));
065    }
066
067    @Test
068    public void shouldHaveMajorVersion() {
069        assertThat(driver.getMajorVersion(), is(TestUtil.majorVersion(JdbcI18n.driverVersion.text())));
070    }
071
072    @Test
073    public void shouldHaveMinorVersion() {
074        assertThat(driver.getMinorVersion(), is(TestUtil.minorVersion(JdbcI18n.driverVersion.text())));
075    }
076
077    @Test
078    public void shouldHaveVendorUrl() {
079        assertThat(driver.getVendorUrl(), is(JdbcI18n.driverVendorUrl.text()));
080    }
081
082    @Test
083    public void shouldHaveVendorName() {
084        assertThat(driver.getVendorName(), is(JdbcI18n.driverVendor.text()));
085    }
086
087    @Test
088    public void shouldHaveVersion() {
089        assertThat(driver.getVersion(), is(JdbcI18n.driverVersion.text()));
090    }
091
092    @Test
093    public void shouldReturnEmptyPropertyInfosWhenSuppliedValidAndCompleteUrlAndNoProperties() throws SQLException {
094        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
095        assertThat(infos.length, is(0));
096    }
097
098    @Test
099    public void shouldReturnEmptyPropertyInfosWhenSuppliedValidUrlAndAllPropertiesWithRepositoryInHttp() throws SQLException {
100        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest";
101        validProperties.put(JcrDriver.WORKSPACE_PROPERTY_NAME, "MyWorkspace");
102        validProperties.put(JcrDriver.USERNAME_PROPERTY_NAME, "jsmith");
103        validProperties.put(JcrDriver.PASSWORD_PROPERTY_NAME, "secret");
104        validProperties.put(JcrDriver.REPOSITORY_PROPERTY_NAME, validRepositoryName);
105        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
106        assertThat(infos.length, is(0));
107    }
108
109    @Test
110    public void shouldReturnRepositoryPropertyInfoWhenMissingRequiredRepositoryName() throws SQLException {
111        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest";
112        validProperties.put(JcrDriver.WORKSPACE_PROPERTY_NAME, "MyWorkspace");
113        validProperties.put(JcrDriver.USERNAME_PROPERTY_NAME, "jsmith");
114        validProperties.put(JcrDriver.PASSWORD_PROPERTY_NAME, "secret");
115
116        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
117        assertThat(infos.length, is(1));
118        assertThat(infos[0].name, is(JdbcLocalI18n.repositoryNamePropertyName.text()));
119        assertThat(infos[0].description, is(JdbcLocalI18n.repositoryNamePropertyDescription.text()));
120        assertThat(infos[0].required, is(true));
121    }
122
123    @Test
124    public void shouldReturnRepositoryPropertyInfoWhenMissingWorkspaceName() throws SQLException {
125        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest";
126        validProperties.put(JcrDriver.USERNAME_PROPERTY_NAME, "jsmith");
127        validProperties.put(JcrDriver.PASSWORD_PROPERTY_NAME, "secret");
128        validProperties.put(JcrDriver.REPOSITORY_PROPERTY_NAME, validRepositoryName);
129
130        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
131        assertThat(infos.length, is(1));
132        assertThat(infos[0].name, is(JdbcLocalI18n.workspaceNamePropertyName.text()));
133        assertThat(infos[0].description, is(JdbcLocalI18n.workspaceNamePropertyDescription.text()));
134        assertThat(infos[0].required, is(false));
135    }
136
137    @Test
138    public void shouldReturnRepositoryPropertyInfoWhenMissingUsername() throws SQLException {
139        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest";
140        validProperties.put(JcrDriver.WORKSPACE_PROPERTY_NAME, "MyWorkspace");
141        validProperties.put(JcrDriver.PASSWORD_PROPERTY_NAME, "secret");
142        validProperties.put(JcrDriver.REPOSITORY_PROPERTY_NAME, validRepositoryName);
143        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
144        assertThat(infos.length, is(1));
145        assertThat(infos[0].name, is(JdbcLocalI18n.usernamePropertyName.text()));
146        assertThat(infos[0].description, is(JdbcLocalI18n.usernamePropertyDescription.text()));
147        assertThat(infos[0].required, is(false));
148    }
149
150    @Test
151    public void shouldReturnRepositoryPropertyInfoWhenMissingPassword() throws SQLException {
152        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest";
153        validProperties.put(JcrDriver.WORKSPACE_PROPERTY_NAME, "MyWorkspace");
154        validProperties.put(JcrDriver.USERNAME_PROPERTY_NAME, "jsmith");
155        validProperties.put(JcrDriver.REPOSITORY_PROPERTY_NAME, validRepositoryName);
156        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
157        assertThat(infos.length, is(1));
158        assertThat(infos[0].name, is(JdbcLocalI18n.passwordPropertyName.text()));
159        assertThat(infos[0].description, is(JdbcLocalI18n.passwordPropertyDescription.text()));
160        assertThat(infos[0].required, is(false));
161    }
162
163    @Test
164    public void shouldAcceptValidUrls() {
165        assertThat(driver.acceptsURL(
166                JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest/" + this.validRepositoryName + "/MyWorkspace&user=jsmith&password=secret"),
167                   is(true));
168        assertThat(driver.acceptsURL(
169                JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest/" + this.validRepositoryName + "&user=jsmith&password=secret"),
170                   is(true));
171        assertThat(driver.acceptsURL(
172                JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest/&user=jsmith&password=secret"), is(true));
173    }
174
175    @Test
176    public void shouldNotAcceptInvalidUrls() {
177        assertThat(driver.acceptsURL("jdbc:jcr:http"), is(false));
178        assertThat(driver.acceptsURL(JcrDriver.HTTP_URL_PREFIX), is(false));
179        assertThat(driver.acceptsURL(JcrDriver.HTTP_URL_PREFIX + " "), is(false));
180        assertThat(driver.acceptsURL("file://"), is(false));
181        assertThat(driver.acceptsURL("jdbc://"), is(false));
182    }
183
184    @Test
185    public void shouldCreateConnectionInfoForUrlWithEscapedCharacters() throws SQLException {
186        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape%20rest?repositoryName=My%20Repository&workspace=My%20Workspace&user=j%20smith&password=secret";
187        ConnectionInfo info = driver.createConnectionInfo(validUrl, validProperties);
188        assertThat(info.getWorkspaceName(), is("My Workspace"));
189        assertThat(info.getUsername(), is("j smith"));
190        assertThat(info.getPassword(), is("secret".toCharArray()));
191        assertThat(info.getRepositoryName(), is("My Repository"));
192    }
193}