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     7042594 8028206
27  * @summary Test verifies that ICC_Profile.setData() conforms the spec.
28  *
29  * @run     main SetDataTest
30  */
31 
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.awt.color.ICC_Profile;
36 import static java.awt.color.ICC_ColorSpace.CS_GRAY;
37 import static java.awt.color.ICC_Profile.icSigGrayTRCTag;
38 import static java.awt.color.ICC_Profile.icSigRedTRCTag;
39 import static java.awt.color.ICC_Profile.icSigGreenTRCTag;
40 
41 public class SetDataTest {
42 
43     static class TestCase {
44 
45         static ICC_Profile profile;
46         static byte[] validTRCdata;
47         static byte[] invalidTRCData;
48 
49         static {
50             profile = ICC_Profile.getInstance(CS_GRAY);
51             validTRCdata = profile.getData(icSigGrayTRCTag);
52             invalidTRCData = new byte[]{0x42, 0x42, 0x42, 0x42, 1, 3, 4, 6,};
53         }
54         String desciption;
55         int signature;
56         boolean useValidData;
57         Throwable err;
58         boolean isIAEexpected;
59 
TestCase(String descr, int sig, boolean useValidData, boolean isIAEexpected)60         public TestCase(String descr, int sig,
61                 boolean useValidData,
62                 boolean isIAEexpected) {
63             this.desciption = descr;
64             this.signature = sig;
65 
66             this.useValidData = useValidData;
67             this.isIAEexpected = isIAEexpected;
68 
69         }
70 
doTest()71         public void doTest() {
72             System.out.println(desciption);
73             byte[] data = useValidData
74                     ? validTRCdata : invalidTRCData;
75             err = null;
76             try {
77                 profile.setData(signature, data);
78             } catch (Throwable e) {
79                 err = e;
80                 System.out.println("Got exception: " +
81                         e.getClass().getName() + ": " +
82                         e.getMessage());
83             }
84 
85             if (isIAEexpected) {
86                 if (err == null) {
87                     throw new RuntimeException(
88                             "Test failed: expected exception was not thrown");
89                 }
90                 if (!(err instanceof IllegalArgumentException)) {
91                     throw new RuntimeException(
92                             "Unexpected exception was thrown: " +
93                             err.getMessage(), err);
94                 }
95             } else {
96                 if (err != null) {
97                     throw new RuntimeException(
98                             "Unexpected exception was thrown: " +
99                             err.getMessage(), err);
100                 }
101             }
102             System.out.println("Testcase PASSED");
103         }
104     }
105 
main(String[] args)106     public static void main(String[] args) {
107         List<TestCase> tests = new ArrayList<TestCase>();
108 
109         TestCase selfupdate = new TestCase(
110                 "Selfupdate: update grayTRC with the same data.",
111                 icSigGrayTRCTag, true, false);
112         tests.add(selfupdate);
113 
114         TestCase newValdTag = new TestCase(
115                 "Insert new valid tag",
116                 icSigRedTRCTag,
117                 true, false);
118         tests.add(newValdTag);
119 
120         TestCase newInvalidTag = new TestCase(
121                 "Insert new tag with invalid contet",
122                 icSigGreenTRCTag,
123                 false, true);
124         tests.add(newInvalidTag);
125 
126         TestCase newUnknowInvalidTag = new TestCase(
127                 "Insert new tag with invalid data and unknown signature",
128                 0x41414141,
129                 false, true);
130         tests.add(newUnknowInvalidTag);
131 
132         TestCase newUnknownValidTag = new TestCase(
133                 "Insert new tag with valid data and unknown signatiure",
134                 0x41414141,
135                 true, true);
136         tests.add(newUnknownValidTag);
137 
138         for (TestCase t: tests) {
139             t.doTest();
140         }
141         System.out.println("Test passed!.");
142     }
143 }
144