camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r1300804 - in /camel/trunk/components/camel-printer: ./ src/main/java/org/apache/camel/component/printer/ src/test/java/org/apache/camel/component/printer/
Date Thu, 15 Mar 2012 02:08:03 GMT
Author: ningjiang
Date: Thu Mar 15 02:08:03 2012
New Revision: 1300804

URL: http://svn.apache.org/viewvc?rev=1300804&view=rev
Log:
CAMEL-5082 enhanments on the camel-printer

Modified:
    camel/trunk/components/camel-printer/pom.xml
    camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterComponent.java
    camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterEndpoint.java
    camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java
    camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperationsInterface.java
    camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
    camel/trunk/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java

Modified: camel/trunk/components/camel-printer/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/pom.xml?rev=1300804&r1=1300803&r2=1300804&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/pom.xml (original)
+++ camel/trunk/components/camel-printer/pom.xml Thu Mar 15 02:08:03 2012
@@ -53,7 +53,12 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
-
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.8.5</version>
+            <scope>test</scope>
+        </dependency>
         <!-- logging -->
         <dependency>
             <groupId>org.slf4j</groupId>
@@ -76,7 +81,6 @@
                     </filesets>
                 </configuration>
             </plugin>
-
         </plugins>
     </build>
     

Modified: camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterComponent.java?rev=1300804&r1=1300803&r2=1300804&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterComponent.java
(original)
+++ camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterComponent.java
Thu Mar 15 02:08:03 2012
@@ -19,29 +19,18 @@ package org.apache.camel.component.print
 import java.net.URI;
 import java.util.Map;
 
-import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
 
 public class PrinterComponent extends DefaultComponent {
-    PrinterConfiguration config;
-    
-    public PrinterComponent() {
-        config = new PrinterConfiguration();
-    }
-
-    public PrinterComponent(CamelContext context) {
-        super(context);
-        config = new PrinterConfiguration();
-    }
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
+        PrinterConfiguration config = new PrinterConfiguration();
         config.parseURI(new URI(uri));
-        
+
         PrinterEndpoint printerEndpoint = new PrinterEndpoint(uri, this, config);
         setProperties(printerEndpoint.getConfig(), parameters);
         return new PrinterEndpoint(uri, this, config);
     }
-
 }

Modified: camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterEndpoint.java?rev=1300804&r1=1300803&r2=1300804&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterEndpoint.java
(original)
+++ camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterEndpoint.java
Thu Mar 15 02:08:03 2012
@@ -23,6 +23,9 @@ import org.apache.camel.Producer;
 import org.apache.camel.impl.DefaultEndpoint;
 
 public class PrinterEndpoint extends DefaultEndpoint {
+    
+    public static final String JOB_NAME = "PrinterJobName";
+    
     private PrinterConfiguration config;
 
     public PrinterEndpoint() {

Modified: camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java?rev=1300804&r1=1300803&r2=1300804&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java
(original)
+++ camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java
Thu Mar 15 02:08:03 2012
@@ -19,6 +19,7 @@ package org.apache.camel.component.print
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
+import java.util.Locale;
 import java.util.UUID;
 import javax.print.Doc;
 import javax.print.DocFlavor;
@@ -29,6 +30,7 @@ import javax.print.PrintServiceLookup;
 import javax.print.attribute.HashPrintRequestAttributeSet;
 import javax.print.attribute.PrintRequestAttributeSet;
 import javax.print.attribute.standard.Copies;
+import javax.print.attribute.standard.JobName;
 import javax.print.attribute.standard.MediaSizeName;
 import javax.print.attribute.standard.Sides;
 
@@ -56,13 +58,12 @@ public class PrinterOperations implement
     }
 
     public PrinterOperations(PrintService printService, DocFlavor flavor, PrintRequestAttributeSet
printRequestAttributeSet) throws PrintException {
-        this();
         this.setPrintService(printService);
         this.setFlavor(flavor);
         this.setPrintRequestAttributeSet(printRequestAttributeSet);
     }
 
-    public void print(Doc doc, int copies, boolean sendToPrinter, String mimeType) throws
PrintException {
+    public void print(Doc doc, int copies, boolean sendToPrinter, String mimeType, String
jobName) throws PrintException {
         LOG.trace("Print Service: " + this.printService.getName());
         LOG.trace("About to print " + copies + " copy(s)");
         
@@ -95,15 +96,17 @@ public class PrinterOperations implement
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Issuing Job {} to Printer: {}", i, this.printService.getName());
                 }
-                print(doc);
+                print(doc, jobName);
             }
         }
     }
         
-    public void print(Doc doc) throws PrintException {
+    public void print(Doc doc, String jobName) throws PrintException {
         // we need create a new job for each print 
         DocPrintJob job = getPrintService().createPrintJob();
-        job.print(doc, printRequestAttributeSet);
+        PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(printRequestAttributeSet);
+        attrs.add(new JobName(jobName, Locale.getDefault()));
+        job.print(doc, attrs);
     }
 
     public PrintService getPrintService() {

Modified: camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperationsInterface.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperationsInterface.java?rev=1300804&r1=1300803&r2=1300804&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperationsInterface.java
(original)
+++ camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperationsInterface.java
Thu Mar 15 02:08:03 2012
@@ -27,6 +27,6 @@ public interface PrinterOperationsInterf
      * @param doc document to print
      * @throws PrintException is thrown if printing failed
      */
-    void print(Doc doc) throws PrintException;
+    void print(Doc doc, String jobName) throws PrintException;
 
 }

Modified: camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java?rev=1300804&r1=1300803&r2=1300804&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
(original)
+++ camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
Thu Mar 15 02:08:03 2012
@@ -45,13 +45,14 @@ public class PrinterProducer extends Def
     public void process(Exchange exchange) throws Exception {
         Object body = exchange.getIn().getBody();
         InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class,
body);
-        print(is);
+        String jobName = exchange.getIn().getHeader(PrinterEndpoint.JOB_NAME, "Camel: lpr",
String.class);
+        print(is, jobName);
     }
     
-    private void print(InputStream body) throws PrintException { 
+    private void print(InputStream body, String jobName) throws PrintException { 
         if (printerOperations.getPrintService().isDocFlavorSupported(printerOperations.getFlavor()))
{
             PrintDocument printDoc = new PrintDocument(body, printerOperations.getFlavor());
       
-            printerOperations.print(printDoc, config.getCopies(), config.isSendToPrinter(),
config.getMimeType()); 
+            printerOperations.print(printDoc, config.getCopies(), config.isSendToPrinter(),
config.getMimeType(), jobName); 
         }
     }
 

Modified: camel/trunk/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java?rev=1300804&r1=1300803&r2=1300804&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java
(original)
+++ camel/trunk/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java
Thu Mar 15 02:08:03 2012
@@ -18,19 +18,39 @@ package org.apache.camel.component.print
 
 import java.io.FileInputStream;
 import java.io.InputStream;
+import java.util.Map;
 
+import javax.print.Doc;
+import javax.print.DocFlavor;
+import javax.print.DocPrintJob;
+import javax.print.PrintService;
+import javax.print.PrintServiceLookup;
+import javax.print.attribute.PrintRequestAttributeSet;
+
+import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.apache.camel.util.IOHelper;
-
+import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
 public class PrinterPrintTest extends CamelTestSupport {
 
+    @Before
+    public void setup() {
+        setupJavaPrint();
+    }
+
     @Override
     public boolean isUseRouteBuilder() {
         return false;
@@ -174,4 +194,94 @@ public class PrinterPrintTest extends Ca
         });
         context.start();
     }
+    
+    @Test
+    public void moreThanOneLprEndpoint() throws Exception {
+
+        if (isAwtHeadless()) {
+            return;
+        }
+        
+        int numberOfPrintservicesBefore = PrintServiceLookup.lookupPrintServices(null, null).length;
+
+        // setup javax.print 
+        PrintService ps1 = mock(PrintService.class);
+        when(ps1.getName()).thenReturn("printer1");
+        when(ps1.isDocFlavorSupported(any(DocFlavor.class))).thenReturn(Boolean.TRUE);
+        PrintService ps2 = mock(PrintService.class);
+        when(ps2.getName()).thenReturn("printer2");
+        boolean res1 = PrintServiceLookup.registerService(ps1);
+        assertTrue("PrintService #1 should be registered.", res1);
+        boolean res2 = PrintServiceLookup.registerService(ps2);
+        assertTrue("PrintService #2 should be registered.", res2);
+        PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, null);
+        assertEquals("lookup should report two PrintServices.", numberOfPrintservicesBefore
+ 2, pss.length);
+
+        DocPrintJob job1 = mock(DocPrintJob.class);
+        when(ps1.createPrintJob()).thenReturn(job1);
+        
+        context.addRoutes(new RouteBuilder() {
+
+            public void configure() {
+                from("direct:start1").to("lpr://localhost/printer1?sendToPrinter=true");
+                from("direct:start2").to("lpr://localhost/printer2?sendToPrinter=false");
+            }
+        });
+        context.start();
+
+        // Are there two different PrintConfigurations?
+        Map<String, Endpoint> epm = context().getEndpointMap();
+        assertEquals("Four endpoints", 4, epm.size());
+        Endpoint lp1 = null;
+        Endpoint lp2 = null;
+        for (Map.Entry<String, Endpoint> ep : epm.entrySet()) {
+            if (ep.getKey().contains("printer1")) {
+                lp1 = ep.getValue();
+            }
+            if (ep.getKey().contains("printer2")) {
+                lp2 = ep.getValue();
+            }
+        }
+        assertNotNull(lp1);
+        assertNotNull(lp2);
+        assertEquals("printer1", ((PrinterEndpoint)lp1).getConfig().getPrintername());
+        assertEquals("printer2", ((PrinterEndpoint)lp2).getConfig().getPrintername());
+
+        template.sendBody("direct:start1", "Hello Printer 1");
+
+        context.stop();
+
+        verify(job1, times(1)).print(any(Doc.class), any(PrintRequestAttributeSet.class));
+    }
+    
+    @Test
+    public void setJobName() throws Exception {
+        if (isAwtHeadless()) {
+            return;
+        }
+
+        getMockEndpoint("mock:output").setExpectedMessageCount(1);
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("direct:start").to("lpr://localhost/default").to("mock:output");
+            }
+        });
+        context.start();
+        template.sendBodyAndHeader("direct:start", "Hello Printer", PrinterEndpoint.JOB_NAME,
"Test-Job-Name");
+        context.stop();
+        assertMockEndpointsSatisfied();
+    }
+    
+    protected void setupJavaPrint() {
+        // "install" another default printer
+        PrintService psDefault = mock(PrintService.class);
+        when(psDefault.getName()).thenReturn("DefaultPrinter");
+        when(psDefault.isDocFlavorSupported(any(DocFlavor.class))).thenReturn(Boolean.TRUE);
+        PrintServiceLookup psLookup = mock(PrintServiceLookup.class);
+        when(psLookup.getPrintServices()).thenReturn(new PrintService[]{psDefault});
+        when(psLookup.getDefaultPrintService()).thenReturn(psDefault);
+        DocPrintJob docPrintJob = mock(DocPrintJob.class);
+        when(psDefault.createPrintJob()).thenReturn(docPrintJob);
+        PrintServiceLookup.registerServiceProvider(psLookup);
+    }
 }



Mime
View raw message