Skip to content

Commit 917daec

Browse files
authored
Factory link (#43)
1 parent 73d7e41 commit 917daec

File tree

13 files changed

+164
-28
lines changed

13 files changed

+164
-28
lines changed

android/src/main/java/io/split/splitio/SplitFactoryProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import io.split.android.client.SplitFactory;
44

5-
interface SplitFactoryProvider {
5+
public interface SplitFactoryProvider {
66

77
SplitFactory getSplitFactory();
88
}

android/src/main/java/io/split/splitio/SplitMethodParserImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ class SplitMethodParserImpl implements SplitMethodParser {
6363
private final MethodChannel mMethodChannel;
6464
private final SplitProviderHelper mProviderHelper;
6565

66-
public SplitMethodParserImpl(@NonNull Context context, MethodChannel channel) {
66+
public SplitMethodParserImpl(@NonNull Context context, @NonNull MethodChannel channel, @Nullable SplitFactoryProvider splitFactoryProvider) {
6767
mContext = context;
6868
mArgumentParser = new ArgumentParserImpl();
6969
mMethodChannel = channel;
70-
mProviderHelper = new SplitProviderHelperImpl();
70+
mProviderHelper = new SplitProviderHelperImpl(splitFactoryProvider);
7171
}
7272

7373
@VisibleForTesting

android/src/main/java/io/split/splitio/SplitProviderHelper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@ SplitFactoryProvider getProvider(
1818

1919
class SplitProviderHelperImpl implements SplitProviderHelper {
2020

21+
@Nullable private final SplitFactoryProvider mSplitFactoryProvider;
22+
23+
SplitProviderHelperImpl(@Nullable SplitFactoryProvider splitFactoryProvider) {
24+
mSplitFactoryProvider = splitFactoryProvider;
25+
}
26+
2127
@Override
2228
public SplitFactoryProvider getProvider(Context context, String apiKey, String matchingKey, @Nullable String bucketingKey, SplitClientConfig splitClientConfig) {
29+
if (mSplitFactoryProvider != null) {
30+
return mSplitFactoryProvider;
31+
}
32+
2333
return new SplitFactoryProviderImpl(context, apiKey, matchingKey, bucketingKey, splitClientConfig);
2434
}
2535
}

android/src/main/java/io/split/splitio/SplitioPlugin.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import static io.split.splitio.Constants.Error.METHOD_PARSER_NOT_INITIALIZED;
44

5+
import android.content.Context;
6+
57
import androidx.annotation.NonNull;
6-
import androidx.annotation.VisibleForTesting;
8+
import androidx.annotation.Nullable;
79

810
import io.flutter.embedding.engine.plugins.FlutterPlugin;
911
import io.flutter.plugin.common.MethodCall;
@@ -16,18 +18,24 @@
1618
* SplitioPlugin
1719
*/
1820
public class SplitioPlugin implements FlutterPlugin, MethodCallHandler {
19-
/// The MethodChannel that will the communication between Flutter and native Android
20-
///
21-
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
22-
/// when the Flutter Engine is detached from the Activity
2321
private MethodChannel channel;
2422
private SplitMethodParser methodParser;
2523

2624
@Override
2725
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
2826
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "splitio");
2927
channel.setMethodCallHandler(this);
30-
methodParser = new SplitMethodParserImpl(flutterPluginBinding.getApplicationContext(), channel);
28+
SplitFactoryProvider provider = getSplitFactoryProvider(flutterPluginBinding.getApplicationContext());
29+
methodParser = new SplitMethodParserImpl(flutterPluginBinding.getApplicationContext(), channel, provider);
30+
}
31+
32+
@Nullable
33+
private static SplitFactoryProvider getSplitFactoryProvider(Context applicationContext) {
34+
try {
35+
return (SplitFactoryProvider) applicationContext;
36+
} catch (ClassCastException exception) {
37+
return null;
38+
}
3139
}
3240

3341
@Override

android/src/test/java/io/split/splitio/ArgumentParserImplTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNotNull;
56
import static org.junit.Assert.assertNull;
67
import static org.junit.Assert.assertTrue;
78

@@ -108,4 +109,44 @@ public void testGetListArgumentFailure() {
108109

109110
assertTrue(listArgument.isEmpty());
110111
}
112+
113+
@Test
114+
public void testGetDoubleArgument() {
115+
Map<String, Object> arguments = new HashMap<>();
116+
arguments.put("doubleArgument", 25.25);
117+
118+
Double doubleArgument = mArgumentParser.getDoubleArgument("doubleArgument", arguments);
119+
120+
assertEquals(new Double(25.25), doubleArgument);
121+
}
122+
123+
@Test
124+
public void testGetDoubleArgumentFailure() {
125+
Map<String, Object> arguments = new HashMap<>();
126+
arguments.put("doubleArgument", "error");
127+
128+
Double doubleArgument = mArgumentParser.getDoubleArgument("doubleArgument", arguments);
129+
130+
assertNull(doubleArgument);
131+
}
132+
133+
@Test
134+
public void testGetObjectArgument() {
135+
Map<String, Object> arguments = new HashMap<>();
136+
arguments.put("objectArgument", "error");
137+
138+
Object objectArgument = mArgumentParser.getObjectArgument("objectArgument", arguments);
139+
140+
assertNotNull(objectArgument);
141+
}
142+
143+
@Test
144+
public void testGetObjectArgumentFailure() {
145+
Map<String, Object> arguments = new HashMap<>();
146+
arguments.put("objectArgument", null);
147+
148+
Object objectArgument = mArgumentParser.getObjectArgument("objectArgument", arguments);
149+
150+
assertNull(objectArgument);
151+
}
111152
}

android/src/test/java/io/split/splitio/SplitMethodParserImplTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public class SplitMethodParserImplTest {
3434
@Mock
3535
private MethodChannel mMethodChannel;
3636
@Mock
37-
private SplitProviderHelper mProviderFactory;
37+
private SplitProviderHelper mProviderHelper;
3838

3939
@Before
4040
public void setUp() {
4141
MockitoAnnotations.openMocks(this);
42-
mMethodParser = new SplitMethodParserImpl(mSplitWrapper, mArgumentParser, mMethodChannel, mProviderFactory);
42+
mMethodParser = new SplitMethodParserImpl(mSplitWrapper, mArgumentParser, mMethodChannel, mProviderHelper);
4343
}
4444

4545
@Test
@@ -60,7 +60,7 @@ public void successfulGetClient() {
6060

6161
@Test
6262
public void failingGetClient() {
63-
mMethodParser = new SplitMethodParserImpl(null, mArgumentParser, mMethodChannel, mProviderFactory);
63+
mMethodParser = new SplitMethodParserImpl(null, mArgumentParser, mMethodChannel, mProviderHelper);
6464

6565
Map<String, Object> map = new HashMap<>();
6666
map.put("matchingKey", "user-key");
@@ -465,11 +465,11 @@ public void initialization() {
465465
when(mArgumentParser.getMapArgument("sdkConfiguration", arguments)).thenReturn(configurationMap);
466466

467467
SplitFactoryProvider splitFactoryProvider = mock(SplitFactoryProvider.class);
468-
when(mProviderFactory.getProvider(any(), any(), any(), any(), any())).thenReturn(splitFactoryProvider);
468+
when(mProviderHelper.getProvider(any(), any(), any(), any(), any())).thenReturn(splitFactoryProvider);
469469

470470
mMethodParser.onMethodCall("init", arguments, mResult);
471471

472-
verify(mProviderFactory).getProvider(any(),
472+
verify(mProviderHelper).getProvider(any(),
473473
eq("key"),
474474
eq("matching-key"),
475475
eq("bucketing-key"),
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.split.splitio;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.mockito.Mockito.mock;
6+
7+
import org.junit.Test;
8+
9+
public class SplitProviderHelperImplTest {
10+
11+
private SplitProviderHelperImpl mHelper;
12+
13+
@Test
14+
public void helperBuildsItsOwnProviderWhenNoneIsSupplied() {
15+
mHelper = new SplitProviderHelperImpl(null);
16+
17+
SplitFactoryProvider provider = mHelper.getProvider(null, "", "", "", null);
18+
19+
assertNotNull(provider);
20+
}
21+
22+
@Test
23+
public void helperReturnsSuppliedProviderWhenExists() {
24+
SplitFactoryProvider factoryProvider = mock(SplitFactoryProvider.class);
25+
mHelper = new SplitProviderHelperImpl(factoryProvider);
26+
SplitFactoryProvider provider = mHelper.getProvider(null, "", "", "", null);
27+
28+
assertEquals(factoryProvider, provider);
29+
}
30+
}

example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
C51277632847F3D800B7D1A2 /* SplitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C51277622847F3D800B7D1A2 /* SplitTests.swift */; };
1919
C5538DE328A5483400EE141E /* ExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5538DE228A5483400EE141E /* ExtensionsTests.swift */; };
2020
C56515F82857B47E007D9112 /* SplitClientConfigHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C56515F72857B47E007D9112 /* SplitClientConfigHelperTests.swift */; };
21+
C5E9331728BE5433007A9A93 /* SplitProviderHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5E9331628BE5433007A9A93 /* SplitProviderHelperTests.swift */; };
2122
C5EAE748284FCC610025614D /* ArgumentParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5EAE747284FCC610025614D /* ArgumentParserTests.swift */; };
2223
C5EAE74C2853A38C0025614D /* SplitMethodParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5EAE74B2853A38C0025614D /* SplitMethodParserTests.swift */; };
2324
/* End PBXBuildFile section */
@@ -69,6 +70,7 @@
6970
C51277622847F3D800B7D1A2 /* SplitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitTests.swift; sourceTree = "<group>"; };
7071
C5538DE228A5483400EE141E /* ExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionsTests.swift; sourceTree = "<group>"; };
7172
C56515F72857B47E007D9112 /* SplitClientConfigHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitClientConfigHelperTests.swift; sourceTree = "<group>"; };
73+
C5E9331628BE5433007A9A93 /* SplitProviderHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitProviderHelperTests.swift; sourceTree = "<group>"; };
7274
C5EAE747284FCC610025614D /* ArgumentParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgumentParserTests.swift; sourceTree = "<group>"; };
7375
C5EAE74B2853A38C0025614D /* SplitMethodParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitMethodParserTests.swift; sourceTree = "<group>"; };
7476
CDE81357140D4D18CD672621 /* Pods_SplitTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SplitTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -167,6 +169,7 @@
167169
C51277612847F3D800B7D1A2 /* SplitTests */ = {
168170
isa = PBXGroup;
169171
children = (
172+
C5E9331628BE5433007A9A93 /* SplitProviderHelperTests.swift */,
170173
C5538DE228A5483400EE141E /* ExtensionsTests.swift */,
171174
C56515F72857B47E007D9112 /* SplitClientConfigHelperTests.swift */,
172175
C5EAE74B2853A38C0025614D /* SplitMethodParserTests.swift */,
@@ -405,6 +408,7 @@
405408
buildActionMask = 2147483647;
406409
files = (
407410
C5538DE328A5483400EE141E /* ExtensionsTests.swift in Sources */,
411+
C5E9331728BE5433007A9A93 /* SplitProviderHelperTests.swift in Sources */,
408412
C5EAE74C2853A38C0025614D /* SplitMethodParserTests.swift in Sources */,
409413
C56515F82857B47E007D9112 /* SplitClientConfigHelperTests.swift in Sources */,
410414
C51277632847F3D800B7D1A2 /* SplitTests.swift in Sources */,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import XCTest
2+
@testable import splitio
3+
@testable import Split
4+
5+
class SplitProviderHelperTests: XCTestCase {
6+
7+
private var helper: DefaultSplitProviderHelper?
8+
9+
func testHelperBuildsItsOwnProviderWhenNoneIsSupplied() throws {
10+
helper = DefaultSplitProviderHelper(splitFactoryProvider: nil)
11+
let provider = helper?.getProvider(apiKey: "", matchingKey: "", bucketingKey: "", splitClientConfig: SplitClientConfig())
12+
13+
XCTAssertNotNil(provider)
14+
}
15+
16+
func testHelperReturnsSuppliedProviderWhenExists() throws {
17+
let factoryProvider: SplitFactoryProviderStub? = SplitFactoryProviderStub()
18+
helper = DefaultSplitProviderHelper(splitFactoryProvider: factoryProvider)
19+
let provider: SplitFactoryProviderStub? = helper?.getProvider(apiKey: "", matchingKey: "", bucketingKey: "", splitClientConfig: SplitClientConfig()) as? SplitFactoryProviderStub
20+
21+
XCTAssert(factoryProvider?.uuid == provider?.uuid)
22+
}
23+
}

example/ios/SplitTests/SplitTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ class SplitTests: XCTestCase {
185185

186186
class SplitFactoryProviderStub: SplitFactoryProvider {
187187

188+
// For testing purposes only
189+
let uuid: Int = Int.random(in: 0..<1000)
190+
188191
var manager: SplitManagerStub?
189192

190193
init(manager: SplitManagerStub?) {
@@ -206,6 +209,9 @@ class SplitFactoryProviderStub: SplitFactoryProvider {
206209

207210
class SplitFactoryProviderStubWithClient: SplitFactoryProvider {
208211

212+
// For testing purposes only
213+
let uuid: Int = Int.random(in: 0..<1000)
214+
209215
let client: SplitClientStub
210216

211217
init(client: SplitClientStub) {

0 commit comments

Comments
 (0)