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

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

commit cc7ec0414f12ae5d68d7752b183034140cd85a4b
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Feb 12 11:33:50 2021 +0100

    Correctly fix printer tests
---
 .../camel/component/printer/PrinterProducer.java   |  2 +-
 .../camel/component/printer/PrinterPrintTest.java  | 28 ++++++++++++++++------
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git 
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
 
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
index 2bb08dc..dcc78bf 100644
--- 
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
+++ 
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
@@ -157,7 +157,7 @@ public class PrinterProducer extends DefaultProducer {
             // align slashes so we match / or \
             printerName = printerName.toLowerCase(Locale.US);
             printerName = printerName.replace('\\', '/');
-            if (printer.endsWith(printerName)) {
+            if (printer.endsWith(printerName) || 
printerName.endsWith(printer)) {
                 position = i;
                 break;
             }
diff --git 
a/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java
 
b/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java
index cc3b349..87c5afe 100644
--- 
a/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java
+++ 
b/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.printer;
 
 import java.io.FileInputStream;
 import java.io.InputStream;
+import java.lang.reflect.Method;
 import java.util.Map;
 
 import javax.print.Doc;
@@ -58,13 +59,13 @@ import static org.mockito.Mockito.when;
 public class PrinterPrintTest extends CamelTestSupport {
 
     @BeforeEach
-    public void setup() {
+    public void setup() throws Exception {
         setupJavaPrint();
     }
 
     @AfterEach
     public void tearDown() {
-        
sun.awt.AppContext.getAppContext().put(PrintServiceLookup.class.getDeclaredClasses()[0],
 null);
+        restoreJavaPrint();
     }
 
     @Override
@@ -224,10 +225,7 @@ public class PrinterPrintTest extends CamelTestSupport {
 
     @Test
     public void moreThanOneLprEndpoint() throws Exception {
-
-        if (isAwtHeadless()) {
-            return;
-        }
+        assumeTrue(isAwtHeadless());
 
         int numberOfPrintservicesBefore = 
PrintServiceLookup.lookupPrintServices(null, null).length;
 
@@ -402,7 +400,18 @@ public class PrinterPrintTest extends CamelTestSupport {
         assertEquals("reverse-landscape", attribute.toString());
     }
 
-    protected void setupJavaPrint() {
+    Class<?> printServiceLookupServicesClass = 
PrintServiceLookup.class.getDeclaredClasses()[0];
+    Object printServiceLookup;
+
+    protected void setupJavaPrint() throws Exception {
+        // save the current print services
+        printServiceLookup = 
sun.awt.AppContext.getAppContext().get(printServiceLookupServicesClass);
+        // setup a new empty list of printer services
+        
sun.awt.AppContext.getAppContext().put(printServiceLookupServicesClass, null);
+        Method method = 
PrintServiceLookup.class.getDeclaredMethod("initListOfLookupServices");
+        method.setAccessible(true);
+        method.invoke(null);
+
         // "install" another default printer
         PrintService psDefault = mock(PrintService.class);
         when(psDefault.getName()).thenReturn("DefaultPrinter");
@@ -421,4 +430,9 @@ public class PrinterPrintTest extends CamelTestSupport {
         PrintServiceLookup.registerServiceProvider(psLookup);
     }
 
+    protected void restoreJavaPrint() {
+        // restore print services
+        
sun.awt.AppContext.getAppContext().put(printServiceLookupServicesClass, 
printServiceLookup);
+    }
+
 }

Reply via email to