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

This commit is contained in:
Narendra Pathai
2018-10-20 17:50:52 +05:30
parent 2f569d670a
commit 543eb9a4be
73 changed files with 207 additions and 298 deletions

View File

@@ -1,50 +0,0 @@
<?xml version="1.0"?>
<!--
The MIT License
Copyright (c) 2014-2016 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>dao</artifactId>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -38,10 +38,9 @@ public class SpellDaoImpl extends DaoBaseImpl<Spell> implements SpellDao {
@Override
public Spell findByName(String name) {
Session session = getSessionFactory().openSession();
Transaction tx = null;
Spell result = null;
try {
try (Session session = getSessionFactory().openSession()) {
tx = session.beginTransaction();
Criteria criteria = session.createCriteria(persistentClass);
criteria.add(Restrictions.eq("name", name));
@@ -52,8 +51,6 @@ public class SpellDaoImpl extends DaoBaseImpl<Spell> implements SpellDao {
tx.rollback();
}
throw e;
} finally {
session.close();
}
return result;
}

View File

@@ -40,7 +40,7 @@ public class AppTest {
}
@AfterEach
public void tearDown() throws Exception {
public void tearDown() {
HibernateUtil.dropSession();
}

View File

@@ -76,7 +76,7 @@ public abstract class BaseDaoTest<E extends BaseEntity, D extends DaoBaseImpl<E>
}
@BeforeEach
public void setUp() throws Exception {
public void setUp() {
for (int i = 0; i < INITIAL_COUNT; i++) {
final String className = dao.persistentClass.getSimpleName();
final String entityName = String.format("%s%d", className, ID_GENERATOR.incrementAndGet());
@@ -85,7 +85,7 @@ public abstract class BaseDaoTest<E extends BaseEntity, D extends DaoBaseImpl<E>
}
@AfterEach
public void tearDown() throws Exception {
public void tearDown() {
HibernateUtil.dropSession();
}
@@ -94,7 +94,7 @@ public abstract class BaseDaoTest<E extends BaseEntity, D extends DaoBaseImpl<E>
}
@Test
public void testFind() throws Exception {
public void testFind() {
final List<E> all = this.dao.findAll();
for (final E entity : all) {
final E byId = this.dao.find(entity.getId());
@@ -104,7 +104,7 @@ public abstract class BaseDaoTest<E extends BaseEntity, D extends DaoBaseImpl<E>
}
@Test
public void testDelete() throws Exception {
public void testDelete() {
final List<E> originalEntities = this.dao.findAll();
this.dao.delete(originalEntities.get(1));
this.dao.delete(originalEntities.get(2));
@@ -115,24 +115,24 @@ public abstract class BaseDaoTest<E extends BaseEntity, D extends DaoBaseImpl<E>
}
@Test
public void testFindAll() throws Exception {
public void testFindAll() {
final List<E> all = this.dao.findAll();
assertNotNull(all);
assertEquals(INITIAL_COUNT, all.size());
}
@Test
public void testSetId() throws Exception {
public void testSetId() {
final E entity = this.factory.apply("name");
assertNull(entity.getId());
final Long expectedId = Long.valueOf(1);
final Long expectedId = 1L;
entity.setId(expectedId);
assertEquals(expectedId, entity.getId());
}
@Test
public void testSetName() throws Exception {
public void testSetName() {
final E entity = this.factory.apply("name");
assertEquals("name", entity.getName());
assertEquals("name", entity.toString());

View File

@@ -51,7 +51,7 @@ import static org.mockito.Mockito.when;
public class MagicServiceImplTest {
@Test
public void testFindAllWizards() throws Exception {
public void testFindAllWizards() {
final WizardDao wizardDao = mock(WizardDao.class);
final SpellbookDao spellbookDao = mock(SpellbookDao.class);
final SpellDao spellDao = mock(SpellDao.class);

View File

@@ -42,7 +42,7 @@ public class SpellDaoImplTest extends BaseDaoTest<Spell, SpellDaoImpl> {
}
@Test
public void testFindByName() throws Exception {
public void testFindByName() {
final SpellDaoImpl dao = getDao();
final List<Spell> allSpells = dao.findAll();
for (final Spell spell : allSpells) {

View File

@@ -42,7 +42,7 @@ public class SpellbookDaoImplTest extends BaseDaoTest<Spellbook, SpellbookDaoImp
}
@Test
public void testFindByName() throws Exception {
public void testFindByName() {
final SpellbookDaoImpl dao = getDao();
final List<Spellbook> allBooks = dao.findAll();
for (final Spellbook book : allBooks) {

View File

@@ -42,7 +42,7 @@ public class WizardDaoImplTest extends BaseDaoTest<Wizard, WizardDaoImpl> {
}
@Test
public void testFindByName() throws Exception {
public void testFindByName() {
final WizardDaoImpl dao = getDao();
final List<Wizard> allWizards = dao.findAll();
for (final Wizard spell : allWizards) {