Skip to content

Commit ccc36ab

Browse files
authored
fix: Using Java 8 in release and nightly builds (#573)
* fix: Using Java 8 in release and nightly builds * fix: Removing env variable manipulation from tests * Downgraded to mockito 2; Using wrapper classes to mock ADC
1 parent 39ebc14 commit ccc36ab

18 files changed

+236
-205
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ jobs:
1919
build:
2020
runs-on: ubuntu-latest
2121

22+
strategy:
23+
matrix:
24+
java-version: [7, 8, 11]
25+
2226
steps:
2327
- uses: actions/checkout@v1
2428

25-
- name: Set up JDK 1.7
29+
- name: Set up JDK
2630
uses: actions/setup-java@v1
2731
with:
28-
java-version: 1.7
32+
java-version: ${{ matrix.java-version }}
2933

3034
# Does the following:
3135
# 1. Runs the Checkstyle plugin (validate phase)

.github/workflows/nightly.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ jobs:
3333
with:
3434
ref: ${{ github.event.client_payload.ref || github.ref }}
3535

36-
- name: Set up JDK 1.7
36+
- name: Set up JDK 1.8
3737
uses: actions/setup-java@v1
3838
with:
39-
java-version: 1.7
39+
java-version: 1.8
4040

4141
- name: Compile, test and package
4242
run: ./.github/scripts/package_artifacts.sh
@@ -63,7 +63,7 @@ jobs:
6363
subject: 'Nightly build ${{github.run_id}} of ${{github.repository}} failed!'
6464
html: >
6565
<b>Nightly workflow ${{github.run_id}} failed on: ${{github.repository}}</b>
66-
<br /><br />Navigate to the
66+
<br /><br />Navigate to the
6767
<a href="https://github.com/firebase/firebase-admin-java/actions/runs/${{github.run_id}}">failed workflow</a>.
6868
continue-on-error: true
6969

@@ -78,6 +78,6 @@ jobs:
7878
subject: 'Nightly build ${{github.run_id}} of ${{github.repository}} cancelled!'
7979
html: >
8080
<b>Nightly workflow ${{github.run_id}} cancelled on: ${{github.repository}}</b>
81-
<br /><br />Navigate to the
81+
<br /><br />Navigate to the
8282
<a href="https://github.com/firebase/firebase-admin-java/actions/runs/${{github.run_id}}">cancelled workflow</a>.
8383
continue-on-error: true

.github/workflows/release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ jobs:
4444
with:
4545
ref: ${{ github.event.client_payload.ref || github.ref }}
4646

47-
- name: Set up JDK 1.7
47+
- name: Set up JDK 1.8
4848
uses: actions/setup-java@v1
4949
with:
50-
java-version: 1.7
50+
java-version: 1.8
5151

5252
- name: Compile, test and package
5353
run: ./.github/scripts/package_artifacts.sh
@@ -82,10 +82,10 @@ jobs:
8282
- name: Checkout source for publish
8383
uses: actions/checkout@v2
8484

85-
- name: Set up JDK 1.7
85+
- name: Set up JDK 1.8
8686
uses: actions/setup-java@v1
8787
with:
88-
java-version: 1.7
88+
java-version: 1.8
8989

9090
- name: Publish preflight check
9191
id: preflight

src/main/java/com/google/firebase/FirebaseApp.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.common.base.Strings;
3636
import com.google.common.collect.ImmutableList;
3737
import com.google.firebase.internal.ApiClientUtils;
38+
import com.google.firebase.internal.FirebaseProcessEnvironment;
3839
import com.google.firebase.internal.FirebaseScheduledExecutor;
3940
import com.google.firebase.internal.FirebaseService;
4041
import com.google.firebase.internal.ListenableFuture2ApiFuture;
@@ -292,10 +293,10 @@ String getProjectId() {
292293

293294
// Try to get project ID from the environment.
294295
if (Strings.isNullOrEmpty(projectId)) {
295-
projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
296+
projectId = FirebaseProcessEnvironment.getenv("GOOGLE_CLOUD_PROJECT");
296297
}
297298
if (Strings.isNullOrEmpty(projectId)) {
298-
projectId = System.getenv("GCLOUD_PROJECT");
299+
projectId = FirebaseProcessEnvironment.getenv("GCLOUD_PROJECT");
299300
}
300301
return projectId;
301302
}
@@ -563,7 +564,7 @@ enum State {
563564
}
564565

565566
private static FirebaseOptions getOptionsFromEnvironment() throws IOException {
566-
String defaultConfig = System.getenv(FIREBASE_CONFIG_ENV_VAR);
567+
String defaultConfig = FirebaseProcessEnvironment.getenv(FIREBASE_CONFIG_ENV_VAR);
567568
if (Strings.isNullOrEmpty(defaultConfig)) {
568569
return FirebaseOptions.builder()
569570
.setCredentials(APPLICATION_DEFAULT_CREDENTIALS)

src/main/java/com/google/firebase/FirebaseOptions.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.common.base.Suppliers;
3030
import com.google.common.collect.ImmutableList;
3131
import com.google.firebase.internal.ApiClientUtils;
32+
import com.google.firebase.internal.ApplicationDefaultCredentialsProvider;
3233
import com.google.firebase.internal.FirebaseThreadManagers;
3334
import com.google.firebase.internal.NonNull;
3435
import com.google.firebase.internal.Nullable;
@@ -64,7 +65,8 @@ public final class FirebaseOptions {
6465
@Override
6566
public GoogleCredentials get() {
6667
try {
67-
return GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES);
68+
return ApplicationDefaultCredentialsProvider.getApplicationDefault()
69+
.createScoped(FIREBASE_SCOPES);
6870
} catch (IOException e) {
6971
throw new IllegalStateException(e);
7072
}

src/main/java/com/google/firebase/auth/internal/Utils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.annotations.VisibleForTesting;
2020
import com.google.common.base.Strings;
21+
import com.google.firebase.internal.FirebaseProcessEnvironment;
2122

2223
public class Utils {
2324
@VisibleForTesting
@@ -28,7 +29,7 @@ public static boolean isEmulatorMode() {
2829
}
2930

3031
public static String getEmulatorHost() {
31-
return System.getenv(AUTH_EMULATOR_HOST);
32+
return FirebaseProcessEnvironment.getenv(AUTH_EMULATOR_HOST);
3233
}
3334

3435
}

src/main/java/com/google/firebase/database/util/EmulatorHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.firebase.database.core.RepoInfo;
2323
import com.google.firebase.database.utilities.ParsedUrl;
2424
import com.google.firebase.database.utilities.Utilities;
25+
import com.google.firebase.internal.FirebaseProcessEnvironment;
2526

2627
public final class EmulatorHelper {
2728

@@ -33,7 +34,7 @@ private EmulatorHelper() {
3334
"FIREBASE_DATABASE_EMULATOR_HOST";
3435

3536
public static String getEmulatorHostFromEnv() {
36-
return System.getenv(FIREBASE_RTDB_EMULATOR_HOST_ENV_VAR);
37+
return FirebaseProcessEnvironment.getenv(FIREBASE_RTDB_EMULATOR_HOST_ENV_VAR);
3738
}
3839

3940
@VisibleForTesting
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2021 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.internal;
18+
19+
import com.google.auth.oauth2.GoogleCredentials;
20+
import java.io.IOException;
21+
22+
/**
23+
* Provides a hook to override application default credentials (ADC) lookup for tests. ADC has
24+
* a dependency on environment variables, and Java famously doesn't support environment variable
25+
* manipulation at runtime. With this class, the test cases that require ADC has a way to register
26+
* their own mock credentials as ADC.
27+
*
28+
* <p>Once we are able to upgrade to Mockito 3.x (requires Java 8+), we can drop this class
29+
* altogether, and use Mockito tools to mock the behavior of the GoogleCredentials static methods.
30+
*/
31+
public class ApplicationDefaultCredentialsProvider {
32+
33+
private static GoogleCredentials cachedCredentials;
34+
35+
public static GoogleCredentials getApplicationDefault() throws IOException {
36+
if (cachedCredentials != null) {
37+
return cachedCredentials;
38+
}
39+
40+
return GoogleCredentials.getApplicationDefault();
41+
}
42+
43+
public static void setApplicationDefault(GoogleCredentials credentials) {
44+
cachedCredentials = credentials;
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2021 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.internal;
18+
19+
import com.google.common.base.Strings;
20+
import java.util.Map;
21+
import java.util.concurrent.ConcurrentHashMap;
22+
23+
/**
24+
* A utility for overriding environment variables during tests.
25+
*/
26+
public class FirebaseProcessEnvironment {
27+
28+
private static final Map<String, String> localCache = new ConcurrentHashMap<>();
29+
30+
public static String getenv(String name) {
31+
String cachedValue = localCache.get(name);
32+
if (!Strings.isNullOrEmpty(cachedValue)) {
33+
return cachedValue;
34+
}
35+
36+
return System.getenv(name);
37+
}
38+
39+
public static void setenv(String name, String value) {
40+
localCache.put(name, value);
41+
}
42+
43+
public static void clearCache() {
44+
localCache.clear();
45+
}
46+
}

0 commit comments

Comments
 (0)