|
1 | 1 | /* |
2 | | - * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -/* |
25 | | - @test |
26 | | - @bug 4199506 |
27 | | - @summary java.awt.print.PageFormat.setpaper(Paper paper) |
28 | | - assertion test fails by not throwing |
29 | | - NullPointerException when a null paper instance is |
30 | | - passed as argument and this is specified in the doc. |
31 | | - @run main NullPaper |
32 | | -*/ |
33 | | - |
34 | | - |
35 | | -//*** global search and replace NullPaper with name of the test *** |
36 | | - |
37 | | -/** |
38 | | - * NullPaper.java |
39 | | - * |
40 | | - * summary: java.awt.print.PageFormat.setpaper(Paper paper) |
41 | | - assertion test fails by not throwing |
42 | | - NullPointerException when a null paper instance is |
43 | | - passed as argument and this is specified in the doc. |
| 24 | +import java.awt.print.PageFormat; |
44 | 25 |
|
| 26 | +/* |
| 27 | + * @test |
| 28 | + * @bug 4199506 |
| 29 | + * @summary Verify PageFormat.setPaper(null) throws NullPointerException |
| 30 | + * as specified |
| 31 | + * @run main NullPaper |
45 | 32 | */ |
46 | | - |
47 | | -import java.awt.print.*; |
48 | | - |
49 | | -// This test is a "main" test as applets would need Runtime permission |
50 | | -// "queuePrintJob". |
51 | | - |
52 | | -public class NullPaper { |
53 | | - |
54 | | - private static void init() |
55 | | - { |
56 | | - boolean settingNullWorked = false; |
57 | | - |
58 | | - try { |
59 | | - /* Setting the paper to null should throw an exception. |
60 | | - * The bug was the exception was not being thrown. |
61 | | - */ |
62 | | - new PageFormat().setPaper(null); |
63 | | - settingNullWorked = true; |
64 | | - |
65 | | - /* If the test succeeds we'll end up here, so write |
66 | | - * to standard out. |
67 | | - */ |
68 | | - } catch (NullPointerException e) { |
69 | | - pass(); |
70 | | - |
71 | | - /* The test failed if we end up here because an exception |
72 | | - * other than the one we were expecting was thrown. |
73 | | - */ |
74 | | - } catch (Exception e) { |
75 | | - fail("Instead of the expected NullPointerException, '" + e + "' was thrown."); |
76 | | - } |
77 | | - |
78 | | - if (settingNullWorked) { |
79 | | - fail("The expected NullPointerException was not thrown"); |
80 | | - } |
81 | | - |
82 | | - }//End init() |
83 | | - |
84 | | - |
85 | | - /***************************************************** |
86 | | - Standard Test Machinery Section |
87 | | - DO NOT modify anything in this section -- it's a |
88 | | - standard chunk of code which has all of the |
89 | | - synchronisation necessary for the test harness. |
90 | | - By keeping it the same in all tests, it is easier |
91 | | - to read and understand someone else's test, as |
92 | | - well as insuring that all tests behave correctly |
93 | | - with the test harness. |
94 | | - There is a section following this for test-defined |
95 | | - classes |
96 | | - ******************************************************/ |
97 | | - private static boolean theTestPassed = false; |
98 | | - private static boolean testGeneratedInterrupt = false; |
99 | | - private static String failureMessage = ""; |
100 | | - |
101 | | - private static Thread mainThread = null; |
102 | | - |
103 | | - private static int sleepTime = 300000; |
104 | | - |
105 | | - public static void main( String args[] ) throws InterruptedException |
106 | | - { |
107 | | - mainThread = Thread.currentThread(); |
108 | | - try |
109 | | - { |
110 | | - init(); |
111 | | - } |
112 | | - catch( TestPassedException e ) |
113 | | - { |
114 | | - //The test passed, so just return from main and harness will |
115 | | - // interepret this return as a pass |
116 | | - return; |
117 | | - } |
118 | | - //At this point, neither test passed nor test failed has been |
119 | | - // called -- either would have thrown an exception and ended the |
120 | | - // test, so we know we have multiple threads. |
121 | | - |
122 | | - //Test involves other threads, so sleep and wait for them to |
123 | | - // called pass() or fail() |
124 | | - try |
125 | | - { |
126 | | - Thread.sleep( sleepTime ); |
127 | | - //Timed out, so fail the test |
128 | | - throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); |
129 | | - } |
130 | | - catch (InterruptedException e) |
131 | | - { |
132 | | - if( ! testGeneratedInterrupt ) throw e; |
133 | | - |
134 | | - //reset flag in case hit this code more than once for some reason (just safety) |
135 | | - testGeneratedInterrupt = false; |
136 | | - if ( theTestPassed == false ) |
137 | | - { |
138 | | - throw new RuntimeException( failureMessage ); |
139 | | - } |
140 | | - } |
141 | | - |
142 | | - }//main |
143 | | - |
144 | | - public static synchronized void setTimeoutTo( int seconds ) |
145 | | - { |
146 | | - sleepTime = seconds * 1000; |
147 | | - } |
148 | | - |
149 | | - public static synchronized void pass() |
150 | | - { |
151 | | - System.out.println( "The test passed." ); |
152 | | - //first check if this is executing in main thread |
153 | | - if ( mainThread == Thread.currentThread() ) |
154 | | - { |
155 | | - //Still in the main thread, so set the flag just for kicks, |
156 | | - // and throw a test passed exception which will be caught |
157 | | - // and end the test. |
158 | | - theTestPassed = true; |
159 | | - throw new TestPassedException(); |
160 | | - } |
161 | | - //pass was called from a different thread, so set the flag and interrupt |
162 | | - // the main thead. |
163 | | - theTestPassed = true; |
164 | | - testGeneratedInterrupt = true; |
165 | | - mainThread.interrupt(); |
166 | | - }//pass() |
167 | | - |
168 | | - public static synchronized void fail() |
169 | | - { |
170 | | - //test writer didn't specify why test failed, so give generic |
171 | | - fail( "it just plain failed! :-)" ); |
| 33 | +public final class NullPaper { |
| 34 | + public static void main(String[] args) { |
| 35 | + try { |
| 36 | + /* Setting the paper to null should throw an exception. |
| 37 | + * The bug was the exception was not being thrown. |
| 38 | + */ |
| 39 | + new PageFormat().setPaper(null); |
| 40 | + |
| 41 | + throw new RuntimeException("NullPointerException is expected " |
| 42 | + + "but not thrown"); |
| 43 | + } catch (NullPointerException e) { |
| 44 | + System.out.println("NullPointerException caught - test passes"); |
| 45 | + } |
172 | 46 | } |
173 | | - |
174 | | - public static synchronized void fail( String whyFailed ) |
175 | | - { |
176 | | - System.out.println( "The test failed: " + whyFailed ); |
177 | | - //check if this called from main thread |
178 | | - if ( mainThread == Thread.currentThread() ) |
179 | | - { |
180 | | - //If main thread, fail now 'cause not sleeping |
181 | | - throw new RuntimeException( whyFailed ); |
182 | | - } |
183 | | - theTestPassed = false; |
184 | | - testGeneratedInterrupt = true; |
185 | | - failureMessage = whyFailed; |
186 | | - mainThread.interrupt(); |
187 | | - }//fail() |
188 | | - |
189 | | - }// class NullPaper |
190 | | - |
191 | | -//This exception is used to exit from any level of call nesting |
192 | | -// when it's determined that the test has passed, and immediately |
193 | | -// end the test. |
194 | | -class TestPassedException extends RuntimeException |
195 | | - { |
196 | | - } |
| 47 | +} |
0 commit comments