-
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
323 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...roidTester/app/src/test/java/io/sentry/react/RNSentryCompositeOptionsConfigurationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.sentry.react | ||
|
||
import io.sentry.Sentry.OptionsConfiguration | ||
import io.sentry.android.core.SentryAndroidOptions | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.verify | ||
|
||
@RunWith(JUnit4::class) | ||
class RNSentryCompositeOptionsConfigurationTest { | ||
@Test | ||
fun `configure should call base and overriding configurations`() { | ||
val baseConfig: OptionsConfiguration<SentryAndroidOptions> = mock() | ||
val overridingConfig: OptionsConfiguration<SentryAndroidOptions> = mock() | ||
|
||
val compositeConfig = RNSentryCompositeOptionsConfiguration(baseConfig, overridingConfig) | ||
val options = SentryAndroidOptions() | ||
compositeConfig.configure(options) | ||
|
||
verify(baseConfig).configure(options) | ||
verify(overridingConfig).configure(options) | ||
} | ||
|
||
@Test | ||
fun `configure should apply base configuration and override values`() { | ||
val baseConfig = | ||
OptionsConfiguration<SentryAndroidOptions> { options -> | ||
options.dsn = "https://[email protected]" | ||
options.isDebug = false | ||
options.release = "some-release" | ||
} | ||
val overridingConfig = | ||
OptionsConfiguration<SentryAndroidOptions> { options -> | ||
options.dsn = "https://[email protected]" | ||
options.isDebug = true | ||
options.environment = "production" | ||
} | ||
|
||
val compositeConfig = RNSentryCompositeOptionsConfiguration(baseConfig, overridingConfig) | ||
val options = SentryAndroidOptions() | ||
compositeConfig.configure(options) | ||
|
||
assert(options.dsn == "https://[email protected]") // overridden value | ||
assert(options.isDebug) // overridden value | ||
assert(options.release == "some-release") // base value not overridden | ||
assert(options.environment == "production") // overridden value not in base | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ges/core/android/src/main/java/io/sentry/react/RNSentryCompositeOptionsConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.sentry.react; | ||
|
||
import io.sentry.Sentry.OptionsConfiguration; | ||
import io.sentry.android.core.SentryAndroidOptions; | ||
import java.util.List; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
class RNSentryCompositeOptionsConfiguration implements OptionsConfiguration<SentryAndroidOptions> { | ||
private final @NotNull List<OptionsConfiguration<SentryAndroidOptions>> configurations; | ||
|
||
@SafeVarargs | ||
protected RNSentryCompositeOptionsConfiguration( | ||
@NotNull OptionsConfiguration<SentryAndroidOptions>... configurations) { | ||
this.configurations = List.of(configurations); | ||
} | ||
|
||
@Override | ||
public void configure(@NotNull SentryAndroidOptions options) { | ||
for (OptionsConfiguration<SentryAndroidOptions> configuration : configurations) { | ||
if (configuration != null) { | ||
configuration.configure(options); | ||
} | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/core/android/src/main/java/io/sentry/react/RNSentryJsonUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.sentry.react; | ||
|
||
import android.content.Context; | ||
import io.sentry.ILogger; | ||
import io.sentry.SentryLevel; | ||
import java.io.BufferedReader; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.json.JSONObject; | ||
|
||
final class RNSentryJsonUtils { | ||
private RNSentryJsonUtils() { | ||
throw new AssertionError("Utility class should not be instantiated"); | ||
} | ||
|
||
static @Nullable JSONObject getOptionsFromConfigurationFile( | ||
@NotNull Context context, @NotNull String fileName, @NotNull ILogger logger) { | ||
try (InputStream inputStream = context.getAssets().open(fileName); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { | ||
|
||
StringBuilder stringBuilder = new StringBuilder(); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
stringBuilder.append(line); | ||
} | ||
String configFileContent = stringBuilder.toString(); | ||
return new JSONObject(configFileContent); | ||
|
||
} catch (Exception e) { | ||
logger.log( | ||
SentryLevel.ERROR, | ||
"Failed to read configuration file. Please make sure " | ||
+ fileName | ||
+ " exists in the root of your project.", | ||
e); | ||
return null; | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/core/android/src/main/java/io/sentry/react/RNSentrySDK.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.sentry.react; | ||
|
||
import android.content.Context; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import io.sentry.ILogger; | ||
import io.sentry.Sentry; | ||
import io.sentry.SentryLevel; | ||
import io.sentry.android.core.AndroidLogger; | ||
import io.sentry.android.core.SentryAndroidOptions; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.json.JSONObject; | ||
|
||
public final class RNSentrySDK { | ||
private static final String CONFIGURATION_FILE = "sentry.options.json"; | ||
private static final String NAME = "RNSentrySDK"; | ||
|
||
private static final ILogger logger = new AndroidLogger(NAME); | ||
|
||
private RNSentrySDK() { | ||
throw new AssertionError("Utility class should not be instantiated"); | ||
} | ||
|
||
/** | ||
* Start the Native Android SDK with the provided options | ||
* | ||
* @param context Android Context | ||
* @param configuration configuration options | ||
* @param logger logger | ||
*/ | ||
public static void init( | ||
@NotNull final Context context, | ||
@NotNull Sentry.OptionsConfiguration<SentryAndroidOptions> configuration, | ||
@NotNull ILogger logger) { | ||
try { | ||
JSONObject jsonObject = | ||
RNSentryJsonUtils.getOptionsFromConfigurationFile(context, CONFIGURATION_FILE, logger); | ||
if (jsonObject == null) { | ||
RNSentryStart.startWithConfiguration(context, configuration); | ||
return; | ||
} | ||
ReadableMap rnOptions = RNSentryJsonConverter.convertToWritable(jsonObject); | ||
if (rnOptions == null) { | ||
RNSentryStart.startWithConfiguration(context, configuration); | ||
return; | ||
} | ||
RNSentryStart.startWithOptions(context, rnOptions, configuration, logger); | ||
} catch (Exception e) { | ||
logger.log( | ||
SentryLevel.ERROR, "Failed to start Sentry with options from configuration file.", e); | ||
throw new RuntimeException("Failed to initialize Sentry's React Native SDK", e); | ||
} | ||
} | ||
|
||
/** | ||
* Start the Native Android SDK with the provided options | ||
* | ||
* @param context Android Context | ||
* @param configuration configuration options | ||
*/ | ||
public static void init( | ||
@NotNull final Context context, | ||
@NotNull Sentry.OptionsConfiguration<SentryAndroidOptions> configuration) { | ||
init(context, configuration, logger); | ||
} | ||
|
||
/** | ||
* Start the Native Android SDK with options from `sentry.options.json` configuration file | ||
* | ||
* @param context Android Context | ||
*/ | ||
public static void init(@NotNull final Context context) { | ||
init(context, options -> {}, logger); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.