Skip to content

Commit

Permalink
4_5 applied
Browse files Browse the repository at this point in the history
  • Loading branch information
jet_vorobev committed Oct 5, 2016
1 parent e80e253 commit 96c8ab2
Show file tree
Hide file tree
Showing 21 changed files with 59 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
language: java
jdk: oraclejdk8
1 change: 1 addition & 0 deletions config/setenv.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
# run tomcat with JMX ability as admin
# for remote connection add -Djava.rmi.server.hostname=TomcatServer_IP
export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.repository.MealRepository;
import ru.javawebinar.topjava.util.exception.ExceptionUtil;
Expand Down Expand Up @@ -31,6 +32,8 @@ public void delete(int id, int userId) {

@Override
public Collection<Meal> getBetweenDateTimes(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId) {
Assert.notNull(startDateTime, "startDateTime must not be null");
Assert.notNull(endDateTime, "endDateTime must not be null");
return repository.getBetween(startDateTime, endDateTime, userId);
}

Expand All @@ -41,11 +44,13 @@ public Collection<Meal> getAll(int userId) {

@Override
public Meal update(Meal meal, int userId) {
Assert.notNull(meal, "meal must not be null");
return ExceptionUtil.checkNotFoundWithId(repository.save(meal, userId), meal.getId());
}

@Override
public Meal save(Meal meal, int userId) {
Assert.notNull(meal, "meal must not be null");
return repository.save(meal, userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import ru.javawebinar.topjava.model.User;
import ru.javawebinar.topjava.repository.UserRepository;
import ru.javawebinar.topjava.util.exception.ExceptionUtil;
Expand All @@ -21,6 +22,7 @@ public class UserServiceImpl implements UserService {

@Override
public User save(User user) {
Assert.notNull(user, "user must not be null");
return repository.save(user);
}

Expand All @@ -36,6 +38,7 @@ public User get(int id) throws NotFoundException {

@Override
public User getByEmail(String email) throws NotFoundException {
Assert.notNull(email, "email must not be null");
return ExceptionUtil.checkNotFound(repository.getByEmail(email), "email=" + email);
}

Expand All @@ -46,6 +49,7 @@ public List<User> getAll() {

@Override
public void update(User user) {
Assert.notNull(user, "user must not be null");
repository.save(user);
}
}
28 changes: 0 additions & 28 deletions src/main/java/ru/javawebinar/topjava/util/DbPopulator.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/ru/javawebinar/topjava/util/TimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class TimeUtil {
public static final LocalDate MIN_DATE = LocalDate.of(1, 1, 1);
public static final LocalDate MAX_DATE = LocalDate.of(3000, 1, 1);

private TimeUtil() {
}

public static <T extends Comparable<? super T>> boolean isBetween(T value, T start, T end) {
return value.compareTo(start) >= 0 && value.compareTo(end) <= 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* Date: 14.05.2014
*/
public class ExceptionUtil {
private ExceptionUtil() {
}

public static void checkNotFoundWithId(boolean found, int id) {
checkNotFound(found, "id=" + id);
}
Expand All @@ -20,6 +23,8 @@ public static <T> T checkNotFound(T object, String msg) {
}

public static void checkNotFound(boolean found, String msg) {
if (!found) throw new NotFoundException("Not found entity with " + msg);
if (!found) {
throw new NotFoundException("Not found entity with " + msg);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/ru/javawebinar/topjava/web/MealServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
response.sendRedirect("meals");

} else if ("create".equals(action) || "update".equals(action)) {
final Meal meal = action.equals("create") ?
final Meal meal = "create".equals(action) ?
new Meal(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS), "", 1000) :
mealController.get(getId(request));
request.setAttribute("meal", meal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,40 @@
* User: gkislin
*/
public abstract class AbstractUserController {
protected final Logger LOG = LoggerFactory.getLogger(getClass());
protected final Logger log = LoggerFactory.getLogger(getClass());

@Autowired
private UserService service;

public List<User> getAll() {
LOG.info("getAll");
log.info("getAll");
return service.getAll();
}

public User get(int id) {
LOG.info("get " + id);
log.info("get " + id);
return service.get(id);
}

public User create(User user) {
user.setId(null);
LOG.info("create " + user);
log.info("create " + user);
return service.save(user);
}

public void delete(int id) {
LOG.info("delete " + id);
log.info("delete " + id);
service.delete(id);
}

public void update(User user, int id) {
user.setId(id);
LOG.info("update " + user);
log.info("update " + user);
service.update(user);
}

public User getByMail(String email) {
LOG.info("getByEmail " + email);
log.info("getByEmail " + email);
return service.getByEmail(email);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/db/postgres.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
database.url=jdbc:postgresql://localhost:5432/topjava
database.username=user
database.password=password

database.init=true
2 changes: 0 additions & 2 deletions src/main/resources/spring/spring-app.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

<context:annotation-config/>

<context:component-scan base-package="ru.javawebinar.**.repository.jdbc"/>

<context:component-scan base-package="ru.javawebinar.**.service"/>

<context:component-scan base-package="ru.javawebinar.**.web"/>
Expand Down
13 changes: 8 additions & 5 deletions src/main/resources/spring/spring-db.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

<context:property-placeholder location="classpath:db/postgres.properties" system-properties-mode="OVERRIDE"/>

<bean class="ru.javawebinar.topjava.util.DbPopulator">
<constructor-arg name="scriptLocation" value="classpath:db/populateDB.sql"/>
</bean>
<context:component-scan base-package="ru.javawebinar.**.repository.jdbc"/>

<jdbc:initialize-database data-source="dataSource" enabled="${database.init}">
<jdbc:script location="classpath:db/initDB.sql"/>
<jdbc:script encoding="utf-8" location="classpath:db/populateDB.sql"/>
</jdbc:initialize-database>

<!--no pooling-->
<bean id="dataSource"
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/ru/javawebinar/topjava/SpringMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class SpringMain {
public static void main(String[] args) {
// java 7 Automatic resource management
try (ConfigurableApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/spring-app.xml")) {
try (ConfigurableApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/spring-app.xml","spring/mock.xml")) {
System.out.println("Bean definition names: " + Arrays.toString(appCtx.getBeanDefinitionNames()));
AdminRestController adminUserController = appCtx.getBean(AdminRestController.class);
adminUserController.create(UserTestData.USER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
* @param <T> : Entity
*/
public class ModelMatcher<T> {
public interface Comparator<T> {
boolean compare(T expected, T actual);
}

private static final Comparator DEFAULT_COMPARATOR =
(Object expected, Object actual) -> expected == actual || String.valueOf(expected).equals(String.valueOf(actual));

private Comparator<T> comparator;

public interface Comparator<T> {
boolean compare(T expected, T actual);
}

public ModelMatcher() {
this((Comparator<T>) DEFAULT_COMPARATOR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand All @@ -30,8 +29,6 @@ public class InMemoryMealRepositoryImpl implements MealRepository {

@Override
public Meal save(Meal meal, int userId) {
Objects.requireNonNull(meal);

Integer mealId = meal.getId();
if (meal.isNew()) {
mealId = counter.incrementAndGet();
Expand Down Expand Up @@ -63,8 +60,6 @@ public Collection<Meal> getAll(int userId) {

@Override
public Collection<Meal> getBetween(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId) {
Objects.requireNonNull(startDateTime);
Objects.requireNonNull(endDateTime);
return getAllStream(userId)
.filter(um -> TimeUtil.isBetween(um.getDateTime(), startDateTime, endDateTime))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand All @@ -31,7 +30,6 @@ public class InMemoryUserRepositoryImpl implements UserRepository {

@Override
public User save(User user) {
Objects.requireNonNull(user);
if (user.isNew()) {
user.setId(counter.incrementAndGet());
}
Expand Down Expand Up @@ -68,7 +66,6 @@ public List<User> getAll() {

@Override
public User getByEmail(String email) {
Objects.requireNonNull(email);
return repository.values().stream()
.filter(u -> email.equals(u.getEmail()))
.findFirst()
Expand Down
13 changes: 3 additions & 10 deletions src/test/java/ru/javawebinar/topjava/service/MealServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ru.javawebinar.topjava.service;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import ru.javawebinar.topjava.MealTestData;
import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.util.DbPopulator;
import ru.javawebinar.topjava.util.exception.NotFoundException;

import java.time.LocalDate;
Expand All @@ -24,19 +24,12 @@
"classpath:spring/spring-db.xml"
})
@RunWith(SpringJUnit4ClassRunner.class)
@Sql(scripts = "classpath:db/populateDB.sql", config = @SqlConfig(encoding = "UTF-8"))
public class MealServiceTest {

@Autowired
protected MealService service;

@Autowired
private DbPopulator dbPopulator;

@Before
public void setUp() throws Exception {
dbPopulator.execute();
}

@Test
public void testDelete() throws Exception {
service.delete(MealTestData.MEAL1_ID, USER_ID);
Expand Down
13 changes: 3 additions & 10 deletions src/test/java/ru/javawebinar/topjava/service/UserServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package ru.javawebinar.topjava.service;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import ru.javawebinar.topjava.model.Role;
import ru.javawebinar.topjava.model.User;
import ru.javawebinar.topjava.util.DbPopulator;
import ru.javawebinar.topjava.util.exception.NotFoundException;

import java.util.Arrays;
Expand All @@ -23,19 +23,12 @@
"classpath:spring/spring-db.xml"
})
@RunWith(SpringJUnit4ClassRunner.class)
@Sql(scripts = "classpath:db/populateDB.sql", config = @SqlConfig(encoding = "UTF-8"))
public class UserServiceTest {

@Autowired
protected UserService service;

@Autowired
private DbPopulator dbPopulator;

@Before
public void setUp() throws Exception {
dbPopulator.execute();
}

@Test
public void testSave() throws Exception {
User newUser = new User(null, "New", "[email protected]", "newPass", 1555, false, Collections.singleton(Role.ROLE_USER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* GKislin
* 13.03.2015.
*/
@ContextConfiguration("classpath:spring/spring-app.xml")
@ContextConfiguration({"classpath:spring/spring-app.xml", "classpath:spring/mock.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class InMemoryAdminRestControllerSpringTest {

Expand Down
Loading

0 comments on commit 96c8ab2

Please sign in to comment.