Skip to content

Commit 2aa9e78

Browse files
authored
Minor refactorings and code style changes (iluwatar#807)
* 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
1 parent 25ed7c0 commit 2aa9e78

File tree

112 files changed

+312
-463
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+312
-463
lines changed

abstract-document/src/test/java/com/iluwatar/abstractdocument/AbstractDocumentTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import static org.junit.jupiter.api.Assertions.assertEquals;
3434
import static org.junit.jupiter.api.Assertions.assertNotNull;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
3536

3637
/**
3738
* AbstractDocument test class
@@ -81,8 +82,8 @@ public void shouldIncludePropsInToString() {
8182
Map<String, Object> props = new HashMap<>();
8283
props.put(KEY, VALUE);
8384
DocumentImplementation document = new DocumentImplementation(props);
84-
assertNotNull(document.toString().contains(KEY));
85-
assertNotNull(document.toString().contains(VALUE));
85+
assertTrue(document.toString().contains(KEY));
86+
assertTrue(document.toString().contains(VALUE));
8687
}
8788

8889
}

acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java

+5-13
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,19 @@
2525
import static org.assertj.core.api.Assertions.assertThat;
2626
import static org.assertj.core.groups.Tuple.tuple;
2727
import static uk.org.lidalia.slf4jext.Level.INFO;
28-
import static org.mockito.Mockito.mock;
2928

30-
import uk.org.lidalia.slf4jtest.TestLogger;
31-
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
29+
import org.junit.jupiter.api.AfterEach;
3230
import org.junit.jupiter.api.Test;
3331

34-
import com.iluwatar.acyclicvisitor.ConfigureForUnixVisitor;
35-
import com.iluwatar.acyclicvisitor.Hayes;
36-
import com.iluwatar.acyclicvisitor.HayesVisitor;
37-
import com.iluwatar.acyclicvisitor.Zoom;
38-
import com.iluwatar.acyclicvisitor.ZoomVisitor;
39-
40-
import org.junit.jupiter.api.AfterEach;
41-
import org.junit.jupiter.api.Assertions;
32+
import uk.org.lidalia.slf4jtest.TestLogger;
33+
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
4234

4335
/**
4436
* ConfigureForUnixVisitor test class
4537
*/
4638
public class ConfigureForUnixVisitorTest {
4739

48-
TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForUnixVisitor.class);
40+
private static final TestLogger LOGGER = TestLoggerFactory.getTestLogger(ConfigureForUnixVisitor.class);
4941

5042
@AfterEach
5143
public void clearLoggers() {
@@ -59,7 +51,7 @@ public void testVisitForZoom() {
5951

6052
conUnix.visit(zoom);
6153

62-
assertThat(logger.getLoggingEvents()).extracting("level", "message").contains(
54+
assertThat(LOGGER.getLoggingEvents()).extracting("level", "message").contains(
6355
tuple(INFO, zoom + " used with Unix configurator."));
6456
}
6557
}

aggregator-microservices/inventory-microservice/src/test/java/com/iluwatar/inventory/microservice/InventoryControllerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
public class InventoryControllerTest {
3333
@Test
34-
public void testGetProductInventories() throws Exception {
34+
public void testGetProductInventories() {
3535
InventoryController inventoryController = new InventoryController();
3636

3737
int numberOfInventories = inventoryController.getProductInventories();

bridge/src/test/java/com/iluwatar/bridge/HammerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class HammerTest extends WeaponTest {
3737
* underlying weapon implementation.
3838
*/
3939
@Test
40-
public void testHammer() throws Exception {
40+
public void testHammer() {
4141
final Hammer hammer = spy(new Hammer(mock(FlyingEnchantment.class)));
4242
testBasicWeaponActions(hammer);
4343
}

bridge/src/test/java/com/iluwatar/bridge/SwordTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class SwordTest extends WeaponTest {
3737
* underlying weapon implementation.
3838
*/
3939
@Test
40-
public void testSword() throws Exception {
40+
public void testSword() {
4141
final Sword sword = spy(new Sword(mock(FlyingEnchantment.class)));
4242
testBasicWeaponActions(sword);
4343
}

callback/src/main/java/com/iluwatar/callback/App.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ public class App {
4141
*/
4242
public static void main(String[] args) {
4343
Task task = new SimpleTask();
44-
Callback callback = new Callback() {
45-
@Override
46-
public void call() {
47-
LOGGER.info("I'm done now.");
48-
}
49-
};
44+
Callback callback = () -> LOGGER.info("I'm done now.");
5045
task.executeWith(callback);
5146
}
5247
}

callback/src/test/java/com/iluwatar/callback/CallbackTest.java

-23
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,6 @@ public class CallbackTest {
3838

3939
@Test
4040
public void test() {
41-
Callback callback = new Callback() {
42-
@Override
43-
public void call() {
44-
callingCount++;
45-
}
46-
};
47-
48-
Task task = new SimpleTask();
49-
50-
assertEquals(new Integer(0), callingCount, "Initial calling count of 0");
51-
52-
task.executeWith(callback);
53-
54-
assertEquals(new Integer(1), callingCount, "Callback called once");
55-
56-
task.executeWith(callback);
57-
58-
assertEquals(new Integer(2), callingCount, "Callback called twice");
59-
60-
}
61-
62-
@Test
63-
public void testWithLambdasExample() {
6441
Callback callback = () -> callingCount++;
6542

6643
Task task = new SimpleTask();

chain/src/test/java/com/iluwatar/chain/OrcKingTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class OrcKingTest {
4343
};
4444

4545
@Test
46-
public void testMakeRequest() throws Exception {
46+
public void testMakeRequest() {
4747
final OrcKing king = new OrcKing();
4848

4949
for (final Request request : REQUESTS) {

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
* A Car class that has the properties of make, model, year and category.
2727
*/
2828
public class Car {
29-
private String make;
30-
private String model;
31-
private int year;
32-
private Category category;
29+
private final String make;
30+
private final String model;
31+
private final int year;
32+
private final Category category;
3333

3434
/**
3535
* Constructor to create an instance of car.

converter/src/test/java/com/iluwatar/converter/ConverterTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void testCustomConverter() {
7171
user.getFirstName().toLowerCase() + user.getLastName().toLowerCase() + "@whatever.com"));
7272
User u1 = new User("John", "Doe", false, "12324");
7373
UserDto userDto = converter.convertFromEntity(u1);
74-
assertEquals(userDto.getEmail(), "johndoe@whatever.com");
74+
assertEquals("johndoe@whatever.com", userDto.getEmail());
7575
}
7676

7777
/**
@@ -83,6 +83,6 @@ public void testCollectionConversion() {
8383
ArrayList<User> users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"),
8484
new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
8585
List<User> fromDtos = userConverter.createFromDtos(userConverter.createFromEntities(users));
86-
assertEquals(fromDtos, users);
86+
assertEquals(users, fromDtos);
8787
}
8888
}

cqrs/src/test/java/com/iluwatar/cqrs/IntegrationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void testGetBook() {
9696
@Test
9797
public void testGetAuthorBooks() {
9898
List<Book> books = queryService.getAuthorBooks("username1");
99-
assertTrue(books.size() == 2);
99+
assertEquals(2, books.size());
100100
assertTrue(books.contains(new Book("title1", 10)));
101101
assertTrue(books.contains(new Book("new_title2", 30)));
102102
}

dao/src/test/java/com/iluwatar/dao/CustomerTest.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,7 @@ public void equalsWithSameObjects() {
8888

8989
@Test
9090
public void testToString() {
91-
final StringBuffer buffer = new StringBuffer();
92-
buffer.append("Customer{id=")
93-
.append("" + customer.getId())
94-
.append(", firstName='")
95-
.append(customer.getFirstName())
96-
.append("\', lastName='")
97-
.append(customer.getLastName() + "\'}");
98-
assertEquals(buffer.toString(), customer.toString());
91+
assertEquals(String.format("Customer{id=%s, firstName='%s', lastName='%s'}",
92+
customer.getId(), customer.getFirstName(), customer.getLastName()), customer.toString());
9993
}
10094
}

dao/src/test/java/com/iluwatar/dao/DbCustomerDaoTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public void deleteSchema() throws SQLException {
257257

258258
private void assertCustomerCountIs(int count) throws Exception {
259259
try (Stream<Customer> allCustomers = dao.getAll()) {
260-
assertTrue(allCustomers.count() == count);
260+
assertEquals(count, allCustomers.count());
261261
}
262262
}
263263

dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private int getNonExistingCustomerId() {
156156

157157
private void assertCustomerCountIs(int count) throws Exception {
158158
try (Stream<Customer> allCustomers = dao.getAll()) {
159-
assertTrue(allCustomers.count() == count);
159+
assertEquals(count, allCustomers.count());
160160
}
161161
}
162162
}

data-mapper/src/test/java/com/iluwatar/datamapper/DataMapperTest.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertFalse;
2425

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

4546
/* Create new student */
46-
Student student = new Student(1, "Adam", 'A');
47+
int studentId = 1;
48+
Student student = new Student(studentId, "Adam", 'A');
4749

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

5153
/* Check if student is added in db */
52-
assertEquals(student.getStudentId(), mapper.find(student.getStudentId()).get().getStudentId());
54+
assertEquals(studentId, mapper.find(student.getStudentId()).get().getStudentId());
5355

5456
/* Update existing student object */
55-
student = new Student(student.getStudentId(), "AdamUpdated", 'A');
57+
String updatedName = "AdamUpdated";
58+
student = new Student(student.getStudentId(), updatedName, 'A');
5659

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

6063
/* Check if student is updated in db */
61-
assertEquals(mapper.find(student.getStudentId()).get().getName(), "AdamUpdated");
64+
assertEquals(updatedName, mapper.find(student.getStudentId()).get().getName());
6265

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

6669
/* Result should be false */
67-
assertEquals(false, mapper.find(student.getStudentId()).isPresent());
70+
assertFalse(mapper.find(student.getStudentId()).isPresent());
6871
}
6972
}

data-mapper/src/test/java/com/iluwatar/datamapper/StudentTest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,37 @@
2020

2121
import org.junit.jupiter.api.Test;
2222

23+
import static org.junit.jupiter.api.Assertions.assertEquals;
2324
import static org.junit.jupiter.api.Assertions.assertFalse;
25+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2426
import static org.junit.jupiter.api.Assertions.assertTrue;
2527

2628
/**
2729
* Tests {@link Student}.
2830
*/
2931
public final class StudentTest {
3032

31-
@Test
3233
/**
3334
* This API tests the equality behaviour of Student object
3435
* Object Equality should work as per logic defined in equals method
3536
*
3637
* @throws Exception if any execution error during test
3738
*/
39+
@Test
3840
public void testEquality() throws Exception {
3941

4042
/* Create some students */
4143
final Student firstStudent = new Student(1, "Adam", 'A');
4244
final Student secondStudent = new Student(2, "Donald", 'B');
4345
final Student secondSameStudent = new Student(2, "Donald", 'B');
44-
final Student firstSameStudent = firstStudent;
4546

4647
/* Check equals functionality: should return 'true' */
47-
assertTrue(firstStudent.equals(firstSameStudent));
48+
assertEquals(firstStudent, firstStudent);
4849

4950
/* Check equals functionality: should return 'false' */
50-
assertFalse(firstStudent.equals(secondStudent));
51+
assertNotEquals(firstStudent, secondStudent);
5152

5253
/* Check equals functionality: should return 'true' */
53-
assertTrue(secondStudent.equals(secondSameStudent));
54+
assertEquals(secondStudent, secondSameStudent);
5455
}
5556
}

data-transfer-object/src/test/java/com/iluwatar/datatransfer/CustomerResourceTest.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.List;
3131

3232
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
3334

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

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

48-
assertEquals(allCustomers.size(), 1);
49-
assertEquals(allCustomers.get(0).getId(), "1");
50-
assertEquals(allCustomers.get(0).getFirstName(), "Melody");
51-
assertEquals(allCustomers.get(0).getLastName(), "Yates");
49+
assertEquals(1, allCustomers.size());
50+
assertEquals("1", allCustomers.get(0).getId());
51+
assertEquals("Melody", allCustomers.get(0).getFirstName());
52+
assertEquals("Yates", allCustomers.get(0).getLastName());
5253
}
5354

5455
@Test
@@ -59,9 +60,9 @@ public void shouldSaveCustomer() {
5960
customerResource.save(customer);
6061

6162
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
62-
assertEquals(allCustomers.get(0).getId(), "1");
63-
assertEquals(allCustomers.get(0).getFirstName(), "Rita");
64-
assertEquals(allCustomers.get(0).getLastName(), "Reynolds");
63+
assertEquals("1", allCustomers.get(0).getId());
64+
assertEquals("Rita", allCustomers.get(0).getFirstName());
65+
assertEquals("Reynolds", allCustomers.get(0).getLastName());
6566
}
6667

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

7778
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
78-
assertEquals(allCustomers.size(), 0);
79+
assertTrue(allCustomers.isEmpty());
7980
}
8081

8182
}

delegation/src/main/java/com/iluwatar/delegation/simple/App.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*/
4141
public class App {
4242

43-
public static final String MESSAGE_TO_PRINT = "hello world";
43+
private static final String MESSAGE_TO_PRINT = "hello world";
4444

4545
/**
4646
* Program entry point

dirty-flag/src/test/java/org/dirty/flag/DirtyFlagTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package org.dirty.flag;
2424

25+
import static org.junit.jupiter.api.Assertions.assertFalse;
2526
import static org.junit.jupiter.api.Assertions.assertTrue;
2627

2728
import java.util.List;
@@ -41,7 +42,7 @@ public class DirtyFlagTest {
4142
public void testIsDirty() {
4243
DataFetcher df = new DataFetcher();
4344
List<String> countries = df.fetch();
44-
assertTrue(!countries.isEmpty());
45+
assertFalse(countries.isEmpty());
4546
}
4647

4748
@Test

eip-aggregator/src/main/java/com/iluwatar/eip/aggregator/routes/AggregatorRoute.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class AggregatorRoute extends RouteBuilder {
5151
* @throws Exception in case of exception during configuration
5252
*/
5353
@Override
54-
public void configure() throws Exception {
54+
public void configure() {
5555
// Main route
5656
from("{{entry}}").aggregate(constant(true), aggregator)
5757
.completionSize(3).completionInterval(2000)

eip-message-channel/src/main/java/com/iluwatar/eip/message/channel/App.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void configure() throws Exception {
7070
});
7171

7272
context.start();
73-
context.getRoutes().stream().forEach(r -> LOGGER.info(r.toString()));
73+
context.getRoutes().forEach(r -> LOGGER.info(r.toString()));
7474
context.stop();
7575
}
7676
}

eip-publish-subscribe/src/main/java/com/iluwatar/eip/publish/subscribe/App.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void configure() throws Exception {
6565
});
6666
ProducerTemplate template = context.createProducerTemplate();
6767
context.start();
68-
context.getRoutes().stream().forEach(r -> LOGGER.info(r.toString()));
68+
context.getRoutes().forEach(r -> LOGGER.info(r.toString()));
6969
template.sendBody("direct:origin", "Hello from origin");
7070
context.stop();
7171
}

0 commit comments

Comments
 (0)