Skip to content

Commit

Permalink
Create a Java Only Example (#465)
Browse files Browse the repository at this point in the history
* Create a Java ONLY example to ensure that the Kotlin Dependency isn't required for non-android usage.

* Dependency Guard
  • Loading branch information
handstandsam authored Oct 3, 2022
1 parent 7639bd5 commit d45dd9e
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
run: ./gradlew check

- name: Run Integration Tests for Examples
run: ./gradlew :examples:examples:test -Pci=true --info
run: ./gradlew :examples:examples:test :examples:java:test -Pci=true --info

- name: Run Integration Tests - OkHttpRequestor
run: ./gradlew -Pcom.dropbox.test.httpRequestor=OkHttpRequestor -Pcom.dropbox.test.authInfoFile=../auth_output integrationTest &&
Expand Down
6 changes: 3 additions & 3 deletions dropbox-sdk-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ sourceSets {
}

dependencies {
// Important: Jackson 2.8+ will be free to use JDK7 features and no longer guarantees JDK6
// compatibility
api(dropboxJavaSdkLibs.jackson.core)

// Android
compileOnly 'com.google.android:android:4.1.1.4' // Until 6.x when we have an Android Artifact
compileOnly(dropboxJavaSdkLibs.kotlin.stdlib) // Only used in Android Code

compileOnly(dropboxJavaSdkLibs.servlet.api)
compileOnly(dropboxJavaSdkLibs.okhttp2) // support both v2 and v3 to avoid
compileOnly(dropboxJavaSdkLibs.okhttp3) // method count bloat
compileOnly(dropboxJavaSdkLibs.appengine.api)
compileOnly(dropboxJavaSdkLibs.kotlin.stdlib)

testImplementation 'org.testng:testng:6.9.10'
testImplementation 'org.mockito:mockito-core:1.10.19'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ object CredentialsUtil {
return DbxCredential(accessToken)
}

/**
* JSON file that is generated at auth_output when the ./generate-ci-auth-file script is run
* with the required parameters. This is read in CI during integration tests.
*/
private val authOutputFile = File("../../auth_output")

fun getDbxCredential(): DbxCredential {
Expand Down
30 changes: 30 additions & 0 deletions examples/java/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
plugins {
id("java")
id("com.dropbox.dependency-guard")
}

dependencyGuard {
configuration("compileClasspath")
configuration("runtimeClasspath")
}

description = 'Java Examples'

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

dependencies {
implementation(project(":dropbox-sdk-java"))

testImplementation(dropboxJavaSdkLibs.test.junit)
}

test {
// Uses the "ci" gradle property and sets it as an environment variable for the test
environment("ci", project.findProperty("ci"))

// Ensure that tests are always run because integration tests are non-deterministic
outputs.upToDateWhen { false }
}
1 change: 1 addition & 0 deletions examples/java/dependencies/compileClasspath.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.fasterxml.jackson.core:jackson-core:2.7.9
1 change: 1 addition & 0 deletions examples/java/dependencies/runtimeClasspath.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.fasterxml.jackson.core:jackson-core:2.7.9
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.dropbox.core.examples;

import com.dropbox.core.DbxAuthInfo;
import com.dropbox.core.json.JsonReader;
import com.dropbox.core.oauth.DbxCredential;

import java.io.File;

public final class CredentialsUtil {
/**
* JSON file that is generated at auth_output when the ./generate-ci-auth-file script is run
* with the required parameters. This is read in CI during integration tests.
*/
public static final File authOutputFile = new File("../../auth_output");

public static DbxCredential getDbxCredential() {
try {
return DbxCredential.Reader.readFromFile(authOutputFile);
} catch (JsonReader.FileLoadException e) {
throw new RuntimeException(e);
}
}

public final DbxAuthInfo getAuthInfo() {
try {
return DbxAuthInfo.Reader.readFromFile(authOutputFile);
} catch (JsonReader.FileLoadException e) {
throw new RuntimeException(e);
}
}

public static final class OAuth2TokenInputs {
public final String appKey;
public final String appSecret;
public final String refreshToken;

public OAuth2TokenInputs(String appKey, String appSecret, String refreshToken) {
this.appKey = appKey;
this.appSecret = appSecret;
this.refreshToken = refreshToken;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.dropbox.core.examples.account_info;

import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.examples.CredentialsUtil;
import com.dropbox.core.oauth.DbxCredential;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.users.FullAccount;


public final class AccountInfoExample {

public static void main(String[] args) {
DbxCredential credential = CredentialsUtil.getDbxCredential();
new AccountInfoExample().runExample(credential);
}

public void runExample(DbxCredential credential) {
DbxRequestConfig requestConfig = new DbxRequestConfig("examples-account-info");
DbxClientV2 dbxClient = new DbxClientV2(requestConfig, credential);

try {
FullAccount dbxAccountInfo = dbxClient.users().getCurrentAccount();
System.out.println(dbxAccountInfo.toStringMultiline());
} catch (DbxException ex) {
throw new RuntimeException("Error making API call: " + ex.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.dropbox.core.examples.account_info;

import static org.junit.Assume.assumeTrue;

import com.dropbox.core.examples.CredentialsUtil;

import org.junit.Before;
import org.junit.Test;

public class AccountInfoExampleTest {

@Before
public void setup() {
// Only run if this is running in CI
Boolean isRunningInCI = System.getenv("ci").equals("true");
assumeTrue(isRunningInCI);
}

@Test
public void accountInfo() {
new AccountInfoExample().runExample(CredentialsUtil.getDbxCredential());
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include ':proguard'
include ':dropbox-sdk-java'
include ':examples:android'
include ':examples:examples'
include ':examples:java'

dependencyResolutionManagement {
versionCatalogs {
Expand Down

0 comments on commit d45dd9e

Please sign in to comment.