Skip to content

Commit

Permalink
Remove Java 8 dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
George Venios committed Sep 1, 2017
1 parent b6ac574 commit ac096fa
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 62 deletions.
10 changes: 4 additions & 6 deletions README.md
Expand Up @@ -138,7 +138,7 @@ characteristic | Characteristic | Possible values: "sw"\|"ap
groups | Map<String, Integer> | map the name of a pool to their lowest dimension for a characteristic

## Runtime Permissions
By default fork auto-grants all runtime permissions on Android Marshmallow +. It is possible anyway to selectively revoke one or more permissions per single test case.
By default Fork auto-grants all runtime permissions on Android Marshmallow +. It is possible anyway to selectively revoke one or more permissions per single test case.
To do so, you have to add an annotation called `RevokePermission`. Here is an example:
```java
@Test
Expand All @@ -159,18 +159,16 @@ After every test case, all the runtime permissions will be automatically re-gran
This feature will impact only Marshmallow and subsequent devices.

## Arbitrary metadata
Fork supports adding arbitrary metadata on any test and get them returned as `properties` on the suite level of the JUnit xml report.
Using Fork you can set metadata on tests and get them back in its JUnit xml reports. The metadata are added as additional `property` tags on the suite level of the report, as each test produces its own report.

```java
@Test
@TestProperty(key = "key1", value = "value1")
@TestProperty(key = "key2", value = "value2")
@TestProperties(keys = {"k1", "k2"}, values = {"v1", "v2"})
public void testWithProperties() {
// Test normally here
}
```

This feature relies on Repeated Annotations and therefore requires Java 8.
Note that Fork will stop adding pairs after it encounters an unpaired key or value, so make sure you have the same number of keys and values.

## Examples

Expand Down
Expand Up @@ -9,5 +9,6 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface TestProperties {
TestProperty[] value() default {};
String[] keys() default {};
String[] values() default {};
}
17 changes: 0 additions & 17 deletions fork-client/src/main/java/com/shazam/fork/TestProperty.java

This file was deleted.

4 changes: 2 additions & 2 deletions fork-common-test-dex/app/build.gradle
Expand Up @@ -12,8 +12,8 @@ android {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
Expand Down
@@ -1,6 +1,6 @@
package com.shazam.forktest;

import com.shazam.fork.TestProperty;
import com.shazam.fork.TestProperties;

import org.junit.Ignore;
import org.junit.Test;
Expand All @@ -10,14 +10,13 @@
public class PropertiesClassTest {

@Test
@TestProperty(key = "foo", value = "bar")
@TestProperties(keys = {"foo"}, values = {"bar"})
public void methodWithProperties() {
assertEquals(4, 2 + 2);
}

@Test
@TestProperty(key = "foo", value = "bar")
@TestProperty(key = "bux", value = "poi")
@TestProperties(keys = {"foo", "bux"}, values = {"bar", "poi"})
public void methodWithMultipleProperties() {
assertEquals(4, 2 + 2);
}
Expand Down
Expand Up @@ -14,26 +14,25 @@
import com.shazam.fork.model.TestCaseEvent;
import org.jf.dexlib.*;
import org.jf.dexlib.EncodedValue.AnnotationEncodedSubValue;
import org.jf.dexlib.EncodedValue.AnnotationEncodedValue;
import org.jf.dexlib.EncodedValue.ArrayEncodedValue;
import org.jf.dexlib.EncodedValue.EncodedValue;
import org.jf.dexlib.EncodedValue.StringEncodedValue;

import javax.annotation.Nonnull;
import java.io.File;
import java.util.*;

import static com.shazam.fork.model.TestCaseEvent.newTestCase;
import static java.lang.Math.min;
import static java.util.Arrays.stream;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

public class TestSuiteLoader {
private static final String TEST_ANNOTATION = "Lorg/junit/Test;";
private static final String IGNORE_ANNOTATION = "Lorg/junit/Ignore;";
private static final String REVOKE_PERMISSION_ANNOTATION = "Lcom/shazam/fork/RevokePermission;";
private static final String TEST_PROPERTIES_ANNOTATION = "Lcom/shazam/fork/TestProperties;";
private static final String TEST_PROPERTY_ANNOTATION = "Lcom/shazam/fork/TestProperty;";

private final File instrumentationApkFile;
private final DexFileExtractor dexFileExtractor;
Expand Down Expand Up @@ -111,30 +110,27 @@ private List<String> getPermissionsToRevoke(AnnotationItem[] annotations) {

private Map<String, String> getTestProperties(AnnotationItem[] annotations) {
Map<String, String> properties = new HashMap<>();

// If only one was found on the top level.
properties.putAll(stream(annotations)
.filter(annotationItem -> TEST_PROPERTY_ANNOTATION.equals(stringType(annotationItem)))
.map(AnnotationItem::getEncodedAnnotation)
.collect(toMap(
p -> ((StringEncodedValue) p.values[indexOfName(p, "key")]).value.getStringValue(),
p -> ((StringEncodedValue) p.values[indexOfName(p, "value")]).value.getStringValue()
)));

// Else, multiple, and by definition not on top level
properties.putAll(stream(annotations)
stream(annotations)
.filter(annotationItem -> TEST_PROPERTIES_ANNOTATION.equals(stringType(annotationItem)))
.map(propertiesAnnotationItem -> propertiesAnnotationItem.getEncodedAnnotation().values)
.map(propertiesAnnotationValues -> propertiesAnnotationValues[0])
.flatMap(propertyAnnotationsContainer -> stream(((ArrayEncodedValue) propertyAnnotationsContainer).values))
.map(propertyAnnotation -> ((AnnotationEncodedValue) propertyAnnotation))
.collect(toMap(
p -> ((StringEncodedValue) p.values[indexOfName(p, "key")]).value.getStringValue(),
p -> ((StringEncodedValue) p.values[indexOfName(p, "value")]).value.getStringValue()
)));
.map(AnnotationItem::getEncodedAnnotation)
.forEach(an -> {
List<String> keys = getAnnotationProperty(an, "keys");
List<String> values = getAnnotationProperty(an, "values");

for (int i = 0; i < min(values.size(), keys.size()); i++) {
properties.put(keys.get(i), values.get(i));
}
});
return properties;
}

private List<String> getAnnotationProperty(AnnotationEncodedSubValue an, String propertyName) {
EncodedValue[] values = ((ArrayEncodedValue) an.values[indexOfName(an, propertyName)]).values;
return stream(values)
.map(stringEncodedValue -> ((StringEncodedValue) stringEncodedValue).value.getStringValue())
.collect(toList());
}

private int indexOfName(AnnotationEncodedSubValue p, String key) {
int index = -1;
StringIdItem[] names = p.names;
Expand Down
Expand Up @@ -12,29 +12,25 @@

import com.shazam.fork.io.DexFileExtractor;
import com.shazam.fork.model.TestCaseEvent;

import org.hamcrest.Matcher;
import org.jf.dexlib.DexFile;
import org.junit.Test;

import javax.annotation.Nonnull;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;

import static com.shazam.fork.io.FakeDexFileExtractor.fakeDexFileExtractor;
import static com.shazam.fork.io.Files.convertFileToDexFile;
import static com.shazam.fork.model.TestCaseEvent.newTestCase;
import static com.shazam.fork.suite.FakeTestClassMatcher.fakeTestClassMatcher;
import static com.shazam.shazamcrest.MatcherAssert.assertThat;
import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static java.util.Collections.*;
import static org.hamcrest.Matchers.containsInAnyOrder;

/**
Expand Down Expand Up @@ -102,7 +98,7 @@ public void populatesTestCaseEvents() throws Exception {
false, asList("android.permission.RECORD_AUDIO", "android.permission.ACCESS_FINE_LOCATION")),
sameTestEventAs("methodAnnotatedWithEmptyRevokePermissionsTest", "com.shazam.forktest.RevokePermissionsClassTest", false),
sameTestEventAs("methodWithProperties", "com.shazam.forktest.PropertiesClassTest", singletonMap("foo", "bar")),
sameTestEventAs("methodWithMultipleProperties", "com.shazam.forktest.PropertiesClassTest", multiPropertiesMap )
sameTestEventAs("methodWithMultipleProperties", "com.shazam.forktest.PropertiesClassTest", multiPropertiesMap)
)
);

Expand Down
6 changes: 4 additions & 2 deletions fork-common/src/test/resources/_readme_when_updating.txt
Expand Up @@ -3,6 +3,8 @@ Source code to generate tests.dex and app-debug.apk in the folder fork-common-te
1)Cd to the sub-folder `fork-common-test-dex`
2)run `./gradlew clean assemble assembleAndroidTest`
3)copy ./app/build/intermediates/transforms/dexMerger/androidTest/debug/0/classes.dex into the main module called `fork-common` in src/test/resources (and rename the file in tests.dex)
aka: cp app/build/intermediates/transforms/dexMerger/androidTest/debug/0/classes.dex ../fork-common/src/test/resources/tests.dex
4)copy ./app/build/outputs/apk/debug/app-debug.apk into the main module called `fork-common` in src/test/resources
4)Cd back to `fork`
5)run at least once `./gradlew test`
aka: cp app/build/outputs/apk/debug/app-debug.apk ../fork-common/src/test/resources
5)Cd back to `fork`
6)run at least once `./gradlew test`
Binary file modified fork-common/src/test/resources/app-debug.apk
Binary file not shown.
Binary file modified fork-common/src/test/resources/tests.dex
Binary file not shown.

0 comments on commit ac096fa

Please sign in to comment.