1/*
2 * Copyright (c) 2012, 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 4403350 4403352 4436995 4438977
27 * @run main IIOMetadataFormatImplTest
28 * @summary Tests various methods of IIOMetadataFormatImpl:
29 *
30 * getElement{Min,Max}Children and getAttribute{Min,Max}Value
31 * getAttributeDescription
32 * getAttributeEnumerations
33 */
34
35import javax.imageio.ImageTypeSpecifier;
36import javax.imageio.metadata.IIOMetadataFormat;
37import javax.imageio.metadata.IIOMetadataFormatImpl;
38
39public class IIOMetadataFormatImplTest {
40
41    public static void main(String[] args) {
42        test440335x();
43        test4436995();
44        test4438977();
45    }
46
47    static class IIOMetadataFormatImpl440335x extends IIOMetadataFormatImpl {
48
49        public IIOMetadataFormatImpl440335x() {
50            super("rootNode", 0, 1);
51            addElement("anElement", "rootNode", 20, 200);
52            addAttribute("anElement", "exclusiveAttr",
53                         IIOMetadataFormat.DATATYPE_INTEGER,
54                         true, null,
55                         "50", "500",
56                         false, false);
57            addAttribute("anElement", "minAttr",
58                         IIOMetadataFormat.DATATYPE_INTEGER,
59                         true, null,
60                         "60", "600",
61                         true, false);
62            addAttribute("anElement", "maxAttr",
63                         IIOMetadataFormat.DATATYPE_INTEGER,
64                         true, null,
65                         "70", "700",
66                         false, true);
67            addAttribute("anElement", "minMaxAttr",
68                         IIOMetadataFormat.DATATYPE_INTEGER,
69                         true, null,
70                         "80", "800",
71                         true, true);
72        }
73
74        public boolean canNodeAppear(String nodeName,
75                                     ImageTypeSpecifier imageType) {
76            return true;
77        }
78    }
79
80    private static void test440335x() {
81        IIOMetadataFormat format = new IIOMetadataFormatImpl440335x();
82
83        // Check that correct value is returned
84        if (format.getElementMinChildren("anElement") != 20) {
85            throw new RuntimeException("Error on getElementMinChildren!");
86        }
87        if (format.getElementMaxChildren("anElement") != 200) {
88            throw new RuntimeException("Error on getElementMaxChildren!");
89        }
90
91        // Check that correct value is returned and no exception is thrown
92        try {
93            if (!format.getAttributeMinValue("anElement",
94                                             "exclusiveAttr").equals("50")) {
95                throw new RuntimeException("Error on exclusiveAttr min!");
96            }
97            if (!format.getAttributeMaxValue("anElement",
98                                             "exclusiveAttr").equals("500")) {
99                throw new RuntimeException("Error on exclusiveAttr max!");
100            }
101            if (!format.getAttributeMinValue("anElement",
102                                             "minAttr").equals("60")) {
103                throw new RuntimeException("Error on minAttr min!");
104            }
105            if (!format.getAttributeMaxValue("anElement",
106                                             "minAttr").equals("600")) {
107                throw new RuntimeException("Error on minAttr max!");
108            }
109            if (!format.getAttributeMinValue("anElement",
110                                             "maxAttr").equals("70")) {
111                throw new RuntimeException("Error on maxAttr min!");
112            }
113            if (!format.getAttributeMaxValue("anElement",
114                                             "maxAttr").equals("700")) {
115                throw new RuntimeException("Error on maxAttr max!");
116            }
117            if (!format.getAttributeMinValue("anElement",
118                                             "minMaxAttr").equals("80")) {
119                throw new RuntimeException("Error on minMaxAttr min!");
120            }
121            if (!format.getAttributeMaxValue("anElement",
122                                             "minMaxAttr").equals("800")) {
123                throw new RuntimeException("Error on minMaxAttr max!");
124            }
125        } catch (IllegalStateException e) {
126            throw new RuntimeException("Got IllegalStateException!");
127        }
128    }
129
130    static class IIOMetadataFormatImpl4436995 extends IIOMetadataFormatImpl {
131
132        public IIOMetadataFormatImpl4436995(String root,
133                                            int minChildren, int maxChildren) {
134            super(root, minChildren, maxChildren);
135        }
136
137        public void addAttribute(String elementName,
138                                 String attrName,
139                                 int dataType,
140                                 boolean required,
141                                 int listMinLength, int listMaxLength) {
142            super.addAttribute(elementName,
143                               attrName,
144                               dataType,
145                               required, listMinLength,
146                               listMaxLength);
147        }
148
149        public boolean canNodeAppear(String elementName,
150                                     ImageTypeSpecifier imageType) {
151            return true;
152        }
153    }
154
155    private static void test4436995() {
156        String result;
157
158        IIOMetadataFormatImpl4436995 fmt =
159            new IIOMetadataFormatImpl4436995("root", 1, 10);
160        fmt.addAttribute("root", "attr", fmt.DATATYPE_INTEGER, true, 2, 5);
161        try {
162            result = fmt.getAttributeDescription("root", "non-existent", null);
163            throw new RuntimeException("Failed to get IAE!");
164        } catch(IllegalArgumentException e) {
165        }
166    }
167
168    static class IIOMetadataFormatImpl4438977 extends IIOMetadataFormatImpl {
169
170        public IIOMetadataFormatImpl4438977(String root,
171                                            int minChildren, int maxChildren) {
172            super(root, minChildren, maxChildren);
173        }
174
175        public void addAttribute(String elementName,
176                                 String attrName,
177                                 int dataType,
178                                 boolean required,
179                                 int listMinLength, int listMaxLength) {
180            super.addAttribute(elementName,
181                               attrName,
182                               dataType,
183                               required, listMinLength,
184                               listMaxLength);
185        }
186
187        public boolean canNodeAppear(String elementName,
188                                     ImageTypeSpecifier imageType) {
189            return true;
190        }
191    }
192
193    private static void test4438977() {
194        String[] result;
195
196        IIOMetadataFormatImpl4438977 fmt =
197            new IIOMetadataFormatImpl4438977("root", 1, 10);
198        fmt.addAttribute("root", "attr", fmt.DATATYPE_INTEGER, true, 2, 5);
199        try {
200            result = fmt.getAttributeEnumerations("root", "attr");
201            throw new RuntimeException("Failed to get IAE!");
202        } catch(IllegalArgumentException e) {
203        }
204    }
205
206}
207