Skip to content

Commit 4d66c26

Browse files
committed
added support for treating shorter scannable codes as ean 13
1 parent b757b95 commit 4d66c26

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed
0 Bytes
Binary file not shown.

core/src/androidTest/java/io/snabble/sdk/EAN13Test.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.snabble.sdk;
22

3-
import junit.framework.Assert;
4-
3+
import org.junit.Assert;
54
import org.junit.Test;
65

76
import io.snabble.sdk.codes.EAN13;

core/src/androidTest/java/io/snabble/sdk/ProductDatabaseTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import android.support.test.runner.AndroidJUnit4;
88

99
import org.apache.commons.io.IOUtils;
10+
import org.apache.commons.lang3.ArrayUtils;
11+
import org.junit.Assert;
1012
import org.junit.Test;
1113
import org.junit.runner.RunWith;
1214

@@ -340,4 +342,34 @@ public void testFullUpdateDoesNotModifyOnWrongMajorVersion() throws IOException
340342
product = productDatabase.findBySku("1");
341343
assertEquals(name, product.getName());
342344
}
345+
346+
@Test
347+
public void testFindProductWithByEan8WhenScanningEan13() throws IOException, Snabble.SnabbleException {
348+
setupSdkWithDb("demoDb_1_6.sqlite3");
349+
350+
ProductDatabase productDatabase = project.getProductDatabase();
351+
Product product = productDatabase.findByCode("42276630");
352+
Assert.assertNotNull(product);
353+
354+
String[] codes = product.getScannableCodes();
355+
Assert.assertTrue(ArrayUtils.contains(codes, "42276630"));
356+
357+
Product product2 = productDatabase.findByCode("0000042276630");
358+
Assert.assertNotNull(product2);
359+
360+
String[] codes2 = product2.getScannableCodes();
361+
Assert.assertTrue(ArrayUtils.contains(codes2, "42276630"));
362+
}
363+
364+
@Test
365+
public void testFindProductWithEan13ByEan8() throws IOException, Snabble.SnabbleException {
366+
setupSdkWithDb("demoDb_1_6.sqlite3");
367+
368+
ProductDatabase productDatabase = project.getProductDatabase();
369+
Product product = productDatabase.findByCode("40084015");
370+
Assert.assertNotNull(product);
371+
372+
String[] codes = product.getScannableCodes();
373+
Assert.assertTrue(ArrayUtils.contains(codes, "0000040084015"));
374+
}
343375
}

core/src/androidTest/java/io/snabble/sdk/SnabbleSdkTest.java

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

1919
import java.io.File;
2020
import java.io.IOException;
21-
import java.nio.charset.StandardCharsets;
21+
import java.nio.charset.Charset;
2222
import java.util.concurrent.CountDownLatch;
2323
import java.util.concurrent.TimeUnit;
2424

@@ -44,7 +44,7 @@ public static void setupMockWebServer() throws Exception {
4444
mockWebServer = new MockWebServer();
4545
mockWebServer.start();
4646

47-
final String metadataJson = IOUtils.toString(context.getAssets().open("metadata.json"), StandardCharsets.UTF_8);
47+
final String metadataJson = IOUtils.toString(context.getAssets().open("metadata.json"), Charset.forName("UTF-8"));
4848

4949
final Buffer product1Buffer = new Buffer();
5050
product1Buffer.readFrom(context.getAssets().open("product.json"));

core/src/main/java/io/snabble/sdk/ProductDatabase.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import org.apache.commons.io.FileUtils;
1818
import org.apache.commons.io.IOUtils;
19+
import org.apache.commons.lang3.StringUtils;
1920

2021
import java.io.File;
2122
import java.io.FileOutputStream;
@@ -1033,6 +1034,10 @@ public Product[] findBySkus(String... skus) {
10331034
* @return The first product containing the given EAN, otherwise null if no product was found.
10341035
*/
10351036
public Product findByCode(String code) {
1037+
return findByCodeInternal(code, true);
1038+
}
1039+
1040+
private Product findByCodeInternal(String code, boolean recursive) {
10361041
if (code == null || code.length() == 0) {
10371042
return null;
10381043
}
@@ -1046,11 +1051,16 @@ public Product findByCode(String code) {
10461051

10471052
if (p != null) {
10481053
return p;
1049-
} else if (code.startsWith("0")){
1050-
return findByCode(code.substring(1, code.length()));
1051-
} else {
1052-
return null;
1054+
} else if (recursive) {
1055+
if (code.startsWith("0")){
1056+
return findByCodeInternal(code.substring(1, code.length()), true);
1057+
} else if (code.length() >= 8 && code.length() < 13) {
1058+
String newCode = StringUtils.repeat('0', 13 - code.length()) + code;
1059+
return findByCodeInternal(newCode, false);
1060+
}
10531061
}
1062+
1063+
return null;
10541064
}
10551065

10561066
/**

0 commit comments

Comments
 (0)