Skip to content

Commit 3fa03fd

Browse files
committed
8329320: Simplify awt/print/PageFormat/NullPaper.java test
Backport-of: 5cf457b74334c08bab40e2e6c1a8544a2555fb82
1 parent 42220f3 commit 3fa03fd

File tree

1 file changed

+22
-171
lines changed

1 file changed

+22
-171
lines changed
Lines changed: 22 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,176 +21,27 @@
2121
* questions.
2222
*/
2323

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;
4425

26+
/*
27+
* @test
28+
* @bug 4199506
29+
* @summary Verify PageFormat.setPaper(null) throws NullPointerException
30+
* as specified
31+
* @run main NullPaper
4532
*/
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+
}
17246
}
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

Comments
 (0)