changeset 8511:b164c8eb1295

8022536: closed/javax/print/TextFlavorTest.java fails Reviewed-by: prr, jchen
author jgodinez
date Mon, 14 Oct 2013 09:15:21 -0700
parents 478f7a9b3b12
children 8a59181b3c6d
files src/solaris/classes/sun/print/CUPSPrinter.java src/solaris/classes/sun/print/IPPPrintService.java src/solaris/classes/sun/print/UnixPrintServiceLookup.java test/java/awt/print/PrinterJob/PrintLatinCJKTest.java test/javax/print/TextFlavorTest.java
diffstat 5 files changed, 134 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/solaris/classes/sun/print/CUPSPrinter.java	Mon Oct 14 16:00:03 2013 +0400
+++ b/src/solaris/classes/sun/print/CUPSPrinter.java	Mon Oct 14 09:15:21 2013 -0700
@@ -237,8 +237,9 @@
 
     /**
      * Get CUPS default printer using IPP.
+     * Returns 2 values - index 0 is printer name, index 1 is the uri.
      */
-    public static String getDefaultPrinter() {
+    static String[] getDefaultPrinter() {
         try {
             URL url = new URL("http", getServer(), getPort(), "");
             final HttpURLConnection urlConnection =
@@ -264,8 +265,8 @@
                     AttributeClass.ATTRIBUTES_CHARSET,
                     AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
                     new AttributeClass("requested-attributes",
-                                       AttributeClass.TAG_KEYWORD,
-                                       "printer-name")
+                                       AttributeClass.TAG_URI,
+                                       "printer-uri")
                 };
 
                 if (IPPPrintService.writeIPPRequest(os,
@@ -273,6 +274,7 @@
                                         attCl)) {
 
                     HashMap defaultMap = null;
+                    String[] printerInfo = new String[2];
                     InputStream is = urlConnection.getInputStream();
                     HashMap[] responseMap = IPPPrintService.readIPPResponse(
                                          is);
@@ -293,21 +295,30 @@
                          * special behaviour for this built in.
                          */
                          if (UnixPrintServiceLookup.isMac()) {
-                             return UnixPrintServiceLookup.
+                             printerInfo[0] = UnixPrintServiceLookup.
                                                    getDefaultPrinterNameSysV();
+                             printerInfo[1] = null;
+                             return (String[])printerInfo.clone();
                          } else {
                              return null;
                          }
                     }
 
+
                     AttributeClass attribClass = (AttributeClass)
                         defaultMap.get("printer-name");
 
                     if (attribClass != null) {
-                        String nameStr = attribClass.getStringValue();
+                        printerInfo[0] = attribClass.getStringValue();
+                        attribClass = (AttributeClass)defaultMap.get("device-uri");
+                        if (attribClass != null) {
+                            printerInfo[1] = attribClass.getStringValue();
+                        } else {
+                            printerInfo[1] = null;
+                        }
                         os.close();
                         urlConnection.disconnect();
-                        return nameStr;
+                        return (String [])printerInfo.clone();
                     }
                 }
                 os.close();
@@ -322,7 +333,7 @@
     /**
      * Get list of all CUPS printers using IPP.
      */
-    public static String[] getAllPrinters() {
+    static String[] getAllPrinters() {
         try {
             URL url = new URL("http", getServer(), getPort(), "");
 
--- a/src/solaris/classes/sun/print/IPPPrintService.java	Mon Oct 14 16:00:03 2013 +0400
+++ b/src/solaris/classes/sun/print/IPPPrintService.java	Mon Oct 14 09:15:21 2013 -0700
@@ -366,6 +366,7 @@
                                           " IPPPrintService, myURL="+
                                           myURL+" Exception= "+
                                           e);
+            throw new IllegalArgumentException("invalid url");
         }
 
         isCupsPrinter = isCups;
@@ -1145,6 +1146,8 @@
                 // REMIND: check attribute values
                 return (T)PDLOverrideSupported.NOT_ATTEMPTED;
             }
+        } else if (category == PrinterURI.class) {
+            return (T)(new PrinterURI(myURI));
         } else {
             return null;
         }
--- a/src/solaris/classes/sun/print/UnixPrintServiceLookup.java	Mon Oct 14 16:00:03 2013 +0400
+++ b/src/solaris/classes/sun/print/UnixPrintServiceLookup.java	Mon Oct 14 09:15:21 2013 -0700
@@ -48,6 +48,7 @@
 import javax.print.attribute.PrintServiceAttribute;
 import javax.print.attribute.PrintServiceAttributeSet;
 import javax.print.attribute.standard.PrinterName;
+import javax.print.attribute.standard.PrinterURI;
 import java.io.File;
 import java.io.FileReader;
 import java.net.URL;
@@ -203,6 +204,33 @@
         }
     }
 
+    private int addPrintServiceToList(ArrayList printerList, PrintService ps) {
+        int index = printerList.indexOf(ps);
+        // Check if PrintService with same name is already in the list.
+        if (CUPSPrinter.isCupsRunning() && index != -1) {
+            // Bug in Linux: Duplicate entry of a remote printer
+            // and treats it as local printer but it is returning wrong
+            // information when queried using IPP. Workaround is to remove it.
+            // Even CUPS ignores these entries as shown in lpstat or using
+            // their web configuration.
+            PrinterURI uri = (PrinterURI)ps.getAttribute(PrinterURI.class);
+            if (uri.getURI().getHost().equals("localhost")) {
+                IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, ignoring the new local printer: "+ps);
+                return index;  // Do not add this.
+            }
+            PrintService oldPS = (PrintService)(printerList.get(index));
+            uri = (PrinterURI)oldPS.getAttribute(PrinterURI.class);
+            if (uri.getURI().getHost().equals("localhost")) {
+                IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, removing existing local printer: "+oldPS);
+                printerList.remove(oldPS);
+            } else {
+                return index;
+            }
+        }
+        printerList.add(ps);
+        return (printerList.size() - 1);
+    }
+
 
     // refreshes "printServices"
     public synchronized void refreshServices() {
@@ -246,8 +274,7 @@
             }
             if ((defaultPrintService != null)
                 && printers[p].equals(getPrinterDestName(defaultPrintService))) {
-                printerList.add(defaultPrintService);
-                defaultIndex = printerList.size() - 1;
+                defaultIndex = addPrintServiceToList(printerList, defaultPrintService);
             } else {
                 if (printServices == null) {
                     IPPPrintService.debug_println(debugPrefix+
@@ -255,9 +282,10 @@
 
                     if (CUPSPrinter.isCupsRunning()) {
                         try {
-                            printerList.add(new IPPPrintService(printers[p],
-                                                                printerURIs[p],
-                                                                true));
+                            addPrintServiceToList(printerList,
+                                                  new IPPPrintService(printers[p],
+                                                                   printerURIs[p],
+                                                                   true));
                         } catch (Exception e) {
                             IPPPrintService.debug_println(debugPrefix+
                                                           " getAllPrinters Exception "+
@@ -282,10 +310,10 @@
                     if (j == printServices.length) {      // not found?
                         if (CUPSPrinter.isCupsRunning()) {
                             try {
-                                printerList.add(new IPPPrintService(
-                                                               printers[p],
-                                                               printerURIs[p],
-                                                               true));
+                                addPrintServiceToList(printerList,
+                                             new IPPPrintService(printers[p],
+                                                                 printerURIs[p],
+                                                                 true));
                             } catch (Exception e) {
                                 IPPPrintService.debug_println(debugPrefix+
                                                               " getAllPrinters Exception "+
@@ -312,9 +340,7 @@
 
         //if defaultService is not found in printerList
         if (defaultIndex == -1 && defaultPrintService != null) {
-            //add default to the list
-            printerList.add(defaultPrintService);
-            defaultIndex = printerList.size() - 1;
+            defaultIndex = addPrintServiceToList(printerList, defaultPrintService);
         }
 
         printServices = (PrintService[])printerList.toArray(
@@ -563,11 +589,14 @@
 
         // clear defaultPrintService
         defaultPrintService = null;
+        String psuri = null;
 
         IPPPrintService.debug_println("isRunning ? "+
                                       (CUPSPrinter.isCupsRunning()));
         if (CUPSPrinter.isCupsRunning()) {
-            defaultPrinter = CUPSPrinter.getDefaultPrinter();
+            String[] printerInfo = CUPSPrinter.getDefaultPrinter();
+            defaultPrinter = printerInfo[0];
+            psuri = printerInfo[1];
         } else {
             if (isMac() || isSysV()) {
                 defaultPrinter = getDefaultPrinterNameSysV();
@@ -590,12 +619,17 @@
         if (defaultPrintService == null) {
             if (CUPSPrinter.isCupsRunning()) {
                 try {
-                    PrintService defaultPS =
-                        new IPPPrintService(defaultPrinter,
+                    PrintService defaultPS;
+                    if (psuri != null) {
+                        defaultPS = new IPPPrintService(defaultPrinter,
+                                                        psuri, true);
+                    } else {
+                        defaultPS = new IPPPrintService(defaultPrinter,
                                             new URL("http://"+
                                                     CUPSPrinter.getServer()+":"+
                                                     CUPSPrinter.getPort()+"/"+
                                                     defaultPrinter));
+                    }
                     defaultPrintService = defaultPS;
                 } catch (Exception e) {
                 }
--- a/test/java/awt/print/PrinterJob/PrintLatinCJKTest.java	Mon Oct 14 16:00:03 2013 +0400
+++ b/test/java/awt/print/PrinterJob/PrintLatinCJKTest.java	Mon Oct 14 09:15:21 2013 -0700
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 800535
+ * @bug 800535 8022536
  * @summary JDK7 Printing: CJK and Latin Text in string overlap
  * @run main/manual=yesno PrintLatinCJKTest
  */
@@ -49,6 +49,8 @@
     private PageFormat pf;
 
     static String info =
+       "To test 8022536, if a remote printer is the system default,"+
+       "it should show in the dialog as the selected printer.\n"+
        "You need a printer for this test. If you have none, let "+
        "the test pass. If there is a printer, press Print, send "+
        "the output to the printer, and examine it. It should have "+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/print/TextFlavorTest.java	Mon Oct 14 09:15:21 2013 -0700
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+  @test
+  @bug 6334074 8022536
+  @summary test supported text flavors reported properly
+  @run main TextFlavorTest
+*/
+
+import javax.print.*;
+import javax.print.attribute.standard.*;
+import javax.print.attribute.*;
+import java.io.*;
+
+public class TextFlavorTest {
+
+    public static void main(String[] args) throws Exception {
+
+        PrintService service[] =
+            PrintServiceLookup.lookupPrintServices(null, null);
+
+        if (service.length == 0) {
+            System.out.println("No print service found.");
+            return;
+        }
+
+        for (int y = 0; y < service.length; y ++) {
+            DocFlavor flavors[] = service[y].getSupportedDocFlavors();
+            if (flavors == null) continue;
+            for (int x = 0; x < flavors.length; x ++) {
+                if (!service[y].isDocFlavorSupported(flavors[x])) {
+                    String msg = "DocFlavor " + flavors[x] +
+                        " is not supported by service "+ service[y];
+                    throw new RuntimeException(msg);
+                }
+            }
+        }
+        System.out.println("Test passed.");
+    }
+}