Skip to content

Commit

Permalink
fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
woonsan committed Mar 23, 2016
1 parent 7ac0e2b commit 5e1208f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 17 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ language: java
jdk:
- oraclejdk8
after_success:
- mvn jacoco:report coveralls:report

- mvn clean test jacoco:report coveralls:report
32 changes: 32 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,22 @@

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<javaVersion>1.7</javaVersion>

<jcr.version>2.0</jcr.version>
<jackrabbit.version>2.11.3</jackrabbit.version>

<commons-io.version>2.4</commons-io.version>
<junit.version>4.8.2</junit.version>

<maven-source-plugin.version>2.4</maven-source-plugin.version>
<maven-javadoc-plugin.version>2.10.2</maven-javadoc-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-compiler-plugin.version>3.2</maven-compiler-plugin.version>
<coveralls-maven-plugin.version>4.1.0</coveralls-maven-plugin.version>
<jacoco-maven-plugin.version>0.7.5.201505241946</jacoco-maven-plugin.version>

</properties>

Expand Down Expand Up @@ -105,6 +110,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-jcr2dav</artifactId>
Expand Down Expand Up @@ -134,6 +146,26 @@
</configuration>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>${coveralls-maven-plugin.version}</version>
</plugin>

</plugins>

</build>
Expand Down
47 changes: 38 additions & 9 deletions src/main/java/com/github/woonsan/jdbc/jcr/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import javax.jcr.Credentials;
Expand Down Expand Up @@ -57,6 +59,8 @@ public class Driver implements java.sql.Driver {

protected static final String REPO_HOME_PROPERTY = "REPOSITORY.HOME";

private volatile Map<Properties, Repository> repositoryMap = new ConcurrentHashMap<>();

@Override
public Connection connect(String url, Properties info) throws SQLException {
final Properties connProps = readConnectionProperties(url, info);
Expand Down Expand Up @@ -135,19 +139,44 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException();
}

public synchronized void shutdownTransientRepositories() {
for (Repository repository : repositoryMap.values()) {
if (repository instanceof TransientRepository) {
((TransientRepository) repository).shutdown();
}
}
}

protected Repository getRepository(final Properties connProps) throws SQLException {
Repository repository = null;
Map<Properties, Repository> repoMap = repositoryMap;
Repository repository = repoMap.get(connProps);

try {
final String location = connProps.getProperty(CONNECTION_PROP_LOCATION);
if (repository == null) {
synchronized (this) {
repoMap = repositoryMap;
repository = repoMap.get(connProps);

if (location == null || "".equals(location.trim())) {
repository = getTransientRepository(connProps);
} else {
repository = JcrUtils.getRepository(location);
if (repository == null) {
try {
Repository repo = null;

final String location = connProps.getProperty(CONNECTION_PROP_LOCATION);

if (location == null || "".equals(location.trim())) {
repo = getTransientRepository(connProps);
} else {
repo = JcrUtils.getRepository(location);
}

repoMap.put(connProps, repo);
repositoryMap = repoMap;

repository = repo;
} catch (RepositoryException e) {
throw new SQLException("Cannot get JCR repository. " + e.toString(), e);
}
}
}
} catch (RepositoryException e) {
throw new SQLException("Cannot get JCR repository. " + e.toString(), e);
}

return repository;
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/com/github/woonsan/jdbc/jcr/TestConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@

public class TestConstants {

public static final String TEST_REPOSITORY_CONF = "repository.xml";

public static final String TEST_REPOSITORY_HOME = "repository";

//public static final String DEFAULT_TEST_JDBC_URL = "jdbc:jcr:http://localhost:8080/server/";
public static final String DEFAULT_TEST_JDBC_URL = "jdbc:jcr:?repository.conf=repository.xml&repository.home=repository";
public static final String DEFAULT_TEST_JDBC_URL = "jdbc:jcr:?repository.conf=" + TEST_REPOSITORY_CONF
+ "&repository.home=" + TEST_REPOSITORY_HOME;

private TestConstants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.github.woonsan.jdbc.jcr.impl;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.Calendar;
import java.util.Properties;
Expand All @@ -27,6 +28,7 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;

Expand All @@ -46,11 +48,7 @@ public class AbstractRepositoryEnabledTestCase {

@Before
public void setUp() throws Exception {
File repoLockFile = new File("repository/.lock");

if (repoLockFile.isFile()) {
repoLockFile.delete();
}
deleteTransientRepositoryFolderAndConfig();

jdbcDriver = new Driver();
Properties info = new Properties();
Expand All @@ -74,6 +72,10 @@ public void setUp() throws Exception {
@After
public void tearDown() throws Exception {
connection.close();

((Driver) jdbcDriver).shutdownTransientRepositories();

deleteTransientRepositoryFolderAndConfig();
}

protected Connection getConnection() {
Expand All @@ -100,4 +102,17 @@ private void createEmpData(Node testDataFolderNode) throws RepositoryException {
}
}

private void deleteTransientRepositoryFolderAndConfig() throws IOException {
File repoDir = new File(TestConstants.TEST_REPOSITORY_HOME);

if (repoDir.exists()) {
FileUtils.forceDelete(repoDir);
}

File repoConf = new File(TestConstants.TEST_REPOSITORY_CONF);

if (repoConf.exists()) {
FileUtils.forceDelete(repoConf);
}
}
}

0 comments on commit 5e1208f

Please sign in to comment.