Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ before_script:
- cp .travis/settings.xml $MAVEN_CFG/settings.xml

script:
- mvn test -B
- mvn verify -B

before_deploy:
- if [ ! -z "$TRAVIS_TAG" ]; then VERSION=$(tr -d "/v/" <<<$TRAVIS_TAG); mvn org.codehaus.mojo:versions-maven-plugin:2.1:set -DnewVersion=${VERSION}; fi
Expand Down
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,45 @@ http://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN901

*Sections 6.1 and 6.5 are the more important ones, and the only ones you need if you're using the latest Maven version.*

### Local database with utPLSQL and utPLSQL-demo-project

To usefully contribute you'll have to setup a local database with installed [latest utPLSQL v3](https://github.com/utPLSQL/utPLSQL) and [utPLSQL-demo-project](https://github.com/utPLSQL/utPLSQL-demo-project).
The demo-project will serve as your test user. See .travis.yml to see an example on how it can be installed.

### Maven settings for utPLSQL-local profile

utPLSQL-java-api comes with a preconfigured profile "utPLSQL-local". This profile uses properties to set the correct
environment variables for DB_URL, DB_USER and DB_PASS which is needed to run the integration tests.
You can set these properties by adding the following to your Maven settings.xml:

```xml
<settings>
<!-- ... -->
<profiles>
<profile>
<id>utPLSQL-local</id>
<properties>
<dbUrl>localhost:1521/XE</dbUrl>
<dbUser>app</dbUser>
<dbPass>app</dbPass>
</properties>
</profile>
</profiles>

<activeProfiles>
<activeProfile>utPLSQL-local</activeProfile>
</activeProfiles>
</settings>
```

After configuring your access to Oracle's Maven repository, you will be able to successfully build this API.

```bash
cd utPLSQL-java-api
mvn clean package install -DskipTests
mvn clean package install
```

The cmd above is ignoring unit tests because it needs a database connection with the latest utPLSQL v3 installed. Please take a look on .travis.yml and .travis folder to see how testing was configured.
### Skip the local database part

If you want to skip the local database part, just run ``mvn clean package install -DskipTests``.
You will still be able to run ``mvn test`` because integration tests are run in the ``verify``-phase.
84 changes: 83 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.platform.version>1.0.3</junit.platform.version>
<junit.jupiter.version>5.0.3</junit.jupiter.version>
</properties>

<dependencies>
Expand All @@ -29,11 +31,29 @@
<version>12.2.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<!--<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down Expand Up @@ -66,8 +86,70 @@
<version>0.0.6</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludes>
<exclude>**/*IT.java</exclude>
</excludes>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>utPLSQL-local</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<environmentVariables>
<DB_URL>${dbUrl}</DB_URL>
<DB_USER>${dbUser}</DB_USER>
<DB_PASS>${dbPass}</DB_PASS>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>

<distributionManagement>
<repository>
<id>packagecloud-utPLSQL</id>
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/org/utplsql/api/EnvironmentVariableUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.utplsql.api;

/**
* This class provides an easy way to get environmental variables.
* This is mainly to improve testability but also to standardize the way how utPLSQL API and CLI read from
* environment.
* <p>
* Variables are obtained from the following scopes in that order (chain breaks as soon as a value is obtained):
* <ul>
* <li>Properties (System.getProperty())</li>
* <li>Environment (System.getEnv())</li>
* <li>Default value</li>
* </ul>
* <p>
* An empty string is treated the same as null.
*
* @author pesse
*/
public class EnvironmentVariableUtil {

/**
* Returns the value for a given key from environment (see class description)
*
* @param key Key of environment or property value
* @return Environment value or null
*/
public static String getEnvValue(String key) {
return getEnvValue(key, null);
}

/**
* Returns the value for a given key from environment or a default value (see class description)
*
* @param key Key of environment or property value
* @param defaultValue Default value if nothing found
* @return Environment value or defaultValue
*/
public static String getEnvValue(String key, String defaultValue) {

String val = System.getProperty(key);
if (val == null || val.isEmpty()) val = System.getenv(key);
if (val == null || val.isEmpty()) val = defaultValue;

return val;
}
}
61 changes: 61 additions & 0 deletions src/main/java/org/utplsql/api/ResourceUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.utplsql.api;

import org.utplsql.api.reporter.CoverageHTMLReporter;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** Helper class for dealing with Resources
*
* @author pesse
*/
public class ResourceUtil {

/** Returns the Path to a resource so it is walkable no matter if it's inside a jar or on the file system
*
* @param resourceName The name of the resource
* @return Path to the resource, either in JAR or on file system
* @throws IOException
* @throws URISyntaxException
*/
public static Path getPathToResource( String resourceName ) throws IOException, URISyntaxException {
URI uri = CoverageHTMLReporter.class.getResource(resourceName).toURI();
Path myPath;
if (uri.getScheme().equalsIgnoreCase("jar")) {
FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
myPath = fileSystem.getPath(resourceName);
} else {
myPath = Paths.get(uri);
}

return myPath;
}

/** Returns the relative paths of all children of the given resource. Relative path begins from the first atom of the given path.
*
* @param resourceAsPath The resource to get children from
* @param filesOnly If set to true it will only return files, not directories
* @return List of relative Paths to the children
* @throws IOException
* @throws URISyntaxException
*/
public static List<Path> getListOfChildren( Path resourceAsPath, boolean filesOnly ) throws IOException, URISyntaxException {

Path resourcePath = getPathToResource("/"+resourceAsPath.toString());
int relativeStartIndex = resourcePath.getNameCount()-resourceAsPath.getNameCount();

final List<Path> result = new ArrayList<>();

Files.walk(resourcePath)
.filter(p -> !filesOnly || p.toFile().isFile())
.forEach( p -> result.add(p.subpath(relativeStartIndex, p.getNameCount())));

return result;
}

}
68 changes: 68 additions & 0 deletions src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package org.utplsql.api.reporter;

import org.utplsql.api.CustomTypes;
import org.utplsql.api.ResourceUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;
import java.util.List;
import java.util.function.Consumer;

public class CoverageHTMLReporter extends Reporter {

Expand Down Expand Up @@ -59,4 +68,63 @@ public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeString(getAssetsPath());
}

/** Copies files from Classpath to a target directory.
* Can omit the first x folders of the asset-path when copying to the target directory
*
* @param assetPath Path of the asset in the classpath
* @param targetDirectory Target directory to copy the asset to
* @param filterNumOfFolders Omits the first x folders of the path when copying the asset to the target directory
* @throws IOException
*/
private static void copyFileFromClasspath( Path assetPath, Path targetDirectory, int filterNumOfFolders ) throws IOException {

Path assetStartPath = assetPath.subpath(filterNumOfFolders, assetPath.getNameCount());
Path targetAssetPath = targetDirectory.resolve(assetStartPath);

Files.createDirectories(targetAssetPath.getParent());

try (InputStream is = CoverageHTMLReporter.class.getClassLoader()
.getResourceAsStream(assetPath.toString())
) {
Files.copy( is, targetAssetPath );
}
}

/** Write the bundled assets necessary for the HTML Coverage report to a given targetPath
*
* @param targetDirectory Directory where the assets should be stored
* @throws RuntimeException
*/
public static void writeReportAssetsTo(Path targetDirectory) throws RuntimeException {

try {
Files.createDirectories(targetDirectory);

List<Path> paths = ResourceUtil.getListOfChildren(Paths.get("CoverageHTMLReporter"), true);

paths.forEach((ThrowingConsumer<Path>) p -> copyFileFromClasspath(p, targetDirectory, 1) );
}
catch ( IOException | URISyntaxException e ) {
throw new RuntimeException(e);
}
}

/** Functional Interface just to throw Exception from Consumer
*
* @param <T>
*/
@FunctionalInterface
public interface ThrowingConsumer<T> extends Consumer<T> {

@Override
default void accept(final T elem) {
try {
acceptThrows(elem);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

void acceptThrows( T t ) throws IOException;
}
}
Loading