Skip to content

Commit

Permalink
Minor refactorings and code style changes (iluwatar#807)
Browse files Browse the repository at this point in the history
* Made minor changes in some patterns such as removed throws clause where not needed, changed incorrect order of arguments in assertEquals

* Minor refactorings and code style changes. 1) Removed several use of raw types 2) Removed unnecessary throws clauses 3) Used lambda expressions wherever applicable 4) Used apt assertion methods for readability 5) Use of try with resources wherever applicable 6) Corrected incorrect order of assertXXX arguments

* Removed unused import from Promise

* Addressed review comments

* Addressed checkstyle issue
  • Loading branch information
npathai authored Oct 23, 2018
1 parent 25ed7c0 commit 2aa9e78
Show file tree
Hide file tree
Showing 112 changed files with 312 additions and 463 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* AbstractDocument test class
Expand Down Expand Up @@ -81,8 +82,8 @@ public void shouldIncludePropsInToString() {
Map<String, Object> props = new HashMap<>();
props.put(KEY, VALUE);
DocumentImplementation document = new DocumentImplementation(props);
assertNotNull(document.toString().contains(KEY));
assertNotNull(document.toString().contains(VALUE));
assertTrue(document.toString().contains(KEY));
assertTrue(document.toString().contains(VALUE));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,19 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
import static uk.org.lidalia.slf4jext.Level.INFO;
import static org.mockito.Mockito.mock;

import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import com.iluwatar.acyclicvisitor.ConfigureForUnixVisitor;
import com.iluwatar.acyclicvisitor.Hayes;
import com.iluwatar.acyclicvisitor.HayesVisitor;
import com.iluwatar.acyclicvisitor.Zoom;
import com.iluwatar.acyclicvisitor.ZoomVisitor;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;

/**
* ConfigureForUnixVisitor test class
*/
public class ConfigureForUnixVisitorTest {

TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForUnixVisitor.class);
private static final TestLogger LOGGER = TestLoggerFactory.getTestLogger(ConfigureForUnixVisitor.class);

@AfterEach
public void clearLoggers() {
Expand All @@ -59,7 +51,7 @@ public void testVisitForZoom() {

conUnix.visit(zoom);

assertThat(logger.getLoggingEvents()).extracting("level", "message").contains(
assertThat(LOGGER.getLoggingEvents()).extracting("level", "message").contains(
tuple(INFO, zoom + " used with Unix configurator."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
public class InventoryControllerTest {
@Test
public void testGetProductInventories() throws Exception {
public void testGetProductInventories() {
InventoryController inventoryController = new InventoryController();

int numberOfInventories = inventoryController.getProductInventories();
Expand Down
2 changes: 1 addition & 1 deletion bridge/src/test/java/com/iluwatar/bridge/HammerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class HammerTest extends WeaponTest {
* underlying weapon implementation.
*/
@Test
public void testHammer() throws Exception {
public void testHammer() {
final Hammer hammer = spy(new Hammer(mock(FlyingEnchantment.class)));
testBasicWeaponActions(hammer);
}
Expand Down
2 changes: 1 addition & 1 deletion bridge/src/test/java/com/iluwatar/bridge/SwordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class SwordTest extends WeaponTest {
* underlying weapon implementation.
*/
@Test
public void testSword() throws Exception {
public void testSword() {
final Sword sword = spy(new Sword(mock(FlyingEnchantment.class)));
testBasicWeaponActions(sword);
}
Expand Down
7 changes: 1 addition & 6 deletions callback/src/main/java/com/iluwatar/callback/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ public class App {
*/
public static void main(String[] args) {
Task task = new SimpleTask();
Callback callback = new Callback() {
@Override
public void call() {
LOGGER.info("I'm done now.");
}
};
Callback callback = () -> LOGGER.info("I'm done now.");
task.executeWith(callback);
}
}
23 changes: 0 additions & 23 deletions callback/src/test/java/com/iluwatar/callback/CallbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,6 @@ public class CallbackTest {

@Test
public void test() {
Callback callback = new Callback() {
@Override
public void call() {
callingCount++;
}
};

Task task = new SimpleTask();

assertEquals(new Integer(0), callingCount, "Initial calling count of 0");

task.executeWith(callback);

assertEquals(new Integer(1), callingCount, "Callback called once");

task.executeWith(callback);

assertEquals(new Integer(2), callingCount, "Callback called twice");

}

@Test
public void testWithLambdasExample() {
Callback callback = () -> callingCount++;

Task task = new SimpleTask();
Expand Down
2 changes: 1 addition & 1 deletion chain/src/test/java/com/iluwatar/chain/OrcKingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class OrcKingTest {
};

@Test
public void testMakeRequest() throws Exception {
public void testMakeRequest() {
final OrcKing king = new OrcKing();

for (final Request request : REQUESTS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
* A Car class that has the properties of make, model, year and category.
*/
public class Car {
private String make;
private String model;
private int year;
private Category category;
private final String make;
private final String model;
private final int year;
private final Category category;

/**
* Constructor to create an instance of car.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void testCustomConverter() {
user.getFirstName().toLowerCase() + user.getLastName().toLowerCase() + "@whatever.com"));
User u1 = new User("John", "Doe", false, "12324");
UserDto userDto = converter.convertFromEntity(u1);
assertEquals(userDto.getEmail(), "[email protected]");
assertEquals("[email protected]", userDto.getEmail());
}

/**
Expand All @@ -83,6 +83,6 @@ public void testCollectionConversion() {
ArrayList<User> users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"),
new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
List<User> fromDtos = userConverter.createFromDtos(userConverter.createFromEntities(users));
assertEquals(fromDtos, users);
assertEquals(users, fromDtos);
}
}
2 changes: 1 addition & 1 deletion cqrs/src/test/java/com/iluwatar/cqrs/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void testGetBook() {
@Test
public void testGetAuthorBooks() {
List<Book> books = queryService.getAuthorBooks("username1");
assertTrue(books.size() == 2);
assertEquals(2, books.size());
assertTrue(books.contains(new Book("title1", 10)));
assertTrue(books.contains(new Book("new_title2", 30)));
}
Expand Down
10 changes: 2 additions & 8 deletions dao/src/test/java/com/iluwatar/dao/CustomerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,7 @@ public void equalsWithSameObjects() {

@Test
public void testToString() {
final StringBuffer buffer = new StringBuffer();
buffer.append("Customer{id=")
.append("" + customer.getId())
.append(", firstName='")
.append(customer.getFirstName())
.append("\', lastName='")
.append(customer.getLastName() + "\'}");
assertEquals(buffer.toString(), customer.toString());
assertEquals(String.format("Customer{id=%s, firstName='%s', lastName='%s'}",
customer.getId(), customer.getFirstName(), customer.getLastName()), customer.toString());
}
}
2 changes: 1 addition & 1 deletion dao/src/test/java/com/iluwatar/dao/DbCustomerDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void deleteSchema() throws SQLException {

private void assertCustomerCountIs(int count) throws Exception {
try (Stream<Customer> allCustomers = dao.getAll()) {
assertTrue(allCustomers.count() == count);
assertEquals(count, allCustomers.count());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private int getNonExistingCustomerId() {

private void assertCustomerCountIs(int count) throws Exception {
try (Stream<Customer> allCustomers = dao.getAll()) {
assertTrue(allCustomers.count() == count);
assertEquals(count, allCustomers.count());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* The Data Mapper (DM) is a layer of software that separates the in-memory objects from the
Expand All @@ -43,27 +44,29 @@ public void testFirstDataMapper() {
final StudentDataMapper mapper = new StudentDataMapperImpl();

/* Create new student */
Student student = new Student(1, "Adam", 'A');
int studentId = 1;
Student student = new Student(studentId, "Adam", 'A');

/* Add student in respectibe db */
mapper.insert(student);

/* Check if student is added in db */
assertEquals(student.getStudentId(), mapper.find(student.getStudentId()).get().getStudentId());
assertEquals(studentId, mapper.find(student.getStudentId()).get().getStudentId());

/* Update existing student object */
student = new Student(student.getStudentId(), "AdamUpdated", 'A');
String updatedName = "AdamUpdated";
student = new Student(student.getStudentId(), updatedName, 'A');

/* Update student in respectibe db */
mapper.update(student);

/* Check if student is updated in db */
assertEquals(mapper.find(student.getStudentId()).get().getName(), "AdamUpdated");
assertEquals(updatedName, mapper.find(student.getStudentId()).get().getName());

/* Delete student in db */
mapper.delete(student);

/* Result should be false */
assertEquals(false, mapper.find(student.getStudentId()).isPresent());
assertFalse(mapper.find(student.getStudentId()).isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,37 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests {@link Student}.
*/
public final class StudentTest {

@Test
/**
* This API tests the equality behaviour of Student object
* Object Equality should work as per logic defined in equals method
*
* @throws Exception if any execution error during test
*/
@Test
public void testEquality() throws Exception {

/* Create some students */
final Student firstStudent = new Student(1, "Adam", 'A');
final Student secondStudent = new Student(2, "Donald", 'B');
final Student secondSameStudent = new Student(2, "Donald", 'B');
final Student firstSameStudent = firstStudent;

/* Check equals functionality: should return 'true' */
assertTrue(firstStudent.equals(firstSameStudent));
assertEquals(firstStudent, firstStudent);

/* Check equals functionality: should return 'false' */
assertFalse(firstStudent.equals(secondStudent));
assertNotEquals(firstStudent, secondStudent);

/* Check equals functionality: should return 'true' */
assertTrue(secondStudent.equals(secondSameStudent));
assertEquals(secondStudent, secondSameStudent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* tests {@link CustomerResource}.
Expand All @@ -45,10 +46,10 @@ public void shouldGetAllCustomers() {

List<CustomerDto> allCustomers = customerResource.getAllCustomers();

assertEquals(allCustomers.size(), 1);
assertEquals(allCustomers.get(0).getId(), "1");
assertEquals(allCustomers.get(0).getFirstName(), "Melody");
assertEquals(allCustomers.get(0).getLastName(), "Yates");
assertEquals(1, allCustomers.size());
assertEquals("1", allCustomers.get(0).getId());
assertEquals("Melody", allCustomers.get(0).getFirstName());
assertEquals("Yates", allCustomers.get(0).getLastName());
}

@Test
Expand All @@ -59,9 +60,9 @@ public void shouldSaveCustomer() {
customerResource.save(customer);

List<CustomerDto> allCustomers = customerResource.getAllCustomers();
assertEquals(allCustomers.get(0).getId(), "1");
assertEquals(allCustomers.get(0).getFirstName(), "Rita");
assertEquals(allCustomers.get(0).getLastName(), "Reynolds");
assertEquals("1", allCustomers.get(0).getId());
assertEquals("Rita", allCustomers.get(0).getFirstName());
assertEquals("Reynolds", allCustomers.get(0).getLastName());
}

@Test
Expand All @@ -75,7 +76,7 @@ public void shouldDeleteCustomer() {
customerResource.delete(customer.getId());

List<CustomerDto> allCustomers = customerResource.getAllCustomers();
assertEquals(allCustomers.size(), 0);
assertTrue(allCustomers.isEmpty());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
public class App {

public static final String MESSAGE_TO_PRINT = "hello world";
private static final String MESSAGE_TO_PRINT = "hello world";

/**
* Program entry point
Expand Down
3 changes: 2 additions & 1 deletion dirty-flag/src/test/java/org/dirty/flag/DirtyFlagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package org.dirty.flag;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
Expand All @@ -41,7 +42,7 @@ public class DirtyFlagTest {
public void testIsDirty() {
DataFetcher df = new DataFetcher();
List<String> countries = df.fetch();
assertTrue(!countries.isEmpty());
assertFalse(countries.isEmpty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class AggregatorRoute extends RouteBuilder {
* @throws Exception in case of exception during configuration
*/
@Override
public void configure() throws Exception {
public void configure() {
// Main route
from("{{entry}}").aggregate(constant(true), aggregator)
.completionSize(3).completionInterval(2000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void configure() throws Exception {
});

context.start();
context.getRoutes().stream().forEach(r -> LOGGER.info(r.toString()));
context.getRoutes().forEach(r -> LOGGER.info(r.toString()));
context.stop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void configure() throws Exception {
});
ProducerTemplate template = context.createProducerTemplate();
context.start();
context.getRoutes().stream().forEach(r -> LOGGER.info(r.toString()));
context.getRoutes().forEach(r -> LOGGER.info(r.toString()));
template.sendBody("direct:origin", "Hello from origin");
context.stop();
}
Expand Down
Loading

0 comments on commit 2aa9e78

Please sign in to comment.