Skip to content

Commit 7a54a6d

Browse files
authored
Merge pull request eugenp#21 from eugenp/master
update
2 parents 1b8985a + 51b92a3 commit 7a54a6d

File tree

18 files changed

+791
-1
lines changed

18 files changed

+791
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.scala
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
class RegexUnitTest {
7+
private val polishPostalCode = "([0-9]{2})\\-([0-9]{3})".r
8+
private val timestamp = "([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{3})".r
9+
private val timestampUnanchored = timestamp.unanchored
10+
11+
@Test
12+
def givenRegularExpression_whenCallingFindFirstIn_thenShouldFindCorrectMatches(): Unit = {
13+
val postCode = polishPostalCode.findFirstIn("Warsaw 01-011, Jerusalem Avenue")
14+
assertEquals(Some("01-011"), postCode)
15+
}
16+
17+
@Test
18+
def givenRegularExpression_whenCallingFindFirstMatchIn_thenShouldFindCorrectMatches(): Unit = {
19+
val postCodes = polishPostalCode.findFirstMatchIn("Warsaw 01-011, Jerusalem Avenue")
20+
assertEquals(Some("011"), for (m <- postCodes) yield m.group(2))
21+
}
22+
23+
@Test
24+
def givenRegularExpression_whenCallingFindAllIn_thenShouldFindCorrectMatches(): Unit = {
25+
val postCodes = polishPostalCode.findAllIn("Warsaw 01-011, Jerusalem Avenue, Cracow 30-059, Mickiewicza Avenue")
26+
.toList
27+
assertEquals(List("01-011", "30-059"), postCodes)
28+
29+
polishPostalCode.findAllIn("Warsaw 01-011, Jerusalem Avenue, Cracow 30-059, Mickiewicza Avenue")
30+
}
31+
32+
@Test
33+
def givenRegularExpression_whenCallingFindAlMatchlIn_thenShouldFindCorrectMatches(): Unit = {
34+
val postCodes = polishPostalCode.findAllMatchIn("Warsaw 01-011, Jerusalem Avenue, Cracow 30-059, Mickiewicza Avenue")
35+
.toList
36+
val postalDistricts = for (m <- postCodes) yield m.group(1)
37+
assertEquals(List("01", "30"), postalDistricts)
38+
}
39+
40+
@Test
41+
def givenRegularExpression_whenExtractingValues_thenShouldExtractCorrectValues(): Unit = {
42+
val description = "11:34:01.411" match {
43+
case timestamp(hour, minutes, _, _) => s"It's $minutes minutes after $hour"
44+
}
45+
46+
assertEquals("It's 34 minutes after 11", description)
47+
}
48+
49+
@Test
50+
def givenUnanchoredRegularExpression_whenExtractingValues_thenShouldExtractCorrectValues(): Unit = {
51+
val description = "Timestamp: 11:34:01.411 error appeared" match {
52+
case timestampUnanchored(hour, minutes, _, _) => s"It's $minutes minutes after $hour"
53+
}
54+
55+
assertEquals("It's 34 minutes after 11", description)
56+
}
57+
58+
@Test
59+
def givenRegularExpression_whenCallingReplaceAllIn_thenShouldReplaceText(): Unit = {
60+
val minutes = timestamp.replaceAllIn("11:34:01.311", m => m.group(2))
61+
62+
assertEquals("34", minutes)
63+
}
64+
65+
@Test
66+
def givenRegularExpression_whenCallingReplaceAllInWithMatcher_thenShouldReplaceText(): Unit = {
67+
val secondsThatDayInTotal = timestamp.replaceAllIn("11:34:01.311", _ match {
68+
case timestamp(hours, minutes, seconds, _) => s"$hours-$minutes"
69+
})
70+
71+
assertEquals("11-34", secondsThatDayInTotal)
72+
}
73+
}

json-2/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## JSON
2+
3+
This module contains articles about JSON.
4+
5+
### Relevant Articles:

json-2/pom.xml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>json-2</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
9+
<parent>
10+
<artifactId>parent-modules</artifactId>
11+
<groupId>com.baeldung</groupId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
</parent>
14+
<modelVersion>4.0.0</modelVersion>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.jsoniter</groupId>
19+
<artifactId>jsoniter</artifactId>
20+
<version>${jsoniter.version}</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>${junit.version}</version>
27+
<scope>test</scope>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.assertj</groupId>
32+
<artifactId>assertj-core</artifactId>
33+
<version>${assertj-core.version}</version>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
<properties>
38+
<jsoniter.version>0.9.23</jsoniter.version>
39+
<assertj-core.version>3.11.1</assertj-core.version>
40+
</properties>
41+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.jsoniter.model;
2+
3+
public class Name {
4+
private String firstName;
5+
private String surname;
6+
7+
public String getFirstName() {
8+
return firstName;
9+
}
10+
11+
public void setFirstName(String firstName) {
12+
this.firstName = firstName;
13+
}
14+
15+
public String getSurname() {
16+
return surname;
17+
}
18+
19+
public void setSurname(String surname) {
20+
this.surname = surname;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.jsoniter.model;
2+
3+
import com.jsoniter.annotation.JsonProperty;
4+
import com.jsoniter.fuzzy.MaybeStringIntDecoder;
5+
6+
public class Student {
7+
@JsonProperty(decoder = MaybeStringIntDecoder.class)
8+
private int id;
9+
private Name name;
10+
11+
public int getId() {
12+
return id;
13+
}
14+
15+
public void setId(int id) {
16+
this.id = id;
17+
}
18+
19+
public Name getName() {
20+
return name;
21+
}
22+
23+
public void setName(Name name) {
24+
this.name = name;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.jsoniter;
2+
3+
import com.baeldung.jsoniter.model.Name;
4+
import com.baeldung.jsoniter.model.Student;
5+
import com.jsoniter.JsonIterator;
6+
import com.jsoniter.ValueType;
7+
import com.jsoniter.any.Any;
8+
9+
import org.junit.Test;
10+
11+
import static com.jsoniter.ValueType.STRING;
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
public class JsoniterIntroUnitTest {
15+
16+
@Test
17+
public void whenParsedUsingBindAPI_thenConvertedToJavaObjectCorrectly() {
18+
String input = "{\"id\":1,\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}";
19+
20+
Student student = JsonIterator.deserialize(input, Student.class);
21+
22+
assertThat(student.getId()).isEqualTo(1);
23+
assertThat(student.getName().getFirstName()).isEqualTo("Joe");
24+
assertThat(student.getName().getSurname()).isEqualTo("Blogg");
25+
}
26+
27+
@Test
28+
public void givenTypeInJsonFuzzy_whenFieldIsMaybeDecoded_thenFieldParsedCorrectly() {
29+
String input = "{\"id\":\"1\",\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}";
30+
31+
Student student = JsonIterator.deserialize(input, Student.class);
32+
33+
assertThat(student.getId()).isEqualTo(1);
34+
}
35+
36+
@Test
37+
public void whenParsedUsingAnyAPI_thenFieldValueCanBeExtractedUsingTheFieldName() {
38+
String input = "{\"id\":1,\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}";
39+
40+
Any any = JsonIterator.deserialize(input);
41+
42+
assertThat(any.toInt("id")).isEqualTo(1);
43+
assertThat(any.toString("name", "firstName")).isEqualTo("Joe");
44+
assertThat(any.toString("name", "surname")).isEqualTo("Blogg");
45+
}
46+
47+
@Test
48+
public void whenParsedUsingAnyAPI_thenFieldValueTypeIsCorrect() {
49+
String input = "{\"id\":1,\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}";
50+
51+
Any any = JsonIterator.deserialize(input);
52+
53+
assertThat(any.get("id").valueType()).isEqualTo(ValueType.NUMBER);
54+
assertThat(any.get("name").valueType()).isEqualTo(ValueType.OBJECT);
55+
assertThat(any.get("error").valueType()).isEqualTo(ValueType.INVALID);
56+
}
57+
58+
@Test
59+
public void whenParsedUsingIteratorAPI_thenFieldValuesExtractedCorrectly() throws Exception {
60+
Name name = new Name();
61+
String input = "{ \"firstName\" : \"Joe\", \"surname\" : \"Blogg\" }";
62+
JsonIterator iterator = JsonIterator.parse(input);
63+
64+
for (String field = iterator.readObject(); field != null; field = iterator.readObject()) {
65+
switch (field) {
66+
case "firstName":
67+
if (iterator.whatIsNext() == ValueType.STRING) {
68+
name.setFirstName(iterator.readString());
69+
}
70+
continue;
71+
case "surname":
72+
if (iterator.whatIsNext() == ValueType.STRING) {
73+
name.setSurname(iterator.readString());
74+
}
75+
continue;
76+
default:
77+
iterator.skip();
78+
}
79+
}
80+
81+
assertThat(name.getFirstName()).isEqualTo("Joe");
82+
assertThat(name.getSurname()).isEqualTo("Blogg");
83+
}
84+
85+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"id":1,"name":{"firstName": "Joe", "surname":"Blogg"}}

persistence-modules/redis/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</dependencies>
3939

4040
<properties>
41-
<jedis.version>2.9.0</jedis.version>
41+
<jedis.version>3.2.0</jedis.version>
4242
<embedded-redis.version>0.6</embedded-redis.version>
4343
<redisson.version>3.3.0</redisson.version>
4444
<lettuce-core.version>5.0.1.RELEASE</lettuce-core.version>

0 commit comments

Comments
 (0)