Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-1525-Check-if-a-String-is-a-palindrome
This commit is contained in:
commit
830042c45a
|
@ -0,0 +1,51 @@
|
||||||
|
package com.baeldung.kotlin
|
||||||
|
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class InfixFunctionsTest {
|
||||||
|
@Test
|
||||||
|
fun testColours() {
|
||||||
|
val color = 0x123456
|
||||||
|
val red = (color and 0xff0000) shr 16
|
||||||
|
val green = (color and 0x00ff00) shr 8
|
||||||
|
val blue = (color and 0x0000ff) shr 0
|
||||||
|
|
||||||
|
Assert.assertEquals(0x12, red)
|
||||||
|
Assert.assertEquals(0x34, green)
|
||||||
|
Assert.assertEquals(0x56, blue)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testNewAssertions() {
|
||||||
|
class Assertion<T>(private val target: T) {
|
||||||
|
infix fun isEqualTo(other: T) {
|
||||||
|
Assert.assertEquals(other, target)
|
||||||
|
}
|
||||||
|
|
||||||
|
infix fun isDifferentFrom(other: T) {
|
||||||
|
Assert.assertNotEquals(other, target)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val result = Assertion(5)
|
||||||
|
|
||||||
|
result isEqualTo 5
|
||||||
|
|
||||||
|
// The following two lines are expected to fail
|
||||||
|
// result isEqualTo 6
|
||||||
|
// result isDifferentFrom 5
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testNewStringMethod() {
|
||||||
|
infix fun String.substringMatches(r: Regex) : List<String> {
|
||||||
|
return r.findAll(this)
|
||||||
|
.map { it.value }
|
||||||
|
.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
val matches = "a bc def" substringMatches ".*? ".toRegex()
|
||||||
|
Assert.assertEquals(listOf("a ", "bc "), matches)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,128 @@
|
||||||
|
package com.baeldung.kotlin.stdlib
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import java.beans.ExceptionListener
|
||||||
|
import java.beans.XMLEncoder
|
||||||
|
import java.io.*
|
||||||
|
import java.lang.Exception
|
||||||
|
import kotlin.test.*
|
||||||
|
import kotlin.text.RegexOption.*
|
||||||
|
|
||||||
|
class RegexTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenRegexIsInstantiated_thenIsEqualToToRegexMethod() {
|
||||||
|
val pattern = """a([bc]+)d?\\"""
|
||||||
|
|
||||||
|
assertEquals(Regex.fromLiteral(pattern).pattern, pattern)
|
||||||
|
assertEquals(pattern, Regex(pattern).pattern)
|
||||||
|
assertEquals(pattern, pattern.toRegex().pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenRegexMatches_thenResultIsTrue() {
|
||||||
|
val regex = """a([bc]+)d?""".toRegex()
|
||||||
|
|
||||||
|
assertTrue(regex.containsMatchIn("xabcdy"))
|
||||||
|
assertTrue(regex.matches("abcd"))
|
||||||
|
assertFalse(regex matches "xabcdy")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenCompletelyMatchingRegex_whenMatchResult_thenDestructuring() {
|
||||||
|
val regex = """a([bc]+)d?""".toRegex()
|
||||||
|
|
||||||
|
assertNull(regex.matchEntire("xabcdy"))
|
||||||
|
|
||||||
|
val matchResult = regex.matchEntire("abbccbbd")
|
||||||
|
|
||||||
|
assertNotNull(matchResult)
|
||||||
|
assertEquals(matchResult!!.value, matchResult.groupValues[0])
|
||||||
|
assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1))
|
||||||
|
assertEquals("bbccbb", matchResult.destructured.component1())
|
||||||
|
assertNull(matchResult.next())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenPartiallyMatchingRegex_whenMatchResult_thenGroups() {
|
||||||
|
val regex = """a([bc]+)d?""".toRegex()
|
||||||
|
var matchResult = regex.find("abcb abbd")
|
||||||
|
|
||||||
|
assertNotNull(matchResult)
|
||||||
|
assertEquals(matchResult!!.value, matchResult.groupValues[0])
|
||||||
|
assertEquals("abcb", matchResult.value)
|
||||||
|
assertEquals(IntRange(0, 3), matchResult.range)
|
||||||
|
assertEquals(listOf("abcb", "bcb"), matchResult.groupValues)
|
||||||
|
assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1))
|
||||||
|
|
||||||
|
matchResult = matchResult.next()
|
||||||
|
|
||||||
|
assertNotNull(matchResult)
|
||||||
|
assertEquals("abbd", matchResult!!.value)
|
||||||
|
assertEquals("bb", matchResult.groupValues[1])
|
||||||
|
|
||||||
|
matchResult = matchResult.next()
|
||||||
|
|
||||||
|
assertNull(matchResult)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenPartiallyMatchingRegex_whenMatchResult_thenDestructuring() {
|
||||||
|
val regex = """([\w\s]+) is (\d+) years old""".toRegex()
|
||||||
|
val matchResult = regex.find("Mickey Mouse is 95 years old")
|
||||||
|
val (name, age) = matchResult!!.destructured
|
||||||
|
|
||||||
|
assertEquals("Mickey Mouse", name)
|
||||||
|
assertEquals("95", age)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenNonMatchingRegex_whenFindCalled_thenNull() {
|
||||||
|
val regex = """a([bc]+)d?""".toRegex()
|
||||||
|
val matchResult = regex.find("foo")
|
||||||
|
|
||||||
|
assertNull(matchResult)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenNonMatchingRegex_whenFindAllCalled_thenEmptySet() {
|
||||||
|
val regex = """a([bc]+)d?""".toRegex()
|
||||||
|
val matchResults = regex.findAll("foo")
|
||||||
|
|
||||||
|
assertNotNull(matchResults)
|
||||||
|
assertTrue(matchResults.none())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenReplace_thenReplacement() {
|
||||||
|
val regex = """(red|green|blue)""".toRegex()
|
||||||
|
val beautiful = "Roses are red, Violets are blue"
|
||||||
|
val grim = regex.replace(beautiful, "dark")
|
||||||
|
val shiny = regex.replaceFirst(beautiful, "rainbow")
|
||||||
|
|
||||||
|
assertEquals("Roses are dark, Violets are dark", grim)
|
||||||
|
assertEquals("Roses are rainbow, Violets are blue", shiny)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenComplexReplace_thenReplacement() {
|
||||||
|
val regex = """(red|green|blue)""".toRegex()
|
||||||
|
val beautiful = "Roses are red, Violets are blue"
|
||||||
|
val reallyBeautiful = regex.replace(beautiful) {
|
||||||
|
matchResult -> matchResult.value.toUpperCase() + "!"
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("Roses are RED!, Violets are BLUE!", reallyBeautiful)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenSplit_thenList() {
|
||||||
|
val regex = """\W+""".toRegex()
|
||||||
|
val beautiful = "Roses are red, Violets are blue"
|
||||||
|
|
||||||
|
assertEquals(listOf("Roses", "are", "red", "Violets", "are", "blue"), regex.split(beautiful))
|
||||||
|
assertEquals(listOf("Roses", "are", "red", "Violets are blue"), regex.split(beautiful, 4))
|
||||||
|
assertEquals(regex.toPattern().split(beautiful).asList(), regex.split(beautiful))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -16,11 +16,13 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<spring.version>4.3.7.RELEASE</spring.version>
|
<spring.version>5.0.3.RELEASE</spring.version>
|
||||||
<spring-data-redis>1.8.1.RELEASE</spring-data-redis>
|
<spring-data-redis>2.0.3.RELEASE</spring-data-redis>
|
||||||
<cglib.version>3.2.4</cglib.version>
|
<cglib.version>3.2.4</cglib.version>
|
||||||
<jedis.version>2.9.0</jedis.version>
|
<jedis.version>2.9.0</jedis.version>
|
||||||
<nosqlunit.version>0.10.0</nosqlunit.version>
|
<nosqlunit.version>0.10.0</nosqlunit.version>
|
||||||
|
<spring-data-commons.version>2.0.3.RELEASE</spring-data-commons.version>
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -73,6 +75,12 @@
|
||||||
<artifactId>nosqlunit-redis</artifactId>
|
<artifactId>nosqlunit-redis</artifactId>
|
||||||
<version>${nosqlunit.version}</version>
|
<version>${nosqlunit.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-commons</artifactId>
|
||||||
|
<version>${spring-data-commons.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
package com.baeldung.spring.data.redis.config;
|
package com.baeldung.spring.data.redis.config;
|
||||||
|
|
||||||
import com.baeldung.spring.data.redis.queue.MessagePublisher;
|
|
||||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
|
||||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
@ -11,10 +8,16 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.listener.ChannelTopic;
|
import org.springframework.data.redis.listener.ChannelTopic;
|
||||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||||
|
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
|
||||||
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
||||||
|
|
||||||
|
import com.baeldung.spring.data.redis.queue.MessagePublisher;
|
||||||
|
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||||
|
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan("com.baeldung.spring.data.redis")
|
@ComponentScan("com.baeldung.spring.data.redis")
|
||||||
|
@EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo")
|
||||||
public class RedisConfig {
|
public class RedisConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -2,6 +2,9 @@ package com.baeldung.spring.data.redis.model;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.springframework.data.redis.core.RedisHash;
|
||||||
|
|
||||||
|
@RedisHash("Student")
|
||||||
public class Student implements Serializable {
|
public class Student implements Serializable {
|
||||||
|
|
||||||
public enum Gender {
|
public enum Gender {
|
||||||
|
|
|
@ -1,18 +1,9 @@
|
||||||
package com.baeldung.spring.data.redis.repo;
|
package com.baeldung.spring.data.redis.repo;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.baeldung.spring.data.redis.model.Student;
|
import com.baeldung.spring.data.redis.model.Student;
|
||||||
|
|
||||||
import java.util.Map;
|
@Repository
|
||||||
|
public interface StudentRepository extends CrudRepository<Student, String> {}
|
||||||
public interface StudentRepository {
|
|
||||||
|
|
||||||
void saveStudent(Student person);
|
|
||||||
|
|
||||||
void updateStudent(Student student);
|
|
||||||
|
|
||||||
Student findStudent(String id);
|
|
||||||
|
|
||||||
Map<Object, Object> findAllStudents();
|
|
||||||
|
|
||||||
void deleteStudent(String id);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,49 +0,0 @@
|
||||||
package com.baeldung.spring.data.redis.repo;
|
|
||||||
|
|
||||||
import com.baeldung.spring.data.redis.model.Student;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.redis.core.HashOperations;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class StudentRepositoryImpl implements StudentRepository {
|
|
||||||
|
|
||||||
private static final String KEY = "Student";
|
|
||||||
|
|
||||||
private RedisTemplate<String, Object> redisTemplate;
|
|
||||||
private HashOperations hashOperations;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public StudentRepositoryImpl(RedisTemplate<String, Object> redisTemplate) {
|
|
||||||
this.redisTemplate = redisTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
private void init() {
|
|
||||||
hashOperations = redisTemplate.opsForHash();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveStudent(final Student student) {
|
|
||||||
hashOperations.put(KEY, student.getId(), student);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateStudent(final Student student) {
|
|
||||||
hashOperations.put(KEY, student.getId(), student);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Student findStudent(final String id) {
|
|
||||||
return (Student) hashOperations.get(KEY, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<Object, Object> findAllStudents() {
|
|
||||||
return hashOperations.entries(KEY);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteStudent(final String id) {
|
|
||||||
hashOperations.delete(KEY, id);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,17 +1,20 @@
|
||||||
package com.baeldung.spring.data.redis.repo;
|
package com.baeldung.spring.data.redis.repo;
|
||||||
|
|
||||||
import com.baeldung.spring.data.redis.config.RedisConfig;
|
import static org.junit.Assert.assertEquals;
|
||||||
import com.baeldung.spring.data.redis.model.Student;
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import java.util.Map;
|
import com.baeldung.spring.data.redis.config.RedisConfig;
|
||||||
|
import com.baeldung.spring.data.redis.model.Student;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = RedisConfig.class)
|
@ContextConfiguration(classes = RedisConfig.class)
|
||||||
|
@ -23,18 +26,18 @@ public class StudentRepositoryIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception {
|
public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception {
|
||||||
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||||
studentRepository.saveStudent(student);
|
studentRepository.save(student);
|
||||||
final Student retrievedStudent = studentRepository.findStudent(student.getId());
|
final Student retrievedStudent = studentRepository.findById(student.getId()).get();
|
||||||
assertEquals(student.getId(), retrievedStudent.getId());
|
assertEquals(student.getId(), retrievedStudent.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception {
|
public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception {
|
||||||
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||||
studentRepository.saveStudent(student);
|
studentRepository.save(student);
|
||||||
student.setName("Richard Watson");
|
student.setName("Richard Watson");
|
||||||
studentRepository.saveStudent(student);
|
studentRepository.save(student);
|
||||||
final Student retrievedStudent = studentRepository.findStudent(student.getId());
|
final Student retrievedStudent = studentRepository.findById(student.getId()).get();
|
||||||
assertEquals(student.getName(), retrievedStudent.getName());
|
assertEquals(student.getName(), retrievedStudent.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,18 +45,19 @@ public class StudentRepositoryIntegrationTest {
|
||||||
public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception {
|
public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception {
|
||||||
final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||||
final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
|
final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
|
||||||
studentRepository.saveStudent(engStudent);
|
studentRepository.save(engStudent);
|
||||||
studentRepository.saveStudent(medStudent);
|
studentRepository.save(medStudent);
|
||||||
final Map<Object, Object> retrievedStudent = studentRepository.findAllStudents();
|
List<Student> students = new ArrayList<>();
|
||||||
assertEquals(retrievedStudent.size(), 2);
|
studentRepository.findAll().forEach(students::add);
|
||||||
|
assertEquals(students.size(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception {
|
public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception {
|
||||||
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||||
studentRepository.saveStudent(student);
|
studentRepository.save(student);
|
||||||
studentRepository.deleteStudent(student.getId());
|
studentRepository.deleteById(student.getId());
|
||||||
final Student retrievedStudent = studentRepository.findStudent(student.getId());
|
final Student retrievedStudent = studentRepository.findById(student.getId()).orElse(null);
|
||||||
assertNull(retrievedStudent);
|
assertNull(retrievedStudent);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue