Skip to content

Commit a4b1b63

Browse files
Merge pull request #21 from WURFL/2.1.6
2.1.6
2 parents ee50f4f + 5fc8615 commit a4b1b63

File tree

8 files changed

+59
-32
lines changed

8 files changed

+59
-32
lines changed

example/pom.xml

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

77
<groupId>com.scientiamobile.wurfl</groupId>
88
<artifactId>wmclient-example</artifactId>
9-
<version>2.1.5</version>
9+
<version>2.1.6</version>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -48,7 +48,7 @@
4848
<dependency>
4949
<groupId>com.scientiamobile.wurflmicroservice</groupId>
5050
<artifactId>wurfl-microservice</artifactId>
51-
<version>2.1.5</version>
51+
<version>2.1.6</version>
5252
</dependency>
5353

5454
</dependencies>

wmclient/CHANGELOG.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2.1.6
2+
-------------------------------------
3+
IMPORTANT: dependency commons-lang (v2.6) has been replaced with commons-lang3 (v3.12.0).
4+
You shouldn't experience any difference between version 2.1.6 and 2.1.5
5+
- Some code refactoring and test improvement
6+
17
2.1.5
28
-------------------------------------
39
- Updated Gson to version 2.9.0 to solve vulnerability issue

wmclient/FOSS_THIRD_PARTY_LICENSES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Apache commons-io
66
Apache Software License Version 2.0
77
http://www.apache.org/licenses/LICENSE-2.0
88

9-
Apache commons-lang
9+
Apache commons-lang3
1010
Apache Software License Version 2.0
1111
http://www.apache.org/licenses/LICENSE-2.0
1212

wmclient/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.scientiamobile.wurflmicroservice</groupId>
66
<artifactId>wurfl-microservice</artifactId>
7-
<version>2.1.5</version>
7+
<version>2.1.6</version>
88
<packaging>jar</packaging>
99

1010
<name>wurfl-microservice</name>
@@ -68,9 +68,9 @@
6868
</dependency>
6969
<!-- Apache Commons Lang -->
7070
<dependency>
71-
<groupId>commons-lang</groupId>
72-
<artifactId>commons-lang</artifactId>
73-
<version>2.6</version>
71+
<groupId>org.apache.commons</groupId>
72+
<artifactId>commons-lang3</artifactId>
73+
<version>3.12.0</version>
7474
</dependency>
7575
<!-- Apache Commons IO -->
7676
<dependency>

wmclient/src/main/java/com/scientiamobile/wurfl/wmclient/LRUCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class LRUCache<K, E> {
2828
// We'll use this object to lock
2929
private final Object mutex;
3030

31-
private int size;
31+
private final int size;
3232

3333
private final ConcurrentHashMap<K, Node> cache;
3434
private Node head;

wmclient/src/main/java/com/scientiamobile/wurfl/wmclient/Model.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@
2424
*/
2525
public class Model {
2626

27+
private Model(){}
28+
static Model m = new Model();
29+
2730
// Factory methods
2831
static Request newRequest(Map<String, String> lookupHeaders, String[] requestedCaps, String[] requestedVcaps, String wurflId) {
29-
return new Model().new Request(lookupHeaders, requestedCaps, requestedVcaps, wurflId);
32+
return m.new Request(lookupHeaders, requestedCaps, requestedVcaps, wurflId);
3033
}
3134

3235
static JSONModelMktName newJSONModelMktName(String modelName, String mktName){
33-
return new Model().new JSONModelMktName(modelName, mktName);
36+
return m.new JSONModelMktName(modelName, mktName);
3437
}
3538

3639
/**
37-
* Holds informations about wurfl microservice server and API
40+
* Holds information about wurfl microservice server and API
3841
* <p>
3942
* Created by Andrea Castello on 19/07/2017.
4043
*/

wmclient/src/main/java/com/scientiamobile/wurfl/wmclient/WmClient.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import com.google.gson.Gson;
1919
import com.scientiamobile.wurfl.wmclient.Model.Request;
2020
import org.apache.commons.collections.CollectionUtils;
21-
import org.apache.commons.lang.ArrayUtils;
22-
import org.apache.commons.lang.StringUtils;
21+
import org.apache.commons.lang3.ArrayUtils;
22+
import org.apache.commons.lang3.StringUtils;
2323
import org.apache.http.HttpEntity;
2424
import org.apache.http.HttpResponse;
2525
import org.apache.http.client.ClientProtocolException;
@@ -591,7 +591,7 @@ public void setCacheSize(int uaMaxEntries) {
591591
* @return This client API version
592592
*/
593593
public String getApiVersion() {
594-
return "2.1.5";
594+
return "2.1.6";
595595
}
596596

597597
private void clearCaches() {
@@ -623,20 +623,22 @@ private void clearCachesIfNeeded(String ltime) {
623623
}
624624

625625
private String getUserAgentCacheKey(Map<String, String> headers, String cacheType) throws WmException {
626-
String key = "";
626+
StringBuilder key = new StringBuilder("");
627627

628628
if (headers == null && USERAGENT_CACHE_TYPE.equals(cacheType)) {
629629
throw new WmException("No User-Agent provided");
630630
}
631631

632632
// Using important headers array preserves header name order
633-
for (String h : importantHeaders) {
634-
String hval = headers.get(h);
635-
if (hval != null) {
636-
key += hval;
633+
if(headers!=null) {
634+
for (String h : importantHeaders) {
635+
String headerValue = headers.get(h);
636+
if (headerValue != null) {
637+
key.append(headerValue);
638+
}
637639
}
638640
}
639-
return key;
641+
return key.toString();
640642
}
641643

642644
private void safePutDevice(LRUCache<String, Model.JSONDeviceData> cache, String key, Model.JSONDeviceData device) {
@@ -662,7 +664,7 @@ public int[] getActualCacheSizes() {
662664

663665
class WmDataHandler<T> implements ResponseHandler<T> {
664666

665-
private Class<T> type;
667+
private final Class<T> type;
666668

667669
WmDataHandler(Class<T> type) {
668670
this.type = type;

wmclient/src/test/java/com/scientiamobile/wurfl/wmclient/WmClientTest.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import org.apache.commons.collections.MapUtils;
1919
import org.apache.commons.collections.iterators.EmptyIterator;
2020
import org.apache.commons.collections.iterators.IteratorEnumeration;
21-
import org.apache.commons.lang.StringUtils;
21+
import org.apache.commons.lang3.JavaVersion;
22+
import org.apache.commons.lang3.StringUtils;
23+
import org.apache.commons.lang3.SystemUtils;
2224
import org.testng.Assert;
2325
import org.testng.annotations.BeforeClass;
2426
import org.testng.annotations.Test;
@@ -50,6 +52,7 @@ public class WmClientTest {
5052

5153
@BeforeClass
5254
public void createTestClient() throws WmException {
55+
System.out.println("Running on Java version: " + SystemUtils.JAVA_VERSION);
5356
String host = "localhost";
5457
String port = "8080";
5558
String envHost = System.getenv("WM_HOST");
@@ -64,6 +67,12 @@ public void createTestClient() throws WmException {
6467

6568
}
6669

70+
@Test
71+
public void javaVersionTest(){
72+
boolean isJava8min = SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8);
73+
assertTrue(isJava8min, "Java 8 is the minimum version required to run WMClient and its tests");
74+
}
75+
6776

6877
public WmClient createCachedTestClient(int csize) throws WmException {
6978
String host = "localhost";
@@ -173,7 +182,6 @@ public void lookupUseragentEmptyUaTest() {
173182

174183
@Test
175184
public void testLookupUseragentNullUa() {
176-
boolean exc = false;
177185
try {
178186
Model.JSONDeviceData device = _client.lookupUseragent(null);
179187
assertNotNull(device);
@@ -754,6 +762,7 @@ public void realCacheUsageTest() throws WmException {
754762

755763
@Test
756764
public void realCacheUsageTest_2() throws WmException {
765+
757766
String host = "localhost";
758767
String port = "8080";
759768
String envHost = System.getenv("WM_HOST");
@@ -765,13 +774,20 @@ public void realCacheUsageTest_2() throws WmException {
765774
port = envPort;
766775
}
767776
WmClient client = WmClient.create("http", host, port, "");
777+
778+
// We create a set of 10K elements
779+
List<String> testUserAgents = new ArrayList<>();
780+
for(int i=0; i<100;i++){
781+
testUserAgents.addAll(Arrays.asList(TestData.USER_AGENTS));
782+
}
783+
768784
try {
769785
long start = System.nanoTime();
770-
for (String ua: TestData.USER_AGENTS){
786+
for (String ua: testUserAgents){
771787
client.lookupUseragent(ua);
772788
}
773789
long elapsedNoCache = System.nanoTime() - start;
774-
double avgNoCache = (double)elapsedNoCache/(double)TestData.USER_AGENTS.length;
790+
double avgNoCache = (double)elapsedNoCache/(double)testUserAgents.size();
775791

776792
// Now, let's add a cache layer
777793
client.setCacheSize(100000);
@@ -783,14 +799,14 @@ public void realCacheUsageTest_2() throws WmException {
783799

784800
// now use it
785801
long nu_start = System.nanoTime();
786-
for (String ua: TestData.USER_AGENTS){
802+
for (String ua: testUserAgents){
787803
client.lookupUseragent(ua);
788804
}
789805
long elapsedWithCache = System.nanoTime() - nu_start;
790-
double avgWithCache = (double)elapsedWithCache/(double)TestData.USER_AGENTS.length;
806+
double avgWithCache = (double)elapsedWithCache/(double)testUserAgents.size();
791807

792808
// Cache must be at least an order of magnitude faster
793-
assertTrue(avgNoCache > avgWithCache * 10);
809+
assertTrue(avgNoCache > avgWithCache * 100);
794810

795811
}
796812
finally {
@@ -865,7 +881,7 @@ public WmClient createTestCachedClient(int csize) throws WmException {
865881
// Uses reflection to force invoke of private method clearCacheIfNeeded for testing purposes
866882
private void invokeClearCacheIfNeeded(WmClient client, String ltime) throws ClassNotFoundException, NoSuchMethodException,
867883
InvocationTargetException, IllegalAccessException {
868-
Class clientClass = Class.forName("com.scientiamobile.wurfl.wmclient.WmClient");
884+
Class<?> clientClass = Class.forName("com.scientiamobile.wurfl.wmclient.WmClient");
869885
Method ms = clientClass.getDeclaredMethod("clearCachesIfNeeded", String.class);
870886
ms.setAccessible(true);
871887
ms.invoke(client, ltime);
@@ -876,9 +892,9 @@ private HttpServletRequest createTestRequest(final boolean provideHeaders) {
876892
return new HttpServletRequest() {
877893

878894
private final Map<String, String> headers = new HashMap<>();
879-
private String ua = "Mozilla/5.0 (Nintendo Switch; WebApplet) AppleWebKit/601.6 (KHTML, like Gecko) NF/4.0.0.5.9 NintendoBrowser/5.1.0.13341";
880-
private String xucbr = "Mozilla/5.0 (Nintendo Switch; ShareApplet) AppleWebKit/601.6 (KHTML, like Gecko) NF/4.0.0.5.9 NintendoBrowser/5.1.0.13341";
881-
private String dstkUa = "Mozilla/5.0 (Nintendo Switch; WifiWebAuthApplet) AppleWebKit/601.6 (KHTML, like Gecko) NF/4.0.0.5.9 NintendoBrowser/5.1.0.13341";
895+
private final String ua = "Mozilla/5.0 (Nintendo Switch; WebApplet) AppleWebKit/601.6 (KHTML, like Gecko) NF/4.0.0.5.9 NintendoBrowser/5.1.0.13341";
896+
private final String xucbr = "Mozilla/5.0 (Nintendo Switch; ShareApplet) AppleWebKit/601.6 (KHTML, like Gecko) NF/4.0.0.5.9 NintendoBrowser/5.1.0.13341";
897+
private final String dstkUa = "Mozilla/5.0 (Nintendo Switch; WifiWebAuthApplet) AppleWebKit/601.6 (KHTML, like Gecko) NF/4.0.0.5.9 NintendoBrowser/5.1.0.13341";
882898

883899

884900
@Override

0 commit comments

Comments
 (0)