private Executable setupTest(YamlTestGroup yamlTestGroup, YamlTest yamlTest, String testcaseName) { RequestSpecification rs = given().spec(rspec); Executable executable = () -> { logger.info("\n\n\n-->start " + testcaseName); // Initialize current state to be access globally CurrentState.setState(yamlInitGroup, yamlTestGroup, yamlDataGroup, yamlTest); try { RestRequest.build(rs, yamlTest).request().doAssert(); } catch (Throwable e) { e.printStackTrace(); throw e; } logger.info("-->end " + testcaseName +"\n\n\n"); }; return executable; }
private Executable getExecutable(File file, boolean isMatch) { return () -> { String jsonString = fileUtils.read(file); jsonValidator.validate(String.format("Invalid JSON in file '%s'.", file.getName()), jsonString); Response response = client.target(configuration.getLocalMatchingServiceMatchUrl()) .request(APPLICATION_JSON) .post(Entity.json(jsonString)); assertThat(response.getHeaderString("Content-Type"), is(APPLICATION_JSON)); assertThat(response.getStatus(), is(OK.getStatusCode())); Map<String, String> result = readEntityAsMap(response); assertThat(result.keySet(), is(new HashSet<String>() {{ add("result"); }})); assertThat(result.get("result"), is(isMatch ? "match" : "no-match")); }; }
private Executable Should_Change_Value(final BigInteger value) { return () -> { // given final AllFiledTypes helpClass1 = new AllFiledTypes(value); final AllFiledTypes helpClass2 = new AllFiledTypes(value); // when valueChanger.changeFieldsValues(helpClass1, helpClass2, Lists.newArrayList(AllFiledTypes.class.getDeclaredFields())); final BigInteger result1 = getInternalState(helpClass1, "bigInteger"); final BigInteger result2 = getInternalState(helpClass2, "bigInteger"); // then assertThat(result1).isNotEqualTo(result2); }; }
@Test public void test_resolveEntity_With_UUID_Throws_IllegalStateException_if_passed_Context_is_disposed() { new Expectations() {{ testContext.isDisposed(); result = true; }}; assertThrows(IllegalStateException.class, new Executable() { @Override public void execute() { testee.resolveEntity(testContext, TEST_UUID); } }); }
@Test public void test_removeComponent_Throws_IllegalStateException_if_the_Context_to_which_the_ContextualComponentManager_belongs_to_has_been_disposed() { new Expectations() {{ testContext.isDisposed(); result = true; }}; assertThrows(IllegalStateException.class, new Executable() { @Override public void execute() { testee.removeComponent(testRefA, TestComponentA.class); } }); }
@Test public void test_addComponent_Throws_IllegalArgumentException_if_the_passed_Class_is_null() { new Expectations() {{ testRefA.belongsTo(testContext); result = true; }}; assertThrows(IllegalArgumentException.class, new Executable() { @Override public void execute() { testee.addComponent(testRefA, (Class<Component>) null); } }); }
@Test public void test_getComponent_Throws_IllegalArgumentException_if_the_passed_Class_is_null() { new Expectations() {{ testRefA.belongsTo(testContext); result = true; }}; assertThrows(IllegalArgumentException.class, new Executable() { @Override public void execute() { testee.getComponent(testRefA, null); } }); }
private Executable Should_Change_Wrapped_Value(final Byte value) { return () -> { // given final AllFiledTypes_Wrapped helpClass1 = new AllFiledTypes_Wrapped(value); final AllFiledTypes_Wrapped helpClass2 = new AllFiledTypes_Wrapped(value); // when valueChanger.changeFieldsValues(helpClass1, helpClass2, Lists.newArrayList(AllFiledTypes_Wrapped.class.getDeclaredFields())); final Byte result1 = getInternalState(helpClass1, "byteType"); final Byte result2 = getInternalState(helpClass2, "byteType"); // then assertThat(result1).isNotEqualTo(result2); }; }
private Executable Should_Change_Wrapped_Value(final Long value) { return () -> { // given final AllFiledTypes_Wrapped helpClass1 = new AllFiledTypes_Wrapped(value); final AllFiledTypes_Wrapped helpClass2 = new AllFiledTypes_Wrapped(value); // when valueChanger.changeFieldsValues(helpClass1, helpClass2, Lists.newArrayList(AllFiledTypes_Wrapped.class.getDeclaredFields())); final Long result1 = getInternalState(helpClass1, "longType"); final Long result2 = getInternalState(helpClass2, "longType"); // then assertThat(result1).isNotEqualTo(result2); }; }
private Executable Should_Change_Primitive_Value(final Short value) { return () -> { // given final AllFiledTypes helpClass1 = new AllFiledTypes(value); final AllFiledTypes helpClass2 = new AllFiledTypes(value); // when valueChanger.changeFieldsValues(helpClass1, helpClass2, Lists.newArrayList(AllFiledTypes.class.getDeclaredFields())); final Short result1 = getInternalState(helpClass1, "shortType"); final Short result2 = getInternalState(helpClass2, "shortType"); // then assertThat(result1).isNotEqualTo(result2); }; }
@Test public void test_getEntitiesOfGroup_ThrowsInvalidGroupExceptionWhenPassedGroupIsInvalid() { final Group testGroup = mock(Group.class); when(testGroup.isInvalid()).thenReturn(true); when(testGroup.isValid()).thenReturn(false); assertThrows(InvalidGroupException.class, new Executable() { @Override public void execute() { testee.getEntitiesOfGroup(testGroup); } }); }
@Test public void test_removeComponent_With_EntityRef_and_Class_Throws_IllegalStateException_if_the_Context_to_which_the_passed_EntityRef_belongs_to_has_been_disposed() { new Expectations() {{ testRef.isInvalid(); result = true; testRef.getContext(); result = testContext; testContext.isDisposed(); result = true; }}; assertThrows(IllegalStateException.class, new Executable() { @Override public void execute() throws Throwable { testee.removeComponent(testRef, TestComponentA.class); } }); }
private Executable Should_Change_Wrapped_Value(final Double value) { return () -> { // given final AllFiledTypes_Wrapped helpClass1 = new AllFiledTypes_Wrapped(value); final AllFiledTypes_Wrapped helpClass2 = new AllFiledTypes_Wrapped(value); // when valueChanger.changeFieldsValues(helpClass1, helpClass2, Lists.newArrayList(AllFiledTypes_Wrapped.class.getDeclaredFields())); final Double result1 = getInternalState(helpClass1, "doubleType"); final Double result2 = getInternalState(helpClass2, "doubleType"); // then assertThat(result1).isNotEqualTo(result2); }; }
@Test public void test_getComponent_Throws_IllegalStateException_if_the_Context_to_which_the_ContextualComponentManager_belongs_to_has_been_disposed() { new Expectations() {{ testContext.isDisposed(); result = true; }}; assertThrows(IllegalStateException.class, new Executable() { @Override public void execute() { testee.getComponent(testRefA, TestComponentA.class); } }); }
private Executable Should_Change_Primitive_Value(final char value) { return () -> { // given final AllFiledTypes helpClass1 = new AllFiledTypes(value); final AllFiledTypes helpClass2 = new AllFiledTypes(value); // when valueChanger.changeFieldsValues(helpClass1, helpClass2, Lists.newArrayList(AllFiledTypes.class.getDeclaredFields())); final Character result1 = getInternalState(helpClass1, "characterType"); final Character result2 = getInternalState(helpClass2, "characterType"); // then assertThat(result1).isNotEqualTo(result2); }; }
@Test public void test_addEntityToGroup_ThrowsInvalidGroupExceptionWhenPassedGroupIsInvalid() { final Group testGroup = mock(Group.class); when(testGroup.isInvalid()).thenReturn(true); when(testGroup.isValid()).thenReturn(false); assertThrows(InvalidGroupException.class, new Executable() { @Override public void execute() { testee.addEntityToGroup(testEntityA, testGroup); } }); }
@Test void should_throw_an_XXX_if_an_added_property_does_not_belong_to_the_current_Component() { new Expectations() {{ propertyDescriptor.getDeclaringComponent(); result = AnotherTestComponent.class; }}; assertThrows(Exception.class, new Executable() { @Override public void execute() { testee.describe(TestComponent.class).addProperty(propertyDescriptor); } }); }
private Executable Should_Change_Wrapped_Value(final Integer value) { return () -> { // given final AllFiledTypes_Wrapped helpClass1 = new AllFiledTypes_Wrapped(value); final AllFiledTypes_Wrapped helpClass2 = new AllFiledTypes_Wrapped(value); // when valueChanger.changeFieldsValues(helpClass1, helpClass2, Lists.newArrayList(AllFiledTypes_Wrapped.class.getDeclaredFields())); final Integer result1 = getInternalState(helpClass1, "intType"); final Integer result2 = getInternalState(helpClass2, "intType"); // then assertThat(result1).isNotEqualTo(result2); }; }
@Test public void test_createEntity_Throws_IllegalStateException_if_the_context_has_been_disposed() { new Expectations() {{ testContext.isDisposed(); result = true; }}; assertThrows(IllegalStateException.class, new Executable() { @Override public void execute() { testee.createEntity(); } }); }
@Test public void test_removeEntityFromGroup_ThrowsInvalidEntityRefExceptionWhenPassedEntityRefIsInvalid() { final Group testGroup = mock(Group.class); final EntityRef ref = mock(EntityRef.class); when(ref.isInvalid()).thenReturn(true); when(ref.isValid()).thenReturn(false); when(ref.getContext()).thenReturn(context); assertThrows(InvalidEntityRefException.class, new Executable() { @Override public void execute() { testee.removeEntityFromGroup(ref, testGroup); } }); }
@Test public void test_removeComponent_With_EntityRef_and_Class_Throws_InvalidEntityRefException_if_the_passed_EntityRef_is_invalid() { new Expectations() {{ testRef.isInvalid(); result = true; testRef.getContext(); result = testContext; testContext.isDisposed(); result = false; }}; assertThrows(InvalidEntityRefException.class, new Executable() { @Override public void execute() throws Throwable { testee.removeComponent(testRef, TestComponentA.class); } }); }
private Executable Should_Change_Stream_Value(final String fieldName) { return () -> { // given final ClassContainingStream helpClass1 = new ClassContainingStream(); final ClassContainingStream helpClass2 = new ClassContainingStream(); // when valueChanger.changeFieldsValues(helpClass1, helpClass2, newArrayList(ClassContainingStream.class.getDeclaredField(fieldName))); final Stream result1 = getInternalState(helpClass2, fieldName); final Stream result2 = getInternalState(helpClass1, fieldName); // then assertThat(result1).isNotEqualTo(result2); }; }
@Test public void test_addEntityToGroup_ThrowsIllegalContextExceptionIfEntityAndGroupDoesNotBelongToTheSameContext() { final Context testContext = mock(Context.class); final Group testGroup = mock(Group.class); when(testGroup.getContext()).thenReturn(testContext); when(testGroup.isInvalid()).thenReturn(false); when(testGroup.isValid()).thenReturn(true); assertThrows(IllegalContextException.class, new Executable() { @Override public void execute() { testee.addEntityToGroup(testEntityA, testGroup); } }); }
@Test public void test_removeEntityFromGroup_ThrowsInvalidGroupExceptionWhenPassedGroupIsInvalid() { final Group testGroup = mock(Group.class); when(testGroup.isInvalid()).thenReturn(true); when(testGroup.isValid()).thenReturn(false); assertThrows(InvalidGroupException.class, new Executable() { @Override public void execute() { testee.removeEntityFromGroup(testEntityA, testGroup); } }); }
@Test public void test_DeleteEntities_Throws_IllegalStateException_if_its_Context_has_been_disposed() { new Expectations() {{ testContext.isDisposed(); result = true; }}; assertThrows(IllegalStateException.class, new Executable() { @Override public void execute() { testee.deleteEntities(); } }); }
@Test public void test_removeComponent_With_EntityRef_and_Class_Throws_ComponentManagerException_when_the_Query_times_out() { new Expectations() {{ testRef.isInvalid(); result = false; testRef.getContext(); result = testContext; testResultSet.await(Success, anyLong, (TimeUnit) any); result = false; }}; assertThrows(ComponentManagerException.class, new Executable() { @Override public void execute() throws Throwable { testee.removeComponent(testRef, TestComponentA.class); } }); }
@Test public void test_DeleteEntity_Throws_IllegalStateException_if_its_Context_has_been_disposed() { new Expectations() {{ testContext.isDisposed(); result = true; }}; assertThrows(IllegalStateException.class, new Executable() { @Override public void execute() { testee.deleteEntity(testRefA); } }); }
@Test public void test_DeleteEntity_Throws_IllegalContextException_if_the_passed_EntityRef_belongs_to_another_Context() { new Expectations() {{ testRefA.belongsTo(testContext); result = false; }}; assertThrows(IllegalContextException.class, new Executable() { @Override public void execute() { testee.deleteEntity(testRefA); } }); }
@Test public void test_findComponent_Throws_IllegalStateException_if_the_Context_to_which_the_ContextualComponentManager_belongs_to_has_been_disposed() { new Expectations() {{ testContext.isDisposed(); result = true; }}; assertThrows(IllegalStateException.class, new Executable() { @Override public void execute() { testee.findComponent(testRefA, TestComponentA.class); } }); }
@Test public void test_addComponent_Throws_IllegalStateException_if_the_Context_to_which_the_ContextualComponentManager_belongs_to_has_been_disposed() { new Expectations() {{ testContext.isDisposed(); result = true; }}; assertThrows(IllegalStateException.class, new Executable() { @Override public void execute() { testee.addComponent(testRefA, TestComponentA.class); } }); }
@Test void should_throw_an_IllegalStateException_if_an_accessor_is_added_twice() { new Expectations() {{ accessorDescriptorA.getPropertyType(); result = String.class; accessorDescriptorA.getDeclaringComponent(); result = TestComponent.class; accessorDescriptorA.getAccessorMethodDescriptor(); result = accessorA; accessorDescriptorB.getPropertyType(); result = String.class; accessorDescriptorB.getDeclaringComponent(); result = TestComponent.class; accessorDescriptorB.getAccessorMethodDescriptor(); result = accessorA; }}; assertThrows(IllegalStateException.class, new Executable() { @Override public void execute() { testee.describe(TestComponent.class).addAccessor(accessorDescriptorA).addAccessor(accessorDescriptorB); } }); }
@Test public void test_addEntityToGroup_ThrowsInvalidEntityRefExceptionWhenPassedEntityRefIsInvalid() { final Group testGroup = mock(Group.class); final EntityRef ref = mock(EntityRef.class); when(ref.isInvalid()).thenReturn(true); when(ref.isValid()).thenReturn(false); when(ref.getContext()).thenReturn(context); assertThrows(InvalidEntityRefException.class, new Executable() { @Override public void execute() { testee.addEntityToGroup(ref, testGroup); } }); }
@Test public void test_hasComponent_With_EntityRef_and_Collection_Throws_IllegalContextException_if_the_passed_EntityRef_belongs_to_another_Context() { new Expectations() {{ testRefA.isInvalid(); result = false; testRefA.belongsTo(testContext); result = false; }}; assertThrows(IllegalContextException.class, new Executable() { @Override public void execute() { testee.hasComponent(testRefA, Arrays.<Class<? extends Component>>asList(TestComponentA.class, TestComponentB.class)); } }); }
@Test public void test_addComponent_With_EntityRef_and_Class_Throws_InvalidEntityRefException_if_the_EntityRef_is_invalid() { new Expectations() {{ testRef.isInvalid(); result = true; }}; assertThrows(InvalidEntityRefException.class, new Executable() { @Override public void execute() throws Throwable { testee.addComponent(testRef, TestComponentA.class); } }); }
@Test void should_throw_an_XXX_if_an_added_accessor_is_for_the_same_property_but_has_another_type() { new Expectations() {{ propertyDescriptor.getPropertyName(); result = "text"; propertyDescriptor.getPropertyType(); result = String.class; propertyDescriptor.getDeclaringComponent(); result = TestComponent.class; accessorDescriptorC.getPropertyName(); result = "text"; accessorDescriptorC.getPropertyType(); result = Integer.class; accessorDescriptorC.getDeclaringComponent(); result = TestComponent.class; }}; assertThrows(Exception.class, new Executable() { @Override public void execute() { testee.describe(TestComponent.class).addProperty(propertyDescriptor).addAccessor(accessorDescriptorC); } }); }
/** * Read the rules and generate a dynamic test at runtime for each read rule line. * * @return A {@link Stream} of {@link DynamicTest} objects corresponding to the contents of the test rule file. */ @TestFactory public Stream<DynamicTest> generateTests() { Stream<ParsedTestRule> parsedRules = TestRuleParser.parseRuleFile(TEST_RULES_RESOURCE_NAME); Stream<DynamicTest> result = parsedRules.map(rule -> { String testName = constructTestNameForRule(rule); Executable testCase = constructTestCaseForRule(rule); return DynamicTest.dynamicTest(testName, testCase); }); return result; }
@Test void consumerShouldWaitForFinishing() { TestModule module = new TestModule(); ModuleRegistry moduleRegistry = new ModuleRegistry(null); moduleRegistry.setService(executorService); Assertions.assertTimeout(Duration.ofMillis(2535), new Executable() { @Override public void execute() throws Throwable { moduleRegistry.register(module); module.waitFor(); } }); System.out.println("Done."); }
@SuppressWarnings("unused") // Called via reflection static Stream<Executable> nullPointerExceptionConstructors() { return Stream.of( () -> StringDecorator.builder().prefix(null).build(), () -> StringDecorator.builder().suffix(null).build(), () -> StringDecorator.builder().prefix(null).suffix(null).build(), () -> StringDecorator.ofPrefix(null), () -> StringDecorator.ofSuffix(null), () -> StringDecorator.of(null) ); }
@SuppressWarnings("unused") // Called via reflection static Stream<Executable> illegalArgumentConstructors() { return Stream.of( () -> StringDecorator.builder().prefix("").build(), () -> StringDecorator.builder().suffix("").build(), () -> StringDecorator.ofPrefix(""), () -> StringDecorator.ofSuffix(""), () -> StringDecorator.of("") ); }
@SuppressWarnings("unused") // Called via reflection static Stream<Executable> illegalStateConstructors() { return Stream.of( () -> LinkDecorator.builder().suffix("").build(), () -> LinkDecorator.builder().prefix("").build() ); }