Path: blob/master/test/jdk/javax/print/PrintSEUmlauts/PrintSEUmlauts.java
41149 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24import java.awt.Graphics;25import java.awt.GraphicsEnvironment;26import java.awt.print.PageFormat;27import java.awt.print.Printable;28import java.io.File;29import java.io.FileInputStream;30import java.io.FileOutputStream;31import java.io.IOException;32import java.nio.charset.StandardCharsets;33import javax.print.DocFlavor;34import javax.print.DocPrintJob;35import javax.print.SimpleDoc;36import javax.print.StreamPrintService;37import javax.print.StreamPrintServiceFactory;38import javax.print.attribute.HashDocAttributeSet;39import javax.print.attribute.HashPrintRequestAttributeSet;40import javax.print.event.PrintJobAdapter;41import javax.print.event.PrintJobEvent;4243/*44* @test45* @bug 806736446* @summary Printing to Postscript doesn't support dieresis47* @build PrintSEUmlauts48* @run main/othervm PrintSEUmlauts49*/50public class PrintSEUmlauts implements Printable {5152public static void main(String[] args) throws Exception {5354GraphicsEnvironment.getLocalGraphicsEnvironment();5556DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;57String mime = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();5859StreamPrintServiceFactory[] factories =60StreamPrintServiceFactory.61lookupStreamPrintServiceFactories(flavor, mime);62if (factories.length == 0) {63System.out.println("No print service found.");64return;65}6667FileOutputStream output = new FileOutputStream("out.ps");68StreamPrintService service = factories[0].getPrintService(output);6970SimpleDoc doc =71new SimpleDoc(new PrintSEUmlauts(),72DocFlavor.SERVICE_FORMATTED.PRINTABLE,73new HashDocAttributeSet());74DocPrintJob job = service.createPrintJob();75job.addPrintJobListener(new PrintJobAdapter() {76@Override77public void printJobCompleted(PrintJobEvent pje) {78testPrintAndExit();79}80});8182job.print(doc, new HashPrintRequestAttributeSet());83}8485private static final boolean DEBUG = false;86private static void testPrintAndExit() {87String expected = "<e4>";88String content = "";8990File file = new File("out.ps");91if (!DEBUG) {92file.deleteOnExit();93}9495try (FileInputStream stream = new FileInputStream(file)) {96byte[] data = new byte[(int) file.length()];97stream.read(data);98content = new String(data, StandardCharsets.ISO_8859_1);99} catch (IOException ex) {100ex.printStackTrace();101}102103if (!content.contains(expected)) {104System.err.println("FAIL");105if (DEBUG) {106System.err.println("printing content");107System.err.println(content);108}109throw new RuntimeException("Expected <e4> to represent '\u00e4' but not found!");110}111System.err.println("SUCCESS");112}113114public int print(Graphics g, PageFormat pf, int pg) {115if (pg > 0) return NO_SUCH_PAGE;116g.drawString("\u00e4", 100, 100);117return PAGE_EXISTS;118}119}120121122