test/jdk/java/awt/image/BufferedImage/GetPropertyNames.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 28988 jdk/test/java/awt/image/BufferedImage/GetPropertyNames.java@5694c83d3060
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28988
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
     1
/*
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
     4
 *
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
     8
 *
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    13
 * accompanied this code).
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    14
 *
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    18
 *
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    21
 * questions.
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    22
 */
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    23
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    24
import java.awt.Image;
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    25
import java.awt.image.BufferedImage;
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    26
import java.util.Properties;
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    27
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    28
import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    29
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    30
/**
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    31
 * @test
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    32
 * @bug 8066132
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    33
 * @author Sergey Bylokhov
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    34
 */
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    35
public final class GetPropertyNames {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    36
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    37
    static BufferedImage defaultProps = new BufferedImage(1, 1, TYPE_INT_ARGB);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    38
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    39
    public static void main(final String[] args) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    40
        // default result is null
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    41
        if (defaultProps.getPropertyNames() != null) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    42
            throw new RuntimeException("PropertyNames should be null");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    43
        }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    44
        // for null properties result is null
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    45
        final BufferedImage emptyProps = getBufferedImage(null);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    46
        if (emptyProps.getPropertyNames() != null) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    47
            throw new RuntimeException("PropertyNames should be null");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    48
        }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    49
        // for empty properties result is null
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    50
        final BufferedImage nullProps = getBufferedImage(new Properties());
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    51
        if (nullProps.getPropertyNames() != null) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    52
            throw new RuntimeException("PropertyNames should be null");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    53
        }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    54
        // for non-string keys result is null
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    55
        final Properties properties = new Properties();
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    56
        properties.put(1, 1);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    57
        properties.put(2, 2);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    58
        properties.put(3, 3);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    59
        final BufferedImage nonStringProps = getBufferedImage(properties);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    60
        if (nonStringProps.getPropertyNames() != null) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    61
            throw new RuntimeException("PropertyNames should be null");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    62
        }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    63
        // for string keys result is not null
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    64
        properties.clear();
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    65
        properties.setProperty("1", "1");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    66
        properties.setProperty("2", "2");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    67
        validate(getBufferedImage(properties), 2);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    68
        // for the mix of strings and objects result is not null
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    69
        properties.clear();
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    70
        properties.put(1, 1);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    71
        properties.put(2, 2);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    72
        properties.put(3, 3);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    73
        properties.setProperty("key1", "value1");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    74
        properties.setProperty("key2", "value2");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    75
        final BufferedImage mixProps = getBufferedImage(properties);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    76
        validate(mixProps, 2);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    77
        if (!"value1".equals(mixProps.getProperty("key1"))
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    78
            || !"value2".equals(mixProps.getProperty("key2"))) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    79
            throw new RuntimeException("Wrong key-value pair");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    80
        }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    81
    }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    82
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    83
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    84
    private static BufferedImage getBufferedImage(final Properties properties) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    85
        return new BufferedImage(defaultProps.getColorModel(),
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    86
                                 defaultProps.getRaster(),
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    87
                                 defaultProps.isAlphaPremultiplied(),
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    88
                                 properties);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    89
    }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    90
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    91
    private static void validate(final BufferedImage bi, final int expected) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    92
        final String[] names = bi.getPropertyNames();
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    93
        if (names.length != expected) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    94
            throw new RuntimeException("Wrong number of names");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    95
        }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    96
        for (final String name : names) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    97
            final Object property = bi.getProperty(name);
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    98
            if (property == Image.UndefinedProperty || property == null) {
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
    99
                throw new RuntimeException("Unexpected property");
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
   100
            }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
   101
        }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
   102
    }
5694c83d3060 8066132: BufferedImage::getPropertyNames() always returns null
serb
parents:
diff changeset
   103
}