1 /*
   2  * Copyright (c) 2011, 2013, 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   @bug 7040577
  27   @library ../../../regtesthelpers
  28   @build Sysout
  29   @summary Default implementation of Toolkit.loadSystemColors(int[]) and many others doesn't throw HE in hl env
  30   @author andrei dmitriev: area=awt.headless
  31   @run main/othervm -Djava.awt.headless=true ExceptionContract
  32 */
  33 
  34 import java.awt.*;
  35 import java.util.Properties;
  36 import test.java.awt.regtesthelpers.Sysout;
  37 
  38 import java.awt.datatransfer.Clipboard;
  39 import java.awt.font.TextAttribute;
  40 import java.awt.im.InputMethodHighlight;
  41 import java.awt.image.*;
  42 import java.net.URL;
  43 import java.util.Map;
  44 import java.util.Properties;
  45 
  46 public class ExceptionContract {
  47 
  48     private static boolean passed = false;
  49     public static void main(String[] args)  {
  50         //Case1
  51         try{
  52             new _Toolkit().getLockingKeyState(1);
  53         } catch (HeadlessException he){
  54             passed = true;
  55         }
  56         if (!passed){
  57             throw new RuntimeException("Tk.getLockingKeyState() didn't throw HeadlessException while in the headless mode.");
  58         }
  59 
  60         passed = false;
  61         //Case2
  62         try{
  63             new _Toolkit().setLockingKeyState(1, true);
  64         } catch (HeadlessException he){
  65             passed = true;
  66         }
  67         if (!passed){
  68             throw new RuntimeException("Tk.setLockingKeyState() didn't throw HeadlessException while in the headless mode.");
  69         }
  70 
  71         passed = false;
  72         //Case3
  73         try{
  74             new _Toolkit().createCustomCursor(new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB), new Point(0,0), "Custom cursor");
  75         } catch (HeadlessException he){
  76             he.printStackTrace();
  77             passed = true;
  78         }
  79         if (!passed){
  80             throw new RuntimeException("Tk.createCustomCursor(args) didn't throw HeadlessException while in the headless mode.");
  81         }
  82 
  83     }
  84 
  85     static class _Toolkit extends Toolkit {
  86 
  87         @Override
  88         public Cursor createCustomCursor(Image cursor, Point hotSpot, String name)
  89             throws IndexOutOfBoundsException, HeadlessException
  90         {
  91             return super.createCustomCursor(cursor, hotSpot, name);
  92         }
  93 
  94 
  95         @Override
  96         public void setLockingKeyState(int keyCode, boolean on) throws UnsupportedOperationException {
  97             super.setLockingKeyState(keyCode, on);
  98         }
  99 
 100         @Override
 101         public boolean getLockingKeyState(int keyCode) throws UnsupportedOperationException {
 102             return super.getLockingKeyState(keyCode);
 103         }
 104 
 105 
 106         @Override
 107         public void loadSystemColors(int[] systemColors) throws HeadlessException {
 108             return;
 109         }
 110 
 111         @Override
 112         public Dimension getScreenSize() throws HeadlessException {
 113             return null;
 114         }
 115 
 116         @Override
 117         public int getScreenResolution() throws HeadlessException {
 118             return 0;
 119         }
 120 
 121         @Override
 122         public ColorModel getColorModel() throws HeadlessException {
 123             return null;
 124         }
 125 
 126         @Override
 127         public String[] getFontList() {
 128             return new String[0];
 129         }
 130 
 131         @Override
 132         public FontMetrics getFontMetrics(Font font) {
 133             return null;
 134         }
 135 
 136         @Override
 137         public void sync() {
 138 
 139         }
 140 
 141         @Override
 142         public Image getImage(String filename) {
 143             return null;
 144         }
 145 
 146         @Override
 147         public Image getImage(URL url) {
 148             return null;
 149         }
 150 
 151         @Override
 152         public Image createImage(String filename) {
 153             return null;
 154         }
 155 
 156         @Override
 157         public Image createImage(URL url) {
 158             return null;
 159         }
 160 
 161         @Override
 162         public boolean prepareImage(Image image, int width, int height, ImageObserver observer) {
 163             return false;
 164         }
 165 
 166         @Override
 167         public int checkImage(Image image, int width, int height, ImageObserver observer) {
 168             return 0;
 169         }
 170 
 171         @Override
 172         public Image createImage(ImageProducer producer) {
 173             return null;
 174         }
 175 
 176         @Override
 177         public Image createImage(byte[] imagedata, int imageoffset, int imagelength) {
 178             return null;
 179         }
 180 
 181         @Override
 182         public PrintJob getPrintJob(Frame frame, String jobtitle, Properties props) {
 183             return null;
 184         }
 185 
 186         @Override
 187         public void beep() {
 188 
 189         }
 190 
 191         @Override
 192         public Clipboard getSystemClipboard() throws HeadlessException {
 193             return null;
 194         }
 195 
 196         @Override
 197         protected EventQueue getSystemEventQueueImpl() {
 198             return null;
 199         }
 200 
 201         @Override
 202         public boolean isModalityTypeSupported(Dialog.ModalityType modalityType) {
 203             return false;
 204         }
 205 
 206         @Override
 207         public boolean isModalExclusionTypeSupported(Dialog.ModalExclusionType modalExclusionType) {
 208             return false;
 209         }
 210 
 211         @Override
 212         public Map<TextAttribute, ?> mapInputMethodHighlight(InputMethodHighlight highlight) throws HeadlessException {
 213             return null;
 214         }
 215     }
 216 }