|
| 1 | +/* |
| 2 | + * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @key printer |
| 27 | + * @bug 6255196 |
| 28 | + * @summary Verifies the function of methods edit(java.io.File file) and |
| 29 | + * print(java.io.File file) |
| 30 | + * @library /java/awt/regtesthelpers |
| 31 | + * @build PassFailJFrame |
| 32 | + * @run main/manual EditAndPrintTest |
| 33 | + */ |
| 34 | + |
| 35 | +import java.awt.Desktop; |
| 36 | +import java.awt.Desktop.Action; |
| 37 | +import java.io.File; |
| 38 | +import java.io.FileWriter; |
| 39 | +import java.io.IOException; |
| 40 | +import java.lang.reflect.InvocationTargetException; |
| 41 | +import javax.swing.JPanel; |
| 42 | + |
| 43 | +public class EditAndPrintTest extends JPanel { |
| 44 | + |
| 45 | + static final String INSTRUCTIONS = """ |
| 46 | + This test tries to edit and print a directory, which will expectedly raise IOException. |
| 47 | + Then this test would edit and print a .txt file, which should be successful. |
| 48 | + After test execution close the editor if it was launched by test. |
| 49 | + If you see any EXCEPTION messages in the output press FAIL. |
| 50 | + """; |
| 51 | + |
| 52 | + public EditAndPrintTest() { |
| 53 | + if (!Desktop.isDesktopSupported()) { |
| 54 | + PassFailJFrame.log("Class java.awt.Desktop is not supported on " + |
| 55 | + "current platform. Further testing will not be performed"); |
| 56 | + PassFailJFrame.forcePass(); |
| 57 | + } |
| 58 | + |
| 59 | + Desktop desktop = Desktop.getDesktop(); |
| 60 | + |
| 61 | + if (!desktop.isSupported(Action.PRINT) && !desktop.isSupported(Action.EDIT)) { |
| 62 | + PassFailJFrame.log("Neither EDIT nor PRINT actions are supported. Nothing to test."); |
| 63 | + PassFailJFrame.forcePass(); |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + * Part 1: print or edit a directory, which should throw an IOException. |
| 68 | + */ |
| 69 | + File userHome = new File(System.getProperty("user.home")); |
| 70 | + try { |
| 71 | + if (desktop.isSupported(Action.EDIT)) { |
| 72 | + PassFailJFrame.log("Trying to edit " + userHome); |
| 73 | + desktop.edit(userHome); |
| 74 | + PassFailJFrame.log("No exception has been thrown for editing " + |
| 75 | + "directory " + userHome.getPath()); |
| 76 | + PassFailJFrame.log("Test failed."); |
| 77 | + } else { |
| 78 | + PassFailJFrame.log("Action EDIT is unsupported."); |
| 79 | + } |
| 80 | + } catch (IOException e) { |
| 81 | + PassFailJFrame.log("Expected IOException is caught."); |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + if (desktop.isSupported(Action.PRINT)) { |
| 86 | + PassFailJFrame.log("Trying to print " + userHome); |
| 87 | + desktop.print(userHome); |
| 88 | + PassFailJFrame.log("No exception has been thrown for printing " + |
| 89 | + "directory " + userHome.getPath()); |
| 90 | + PassFailJFrame.log("Test failed."); |
| 91 | + } else { |
| 92 | + PassFailJFrame.log("Action PRINT is unsupported.\n"); |
| 93 | + } |
| 94 | + } catch (IOException e) { |
| 95 | + PassFailJFrame.log("Expected IOException is caught."); |
| 96 | + } |
| 97 | + |
| 98 | + /* |
| 99 | + * Part 2: print or edit a normal .txt file, which may succeed if there |
| 100 | + * is associated application to print or edit the given file. It fails |
| 101 | + * otherwise. |
| 102 | + */ |
| 103 | + // Create a temp .txt file for test. |
| 104 | + String testFilePath = System.getProperty("java.io.tmpdir") + File.separator + "JDIC-test.txt"; |
| 105 | + File testFile = null; |
| 106 | + try { |
| 107 | + PassFailJFrame.log("Creating temporary file."); |
| 108 | + testFile = File.createTempFile("JDIC-test", ".txt", new File(System.getProperty("java.io.tmpdir"))); |
| 109 | + testFile.deleteOnExit(); |
| 110 | + FileWriter writer = new FileWriter(testFile); |
| 111 | + writer.write("This is a temp file used to test print() method of Desktop."); |
| 112 | + writer.flush(); |
| 113 | + writer.close(); |
| 114 | + } catch (java.io.IOException ioe){ |
| 115 | + PassFailJFrame.log("EXCEPTION: " + ioe.getMessage()); |
| 116 | + PassFailJFrame.forceFail("Failed to create temp file for testing."); |
| 117 | + } |
| 118 | + |
| 119 | + try { |
| 120 | + if (desktop.isSupported(Action.EDIT)) { |
| 121 | + PassFailJFrame.log("Try to edit " + testFile); |
| 122 | + desktop.edit(testFile); |
| 123 | + PassFailJFrame.log("Succeed."); |
| 124 | + } |
| 125 | + } catch (IOException e) { |
| 126 | + PassFailJFrame.log("EXCEPTION: " + e.getMessage()); |
| 127 | + } |
| 128 | + |
| 129 | + try { |
| 130 | + if (desktop.isSupported(Action.PRINT)) { |
| 131 | + PassFailJFrame.log("Trying to print " + testFile); |
| 132 | + desktop.print(testFile); |
| 133 | + PassFailJFrame.log("Succeed."); |
| 134 | + } |
| 135 | + } catch (IOException e) { |
| 136 | + PassFailJFrame.log("EXCEPTION: " + e.getMessage()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public static void main(String args[]) throws InterruptedException, |
| 141 | + InvocationTargetException { |
| 142 | + PassFailJFrame.builder() |
| 143 | + .title("Edit and Print test") |
| 144 | + .splitUI(EditAndPrintTest::new) |
| 145 | + .instructions(INSTRUCTIONS) |
| 146 | + .rows((int) INSTRUCTIONS.lines().count() + 1) |
| 147 | + .columns(60) |
| 148 | + .logArea() |
| 149 | + .build() |
| 150 | + .awaitAndCheck(); |
| 151 | + } |
| 152 | +} |
0 commit comments