Skip to content

Commit

Permalink
Introducing dss-test-instrumented and dss-test-unit
Browse files Browse the repository at this point in the history
  • Loading branch information
egidijus-kuzma committed Sep 4, 2022
1 parent 53c0ec0 commit 61848a1
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 43 deletions.
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ allprojects (subproject -> {
apply plugin: 'com.android.library'
apply plugin: 'de.mannodermaus.android-junit5'


configurations {
compile.exclude group: 'org.hamcrest', name: 'hamcrest'

Expand All @@ -61,6 +60,12 @@ allprojects (subproject -> {
enable = true
}

testOptions {
unitTests.all {
systemProperty "projectDir", project.rootDir
}
}

minSdkVersion 29
compileSdkVersion 29
targetSdkVersion 30
Expand Down Expand Up @@ -236,6 +241,9 @@ allprojects (subproject -> {
androidTestImplementation project(':dss-crl-parser-x509crl')
androidTestImplementation testFixtures(project(':dss-test'))
testImplementation testFixtures(project(':dss-test'))

androidTestImplementation testFixtures(project(':dss-test-instrumented'))
testImplementation testFixtures(project(':dss-test-unit'))
// androidTestImplementation project(':dss-pades-pdfbox')


Expand Down
5 changes: 5 additions & 0 deletions dss-test-instrumented/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.signerry.dss.test.instrumented"
android:versionName="1.0" >
</manifest>
10 changes: 10 additions & 0 deletions dss-test-instrumented/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dependencies {
testFixturesImplementation 'androidx.test:monitor:1.5.0'
testFixturesImplementation testFixtures(project(':dss-test'))
testFixturesImplementation 'commons-io:commons-io:1.3.1'
}

description = 'DSS Test Intrumented'



Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package eu.europa.esig.dss.test;
package com.signerry.dss.test;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

public class TestUtils {
import androidx.test.platform.app.InstrumentationRegistry;

public class InstrumentedTestUtils implements ITestUtils {

public static Context getCtx() {
return InstrumentationRegistry.getInstrumentation().getTargetContext();
}

public static Collection<File> listFiles(String folder, String[] extensions, boolean recursive) {
@Override
public Collection<File> listFiles(String folder, String[] extensions, boolean recursive) {
List<File> fileList = new ArrayList<>();

List<String> allowedExtensions = Arrays.asList(extensions);
Expand Down Expand Up @@ -56,8 +59,9 @@ public static Collection<File> listFiles(String folder, String[] extensions, boo
}
}

public static File getTmpFile(String filename) {
File file = new File(getTmpDirectory().getPath() + filename);
@Override
public File getTmpFile(String filename) {
File file = new File(getTmpDirectory().getPath(), filename);
try {
file.createNewFile();
} catch (IOException e) {
Expand All @@ -66,33 +70,20 @@ public static File getTmpFile(String filename) {
return file;
}

public static File getTmpDedicatedDirectory() {
@Override
public File getTmpDedicatedDirectory() {
File folder = new File(getTmpDirectory(), UUID.randomUUID().toString());
folder.mkdir();
return folder;
}

public static File getTmpDirectory() {

try {
return getCtx().getCacheDir();
}
catch (java.lang.IllegalStateException e) {
try {
return Files.createTempDirectory("test").toFile();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
@Override
public File getTmpDirectory() {
return getCtx().getCacheDir();
}

public static InputStream getResourceAsStream(String resourcePath) {
InputStream unitTestStream = TestUtils.class.getResourceAsStream("/" + resourcePath);
if(unitTestStream !=null) {
return unitTestStream;
}

//Fallback android test
@Override
public InputStream getResourceAsStream(String resourcePath) {
try {
InputStream resourceAsStream = getCtx().getAssets().open(resourcePath);

Expand All @@ -106,26 +97,23 @@ public static InputStream getResourceAsStream(String resourcePath) {
}
}

public static File getResourceAsFile(String resourcePath) {

@Override
public File getResourceAsFile(String resourcePath) {
try {

final InputStream in = getResourceAsStream(resourcePath);
if (in == null) {
throw new RuntimeException("resourcePath not found " + resourcePath);
}

File tempFile = File.createTempFile(String.valueOf(in.hashCode()), ".tmp");
tempFile.deleteOnExit();
String fileName = FilenameUtils.getName(resourcePath);

try (FileOutputStream out = new FileOutputStream(tempFile)) {
//copy stream
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
File copyOfTheFile = new File(getCtx().getCacheDir(), fileName);

try (FileOutputStream out = new FileOutputStream(copyOfTheFile)) {
IOUtils.copy(in ,out);
}
return tempFile;
return copyOfTheFile;
} catch (IOException e) {
e.printStackTrace();
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.signerry.dss.test.InstrumentedTestUtils
5 changes: 5 additions & 0 deletions dss-test-unit/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.signerry.dss.test.unit"
android:versionName="1.0" >
</manifest>
10 changes: 10 additions & 0 deletions dss-test-unit/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dependencies {
testFixturesImplementation testFixtures(project(':dss-utils'))
testFixturesImplementation testFixtures(project(':dss-test'))
testFixturesImplementation 'commons-io:commons-io:1.3.1'
}

description = 'DSS Test Unit'



Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.signerry.dss.test;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.UUID;

import eu.europa.esig.dss.utils.Utils;

public class UnitTestUtils implements ITestUtils {
@Override
public Collection<File> listFiles(String folder, String[] extensions, boolean recursive) {
return Utils.listFiles(new File(folder), extensions, true);
}

@Override
public File getTmpFile(String filename) {
return new File(getTmpDirectory(), filename);
}

@Override
public File getTmpDedicatedDirectory() {
File dirToCreate = new File(getTmpDirectory(), UUID.randomUUID().toString());
try {
Path dir = Files.createDirectories(dirToCreate.toPath());
return dir.toFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public File getTmpDirectory() {
String projectDir = System.getProperty("projectDir");

try {
Path dir = Files.createDirectories(Paths.get(projectDir, "build", "tmp"));
return dir.toFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public InputStream getResourceAsStream(String resourcePath) {
return UnitTestUtils.class.getResourceAsStream("/" + resourcePath);
}

@Override
public File getResourceAsFile(String resourcePath) {
return new File(UnitTestUtils.class.getResource("/" + resourcePath).getFile());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.signerry.dss.test.UnitTestUtils
2 changes: 0 additions & 2 deletions dss-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ dependencies {
testFixturesImplementation 'org.mockito:mockito-core:3.12.4'
testFixturesImplementation project(':dss-crl-parser-stream')
testFixturesImplementation 'org.apache.xmlgraphics:fop-core:2.7'
testFixturesImplementation project(path: ':dss-xades')
testFixturesImplementation 'androidx.test:monitor:1.5.0'
testFixturesImplementation project(':dss-xades')
testFixturesImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2'
testFixturesImplementation 'org.glassfish.jaxb:jaxb-runtime:3.1.0-M1'
Expand Down
3 changes: 3 additions & 0 deletions dss-test/src/testFixtures/assets/pki-factory.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pki.factory.host = http://dss.nowina.lu/pki-factory/
pki.alternative.factory.host=http://test-pki.signerry.com:8080
pki.factory.keystore.password = ks-password
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.signerry.dss.test;

import java.io.File;
import java.io.InputStream;
import java.util.Collection;

public interface ITestUtils {
Collection<File> listFiles(String folder, String[] extensions, boolean recursive);

File getTmpFile(String filename);

File getTmpDedicatedDirectory();

File getTmpDirectory();

InputStream getResourceAsStream(String resourcePath);

File getResourceAsFile(String resourcePath);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.signerry.dss.test;

import java.io.File;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.ServiceLoader;

public class TestUtils {

/** The implementation to be used */
private static ITestUtils impl;

static {
ServiceLoader<ITestUtils> loader = ServiceLoader.load(ITestUtils.class);
Iterator<ITestUtils> iterator = loader.iterator();

if (!iterator.hasNext()) {
throw new ExceptionInInitializerError(
"No implementation found for ITestUtils in classpath, please choose between dss-test-instrumented or dss-test-unit");
}
impl = iterator.next();
}

private TestUtils() {
}

public static Collection<File> listFiles(String folder, String[] extensions, boolean recursive) {
return impl.listFiles(folder, extensions, recursive);
}

public static File getTmpDirectory() {
return impl.getTmpDirectory();
}

public static InputStream getResourceAsStream(String resourcePath) {
return impl.getResourceAsStream(resourcePath);
}

public static File getResourceAsFile(String resourcePath) {
return impl.getResourceAsFile(resourcePath);
}

public static File getTmpFile(String filename) {
return impl.getTmpFile(filename);
}
}
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ include(':dss-model')
include(':dss-asic-common')
//include(':dss-bom')
include ':dss-android'

include(':dss-test-unit')
include(':dss-test-instrumented')

0 comments on commit 61848a1

Please sign in to comment.