From 70f6e5435394e0d1eedbc3e17b863b083e3492e8 Mon Sep 17 00:00:00 2001 From: staillebois Date: Thu, 25 Oct 2018 15:13:58 +0200 Subject: [PATCH] Fix blocker issues on Sonar #508 (#810) * Fix blocker issues on Sonar * Replace Assertj assertions with JUnit ones --- .../dom/modules/simple/SimpleObjectTest.java | 33 ++-- .../dom/modules/simple/SimpleObjectsTest.java | 100 +++++----- .../modules/simple/SimpleObjectIntegTest.java | 118 +++++------- .../simple/SimpleObjectsIntegTest.java | 178 +++++++++--------- 4 files changed, 195 insertions(+), 234 deletions(-) diff --git a/naked-objects/dom/src/test/java/domainapp/dom/modules/simple/SimpleObjectTest.java b/naked-objects/dom/src/test/java/domainapp/dom/modules/simple/SimpleObjectTest.java index d80d0786..085a0140 100644 --- a/naked-objects/dom/src/test/java/domainapp/dom/modules/simple/SimpleObjectTest.java +++ b/naked-objects/dom/src/test/java/domainapp/dom/modules/simple/SimpleObjectTest.java @@ -14,7 +14,8 @@ */ package domainapp.dom.modules.simple; -import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import org.junit.Before; import org.junit.Test; @@ -30,24 +31,18 @@ public class SimpleObjectTest { public void setUp() throws Exception { simpleObject = new SimpleObject(); } - - /** - * Test for Names for SimpleObjects - */ - public static class Name extends SimpleObjectTest { - - @Test - public void happyCase() throws Exception { - // given - String name = "Foobar"; - assertThat(simpleObject.getName()).isNull(); - - // when - simpleObject.setName(name); - - // then - assertThat(simpleObject.getName()).isEqualTo(name); - } + + @Test + public void testName() throws Exception { + // given + String name = "Foobar"; + assertNull(simpleObject.getName()); + + // when + simpleObject.setName(name); + + // then + assertEquals(name, simpleObject.getName()); } } diff --git a/naked-objects/dom/src/test/java/domainapp/dom/modules/simple/SimpleObjectsTest.java b/naked-objects/dom/src/test/java/domainapp/dom/modules/simple/SimpleObjectsTest.java index 91fe3d71..52ea8e0a 100644 --- a/naked-objects/dom/src/test/java/domainapp/dom/modules/simple/SimpleObjectsTest.java +++ b/naked-objects/dom/src/test/java/domainapp/dom/modules/simple/SimpleObjectsTest.java @@ -14,9 +14,10 @@ */ package domainapp.dom.modules.simple; -import static org.assertj.core.api.Assertions.assertThat; - import com.google.common.collect.Lists; + +import static org.junit.Assert.assertEquals; + import java.util.List; import org.apache.isis.applib.DomainObjectContainer; import org.apache.isis.core.unittestsupport.jmocking.JUnitRuleMockery2; @@ -46,63 +47,52 @@ public class SimpleObjectsTest { simpleObjects = new SimpleObjects(); simpleObjects.container = mockContainer; } - - /** - * Test Creation of Simple Objects - */ - public static class Create extends SimpleObjectsTest { - - @Test - public void happyCase() throws Exception { - - // given - final SimpleObject simpleObject = new SimpleObject(); - - final Sequence seq = context.sequence("create"); - context.checking(new Expectations() { - { - oneOf(mockContainer).newTransientInstance(SimpleObject.class); - inSequence(seq); - will(returnValue(simpleObject)); - - oneOf(mockContainer).persistIfNotAlready(simpleObject); - inSequence(seq); - } - }); - - // when - final SimpleObject obj = simpleObjects.create("Foobar"); - - // then - assertThat(obj).isEqualTo(simpleObject); - assertThat(obj.getName()).isEqualTo("Foobar"); - } - + + @Test + public void testCreate() throws Exception { + + // given + final SimpleObject simpleObject = new SimpleObject(); + + final Sequence seq = context.sequence("create"); + context.checking(new Expectations() { + { + oneOf(mockContainer).newTransientInstance(SimpleObject.class); + inSequence(seq); + will(returnValue(simpleObject)); + + oneOf(mockContainer).persistIfNotAlready(simpleObject); + inSequence(seq); + } + }); + + // when + String objectName = "Foobar"; + final SimpleObject obj = simpleObjects.create(objectName); + + // then + assertEquals(simpleObject, obj); + assertEquals(objectName, obj.getName()); } + + @Test + public void testListAll() throws Exception { - /** - * Test Listing of Simple Objects - */ - public static class ListAll extends SimpleObjectsTest { + // given + final List all = Lists.newArrayList(); - @Test - public void happyCase() throws Exception { + context.checking(new Expectations() { + { + oneOf(mockContainer).allInstances(SimpleObject.class); + will(returnValue(all)); + } + }); - // given - final List all = Lists.newArrayList(); + // when + final List list = simpleObjects.listAll(); - context.checking(new Expectations() { - { - oneOf(mockContainer).allInstances(SimpleObject.class); - will(returnValue(all)); - } - }); - - // when - final List list = simpleObjects.listAll(); - - // then - assertThat(list).isEqualTo(all); - } + // then + assertEquals(all, list); } + } diff --git a/naked-objects/integtests/src/test/java/domainapp/integtests/tests/modules/simple/SimpleObjectIntegTest.java b/naked-objects/integtests/src/test/java/domainapp/integtests/tests/modules/simple/SimpleObjectIntegTest.java index f2cbb172..3d9009bf 100644 --- a/naked-objects/integtests/src/test/java/domainapp/integtests/tests/modules/simple/SimpleObjectIntegTest.java +++ b/naked-objects/integtests/src/test/java/domainapp/integtests/tests/modules/simple/SimpleObjectIntegTest.java @@ -18,12 +18,11 @@ */ package domainapp.integtests.tests.modules.simple; -import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; -import domainapp.dom.modules.simple.SimpleObject; -import domainapp.fixture.scenarios.RecreateSimpleObjects; -import domainapp.integtests.tests.SimpleAppIntegTest; import javax.inject.Inject; + import org.apache.isis.applib.DomainObjectContainer; import org.apache.isis.applib.fixturescripts.FixtureScripts; import org.apache.isis.applib.services.wrapper.DisabledException; @@ -31,6 +30,10 @@ import org.apache.isis.applib.services.wrapper.InvalidException; import org.junit.Before; import org.junit.Test; +import domainapp.dom.modules.simple.SimpleObject; +import domainapp.fixture.scenarios.RecreateSimpleObjects; +import domainapp.integtests.tests.SimpleAppIntegTest; + /** * Test Fixtures with Simple Objects */ @@ -38,10 +41,14 @@ public class SimpleObjectIntegTest extends SimpleAppIntegTest { @Inject FixtureScripts fixtureScripts; + @Inject + DomainObjectContainer container; RecreateSimpleObjects fs; SimpleObject simpleObjectPojo; SimpleObject simpleObjectWrapped; + + private static final String NEW_NAME = "new name"; @Before public void setUp() throws Exception { @@ -51,80 +58,59 @@ public class SimpleObjectIntegTest extends SimpleAppIntegTest { simpleObjectPojo = fs.getSimpleObjects().get(0); - assertThat(simpleObjectPojo).isNotNull(); + assertNotNull(simpleObjectPojo); simpleObjectWrapped = wrap(simpleObjectPojo); } - - /** - * Test Object Name accessibility - */ - public static class Name extends SimpleObjectIntegTest { - - @Test - public void accessible() throws Exception { - // when - final String name = simpleObjectWrapped.getName(); - // then - assertThat(name).isEqualTo(fs.names.get(0)); - } - - @Test - public void cannotBeUpdatedDirectly() throws Exception { - - // expect - expectedExceptions.expect(DisabledException.class); - - // when - simpleObjectWrapped.setName("new name"); - } + + @Test + public void testNameAccessible() throws Exception { + // when + final String name = simpleObjectWrapped.getName(); + // then + assertEquals(fs.names.get(0), name); } + + @Test + public void testNameCannotBeUpdatedDirectly() throws Exception { - /** - * Test Validation of SimpleObject Names - */ - public static class UpdateName extends SimpleObjectIntegTest { - - @Test - public void happyCase() throws Exception { - - // when - simpleObjectWrapped.updateName("new name"); + // expect + expectedExceptions.expect(DisabledException.class); - // then - assertThat(simpleObjectWrapped.getName()).isEqualTo("new name"); - } - - @Test - public void failsValidation() throws Exception { + // when + simpleObjectWrapped.setName(NEW_NAME); + } + + @Test + public void testUpdateName() throws Exception { - // expect - expectedExceptions.expect(InvalidException.class); - expectedExceptions.expectMessage("Exclamation mark is not allowed"); + // when + simpleObjectWrapped.updateName(NEW_NAME); - // when - simpleObjectWrapped.updateName("new name!"); - } + // then + assertEquals(NEW_NAME, simpleObjectWrapped.getName()); } + + @Test + public void testUpdateNameFailsValidation() throws Exception { - /** - * Test ContainerTitle generation based on SimpleObject Name - */ - public static class Title extends SimpleObjectIntegTest { - - @Inject - DomainObjectContainer container; + // expect + expectedExceptions.expect(InvalidException.class); + expectedExceptions.expectMessage("Exclamation mark is not allowed"); - @Test - public void interpolatesName() throws Exception { + // when + simpleObjectWrapped.updateName(NEW_NAME + "!"); + } + + @Test + public void testInterpolatesName() throws Exception { - // given - final String name = simpleObjectWrapped.getName(); + // given + final String name = simpleObjectWrapped.getName(); - // when - final String title = container.titleOf(simpleObjectWrapped); + // when + final String title = container.titleOf(simpleObjectWrapped); - // then - assertThat(title).isEqualTo("Object: " + name); - } + // then + assertEquals("Object: " + name, title); } } diff --git a/naked-objects/integtests/src/test/java/domainapp/integtests/tests/modules/simple/SimpleObjectsIntegTest.java b/naked-objects/integtests/src/test/java/domainapp/integtests/tests/modules/simple/SimpleObjectsIntegTest.java index d57454dc..168d4865 100644 --- a/naked-objects/integtests/src/test/java/domainapp/integtests/tests/modules/simple/SimpleObjectsIntegTest.java +++ b/naked-objects/integtests/src/test/java/domainapp/integtests/tests/modules/simple/SimpleObjectsIntegTest.java @@ -18,17 +18,13 @@ */ package domainapp.integtests.tests.modules.simple; -import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; -import com.google.common.base.Throwables; -import domainapp.dom.modules.simple.SimpleObject; -import domainapp.dom.modules.simple.SimpleObjects; -import domainapp.fixture.modules.simple.SimpleObjectsTearDown; -import domainapp.fixture.scenarios.RecreateSimpleObjects; -import domainapp.integtests.tests.SimpleAppIntegTest; import java.sql.SQLIntegrityConstraintViolationException; import java.util.List; + import javax.inject.Inject; + import org.apache.isis.applib.fixturescripts.FixtureScript; import org.apache.isis.applib.fixturescripts.FixtureScripts; import org.hamcrest.Description; @@ -36,6 +32,14 @@ import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; import org.junit.Test; +import com.google.common.base.Throwables; + +import domainapp.dom.modules.simple.SimpleObject; +import domainapp.dom.modules.simple.SimpleObjects; +import domainapp.fixture.modules.simple.SimpleObjectsTearDown; +import domainapp.fixture.scenarios.RecreateSimpleObjects; +import domainapp.integtests.tests.SimpleAppIntegTest; + /** * Fixture Pattern Integration Test */ @@ -46,104 +50,90 @@ public class SimpleObjectsIntegTest extends SimpleAppIntegTest { @Inject SimpleObjects simpleObjects; - /** - * Test Listing of All Simple Objects - */ - public static class ListAll extends SimpleObjectsIntegTest { + @Test + public void testListAll() throws Exception { - @Test - public void happyCase() throws Exception { + // given + RecreateSimpleObjects fs = new RecreateSimpleObjects(); + fixtureScripts.runFixtureScript(fs, null); + nextTransaction(); - // given - RecreateSimpleObjects fs = new RecreateSimpleObjects(); - fixtureScripts.runFixtureScript(fs, null); - nextTransaction(); + // when + final List all = wrap(simpleObjects).listAll(); - // when - final List all = wrap(simpleObjects).listAll(); + // then + assertEquals(fs.getSimpleObjects().size(), all.size()); - // then - assertThat(all).hasSize(fs.getSimpleObjects().size()); - - SimpleObject simpleObject = wrap(all.get(0)); - assertThat(simpleObject.getName()).isEqualTo(fs.getSimpleObjects().get(0).getName()); - } - - @Test - public void whenNone() throws Exception { + SimpleObject simpleObject = wrap(all.get(0)); + assertEquals(fs.getSimpleObjects().get(0).getName(), simpleObject.getName()); + } + + @Test + public void testListAllWhenNone() throws Exception { - // given - FixtureScript fs = new SimpleObjectsTearDown(); - fixtureScripts.runFixtureScript(fs, null); - nextTransaction(); + // given + FixtureScript fs = new SimpleObjectsTearDown(); + fixtureScripts.runFixtureScript(fs, null); + nextTransaction(); - // when - final List all = wrap(simpleObjects).listAll(); + // when + final List all = wrap(simpleObjects).listAll(); - // then - assertThat(all).hasSize(0); - } + // then + assertEquals(0, all.size()); } + + @Test + public void testCreate() throws Exception { + // given + FixtureScript fs = new SimpleObjectsTearDown(); + fixtureScripts.runFixtureScript(fs, null); + nextTransaction(); - /** - * Test Creation of Simple Objects - */ - public static class Create extends SimpleObjectsIntegTest { - - @Test - public void happyCase() throws Exception { - - // given - FixtureScript fs = new SimpleObjectsTearDown(); - fixtureScripts.runFixtureScript(fs, null); - nextTransaction(); - - // when - wrap(simpleObjects).create("Faz"); - - // then - final List all = wrap(simpleObjects).listAll(); - assertThat(all).hasSize(1); - } - - @Test - public void whenAlreadyExists() throws Exception { - - // given - FixtureScript fs = new SimpleObjectsTearDown(); - fixtureScripts.runFixtureScript(fs, null); - nextTransaction(); - wrap(simpleObjects).create("Faz"); - nextTransaction(); - - // then - expectedExceptions.expectCause(causalChainContains(SQLIntegrityConstraintViolationException.class)); - - // when - wrap(simpleObjects).create("Faz"); - nextTransaction(); - } - - private static Matcher causalChainContains(final Class cls) { - return new TypeSafeMatcher() { - @Override - protected boolean matchesSafely(Throwable item) { - final List causalChain = Throwables.getCausalChain(item); - for (Throwable throwable : causalChain) { - if (cls.isAssignableFrom(throwable.getClass())) { - return true; - } - } - return false; - } + // when + wrap(simpleObjects).create("Faz"); - @Override - public void describeTo(Description description) { - description.appendText("exception with causal chain containing " + cls.getSimpleName()); + // then + final List all = wrap(simpleObjects).listAll(); + assertEquals(1, all.size()); + } + + @Test + public void testCreateWhenAlreadyExists() throws Exception { + + // given + FixtureScript fs = new SimpleObjectsTearDown(); + fixtureScripts.runFixtureScript(fs, null); + nextTransaction(); + wrap(simpleObjects).create("Faz"); + nextTransaction(); + + // then + expectedExceptions.expectCause(causalChainContains(SQLIntegrityConstraintViolationException.class)); + + // when + wrap(simpleObjects).create("Faz"); + nextTransaction(); + } + + private static Matcher causalChainContains(final Class cls) { + return new TypeSafeMatcher() { + @Override + protected boolean matchesSafely(Throwable item) { + final List causalChain = Throwables.getCausalChain(item); + for (Throwable throwable : causalChain) { + if (cls.isAssignableFrom(throwable.getClass())) { + return true; + } } - }; - } + return false; + } + + @Override + public void describeTo(Description description) { + description.appendText("exception with causal chain containing " + cls.getSimpleName()); + } + }; } - } -- GitLab