Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactorings and code style changes #807

Merged
merged 5 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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
  • Loading branch information
npathai committed Oct 20, 2018
commit 543eb9a4be5d556100db9a43898789d07a1f40a0
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ public List<E> asList() {
/**
* @return a FluentIterable from a given iterable. Calls the SimpleFluentIterable constructor.
*/
public static final <E> FluentIterable<E> from(Iterable<E> iterable) {
public static <E> FluentIterable<E> from(Iterable<E> iterable) {
return new SimpleFluentIterable<>(iterable);
}

public static final <E> FluentIterable<E> fromCopyOf(Iterable<E> iterable) {
public static <E> FluentIterable<E> fromCopyOf(Iterable<E> iterable) {
List<E> copy = FluentIterable.copyToList(iterable);
return new SimpleFluentIterable<>(copy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ public void handleRequest(String request) {
}

private Command getCommand(String request) {
Class commandClass = getCommandClass(request);
Class<?> commandClass = getCommandClass(request);
try {
return (Command) commandClass.newInstance();
} catch (Exception e) {
throw new ApplicationException(e);
}
}

private static Class getCommandClass(String request) {
Class result;
private static Class<?> getCommandClass(String request) {
Class<?> result;
try {
result = Class.forName("com.iluwatar.front.controller." + request + "Command");
} catch (ClassNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class ApplicationExceptionTest {

@Test
public void testCause() throws Exception {
public void testCause() {
final Exception cause = new Exception();
assertSame(cause, new ApplicationException(cause).getCause());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void testGet() {
GuardedQueue g = new GuardedQueue();
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(() -> value = g.get());
executorService.submit(() -> g.put(Integer.valueOf(10)));
executorService.submit(() -> g.put(10));
executorService.shutdown();
try {
executorService.awaitTermination(30, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class AppTest {

@Test
public void test() throws InterruptedException, ExecutionException {
public void test() {
App.main(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package com.iluwatar.halfsynchalfasync;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;

Expand All @@ -44,11 +45,17 @@
* @author Jeroen Meulemeester
*/
public class AsynchronousServiceTest {
private AsynchronousService service;
private AsyncTask<Object> task;

@BeforeEach
public void setUp() {
service = new AsynchronousService(new LinkedBlockingQueue<>());
task = mock(AsyncTask.class);
}

@Test
public void testPerfectExecution() throws Exception {
final AsynchronousService service = new AsynchronousService(new LinkedBlockingQueue<>());
final AsyncTask<Object> task = mock(AsyncTask.class);
final Object result = new Object();
when(task.call()).thenReturn(result);
service.execute(task);
Expand All @@ -65,8 +72,6 @@ public void testPerfectExecution() throws Exception {

@Test
public void testCallException() throws Exception {
final AsynchronousService service = new AsynchronousService(new LinkedBlockingQueue<>());
final AsyncTask<Object> task = mock(AsyncTask.class);
final IOException exception = new IOException();
when(task.call()).thenThrow(exception);
service.execute(task);
Expand All @@ -82,9 +87,7 @@ public void testCallException() throws Exception {
}

@Test
public void testPreCallException() throws Exception {
final AsynchronousService service = new AsynchronousService(new LinkedBlockingQueue<>());
final AsyncTask<Object> task = mock(AsyncTask.class);
public void testPreCallException() {
final IllegalStateException exception = new IllegalStateException();
doThrow(exception).when(task).onPreCall();
service.execute(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
*/
package com.iluwatar.intercepting.filter;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
Expand All @@ -33,6 +30,9 @@
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.GridLayout;

/**
* The Client class is responsible for handling the input and running them through filters inside the
Expand Down Expand Up @@ -60,7 +60,7 @@ public class Client extends JFrame { // NOSONAR
*/
public Client() {
super("Client System");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(300, 300);
jl = new JLabel("RUNNING...");
jtFields = new JTextField[3];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
*/
package com.iluwatar.intercepting.filter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* This is where the requests are displayed after being validated by filters.
Expand All @@ -47,7 +47,6 @@ public class Target extends JFrame { //NOSONAR
private static final long serialVersionUID = 1L;

private JTable jt;
private JScrollPane jsp;
private DefaultTableModel dtm;
private JButton del;

Expand All @@ -56,7 +55,7 @@ public class Target extends JFrame { //NOSONAR
*/
public Target() {
super("Order System");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(640, 480);
dtm =
new DefaultTableModel(new Object[] {"Name", "Contact Number", "Address", "Deposit Number",
Expand All @@ -73,7 +72,7 @@ private void setup() {
bot.setLayout(new BorderLayout());
bot.add(del, BorderLayout.EAST);
add(bot, BorderLayout.SOUTH);
jsp = new JScrollPane(jt);
JScrollPane jsp = new JScrollPane(jt);
jsp.setPreferredSize(new Dimension(500, 250));
add(jsp, BorderLayout.CENTER);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
public class FilterManagerTest {

@Test
public void testFilterRequest() throws Exception {
public void testFilterRequest() {
final Target target = mock(Target.class);
final FilterManager filterManager = new FilterManager();
assertEquals("RUNNING...", filterManager.filterRequest(mock(Order.class)));
verifyZeroInteractions(target);
}

@Test
public void testAddFilter() throws Exception {
public void testAddFilter() {
final Target target = mock(Target.class);
final FilterManager filterManager = new FilterManager();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ static List<Object[]> getTestData() {

@ParameterizedTest
@MethodSource("getTestData")
public void testExecute(Filter filter, Order order, String expectedResult) throws Exception {
public void testExecute(Filter filter, Order order, String expectedResult) {
final String result = filter.execute(order);
assertNotNull(result);
assertEquals(expectedResult, result.trim());
}

@ParameterizedTest
@MethodSource("getTestData")
public void testNext(Filter filter) throws Exception {
public void testNext(Filter filter) {
assertNull(filter.getNext());
assertSame(filter, filter.getLast());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,35 @@ public class OrderTest {
private static final String EXPECTED_VALUE = "test";

@Test
public void testSetName() throws Exception {
public void testSetName() {
final Order order = new Order();
order.setName(EXPECTED_VALUE);
assertEquals(EXPECTED_VALUE, order.getName());
}

@Test
public void testSetContactNumber() throws Exception {
public void testSetContactNumber() {
final Order order = new Order();
order.setContactNumber(EXPECTED_VALUE);
assertEquals(EXPECTED_VALUE, order.getContactNumber());
}

@Test
public void testSetAddress() throws Exception {
public void testSetAddress() {
final Order order = new Order();
order.setAddress(EXPECTED_VALUE);
assertEquals(EXPECTED_VALUE, order.getAddress());
}

@Test
public void testSetDepositNumber() throws Exception {
public void testSetDepositNumber() {
final Order order = new Order();
order.setDepositNumber(EXPECTED_VALUE);
assertEquals(EXPECTED_VALUE, order.getDepositNumber());
}

@Test
public void testSetOrder() throws Exception {
public void testSetOrder() {
final Order order = new Order();
order.setOrderItem(EXPECTED_VALUE);
assertEquals(EXPECTED_VALUE, order.getOrderItem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,49 +50,49 @@ void createTrees() {

@Test
void nextForEmptyTree() {
BstIterator iter = new BstIterator<>(emptyRoot);
BstIterator<Integer> iter = new BstIterator<>(emptyRoot);
assertThrows(NoSuchElementException.class, iter::next,
"next() should throw an IllegalStateException if hasNext() is false.");
}

@Test
void nextOverEntirePopulatedTree() {
BstIterator iter = new BstIterator<>(nonEmptyRoot);
assertEquals(1, iter.next().getVal(), "First Node is 1.");
assertEquals(3, iter.next().getVal(), "Second Node is 3.");
assertEquals(4, iter.next().getVal(), "Third Node is 4.");
assertEquals(5, iter.next().getVal(), "Fourth Node is 5.");
assertEquals(6, iter.next().getVal(), "Fifth Node is 6.");
assertEquals(7, iter.next().getVal(), "Sixth Node is 7.");
BstIterator<Integer> iter = new BstIterator<>(nonEmptyRoot);
assertEquals(Integer.valueOf(1), iter.next().getVal(), "First Node is 1.");
assertEquals(Integer.valueOf(3), iter.next().getVal(), "Second Node is 3.");
assertEquals(Integer.valueOf(4), iter.next().getVal(), "Third Node is 4.");
assertEquals(Integer.valueOf(5), iter.next().getVal(), "Fourth Node is 5.");
assertEquals(Integer.valueOf(6), iter.next().getVal(), "Fifth Node is 6.");
assertEquals(Integer.valueOf(7), iter.next().getVal(), "Sixth Node is 7.");
}

@Test
void hasNextForEmptyTree() {
BstIterator iter = new BstIterator<>(emptyRoot);
BstIterator<Integer> iter = new BstIterator<>(emptyRoot);
assertFalse(iter.hasNext(), "hasNext() should return false for empty tree.");
}

@Test
void hasNextForPopulatedTree() {
BstIterator iter = new BstIterator<>(nonEmptyRoot);
BstIterator<Integer> iter = new BstIterator<>(nonEmptyRoot);
assertTrue(iter.hasNext(), "hasNext() should return true for populated tree.");
}

@Test
void nextAndHasNextOverEntirePopulatedTree() {
BstIterator iter = new BstIterator<>(nonEmptyRoot);
BstIterator<Integer> iter = new BstIterator<>(nonEmptyRoot);
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
assertEquals(1, iter.next().getVal(), "First Node is 1.");
assertEquals(Integer.valueOf(1), iter.next().getVal(), "First Node is 1.");
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
assertEquals(3, iter.next().getVal(), "Second Node is 3.");
assertEquals(Integer.valueOf(3), iter.next().getVal(), "Second Node is 3.");
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
assertEquals(4, iter.next().getVal(), "Third Node is 4.");
assertEquals(Integer.valueOf(4), iter.next().getVal(), "Third Node is 4.");
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
assertEquals(5, iter.next().getVal(), "Fourth Node is 5.");
assertEquals(Integer.valueOf(5), iter.next().getVal(), "Fourth Node is 5.");
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
assertEquals(6, iter.next().getVal(), "Fifth Node is 6.");
assertEquals(Integer.valueOf(6), iter.next().getVal(), "Fifth Node is 6.");
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
assertEquals(7, iter.next().getVal(), "Sixth Node is 7.");
assertEquals(Integer.valueOf(7), iter.next().getVal(), "Sixth Node is 7.");
assertFalse(iter.hasNext(), "Iterator hasNext() should be false, end of tree.");
}

Expand Down
2 changes: 1 addition & 1 deletion layers/src/main/java/com/iluwatar/layers/CakeViewImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public CakeViewImpl(CakeBakingService cakeBakingService) {
}

public void render() {
cakeBakingService.getAllCakes().stream().forEach(cake -> LOGGER.info(cake.toString()));
cakeBakingService.getAllCakes().forEach(cake -> LOGGER.info(cake.toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
public class CakeBakingExceptionTest {

@Test
public void testConstructor() throws Exception {
public void testConstructor() {
final CakeBakingException exception = new CakeBakingException();
assertNull(exception.getMessage());
assertNull(exception.getCause());
}

@Test
public void testConstructorWithMessage() throws Exception {
public void testConstructorWithMessage() {
final String expectedMessage = "message";
final CakeBakingException exception = new CakeBakingException(expectedMessage);
assertEquals(expectedMessage, exception.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public class CakeBakingServiceImplTest {

@Test
public void testLayers() throws CakeBakingException {
public void testLayers() {
final CakeBakingServiceImpl service = new CakeBakingServiceImpl();

final List<CakeLayerInfo> initialLayers = service.getAvailableLayers();
Expand All @@ -65,7 +65,7 @@ public void testLayers() throws CakeBakingException {
}

@Test
public void testToppings() throws CakeBakingException {
public void testToppings() {
final CakeBakingServiceImpl service = new CakeBakingServiceImpl();

final List<CakeToppingInfo> initialToppings = service.getAvailableToppings();
Expand Down Expand Up @@ -125,7 +125,7 @@ public void testBakeCakes() throws CakeBakingException {
}

@Test
public void testBakeCakeMissingTopping() throws CakeBakingException {
public void testBakeCakeMissingTopping() {
final CakeBakingServiceImpl service = new CakeBakingServiceImpl();

final CakeLayerInfo layer1 = new CakeLayerInfo("Layer1", 1000);
Expand All @@ -140,7 +140,7 @@ public void testBakeCakeMissingTopping() throws CakeBakingException {
}

@Test
public void testBakeCakeMissingLayer() throws CakeBakingException {
public void testBakeCakeMissingLayer() {
final CakeBakingServiceImpl service = new CakeBakingServiceImpl();

final List<CakeInfo> initialCakes = service.getAllCakes();
Expand Down
Loading