Skip to content

Instantly share code, notes, and snippets.

@mp911de
Last active July 1, 2016 08:20
Show Gist options
  • Select an option

  • Save mp911de/11d58501b915d7a5aa9f322642f31c83 to your computer and use it in GitHub Desktop.

Select an option

Save mp911de/11d58501b915d7a5aa9f322642f31c83 to your computer and use it in GitHub Desktop.
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import org.bson.types.ObjectId;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.Data;
/**
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringDataTestCase.Config.class)
public class SpringDataTestCase {
@Autowired
MongoTemplate mongoTemplate;
@Test
public void test() throws Exception {
Model model = new Model();
model.setLocalDate(LocalDate.of(2010, 1, 1));
mongoTemplate.save(model);
Model loaded = mongoTemplate.findById(model.getId(), Model.class);
assertThat(loaded.getLocalDate(), is(equalTo(LocalDate.of(1970, 03, 06))));
}
@Document
@Data
public static class Model {
ObjectId id;
LocalDate localDate;
}
@SpringBootApplication
@Configuration
static class Config {
@Bean
CustomConversions customConversions() {
return new CustomConversions(Arrays.asList(LocalDateToDummyDateConverter.INSTANCE));
}
}
@WritingConverter
public static enum LocalDateToDummyDateConverter implements Converter<LocalDate, Date> {
INSTANCE;
private LocalDateToDummyDateConverter() {
}
public Date convert(LocalDate source) {
return source == null ? null : new Date(5555555555L);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-data-playground</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-data-playground</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-data-releasetrain.version>Gosling-SR1</spring-data-releasetrain.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment