Skip to content

Commit 830042c

Browse files
Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-1525-Check-if-a-String-is-a-palindrome
2 parents 0702320 + 6526704 commit 830042c

8 files changed

Lines changed: 225 additions & 86 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.kotlin
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class InfixFunctionsTest {
7+
@Test
8+
fun testColours() {
9+
val color = 0x123456
10+
val red = (color and 0xff0000) shr 16
11+
val green = (color and 0x00ff00) shr 8
12+
val blue = (color and 0x0000ff) shr 0
13+
14+
Assert.assertEquals(0x12, red)
15+
Assert.assertEquals(0x34, green)
16+
Assert.assertEquals(0x56, blue)
17+
}
18+
19+
@Test
20+
fun testNewAssertions() {
21+
class Assertion<T>(private val target: T) {
22+
infix fun isEqualTo(other: T) {
23+
Assert.assertEquals(other, target)
24+
}
25+
26+
infix fun isDifferentFrom(other: T) {
27+
Assert.assertNotEquals(other, target)
28+
}
29+
}
30+
31+
val result = Assertion(5)
32+
33+
result isEqualTo 5
34+
35+
// The following two lines are expected to fail
36+
// result isEqualTo 6
37+
// result isDifferentFrom 5
38+
}
39+
40+
@Test
41+
fun testNewStringMethod() {
42+
infix fun String.substringMatches(r: Regex) : List<String> {
43+
return r.findAll(this)
44+
.map { it.value }
45+
.toList()
46+
}
47+
48+
val matches = "a bc def" substringMatches ".*? ".toRegex()
49+
Assert.assertEquals(listOf("a ", "bc "), matches)
50+
}
51+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.baeldung.kotlin.stdlib
2+
3+
import org.junit.Test
4+
import java.beans.ExceptionListener
5+
import java.beans.XMLEncoder
6+
import java.io.*
7+
import java.lang.Exception
8+
import kotlin.test.*
9+
import kotlin.text.RegexOption.*
10+
11+
class RegexTest {
12+
13+
@Test
14+
fun whenRegexIsInstantiated_thenIsEqualToToRegexMethod() {
15+
val pattern = """a([bc]+)d?\\"""
16+
17+
assertEquals(Regex.fromLiteral(pattern).pattern, pattern)
18+
assertEquals(pattern, Regex(pattern).pattern)
19+
assertEquals(pattern, pattern.toRegex().pattern)
20+
}
21+
22+
@Test
23+
fun whenRegexMatches_thenResultIsTrue() {
24+
val regex = """a([bc]+)d?""".toRegex()
25+
26+
assertTrue(regex.containsMatchIn("xabcdy"))
27+
assertTrue(regex.matches("abcd"))
28+
assertFalse(regex matches "xabcdy")
29+
}
30+
31+
@Test
32+
fun givenCompletelyMatchingRegex_whenMatchResult_thenDestructuring() {
33+
val regex = """a([bc]+)d?""".toRegex()
34+
35+
assertNull(regex.matchEntire("xabcdy"))
36+
37+
val matchResult = regex.matchEntire("abbccbbd")
38+
39+
assertNotNull(matchResult)
40+
assertEquals(matchResult!!.value, matchResult.groupValues[0])
41+
assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1))
42+
assertEquals("bbccbb", matchResult.destructured.component1())
43+
assertNull(matchResult.next())
44+
}
45+
46+
@Test
47+
fun givenPartiallyMatchingRegex_whenMatchResult_thenGroups() {
48+
val regex = """a([bc]+)d?""".toRegex()
49+
var matchResult = regex.find("abcb abbd")
50+
51+
assertNotNull(matchResult)
52+
assertEquals(matchResult!!.value, matchResult.groupValues[0])
53+
assertEquals("abcb", matchResult.value)
54+
assertEquals(IntRange(0, 3), matchResult.range)
55+
assertEquals(listOf("abcb", "bcb"), matchResult.groupValues)
56+
assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1))
57+
58+
matchResult = matchResult.next()
59+
60+
assertNotNull(matchResult)
61+
assertEquals("abbd", matchResult!!.value)
62+
assertEquals("bb", matchResult.groupValues[1])
63+
64+
matchResult = matchResult.next()
65+
66+
assertNull(matchResult)
67+
}
68+
69+
@Test
70+
fun givenPartiallyMatchingRegex_whenMatchResult_thenDestructuring() {
71+
val regex = """([\w\s]+) is (\d+) years old""".toRegex()
72+
val matchResult = regex.find("Mickey Mouse is 95 years old")
73+
val (name, age) = matchResult!!.destructured
74+
75+
assertEquals("Mickey Mouse", name)
76+
assertEquals("95", age)
77+
}
78+
79+
@Test
80+
fun givenNonMatchingRegex_whenFindCalled_thenNull() {
81+
val regex = """a([bc]+)d?""".toRegex()
82+
val matchResult = regex.find("foo")
83+
84+
assertNull(matchResult)
85+
}
86+
87+
@Test
88+
fun givenNonMatchingRegex_whenFindAllCalled_thenEmptySet() {
89+
val regex = """a([bc]+)d?""".toRegex()
90+
val matchResults = regex.findAll("foo")
91+
92+
assertNotNull(matchResults)
93+
assertTrue(matchResults.none())
94+
}
95+
96+
@Test
97+
fun whenReplace_thenReplacement() {
98+
val regex = """(red|green|blue)""".toRegex()
99+
val beautiful = "Roses are red, Violets are blue"
100+
val grim = regex.replace(beautiful, "dark")
101+
val shiny = regex.replaceFirst(beautiful, "rainbow")
102+
103+
assertEquals("Roses are dark, Violets are dark", grim)
104+
assertEquals("Roses are rainbow, Violets are blue", shiny)
105+
}
106+
107+
@Test
108+
fun whenComplexReplace_thenReplacement() {
109+
val regex = """(red|green|blue)""".toRegex()
110+
val beautiful = "Roses are red, Violets are blue"
111+
val reallyBeautiful = regex.replace(beautiful) {
112+
matchResult -> matchResult.value.toUpperCase() + "!"
113+
}
114+
115+
assertEquals("Roses are RED!, Violets are BLUE!", reallyBeautiful)
116+
}
117+
118+
@Test
119+
fun whenSplit_thenList() {
120+
val regex = """\W+""".toRegex()
121+
val beautiful = "Roses are red, Violets are blue"
122+
123+
assertEquals(listOf("Roses", "are", "red", "Violets", "are", "blue"), regex.split(beautiful))
124+
assertEquals(listOf("Roses", "are", "red", "Violets are blue"), regex.split(beautiful, 4))
125+
assertEquals(regex.toPattern().split(beautiful).asList(), regex.split(beautiful))
126+
}
127+
128+
}

‎persistence-modules/spring-data-redis/pom.xml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
<properties>
1818
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19-
<spring.version>4.3.7.RELEASE</spring.version>
20-
<spring-data-redis>1.8.1.RELEASE</spring-data-redis>
19+
<spring.version>5.0.3.RELEASE</spring.version>
20+
<spring-data-redis>2.0.3.RELEASE</spring-data-redis>
2121
<cglib.version>3.2.4</cglib.version>
2222
<jedis.version>2.9.0</jedis.version>
2323
<nosqlunit.version>0.10.0</nosqlunit.version>
24+
<spring-data-commons.version>2.0.3.RELEASE</spring-data-commons.version>
25+
2426
</properties>
2527

2628
<dependencies>
@@ -73,6 +75,12 @@
7375
<artifactId>nosqlunit-redis</artifactId>
7476
<version>${nosqlunit.version}</version>
7577
</dependency>
78+
79+
<dependency>
80+
<groupId>org.springframework.data</groupId>
81+
<artifactId>spring-data-commons</artifactId>
82+
<version>${spring-data-commons.version}</version>
83+
</dependency>
7684
</dependencies>
7785

7886
</project>

‎persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.baeldung.spring.data.redis.config;
22

3-
import com.baeldung.spring.data.redis.queue.MessagePublisher;
4-
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
5-
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
63
import org.springframework.context.annotation.Bean;
74
import org.springframework.context.annotation.ComponentScan;
85
import org.springframework.context.annotation.Configuration;
@@ -11,10 +8,16 @@
118
import org.springframework.data.redis.listener.ChannelTopic;
129
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
1310
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
11+
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
1412
import org.springframework.data.redis.serializer.GenericToStringSerializer;
1513

14+
import com.baeldung.spring.data.redis.queue.MessagePublisher;
15+
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
16+
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
17+
1618
@Configuration
1719
@ComponentScan("com.baeldung.spring.data.redis")
20+
@EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo")
1821
public class RedisConfig {
1922

2023
@Bean

‎persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java‎

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

33
import java.io.Serializable;
44

5+
import org.springframework.data.redis.core.RedisHash;
6+
7+
@RedisHash("Student")
58
public class Student implements Serializable {
69

710
public enum Gender {
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
package com.baeldung.spring.data.redis.repo;
22

3-
import com.baeldung.spring.data.redis.model.Student;
4-
5-
import java.util.Map;
6-
7-
public interface StudentRepository {
8-
9-
void saveStudent(Student person);
3+
import org.springframework.data.repository.CrudRepository;
4+
import org.springframework.stereotype.Repository;
105

11-
void updateStudent(Student student);
12-
13-
Student findStudent(String id);
14-
15-
Map<Object, Object> findAllStudents();
6+
import com.baeldung.spring.data.redis.model.Student;
167

17-
void deleteStudent(String id);
18-
}
8+
@Repository
9+
public interface StudentRepository extends CrudRepository<Student, String> {}

‎persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java‎

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.baeldung.spring.data.redis.repo;
22

3-
import com.baeldung.spring.data.redis.config.RedisConfig;
4-
import com.baeldung.spring.data.redis.model.Student;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Map;
9+
510
import org.junit.Test;
611
import org.junit.runner.RunWith;
712
import org.springframework.beans.factory.annotation.Autowired;
813
import org.springframework.test.context.ContextConfiguration;
914
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1015

11-
import java.util.Map;
12-
13-
import static org.junit.Assert.assertEquals;
14-
import static org.junit.Assert.assertNull;
16+
import com.baeldung.spring.data.redis.config.RedisConfig;
17+
import com.baeldung.spring.data.redis.model.Student;
1518

1619
@RunWith(SpringJUnit4ClassRunner.class)
1720
@ContextConfiguration(classes = RedisConfig.class)
@@ -23,37 +26,38 @@ public class StudentRepositoryIntegrationTest {
2326
@Test
2427
public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception {
2528
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
26-
studentRepository.saveStudent(student);
27-
final Student retrievedStudent = studentRepository.findStudent(student.getId());
29+
studentRepository.save(student);
30+
final Student retrievedStudent = studentRepository.findById(student.getId()).get();
2831
assertEquals(student.getId(), retrievedStudent.getId());
2932
}
3033

3134
@Test
3235
public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception {
3336
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
34-
studentRepository.saveStudent(student);
37+
studentRepository.save(student);
3538
student.setName("Richard Watson");
36-
studentRepository.saveStudent(student);
37-
final Student retrievedStudent = studentRepository.findStudent(student.getId());
39+
studentRepository.save(student);
40+
final Student retrievedStudent = studentRepository.findById(student.getId()).get();
3841
assertEquals(student.getName(), retrievedStudent.getName());
3942
}
4043

4144
@Test
4245
public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception {
4346
final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
4447
final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
45-
studentRepository.saveStudent(engStudent);
46-
studentRepository.saveStudent(medStudent);
47-
final Map<Object, Object> retrievedStudent = studentRepository.findAllStudents();
48-
assertEquals(retrievedStudent.size(), 2);
48+
studentRepository.save(engStudent);
49+
studentRepository.save(medStudent);
50+
List<Student> students = new ArrayList<>();
51+
studentRepository.findAll().forEach(students::add);
52+
assertEquals(students.size(), 2);
4953
}
5054

5155
@Test
5256
public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception {
5357
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
54-
studentRepository.saveStudent(student);
55-
studentRepository.deleteStudent(student.getId());
56-
final Student retrievedStudent = studentRepository.findStudent(student.getId());
58+
studentRepository.save(student);
59+
studentRepository.deleteById(student.getId());
60+
final Student retrievedStudent = studentRepository.findById(student.getId()).orElse(null);
5761
assertNull(retrievedStudent);
5862
}
5963
}

0 commit comments

Comments
 (0)