Skip to content

Commit ba31808

Browse files
authored
fix: throws AlreadyConfiguredException when configured == true (#1274)
1 parent 8b782e7 commit ba31808

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

core/src/main/java/com/amplifyframework/core/Amplify.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import com.amplifyframework.util.UserAgent;
3838

3939
import java.util.LinkedHashMap;
40-
import java.util.Locale;
4140
import java.util.Map;
4241
import java.util.Objects;
4342
import java.util.concurrent.ExecutorService;
@@ -137,7 +136,7 @@ public static void configure(@NonNull final AmplifyConfiguration configuration,
137136

138137
synchronized (CONFIGURATION_LOCK) {
139138
if (CONFIGURATION_LOCK.get()) {
140-
throw new AlreadyConfiguredException();
139+
throw new AlreadyConfiguredException("Remove the duplicate call to `Amplify.configure()`.");
141140
}
142141

143142
// Configure User-Agent utility
@@ -192,11 +191,7 @@ private static <P extends Plugin<?>> void updatePluginRegistry(
192191

193192
synchronized (CONFIGURATION_LOCK) {
194193
if (CONFIGURATION_LOCK.get()) {
195-
final String updateString = registryUpdateType.name().toLowerCase(Locale.US);
196-
throw new AmplifyException(
197-
"The client tried to " + updateString + " a plugin after calling configure().",
198-
"Plugins may not be added or removed after configure(...) is called."
199-
);
194+
throw new AlreadyConfiguredException("Do not add plugins after calling `Amplify.configure()`.");
200195
}
201196

202197
if (Empty.check(plugin.getPluginKey())) {
@@ -246,8 +241,8 @@ public static final class AlreadyConfiguredException extends AmplifyException {
246241
/**
247242
* Constructs an AlreadyConfiguredException, indicating that Amplify has already been configured.
248243
*/
249-
private AlreadyConfiguredException() {
250-
super("Amplify has already been configured.", "Remove the duplicate call to `Amplify.configure()`");
244+
private AlreadyConfiguredException(@NonNull String recoverySuggestion) {
245+
super("Amplify has already been configured.", recoverySuggestion);
251246
}
252247
}
253248
}

core/src/test/java/com/amplifyframework/core/AmplifyTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.os.Build;
2020

2121
import com.amplifyframework.AmplifyException;
22+
import com.amplifyframework.core.plugin.Plugin;
2223

2324
import org.json.JSONObject;
2425
import org.junit.FixMethodOrder;
@@ -90,8 +91,32 @@ public void reconfigurationAttemptRaisesException() throws AmplifyException {
9091
actuallyThrown.getMessage()
9192
);
9293
assertEquals(
93-
"Remove the duplicate call to `Amplify.configure()`",
94+
"Remove the duplicate call to `Amplify.configure()`.",
9495
actuallyThrown.getRecoverySuggestion()
9596
);
9697
}
98+
99+
/**
100+
* It is an error to call {@link Amplify#addPlugin(Plugin)}} after configuration.
101+
* @throws Amplify.AlreadyConfiguredException
102+
* NOTE: The name of this method must result in it running last, according to {@link FixMethodOrder}
103+
*/
104+
@Test
105+
public void secondaryAddPluginAfterConfigurationRaisesException() throws Amplify.AlreadyConfiguredException {
106+
final SimpleLoggingPlugin loggingPlugin = SimpleLoggingPlugin.instance();
107+
Amplify.AlreadyConfiguredException actuallyThrown =
108+
assertThrows(
109+
Amplify.AlreadyConfiguredException.class,
110+
() -> Amplify.addPlugin(loggingPlugin)
111+
);
112+
113+
assertEquals(
114+
"Amplify has already been configured.",
115+
actuallyThrown.getMessage()
116+
);
117+
assertEquals(
118+
"Do not add plugins after calling `Amplify.configure()`.",
119+
actuallyThrown.getRecoverySuggestion()
120+
);
121+
}
97122
}

0 commit comments

Comments
 (0)