@@ -189,6 +189,34 @@ public void should_fail_with_missing_deps() throws Exception {
189189 }
190190 }
191191
192+ @ Test
193+ public void should_fail_with_circular_dependencies () throws Exception {
194+ Factory factory = Factory .builder ()
195+ .addMachine (failingSingleDepMachine ("test5" , "test4" ))
196+ .addMachine (failingSingleDepMachine ("test4" , "test3" ))
197+ .addMachine (failingSingleDepMachine ("test3" , "test2" ))
198+ .addMachine (failingSingleDepMachine ("test2" , "test1" ))
199+ .addMachine (failingSingleDepMachine ("test1" , "test4" ))
200+ .build ();
201+
202+ try {
203+ factory .queryByName (Name .of (String .class , "test5" )).findOne ();
204+ fail ("should raise exception when asking for a component with circular dependency" );
205+ } catch (IllegalStateException e ) {
206+ assertThat (e )
207+ .hasMessage (
208+ "Circular dependency detected : \n " +
209+ "-> Name{name='test4', clazz=java.lang.String[]}\n " +
210+ " -> Name{name='test3', clazz=java.lang.String[]}\n " +
211+ " -> Name{name='test2', clazz=java.lang.String[]}\n " +
212+ " -> Name{name='test1', clazz=java.lang.String[]}\n " +
213+ " -> Name{name='test4', clazz=java.lang.String[]}\n " +
214+ "\n " +
215+ "Please, fix this as current RestX DI can't handle cycles." )
216+ ;
217+ }
218+ }
219+
192220 @ Test
193221 public void should_fail_with_similar_components () throws Exception {
194222 SingleNameFactoryMachine <String > machine = machineWithMissingDependency ();
@@ -587,11 +615,16 @@ protected String doNewComponent(SatisfiedBOM satisfiedBOM) {
587615 }
588616
589617 private SingleNameFactoryMachine <String > machineWithMissingDependency () {
618+ return failingSingleDepMachine ("test" , "missing" );
619+ }
620+
621+ private SingleNameFactoryMachine <String > failingSingleDepMachine (final String name , final String depName ) {
590622 return new SingleNameFactoryMachine <>(
591- 0 , new StdMachineEngine <String >(Name .of (String .class , "test" ), BoundlessComponentBox .FACTORY ) {
623+ 0 , new StdMachineEngine <String >(Name .of (String .class , name ), BoundlessComponentBox .FACTORY ) {
624+
592625 @ Override
593626 public BillOfMaterials getBillOfMaterial () {
594- return BillOfMaterials .of (Factory .Query .byName (Name .of (String .class , "missing" )));
627+ return BillOfMaterials .of (Factory .Query .byName (Name .of (String .class , depName )));
595628 }
596629
597630 @ Override
0 commit comments