This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 5db2b2f  CAMEL-17574: Allow to specify a main class and load 
properties from default package (#6996)
5db2b2f is described below

commit 5db2b2f308d7756a449a810e98e5227252cabc71
Author: Nicolas Filotto <[email protected]>
AuthorDate: Mon Feb 21 12:43:58 2022 +0100

    CAMEL-17574: Allow to specify a main class and load properties from default 
package (#6996)
---
 .../camel-test/camel-test-main-junit5/pom.xml      | 19 ++++++++
 .../src/main/docs/test-main-junit5.adoc            | 27 +++++++++--
 .../test/main/junit5/CamelMainTestSupport.java     | 54 ++++++++++++++++------
 ...nfigurationTest.java => WithMainClassTest.java} | 31 +++++--------
 ...LoadCustomConfigurationDefaultPackageTest.java} | 10 ++--
 ...=> LoadCustomConfigurationSamePackageTest.java} |  4 +-
 ... custom-default-package-application.properties} |  2 +-
 .../src/test/resources/log4j2.properties           |  4 +-
 ... => custom-same-package-application.properties} |  0
 9 files changed, 102 insertions(+), 49 deletions(-)

diff --git a/components/camel-test/camel-test-main-junit5/pom.xml 
b/components/camel-test/camel-test-main-junit5/pom.xml
index 67eae16..5be74c4 100644
--- a/components/camel-test/camel-test-main-junit5/pom.xml
+++ b/components/camel-test/camel-test-main-junit5/pom.xml
@@ -53,5 +53,24 @@
             <artifactId>camel-xml-io-dsl</artifactId>
             <scope>test</scope>
         </dependency>
+        <!-- logging -->
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+            <version>${log4j2-version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+            <version>${log4j2-version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-slf4j-impl</artifactId>
+            <version>${log4j2-version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git 
a/components/camel-test/camel-test-main-junit5/src/main/docs/test-main-junit5.adoc
 
b/components/camel-test/camel-test-main-junit5/src/main/docs/test-main-junit5.adoc
index c1a3631..073bf2a 100644
--- 
a/components/camel-test/camel-test-main-junit5/src/main/docs/test-main-junit5.adoc
+++ 
b/components/camel-test/camel-test-main-junit5/src/main/docs/test-main-junit5.adoc
@@ -26,11 +26,30 @@ their `pom.xml` for this component:
 </dependency>
 ----
 
+== Specify a main class
+
+Most of the time, a Camel Main application has a main class from which all the 
Camel related classes are found. In practice, this is done simply by providing 
the main class of the application in the constructor of Camel Main like for 
example `new Main(SomeApplication.class)` where `SomeApplication.class` is the 
main class of the application. The same behavior can be simulated with 
`CamelMainTestSupport` by overriding the method `getMainClass()` to provide the 
main class of the applicatio [...]
+
+In the next example, the test class `SomeTest` indicates that the class 
`SomeMainClass` is the main class of the application to test.
+
+[source,java]
+----
+class SomeTest extends CamelMainTestSupport {
+
+    @Override
+    protected Class<?> getMainClass() {
+        return SomeMainClass.class;
+    }
+
+    // Rest of the test class
+}
+----
+
 == Configure Camel as a Camel Main application
 
 A Camel Main application has access to many specific configuration properties 
that are not available from the base class `CamelTestSupport`. The base class 
`CamelMainTestSupport` provides the method 
`configure(MainConfigurationProperties configuration)` that can be overridden 
in order to configure Camel for the test like a Camel Main application.
 
-In the next example, the test class `SomeTest` add a configuration class and 
specify the xml routes to include.
+In the next example, the test class `SomeTest` adds a configuration class and 
specifies the xml routes to include.
 
 [source,java]
 ----
@@ -76,9 +95,11 @@ class SomeTest extends CamelMainTestSupport {
 
 === The file name of the property placeholder
 
-For the sake of simplicity, in case you need only one property placeholder 
location, you can specify the file name of the property placeholder by 
overriding the method `getPropertyPlaceholderFileName()`. It can infer the 
location of the property placeholder as it assumes that it is located in the 
same package as test class.
+For the sake of simplicity, in case you need only one property placeholder 
location, you can specify the file name of the property placeholder by 
overriding the method `getPropertyPlaceholderFileName()`. It can then infer the 
locations of the property placeholder as it assumes that it is located either 
in the same package as the test class or directly in the default package.
+
+In the next example, the method `getPropertyPlaceholderFileName()` has been 
overridden in the test class `com.somecompany.SomeTest` to return 
`custom-application.properties` indicating that the actual possible locations 
of the property placeholder are 
`classpath:com/somecompany/custom-application.properties;optional=true,classpath:custom-application.properties;optional=true`
 which means that for each property to find, it tries to get it first from the 
properties file of the same package  [...]
 
-In the next example, the method `getPropertyPlaceholderFileName()` has been 
overridden in the test class `com.somecompany.SomeTest` to return 
`custom-application.properties` indicating that the actual location of the 
property placeholder is 
`classpath:com/somecompany/custom-application.properties`.
+NOTE: Since the properties files are declared as optional, no exception is 
raised if they are both absent.
 
 [source,java]
 ----
diff --git 
a/components/camel-test/camel-test-main-junit5/src/main/java/org/apache/camel/test/main/junit5/CamelMainTestSupport.java
 
b/components/camel-test/camel-test-main-junit5/src/main/java/org/apache/camel/test/main/junit5/CamelMainTestSupport.java
index d05b69b..1ea05ca 100644
--- 
a/components/camel-test/camel-test-main-junit5/src/main/java/org/apache/camel/test/main/junit5/CamelMainTestSupport.java
+++ 
b/components/camel-test/camel-test-main-junit5/src/main/java/org/apache/camel/test/main/junit5/CamelMainTestSupport.java
@@ -94,31 +94,53 @@ public abstract class CamelMainTestSupport extends 
CamelTestSupport {
      * @return the location of the property placeholders to use for the test.
      */
     protected String getPropertyPlaceholderLocations() {
-        final String location = getPropertyPlaceholderLocation();
-        if (location == null) {
+        final String locations = getPropertyPlaceholderLocationsFromFileName();
+        if (locations == null) {
+            LOG.debug("Use the default property placeholder location");
             return BaseMainSupport.DEFAULT_PROPERTY_PLACEHOLDER_LOCATION;
         }
-        return location;
+        LOG.debug("Use the following property placeholder locations: {}", 
locations);
+        return locations;
     }
 
     /**
-     * Gives the file name of the property placeholder to use for the test. 
This method assumes that the property
-     * placeholder is located in the same package as the test class. In other 
words, if the test class is
+     * Gives the file name of the property placeholder to use for the test. 
This method assumes that the file is located
+     * either in the package of the test class or directly in the default 
package. In other words, if the test class is
      * {@code com.company.SomeTest} and this method has been overridden to 
return {@code some-app.properties}, then it
-     * assumes that the actual location of the property placeholder is
-     * {@code classpath:com/company/some-app.properties}.
-     *
-     * @return the file name of the property placeholder located in the same 
package as the test class. {@code null} by
-     *         default.
+     * assumes that the actual possible locations of the property placeholder 
are
+     * {@code 
classpath:com/company/some-app.properties;optional=true,classpath:some-app.properties;optional=true}
 which
+     * means that for each property to find, it tries to get it first from the 
properties file of the same package if it
+     * exists and if it cannot be found, it tries to get it from the 
properties file with the same name but in the
+     * default package if it exists.
+     * <p>
+     * <b>Note:</b> Since the properties files are declared as optional, no 
exception is raised if they are both absent.
+     * 
+     * @return the file name of the property placeholder located in the same 
package as the test class or directly in
+     *         the default package. {@code null} by default.
      */
     protected String getPropertyPlaceholderFileName() {
         return null;
     }
 
+    /**
+     * Allows to specify the main class of the application to test if needed 
in order to simulate the same behavior as
+     * with {@link org.apache.camel.main.Main#Main(Class)}.
+     *
+     * @return the main class of the application to test if any. {@code null} 
by default indicating that there is no
+     *         specific main class.
+     */
+    protected Class<?> getMainClass() {
+        return null;
+    }
+
     @Override
     protected void postProcessTest() throws Exception {
         LOG.debug("Initialize the camel context as a Camel Main application");
-        MainForTest main = new MainForTest();
+        final MainForTest main = new MainForTest();
+        final Class<?> mainClass = getMainClass();
+        if (mainClass != null) {
+            main.configure().withBasePackageScan(mainClass.getPackageName());
+        }
         configure(main.configure());
         
main.setPropertyPlaceholderLocations(getPropertyPlaceholderLocations());
         
main.setOverrideProperties(useOverridePropertiesWithPropertiesComponent());
@@ -133,15 +155,17 @@ public abstract class CamelMainTestSupport extends 
CamelTestSupport {
     }
 
     /**
-     * @return {@code null} if {@link #getPropertyPlaceholderFileName()} 
returns {@code null}, otherwise it will append
-     *         the package of the test class to the file name of the property 
placeholder.
+     * @return {@code null} if {@link #getPropertyPlaceholderFileName()} 
returns {@code null}, otherwise it generates a
+     *         list of locations assuming that the file is either in the 
package of the test class or directly in the
+     *         default package.
      */
-    private String getPropertyPlaceholderLocation() {
+    private String getPropertyPlaceholderLocationsFromFileName() {
         final String location = getPropertyPlaceholderFileName();
         if (location == null) {
             return null;
         }
-        return String.format("classpath:%s/%s", 
this.getClass().getPackageName().replace('.', '/'), location);
+        return 
String.format("classpath:%s/%s;optional=true,classpath:%s;optional=true",
+                this.getClass().getPackageName().replace('.', '/'), location, 
location);
     }
 
     /**
diff --git 
a/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationTest.java
 
b/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/WithMainClassTest.java
similarity index 55%
copy from 
components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationTest.java
copy to 
components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/WithMainClassTest.java
index eaae13b..123f860 100644
--- 
a/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationTest.java
+++ 
b/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/WithMainClassTest.java
@@ -14,41 +14,32 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.test.main.junit5.custom;
+package org.apache.camel.test.main.junit5;
 
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.main.MainConfigurationProperties;
-import org.apache.camel.test.main.junit5.CamelMainTestSupport;
-import org.apache.camel.test.main.junit5.MyConfiguration;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 /**
- * The test class ensuring that a custom property placeholder can be specified 
with the name of the file located in the
- * same package.
+ * A test class ensuring that the main class of the application to test can be 
specified.
  */
-class LoadCustomConfigurationTest extends CamelMainTestSupport {
+class WithMainClassTest extends CamelMainTestSupport {
 
     @Override
-    protected String getPropertyPlaceholderFileName() {
-        return "custom-application.properties";
-    }
-
-    @Override
-    protected void configure(MainConfigurationProperties configuration) {
-        // Add the configuration class
-        configuration.addConfiguration(MyConfiguration.class);
-        // Add all the XML routes
-        configuration.withRoutesIncludePattern("routes/*.xml");
+    protected Class<?> getMainClass() {
+        return MyMainClass.class;
     }
 
     @Test
-    void shouldFindCustomConfiguration() throws Exception {
+    void shouldFindTheRouteBuilder() throws Exception {
         MockEndpoint mock = context.getEndpoint("mock:out", 
MockEndpoint.class);
-        mock.expectedBodiesReceived("Hello John!");
+        mock.expectedBodiesReceived("Hello Will!");
         String result = template.requestBody("direct:in", null, String.class);
         mock.assertIsSatisfied();
-        assertEquals("Hello John!", result);
+        assertEquals("Hello Will!", result);
+    }
+
+    static class MyMainClass {
     }
 }
diff --git 
a/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationTest.java
 
b/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationDefaultPackageTest.java
similarity index 88%
copy from 
components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationTest.java
copy to 
components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationDefaultPackageTest.java
index eaae13b..77bf863 100644
--- 
a/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationTest.java
+++ 
b/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationDefaultPackageTest.java
@@ -26,13 +26,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 
 /**
  * The test class ensuring that a custom property placeholder can be specified 
with the name of the file located in the
- * same package.
+ * default package.
  */
-class LoadCustomConfigurationTest extends CamelMainTestSupport {
+class LoadCustomConfigurationDefaultPackageTest extends CamelMainTestSupport {
 
     @Override
     protected String getPropertyPlaceholderFileName() {
-        return "custom-application.properties";
+        return "custom-default-package-application.properties";
     }
 
     @Override
@@ -46,9 +46,9 @@ class LoadCustomConfigurationTest extends 
CamelMainTestSupport {
     @Test
     void shouldFindCustomConfiguration() throws Exception {
         MockEndpoint mock = context.getEndpoint("mock:out", 
MockEndpoint.class);
-        mock.expectedBodiesReceived("Hello John!");
+        mock.expectedBodiesReceived("Hello Johnny!");
         String result = template.requestBody("direct:in", null, String.class);
         mock.assertIsSatisfied();
-        assertEquals("Hello John!", result);
+        assertEquals("Hello Johnny!", result);
     }
 }
diff --git 
a/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationTest.java
 
b/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationSamePackageTest.java
similarity index 93%
rename from 
components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationTest.java
rename to 
components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationSamePackageTest.java
index eaae13b..06a0019 100644
--- 
a/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationTest.java
+++ 
b/components/camel-test/camel-test-main-junit5/src/test/java/org/apache/camel/test/main/junit5/custom/LoadCustomConfigurationSamePackageTest.java
@@ -28,11 +28,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
  * The test class ensuring that a custom property placeholder can be specified 
with the name of the file located in the
  * same package.
  */
-class LoadCustomConfigurationTest extends CamelMainTestSupport {
+class LoadCustomConfigurationSamePackageTest extends CamelMainTestSupport {
 
     @Override
     protected String getPropertyPlaceholderFileName() {
-        return "custom-application.properties";
+        return "custom-same-package-application.properties";
     }
 
     @Override
diff --git 
a/components/camel-test/camel-test-main-junit5/src/test/resources/org/apache/camel/test/main/junit5/custom/custom-application.properties
 
b/components/camel-test/camel-test-main-junit5/src/test/resources/custom-default-package-application.properties
similarity index 98%
copy from 
components/camel-test/camel-test-main-junit5/src/test/resources/org/apache/camel/test/main/junit5/custom/custom-application.properties
copy to 
components/camel-test/camel-test-main-junit5/src/test/resources/custom-default-package-application.properties
index 4d0c253..551069b 100644
--- 
a/components/camel-test/camel-test-main-junit5/src/test/resources/org/apache/camel/test/main/junit5/custom/custom-application.properties
+++ 
b/components/camel-test/camel-test-main-junit5/src/test/resources/custom-default-package-application.properties
@@ -16,5 +16,5 @@
 ## ---------------------------------------------------------------------------
 
 # application properties
-name = John
+name = Johnny
 
diff --git 
a/components/camel-test/camel-test-main-junit5/src/test/resources/log4j2.properties
 
b/components/camel-test/camel-test-main-junit5/src/test/resources/log4j2.properties
index 53994e6..bac0696 100644
--- 
a/components/camel-test/camel-test-main-junit5/src/test/resources/log4j2.properties
+++ 
b/components/camel-test/camel-test-main-junit5/src/test/resources/log4j2.properties
@@ -17,14 +17,12 @@
 
 appender.file.type = File
 appender.file.name = file
-appender.file.fileName = target/camel-test.log
+appender.file.fileName = target/camel-test-main.log
 appender.file.layout.type = PatternLayout
 appender.file.layout.pattern = %d %-5p %c{1} - %m %n
 appender.out.type = Console
 appender.out.name = out
 appender.out.layout.type = PatternLayout
 appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n
-logger.springframework.name = org.springframework
-logger.springframework.level = WARN
 rootLogger.level = INFO
 rootLogger.appenderRef.file.ref = file
diff --git 
a/components/camel-test/camel-test-main-junit5/src/test/resources/org/apache/camel/test/main/junit5/custom/custom-application.properties
 
b/components/camel-test/camel-test-main-junit5/src/test/resources/org/apache/camel/test/main/junit5/custom/custom-same-package-application.properties
similarity index 100%
rename from 
components/camel-test/camel-test-main-junit5/src/test/resources/org/apache/camel/test/main/junit5/custom/custom-application.properties
rename to 
components/camel-test/camel-test-main-junit5/src/test/resources/org/apache/camel/test/main/junit5/custom/custom-same-package-application.properties

Reply via email to