diff --git a/pom.xml b/pom.xml index 8b811d0..dc8b60b 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ 1.6 2.0.1.RELEASE 4.2.0.Final + 2.1 @@ -126,6 +127,12 @@ guava ${guava.version} + + + joda-time + joda-time + ${joda.version} + diff --git a/src/main/java/eu/enhan/timelord/domain/config/AppConfig.java b/src/main/java/eu/enhan/timelord/domain/config/AppConfig.java index 184ba56..ca13e86 100644 --- a/src/main/java/eu/enhan/timelord/domain/config/AppConfig.java +++ b/src/main/java/eu/enhan/timelord/domain/config/AppConfig.java @@ -16,8 +16,18 @@ */ package eu.enhan.timelord.domain.config; +import java.util.Set; + +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.ConversionServiceFactoryBean; +import org.springframework.core.convert.converter.Converter; + +import com.google.common.collect.Sets; + +import eu.enhan.timelord.domain.util.DateTimeToStringConverter; +import eu.enhan.timelord.domain.util.StringToDateTimeConverter; /** * @author Emmanuel Nhan @@ -26,5 +36,17 @@ @Configuration @ComponentScan("eu.enhan.timelord.domain") public class AppConfig { + + @Bean + public ConversionServiceFactoryBean conversionService(){ + ConversionServiceFactoryBean cf = new ConversionServiceFactoryBean(); + Set> converters = Sets.newHashSet(); + converters.add(new DateTimeToStringConverter()); + converters.add(new StringToDateTimeConverter()); + cf.setConverters(converters); + + return cf; + } + } diff --git a/src/main/java/eu/enhan/timelord/domain/core/TimelordUser.java b/src/main/java/eu/enhan/timelord/domain/core/TimelordUser.java index 1dc67aa..fe1d5ae 100644 --- a/src/main/java/eu/enhan/timelord/domain/core/TimelordUser.java +++ b/src/main/java/eu/enhan/timelord/domain/core/TimelordUser.java @@ -16,6 +16,7 @@ */ package eu.enhan.timelord.domain.core; +import org.joda.time.DateTime; import org.springframework.data.neo4j.annotation.GraphId; import org.springframework.data.neo4j.annotation.NodeEntity; @@ -34,13 +35,14 @@ public class TimelordUser { private String login; private String password; private String email; - + private DateTime registrationDate; public TimelordUser(String login, String password, String email) { super(); this.login = login; this.password = password; this.email = email; + registrationDate = new DateTime(); } public TimelordUser() { @@ -85,6 +87,7 @@ public int hashCode() { result = prime * result + ((email == null) ? 0 : email.hashCode()); result = prime * result + ((login == null) ? 0 : login.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); + result = prime * result + ((registrationDate == null) ? 0 : registrationDate.hashCode()); return result; } @@ -112,6 +115,11 @@ public boolean equals(Object obj) { return false; } else if (!password.equals(other.password)) return false; + if (registrationDate == null) { + if (other.registrationDate != null) + return false; + } else if (!registrationDate.equals(other.registrationDate)) + return false; return true; } diff --git a/src/main/java/eu/enhan/timelord/domain/util/DateTimeToStringConverter.java b/src/main/java/eu/enhan/timelord/domain/util/DateTimeToStringConverter.java new file mode 100644 index 0000000..d917c97 --- /dev/null +++ b/src/main/java/eu/enhan/timelord/domain/util/DateTimeToStringConverter.java @@ -0,0 +1,34 @@ +/* + * This file is part of Timelord. + * + * Timelord is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Timelord is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the Affero GNU General Public License + * along with Timemord. If not, see . + */ +package eu.enhan.timelord.domain.util; + +import org.joda.time.DateTime; +import org.springframework.core.convert.converter.Converter; + +/** + * @author Emmanuel Nhan + * + */ +public class DateTimeToStringConverter implements Converter { + + + @Override + public String convert(DateTime source) { + return source.toString(); + } + +} diff --git a/src/main/java/eu/enhan/timelord/domain/util/StringToDateTimeConverter.java b/src/main/java/eu/enhan/timelord/domain/util/StringToDateTimeConverter.java new file mode 100644 index 0000000..abf626d --- /dev/null +++ b/src/main/java/eu/enhan/timelord/domain/util/StringToDateTimeConverter.java @@ -0,0 +1,34 @@ +/* + * This file is part of Timelord. + * + * Timelord is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Timelord is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the Affero GNU General Public License + * along with Timemord. If not, see . + */ +package eu.enhan.timelord.domain.util; + +import org.joda.time.DateTime; +import org.springframework.core.convert.converter.Converter; + +/** + * @author Emmanuel Nhan + * + */ +public class StringToDateTimeConverter implements Converter { + + @Override + public DateTime convert(String source) { + DateTime result = new DateTime(source); + return result; + } + +} diff --git a/src/test/java/eu/enhan/timelord/test/domain/core/TimlordUserTest.java b/src/test/java/eu/enhan/timelord/test/domain/core/TimelordUserTest.java similarity index 77% rename from src/test/java/eu/enhan/timelord/test/domain/core/TimlordUserTest.java rename to src/test/java/eu/enhan/timelord/test/domain/core/TimelordUserTest.java index c0c1756..99f552d 100644 --- a/src/test/java/eu/enhan/timelord/test/domain/core/TimlordUserTest.java +++ b/src/test/java/eu/enhan/timelord/test/domain/core/TimelordUserTest.java @@ -18,6 +18,8 @@ import org.junit.Test; import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.neo4j.template.Neo4jOperations; import org.springframework.test.context.ActiveProfiles; @@ -38,8 +40,10 @@ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=AppConfig.class) @ActiveProfiles("dev") -public class TimlordUserTest { +public class TimelordUserTest { + private static final Logger logger = LoggerFactory.getLogger(TimelordUserTest.class); + @Autowired Neo4jOperations template; @@ -48,8 +52,16 @@ public void testCreateUser(){ TimelordUser user = new TimelordUser("me", "password", "email@mail.com"); template.save(user); - TimelordUser user2 = template.findOne(user.getId(), TimelordUser.class); - assertEquals(user, user2); + logger.debug("User saved. Sleeping for 1s."); + try { + Thread.sleep(1000); + logger.debug("Wake up, try to get it back."); + TimelordUser user2 = template.findOne(user.getId(), TimelordUser.class); + assertEquals(user, user2); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } diff --git a/src/test/java/eu/enhan/timelord/test/domain/util/DateTimeConversionTest.java b/src/test/java/eu/enhan/timelord/test/domain/util/DateTimeConversionTest.java new file mode 100644 index 0000000..5c08837 --- /dev/null +++ b/src/test/java/eu/enhan/timelord/test/domain/util/DateTimeConversionTest.java @@ -0,0 +1,53 @@ +/* + * This file is part of Timelord. + * + * Timelord is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Timelord is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the Affero GNU General Public License + * along with Timemord. If not, see . + */ +package eu.enhan.timelord.test.domain.util; + +import org.joda.time.DateTime; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import eu.enhan.timelord.domain.util.DateTimeToStringConverter; +import eu.enhan.timelord.domain.util.StringToDateTimeConverter; + +import static org.junit.Assert.*; + +/** + * @author Emmanuel Nhan + * + */ +public class DateTimeConversionTest { + + private static final Logger logger = LoggerFactory.getLogger(DateTimeConversionTest.class); + + @Test + public void testConvertion(){ + StringToDateTimeConverter stringToDate = new StringToDateTimeConverter(); + DateTimeToStringConverter dateToString = new DateTimeToStringConverter(); + + DateTime time = new DateTime(2012, 2, 2, 10, 0); + String converted = dateToString.convert(time); + logger.debug("Converted value = {}.", converted); + DateTime restored = stringToDate.convert(converted); + + assertEquals(time, restored); + + + + } + +}