test/jdk/java/awt/image/BufferedImage/GetPropertyNames.java
changeset 47216 71c04702a3d5
parent 28988 5694c83d3060
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2015, 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 import java.awt.Image;
       
    25 import java.awt.image.BufferedImage;
       
    26 import java.util.Properties;
       
    27 
       
    28 import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
       
    29 
       
    30 /**
       
    31  * @test
       
    32  * @bug 8066132
       
    33  * @author Sergey Bylokhov
       
    34  */
       
    35 public final class GetPropertyNames {
       
    36 
       
    37     static BufferedImage defaultProps = new BufferedImage(1, 1, TYPE_INT_ARGB);
       
    38 
       
    39     public static void main(final String[] args) {
       
    40         // default result is null
       
    41         if (defaultProps.getPropertyNames() != null) {
       
    42             throw new RuntimeException("PropertyNames should be null");
       
    43         }
       
    44         // for null properties result is null
       
    45         final BufferedImage emptyProps = getBufferedImage(null);
       
    46         if (emptyProps.getPropertyNames() != null) {
       
    47             throw new RuntimeException("PropertyNames should be null");
       
    48         }
       
    49         // for empty properties result is null
       
    50         final BufferedImage nullProps = getBufferedImage(new Properties());
       
    51         if (nullProps.getPropertyNames() != null) {
       
    52             throw new RuntimeException("PropertyNames should be null");
       
    53         }
       
    54         // for non-string keys result is null
       
    55         final Properties properties = new Properties();
       
    56         properties.put(1, 1);
       
    57         properties.put(2, 2);
       
    58         properties.put(3, 3);
       
    59         final BufferedImage nonStringProps = getBufferedImage(properties);
       
    60         if (nonStringProps.getPropertyNames() != null) {
       
    61             throw new RuntimeException("PropertyNames should be null");
       
    62         }
       
    63         // for string keys result is not null
       
    64         properties.clear();
       
    65         properties.setProperty("1", "1");
       
    66         properties.setProperty("2", "2");
       
    67         validate(getBufferedImage(properties), 2);
       
    68         // for the mix of strings and objects result is not null
       
    69         properties.clear();
       
    70         properties.put(1, 1);
       
    71         properties.put(2, 2);
       
    72         properties.put(3, 3);
       
    73         properties.setProperty("key1", "value1");
       
    74         properties.setProperty("key2", "value2");
       
    75         final BufferedImage mixProps = getBufferedImage(properties);
       
    76         validate(mixProps, 2);
       
    77         if (!"value1".equals(mixProps.getProperty("key1"))
       
    78             || !"value2".equals(mixProps.getProperty("key2"))) {
       
    79             throw new RuntimeException("Wrong key-value pair");
       
    80         }
       
    81     }
       
    82 
       
    83 
       
    84     private static BufferedImage getBufferedImage(final Properties properties) {
       
    85         return new BufferedImage(defaultProps.getColorModel(),
       
    86                                  defaultProps.getRaster(),
       
    87                                  defaultProps.isAlphaPremultiplied(),
       
    88                                  properties);
       
    89     }
       
    90 
       
    91     private static void validate(final BufferedImage bi, final int expected) {
       
    92         final String[] names = bi.getPropertyNames();
       
    93         if (names.length != expected) {
       
    94             throw new RuntimeException("Wrong number of names");
       
    95         }
       
    96         for (final String name : names) {
       
    97             final Object property = bi.getProperty(name);
       
    98             if (property == Image.UndefinedProperty || property == null) {
       
    99                 throw new RuntimeException("Unexpected property");
       
   100             }
       
   101         }
       
   102     }
       
   103 }