jdk/test/javax/print/PrintServiceLookup/CountPrintServices.java
author ppunegov
Sat, 19 Jul 2014 00:29:20 +0400
changeset 25732 e2c1d053a55f
parent 23287 c0f8cdafef56
permissions -rw-r--r--
8048882: Some regression tests are not robust with VM output Reviewed-by: kvn, iignatyev
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23287
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
     1
/*
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
     4
 *
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
     8
 *
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    13
 * accompanied this code).
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    14
 *
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    18
 *
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    21
 * questions.
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    22
 */
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    23
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    24
import java.io.BufferedReader;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    25
import java.io.InputStreamReader;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    26
import javax.print.PrintService;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    27
import javax.print.PrintServiceLookup;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    28
import javax.print.attribute.AttributeSet;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    29
import javax.print.attribute.HashAttributeSet;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    30
import javax.print.attribute.standard.PrinterName;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    31
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    32
/*
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    33
 * @test
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    34
 * @bug 8032693
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    35
 * @summary Test that lpstat and JDK agree whether there are printers.
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    36
 */
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    37
public class CountPrintServices {
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    38
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    39
  public static void main(String[] args) throws Exception {
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    40
    String os = System.getProperty("os.name").toLowerCase();
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    41
    System.out.println("OS is " + os);
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    42
    if (!os.equals("linux")) {
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    43
        System.out.println("Linux specific test. No need to continue");
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    44
        return;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    45
    }
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    46
    PrintService services[] =
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    47
        PrintServiceLookup.lookupPrintServices(null, null);
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    48
    if (services.length > 0) {
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    49
       System.out.println("Services found. No need to test further.");
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    50
       return;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    51
    }
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    52
    String[] lpcmd = { "lpstat", "-a" };
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    53
    Process proc = Runtime.getRuntime().exec(lpcmd);
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    54
    proc.waitFor();
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    55
    InputStreamReader ir = new InputStreamReader(proc.getInputStream());
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    56
    BufferedReader br = new BufferedReader(ir);
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    57
    int count = 0;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    58
    String printer;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    59
    while ((printer = br.readLine()) != null) {
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    60
       System.out.println("lpstat:: " + printer);
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    61
       count++;
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    62
    }
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    63
    if (count > 0) {
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    64
        throw new RuntimeException("Services exist, but not found by JDK.");
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    65
    }
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    66
 }
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    67
}
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents:
diff changeset
    68