diff --git a/src/main/java/de/codecentric/cxf/autodetection/WebServiceAutoDetector.java b/src/main/java/de/codecentric/cxf/autodetection/WebServiceAutoDetector.java index 7b2d75d..7f871ef 100644 --- a/src/main/java/de/codecentric/cxf/autodetection/WebServiceAutoDetector.java +++ b/src/main/java/de/codecentric/cxf/autodetection/WebServiceAutoDetector.java @@ -2,9 +2,8 @@ import de.codecentric.cxf.common.BootStarterCxfException; import de.codecentric.cxf.diagnostics.SeiImplClassNotFoundException; +import de.codecentric.cxf.diagnostics.SeiNotFoundException; import de.codecentric.cxf.diagnostics.WebServiceClientNotFoundException; -import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner; -import io.github.lukehutch.fastclasspathscanner.scanner.ScanResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,92 +12,52 @@ import javax.xml.ws.WebServiceClient; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.util.List; public class WebServiceAutoDetector { private static final Logger LOG = LoggerFactory.getLogger(WebServiceAutoDetector.class); protected static final String NO_CLASS_FOUND = "No class found"; + private final WebServiceScanner webServiceScanner; + + public static final Class SEI_ANNOTATION = WebService.class; + public static final Class WEB_SERVICE_CLIENT_ANNOTATION = WebServiceClient.class; + + public WebServiceAutoDetector(WebServiceScanner webServiceScanner) { + this.webServiceScanner = webServiceScanner; + } @SuppressWarnings("unchecked") public T searchAndInstantiateSeiImplementation(Class seiName) throws BootStarterCxfException { Class implementingClass = null; try { - implementingClass = scanForClassWhichImplementsAndPickFirst(seiName); + implementingClass = webServiceScanner.scanForClassWhichImplementsAndPickFirst(seiName); LOG.info("Found SEI implementing class: '{}'", implementingClass.getName()); } catch (BootStarterCxfException exception) { - SeiImplClassNotFoundException seiNotFound = new SeiImplClassNotFoundException("No SEI implementing class found"); - seiNotFound.setNotFoundClassName(seiName.getName()); - throw seiNotFound; + throw SeiImplClassNotFoundException.build().setNotFoundClassName(seiName.getName()); } return instantiateFromClass(implementingClass); } public Class searchServiceEndpointInterface() throws BootStarterCxfException { - Class sei = scanForClassWithAnnotationAndIsAnInterface(WebService.class); - LOG.info("Found Service Endpoint Interface (SEI): '{}'", sei.getName()); - return sei; + try{ + Class sei = webServiceScanner.scanForClassWithAnnotationAndIsAnInterface(SEI_ANNOTATION); + LOG.info("Found Service Endpoint Interface (SEI): '{}'", sei.getName()); + return sei; + } catch (BootStarterCxfException exception) { + throw new SeiNotFoundException(); + } } @SuppressWarnings("unchecked") public Service searchAndInstantiateWebServiceClient() throws BootStarterCxfException { try{ - Class webServiceClientClass = scanForClassWithAnnotationAndPickTheFirstOneFound(WebServiceClient.class); + Class webServiceClientClass = webServiceScanner.scanForClassWithAnnotationAndPickTheFirstOneFound(WEB_SERVICE_CLIENT_ANNOTATION); LOG.info("Found WebServiceClient class: '{}'", webServiceClientClass.getName()); return instantiateFromClass(webServiceClientClass); } catch (BootStarterCxfException exception) { - throw new WebServiceClientNotFoundException( - "There was no class found, that´s annotated with javax.xml.ws.WebServiceClient and implements javax.xml.ws.Service"); - } - } - - protected Class scanForClassWithAnnotationAndPickTheFirstOneFound(Class annotationName) throws BootStarterCxfException { - return classForName(scanForClassNamesWithAnnotation(annotationName).get(0)); - } - - protected Class scanForClassWithAnnotationAndIsAnInterface(Class annotationName) throws BootStarterCxfException { - List namesOfClassesWithAnnotation = scanForClassNamesWithAnnotation(annotationName); - - if(namesOfClassesWithAnnotation.size() > 1) { - return justPickTheClassThatIsAnInterface(namesOfClassesWithAnnotation); - } else { - return classForName(namesOfClassesWithAnnotation.get(0)); - } - } - - protected List scanForClassNamesWithAnnotation(Class annotationName) throws BootStarterCxfException { - List namesOfClassesWithAnnotation = initScannerAndScan().getNamesOfClassesWithAnnotation(annotationName); - - if(namesOfClassesWithAnnotation.isEmpty()) { - throw new BootStarterCxfException(NO_CLASS_FOUND); + throw new WebServiceClientNotFoundException(); } - return namesOfClassesWithAnnotation; - } - - protected Class justPickTheClassThatIsAnInterface(List namesOfClassesWithAnnotation) throws BootStarterCxfException { - for (String className : namesOfClassesWithAnnotation) { - if (isInterface(className)) { - return classForName(className); - } - } - throw new BootStarterCxfException(NO_CLASS_FOUND); - } - - protected boolean isInterface(String className) throws BootStarterCxfException { - return classForName(className).isInterface(); - } - - protected Class scanForClassWhichImplementsAndPickFirst(Class interfaceName) throws BootStarterCxfException { - List namesOfClassesImplementing = initScannerAndScan().getNamesOfClassesImplementing(interfaceName); - if(namesOfClassesImplementing.isEmpty()) { - throw new BootStarterCxfException(NO_CLASS_FOUND); - } - return classForName(namesOfClassesImplementing.get(0)); - } - - private ScanResult initScannerAndScan() { - return new FastClasspathScanner().scan(); } private T instantiateFromClass(Class clazz) throws BootStarterCxfException { diff --git a/src/main/java/de/codecentric/cxf/autodetection/WebServiceScanner.java b/src/main/java/de/codecentric/cxf/autodetection/WebServiceScanner.java new file mode 100644 index 0000000..4a58d52 --- /dev/null +++ b/src/main/java/de/codecentric/cxf/autodetection/WebServiceScanner.java @@ -0,0 +1,69 @@ +package de.codecentric.cxf.autodetection; + +import de.codecentric.cxf.common.BootStarterCxfException; +import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner; +import io.github.lukehutch.fastclasspathscanner.scanner.ScanResult; + +import java.util.List; + +public class WebServiceScanner { + + protected static final String NO_CLASS_FOUND = "No class found"; + + protected Class scanForClassWhichImplementsAndPickFirst(Class interfaceName) throws BootStarterCxfException { + List namesOfClassesImplementing = initScannerAndScan().getNamesOfClassesImplementing(interfaceName); + if (namesOfClassesImplementing.isEmpty()) { + throw new BootStarterCxfException(WebServiceAutoDetector.NO_CLASS_FOUND); + } + return classForName(namesOfClassesImplementing.get(0)); + } + + protected Class scanForClassWithAnnotationAndPickTheFirstOneFound(Class annotationName) throws BootStarterCxfException { + return classForName(scanForClassNamesWithAnnotation(annotationName).get(0)); + } + + protected List scanForClassNamesWithAnnotation(Class annotationName) throws BootStarterCxfException { + List namesOfClassesWithAnnotation = initScannerAndScan().getNamesOfClassesWithAnnotation(annotationName); + + if(namesOfClassesWithAnnotation.isEmpty()) { + throw new BootStarterCxfException(NO_CLASS_FOUND); + } + return namesOfClassesWithAnnotation; + } + + protected Class scanForClassWithAnnotationAndIsAnInterface(Class annotationName) throws BootStarterCxfException { + List namesOfClassesWithAnnotation = scanForClassNamesWithAnnotation(annotationName); + + if(namesOfClassesWithAnnotation.size() > 1) { + return justPickTheClassThatIsAnInterface(namesOfClassesWithAnnotation); + } else { + return classForName(namesOfClassesWithAnnotation.get(0)); + } + } + + protected Class justPickTheClassThatIsAnInterface(List namesOfClassesWithAnnotation) throws BootStarterCxfException { + for (String className : namesOfClassesWithAnnotation) { + if (isInterface(className)) { + return classForName(className); + } + } + throw new BootStarterCxfException(NO_CLASS_FOUND); + } + + protected boolean isInterface(String className) throws BootStarterCxfException { + return classForName(className).isInterface(); + } + + private ScanResult initScannerAndScan() { + return new FastClasspathScanner().scan(); + } + + + protected Class classForName(String className) throws BootStarterCxfException { + try { + return Class.forName(className); + } catch (ClassNotFoundException exception) { + throw new BootStarterCxfException(NO_CLASS_FOUND, exception); + } + } +} \ No newline at end of file diff --git a/src/main/java/de/codecentric/cxf/configuration/CxfAutoConfiguration.java b/src/main/java/de/codecentric/cxf/configuration/CxfAutoConfiguration.java index 4aaccd1..d666a84 100644 --- a/src/main/java/de/codecentric/cxf/configuration/CxfAutoConfiguration.java +++ b/src/main/java/de/codecentric/cxf/configuration/CxfAutoConfiguration.java @@ -8,6 +8,7 @@ import javax.xml.ws.Service; import de.codecentric.cxf.autodetection.WebServiceAutoDetector; +import de.codecentric.cxf.autodetection.WebServiceScanner; import de.codecentric.cxf.common.BootStarterCxfException; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; @@ -48,7 +49,7 @@ public class CxfAutoConfiguration { private String serviceUrlEnding = ""; - private WebServiceAutoDetector webServiceAutoDetector = new WebServiceAutoDetector();; + private WebServiceAutoDetector webServiceAutoDetector = new WebServiceAutoDetector(new WebServiceScanner());; @PostConstruct public void setUp() throws BootStarterCxfException { diff --git a/src/main/java/de/codecentric/cxf/diagnostics/SeiImplClassNotFoundException.java b/src/main/java/de/codecentric/cxf/diagnostics/SeiImplClassNotFoundException.java index 3fff5eb..b076319 100644 --- a/src/main/java/de/codecentric/cxf/diagnostics/SeiImplClassNotFoundException.java +++ b/src/main/java/de/codecentric/cxf/diagnostics/SeiImplClassNotFoundException.java @@ -9,8 +9,14 @@ */ public class SeiImplClassNotFoundException extends BootStarterCxfException { - public SeiImplClassNotFoundException(String message) { - super(message); + protected static final String MESSAGE = "The Service Endpoint Interface (SEI) implementing class could´nt be found"; + + public SeiImplClassNotFoundException() { + super(MESSAGE); + } + + public static SeiImplClassNotFoundException build() { + return new SeiImplClassNotFoundException(); } private String notFoundClassName; @@ -19,7 +25,8 @@ public String getNotFoundClassName() { return notFoundClassName; } - public void setNotFoundClassName(String notFoundClassName) { + public SeiImplClassNotFoundException setNotFoundClassName(String notFoundClassName) { this.notFoundClassName = notFoundClassName; + return this; } } diff --git a/src/main/java/de/codecentric/cxf/diagnostics/SeiImplMissingFailureAnalyzer.java b/src/main/java/de/codecentric/cxf/diagnostics/SeiImplMissingFailureAnalyzer.java index b7e990f..5eb4205 100644 --- a/src/main/java/de/codecentric/cxf/diagnostics/SeiImplMissingFailureAnalyzer.java +++ b/src/main/java/de/codecentric/cxf/diagnostics/SeiImplMissingFailureAnalyzer.java @@ -13,7 +13,7 @@ public class SeiImplMissingFailureAnalyzer extends AbstractFailureAnalyzer{ + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, SeiNotFoundException cause) { + return new FailureAnalysis(SeiNotFoundException.MESSAGE, + "Use the cxf-spring-boot-starter-maven-plugin (https://github.com/codecentric/cxf-spring-boot-starter-maven-plugin) " + + "to generate the needed class files from your WSDL & XSDs", cause); + } +} diff --git a/src/main/java/de/codecentric/cxf/diagnostics/SeiNotFoundException.java b/src/main/java/de/codecentric/cxf/diagnostics/SeiNotFoundException.java new file mode 100644 index 0000000..47de9da --- /dev/null +++ b/src/main/java/de/codecentric/cxf/diagnostics/SeiNotFoundException.java @@ -0,0 +1,17 @@ +package de.codecentric.cxf.diagnostics; + +import de.codecentric.cxf.common.BootStarterCxfException; + +/** + * Thrown when the Service Endpoint Interface (SEI) itself isn´t found. + * + * @author jonashackt + */ +public class SeiNotFoundException extends BootStarterCxfException { + + protected static final String MESSAGE = "The Service Endpoint Interface (SEI) could´nt be found - an interface that´s annotated with javax.jws.WebService"; + + public SeiNotFoundException() { + super(MESSAGE); + } +} diff --git a/src/main/java/de/codecentric/cxf/diagnostics/WebServiceClientNotFoundException.java b/src/main/java/de/codecentric/cxf/diagnostics/WebServiceClientNotFoundException.java index ea4df3e..3a7c106 100644 --- a/src/main/java/de/codecentric/cxf/diagnostics/WebServiceClientNotFoundException.java +++ b/src/main/java/de/codecentric/cxf/diagnostics/WebServiceClientNotFoundException.java @@ -9,7 +9,9 @@ */ public class WebServiceClientNotFoundException extends BootStarterCxfException { - public WebServiceClientNotFoundException(String message) { - super(message); + protected static final String MESSAGE = "There was no class found, that´s annotated with javax.xml.ws.WebServiceClient and implements javax.xml.ws.Service"; + + public WebServiceClientNotFoundException() { + super(MESSAGE); } } diff --git a/src/test/java/de/codecentric/cxf/autodetection/WebServiceAutoDetectorTest.java b/src/test/java/de/codecentric/cxf/autodetection/WebServiceAutoDetectorTest.java index be2fcb3..8de02ca 100644 --- a/src/test/java/de/codecentric/cxf/autodetection/WebServiceAutoDetectorTest.java +++ b/src/test/java/de/codecentric/cxf/autodetection/WebServiceAutoDetectorTest.java @@ -3,6 +3,7 @@ import de.codecentric.cxf.TestServiceEndpoint; import de.codecentric.cxf.common.BootStarterCxfException; import de.codecentric.cxf.diagnostics.SeiImplClassNotFoundException; +import de.codecentric.cxf.diagnostics.SeiNotFoundException; import de.codecentric.cxf.diagnostics.WebServiceClientNotFoundException; import de.codecentric.namespace.weatherservice.Weather; import de.codecentric.namespace.weatherservice.WeatherService; @@ -14,99 +15,104 @@ import javax.jws.WebService; import javax.xml.namespace.QName; import javax.xml.ws.Service; +import javax.xml.ws.WebServiceClient; import java.util.Arrays; import java.util.List; +import static de.codecentric.cxf.autodetection.WebServiceAutoDetector.SEI_ANNOTATION; +import static de.codecentric.cxf.autodetection.WebServiceAutoDetector.WEB_SERVICE_CLIENT_ANNOTATION; import static junit.framework.TestCase.fail; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsNull.notNullValue; +import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) public class WebServiceAutoDetectorTest { - private WebServiceAutoDetector webServiceAutoDetector; - private WebServiceAutoDetectorTestable webServiceAutoDetectorTestable; + private static final Class WEATHER_SERVICE_ENDPOINT_INTERFACE = WeatherService.class; + private static final Class WEATHER_WEBSERVICE_CLIENT = Weather.class; + private static final Class WEATHER_SEI_IMPLEMENTING_CLASS = TestServiceEndpoint.class; - @Before - public void setUp() { - webServiceAutoDetector = new WebServiceAutoDetector(); - webServiceAutoDetectorTestable = new WebServiceAutoDetectorTestable(); - } + private final BootStarterCxfException STARTER_EXCEPTION_NO_CLASS_FOUND = new BootStarterCxfException(WebServiceScanner.NO_CLASS_FOUND); @Test public void is_SEI_Successfully_detected() throws BootStarterCxfException { + WebServiceScanner scannerMock = mock(WebServiceScanner.class); + when(scannerMock.scanForClassWithAnnotationAndIsAnInterface(SEI_ANNOTATION)).thenReturn(WEATHER_SERVICE_ENDPOINT_INTERFACE); + WebServiceAutoDetector webServiceAutoDetector = new WebServiceAutoDetector(scannerMock); + Class serviceEndpointInterface = webServiceAutoDetector.searchServiceEndpointInterface(); assertThat(serviceEndpointInterface, is(notNullValue())); - assertThat(serviceEndpointInterface.getSimpleName(), is(equalTo("WeatherService"))); - } - - @Test public void - is_SEI_successfully_detected_when_the_SEI_implementing_class_also_has_the_same_WebService_Annotation() throws BootStarterCxfException { - - Class weather = null; - - try { - weather = webServiceAutoDetectorTestable.scanForClassWithAnnotationAndIsAnInterface(WebService.class); - } catch (BootStarterCxfException exception) { - fail("Interface should have been found!"); - } - - assertThat(weather, is(equalTo(WeatherService.class))); - } - - protected static List generateListWithSeiAndSeiImplNameWithBothWebServiceAnnotation() { - Class sei = WeatherService.class; - Class endpointImplementation = TestServiceEndpoint.class; - return Arrays.asList(sei.getSimpleName(), endpointImplementation.getSimpleName()); - } - - @Test public void - picks_the_interface_from_two_classes_where_one_is_an_interface_and_the_other_not() throws BootStarterCxfException, ClassNotFoundException { - List twoWebServices = generateListWithSeiAndSeiImplNameWithBothWebServiceAnnotation(); - - Class interfaze = webServiceAutoDetectorTestable.justPickTheClassThatIsAnInterface(twoWebServices); - - assertThat(interfaze, is(notNullValue())); - assertThat(interfaze.isInterface(), is(true)); + assertThat(serviceEndpointInterface.getSimpleName(), is(equalTo(WEATHER_SERVICE_ENDPOINT_INTERFACE.getSimpleName()))); } @Test public void is_SEI_Implementation_successfully_found_and_instantiated() throws NoSuchFieldException, BootStarterCxfException { - Class serviceEndpointInterface = webServiceAutoDetector.searchServiceEndpointInterface(); - WeatherService weatherServiceEndpointImpl = webServiceAutoDetector.searchAndInstantiateSeiImplementation(serviceEndpointInterface); + WebServiceScanner scannerMock = mock(WebServiceScanner.class); + when(scannerMock.scanForClassWhichImplementsAndPickFirst(WEATHER_SERVICE_ENDPOINT_INTERFACE)).thenReturn(WEATHER_SEI_IMPLEMENTING_CLASS); + WebServiceAutoDetector autoDetector = new WebServiceAutoDetector(scannerMock); + + WeatherService weatherServiceEndpointImpl = autoDetector.searchAndInstantiateSeiImplementation(WEATHER_SERVICE_ENDPOINT_INTERFACE); assertThat(weatherServiceEndpointImpl, is(notNullValue())); - assertThat(weatherServiceEndpointImpl.getClass().getSimpleName(), is(equalTo("TestServiceEndpoint"))); + assertThat(weatherServiceEndpointImpl.getClass().getSimpleName(), is(equalTo(WEATHER_SEI_IMPLEMENTING_CLASS.getSimpleName()))); } @Test public void is_WebServiceClient_successfully_found_and_instantiated() throws BootStarterCxfException { - Service webServiceClient = webServiceAutoDetector.searchAndInstantiateWebServiceClient(); + WebServiceScanner scannerMock = mock(WebServiceScanner.class); + when(scannerMock.scanForClassWithAnnotationAndPickTheFirstOneFound(WEB_SERVICE_CLIENT_ANNOTATION)).thenReturn(WEATHER_WEBSERVICE_CLIENT); + WebServiceAutoDetector autoDetector = new WebServiceAutoDetector(scannerMock); + + Service webServiceClient = autoDetector.searchAndInstantiateWebServiceClient(); assertThat(webServiceClient, is(notNullValue())); QName serviceNameQName = webServiceClient.getServiceName(); - assertThat(serviceNameQName.getLocalPart(), is(equalTo("Weather"))); + assertThat(serviceNameQName.getLocalPart(), is(equalTo(WEATHER_WEBSERVICE_CLIENT.getSimpleName()))); } @Test(expected = SeiImplClassNotFoundException.class) public void should_react_with_custom_startup_Failure_Message_console_if_SEI_implementing_class_is_missing() throws BootStarterCxfException { - Class serviceEndpointInterface = WeatherService.class; - webServiceAutoDetectorTestable.searchAndInstantiateSeiImplementation(serviceEndpointInterface); + WebServiceScanner scannerMock = mock(WebServiceScanner.class); + when(scannerMock.scanForClassWhichImplementsAndPickFirst(WEATHER_SERVICE_ENDPOINT_INTERFACE)).thenThrow(STARTER_EXCEPTION_NO_CLASS_FOUND); + WebServiceAutoDetector autoDetector = new WebServiceAutoDetector(scannerMock); + + autoDetector.searchAndInstantiateSeiImplementation(WEATHER_SERVICE_ENDPOINT_INTERFACE); } @Test(expected = WebServiceClientNotFoundException.class) public void should_react_with_custom_startup_Failure_Message_if_WebServiceClient_annotated_class_is_missing() throws BootStarterCxfException { - webServiceAutoDetectorTestable.searchAndInstantiateWebServiceClient(); + + WebServiceScanner scannerMock = mock(WebServiceScanner.class); + when(scannerMock.scanForClassWithAnnotationAndPickTheFirstOneFound(WEB_SERVICE_CLIENT_ANNOTATION)).thenThrow(STARTER_EXCEPTION_NO_CLASS_FOUND); + WebServiceAutoDetector autoDetector = new WebServiceAutoDetector(scannerMock); + + autoDetector.searchAndInstantiateWebServiceClient(); + } + + @Test(expected = SeiNotFoundException.class) public void + should_react_with_custom_startup_Failure_Message_if_SEI_is_missing() throws BootStarterCxfException { + + WebServiceScanner scannerMock = mock(WebServiceScanner.class); + when(scannerMock.scanForClassNamesWithAnnotation(SEI_ANNOTATION)).thenThrow(STARTER_EXCEPTION_NO_CLASS_FOUND); + when(scannerMock.scanForClassWithAnnotationAndIsAnInterface(SEI_ANNOTATION)).thenCallRealMethod(); + WebServiceAutoDetector autoDetector = new WebServiceAutoDetector(scannerMock); + + autoDetector.searchServiceEndpointInterface(); + } + + protected static List generateListWithSeiAndSeiImplNameWithBothWebServiceAnnotation() { + return Arrays.asList(WEATHER_SERVICE_ENDPOINT_INTERFACE.getSimpleName(), WEATHER_SEI_IMPLEMENTING_CLASS.getSimpleName()); } } diff --git a/src/test/java/de/codecentric/cxf/autodetection/WebServiceAutoDetectorTestable.java b/src/test/java/de/codecentric/cxf/autodetection/WebServiceAutoDetectorTestable.java deleted file mode 100644 index 6d2c0a5..0000000 --- a/src/test/java/de/codecentric/cxf/autodetection/WebServiceAutoDetectorTestable.java +++ /dev/null @@ -1,57 +0,0 @@ -package de.codecentric.cxf.autodetection; - -import de.codecentric.cxf.common.BootStarterCxfException; -import de.codecentric.namespace.weatherservice.Weather; -import de.codecentric.namespace.weatherservice.WeatherService; - -import javax.jws.WebService; -import javax.xml.ws.WebServiceClient; -import java.util.List; - -public class WebServiceAutoDetectorTestable extends WebServiceAutoDetector { - - @Override - protected Class classForName(String className) throws BootStarterCxfException { - if("WeatherService".equals(className)) { - return WeatherService.class; - } else { - return super.classForName(className); - } - } - - @Override - protected boolean isInterface(String className) throws BootStarterCxfException { - if("WeatherService".equals(className)) { - return true; - } else if ("TestServiceEndpoint".equals(className)) { - return false; - } else { - return super.isInterface(className); - } - } - - @Override - protected List scanForClassNamesWithAnnotation(Class annotationName) throws BootStarterCxfException { - if(WebService.class.equals(annotationName)) { - return WebServiceAutoDetectorTest.generateListWithSeiAndSeiImplNameWithBothWebServiceAnnotation(); - } else { - return super.scanForClassNamesWithAnnotation(annotationName); - } - } - - @Override - protected Class scanForClassWhichImplementsAndPickFirst(Class interfaceName) throws BootStarterCxfException { - if(WeatherService.class.equals(interfaceName)) { - throw new BootStarterCxfException(NO_CLASS_FOUND); - } - return super.scanForClassWhichImplementsAndPickFirst(interfaceName); - } - - @Override - protected Class scanForClassWithAnnotationAndPickTheFirstOneFound(Class annotationName) throws BootStarterCxfException { - if(WebServiceClient.class.equals(annotationName)) { - throw new BootStarterCxfException(NO_CLASS_FOUND); - } - return super.scanForClassWithAnnotationAndPickTheFirstOneFound(annotationName); - } -} diff --git a/src/test/java/de/codecentric/cxf/autodetection/WebServiceScannerTest.java b/src/test/java/de/codecentric/cxf/autodetection/WebServiceScannerTest.java new file mode 100644 index 0000000..24a650e --- /dev/null +++ b/src/test/java/de/codecentric/cxf/autodetection/WebServiceScannerTest.java @@ -0,0 +1,47 @@ +package de.codecentric.cxf.autodetection; + + +import de.codecentric.cxf.common.BootStarterCxfException; +import de.codecentric.namespace.weatherservice.WeatherService; +import org.junit.Test; + +import javax.jws.WebService; +import java.util.List; + +import static de.codecentric.cxf.autodetection.WebServiceAutoDetectorTest.generateListWithSeiAndSeiImplNameWithBothWebServiceAnnotation; +import static junit.framework.TestCase.fail; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.notNullValue; + +public class WebServiceScannerTest { + + private WebServiceScanner webServiceScanner = new WebServiceScanner(); + + @Test public void + is_SEI_successfully_detected_when_the_SEI_implementing_class_also_has_the_same_WebService_Annotation() throws BootStarterCxfException { + + Class weather = null; + + try { + weather = webServiceScanner.scanForClassWithAnnotationAndIsAnInterface(WebService.class); + } catch (BootStarterCxfException exception) { + fail("Interface should have been found!"); + } + + assertThat(weather, is(equalTo(WeatherService.class))); + } + + @Test public void + picks_the_interface_from_two_classes_where_one_is_an_interface_and_the_other_not() throws BootStarterCxfException, ClassNotFoundException { + List twoWebServices = generateListWithSeiAndSeiImplNameWithBothWebServiceAnnotation(); + + Class interfaze = webServiceScanner.justPickTheClassThatIsAnInterface(twoWebServices); + + assertThat(interfaze, is(notNullValue())); + assertThat(interfaze.isInterface(), is(true)); + } + + +} diff --git a/src/test/java/de/codecentric/cxf/diagnostics/ClassesForAutodetectionMissingFailureAnalyzerHelper.java b/src/test/java/de/codecentric/cxf/diagnostics/ClassesForAutodetectionMissingFailureAnalyzerHelper.java deleted file mode 100644 index c5adcbb..0000000 --- a/src/test/java/de/codecentric/cxf/diagnostics/ClassesForAutodetectionMissingFailureAnalyzerHelper.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.codecentric.cxf.diagnostics; - -import de.codecentric.cxf.autodetection.WebServiceAutoDetector; -import de.codecentric.cxf.common.BootStarterCxfException; -import de.codecentric.namespace.weatherservice.WeatherService; - -public class ClassesForAutodetectionMissingFailureAnalyzerHelper { - - protected static SeiImplClassNotFoundException mockSeiImplClassNotFoundException() throws BootStarterCxfException { - Class serviceEndpointInterface = WeatherService.class; - - SeiImplClassNotFoundException seiImplNotFoundException = new SeiImplClassNotFoundException("No SEI implementing class found"); - seiImplNotFoundException.setNotFoundClassName(serviceEndpointInterface.getName()); - return seiImplNotFoundException; - } - - protected static WebServiceClientNotFoundException mockWebServiceClientNotFoundException() throws BootStarterCxfException { - return new WebServiceClientNotFoundException("There was no class found, that´s annotated with javax.xml.ws.WebServiceClient and implements javax.xml.ws.Service"); - } -} \ No newline at end of file diff --git a/src/test/java/de/codecentric/cxf/diagnostics/ClassesForAutodetectionMissingFailureAnalyzersTest.java b/src/test/java/de/codecentric/cxf/diagnostics/ClassesForAutodetectionMissingFailureAnalyzersTest.java index 5dd836a..7ca3b85 100644 --- a/src/test/java/de/codecentric/cxf/diagnostics/ClassesForAutodetectionMissingFailureAnalyzersTest.java +++ b/src/test/java/de/codecentric/cxf/diagnostics/ClassesForAutodetectionMissingFailureAnalyzersTest.java @@ -1,10 +1,10 @@ package de.codecentric.cxf.diagnostics; import de.codecentric.cxf.common.BootStarterCxfException; +import de.codecentric.namespace.weatherservice.WeatherService; import org.junit.Test; import org.springframework.boot.diagnostics.FailureAnalysis; -import static de.codecentric.cxf.diagnostics.ClassesForAutodetectionMissingFailureAnalyzerHelper.*; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; @@ -13,27 +13,33 @@ public class ClassesForAutodetectionMissingFailureAnalyzersTest { private SeiImplMissingFailureAnalyzer seiImplMissingFailureAnalyzer = new SeiImplMissingFailureAnalyzer(); private WebServiceClientMissingFailureAnalyzer webServiceClientMissingFailureAnalyzer = new WebServiceClientMissingFailureAnalyzer(); + private SeiMissingFailureAnalyzer seiMissingFailureAnalyzer = new SeiMissingFailureAnalyzer(); @Test public void does_SeiImplMissing_failure_analysis_contain_correct_description() throws BootStarterCxfException { - SeiImplClassNotFoundException seiNotFoundException = mockSeiImplClassNotFoundException(); + SeiImplClassNotFoundException seiNotFoundException = SeiImplClassNotFoundException.build().setNotFoundClassName(WeatherService.class.getName()); FailureAnalysis failureAnalysis = seiImplMissingFailureAnalyzer.analyze(seiNotFoundException); - assertThat(failureAnalysis.getDescription(), containsString("There was no SEI implementing class found")); + assertThat(failureAnalysis.getDescription(), containsString(SeiImplClassNotFoundException.MESSAGE)); } @Test public void does_WebServiceClientMissing_failure_analysis_contain_correct_description() throws BootStarterCxfException { - WebServiceClientNotFoundException webServiceClientNotFoundException = mockWebServiceClientNotFoundException(); + FailureAnalysis failureAnalysis = webServiceClientMissingFailureAnalyzer.analyze(new WebServiceClientNotFoundException()); - FailureAnalysis failureAnalysis = webServiceClientMissingFailureAnalyzer.analyze(webServiceClientNotFoundException); + assertThat(failureAnalysis.getDescription(), containsString(WebServiceClientNotFoundException.MESSAGE)); + } + + @Test public void + does_SeiMissing_failure_analysis_contain_correct_description() { + FailureAnalysis failureAnalysis = seiMissingFailureAnalyzer.analyze(new SeiNotFoundException()); - assertThat(failureAnalysis.getDescription(), containsString("There was no class found, that´s annotated with javax.xml.ws.WebServiceClient")); + assertThat(failureAnalysis.getDescription(), containsString(SeiNotFoundException.MESSAGE)); }