xref: /aosp_15_r20/external/apache-xml/test/tests/extensions/java/javaSample3.java (revision 1212f9a0ffdc28482b8821715d2222bf16dc14e2)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 // explicitly packageless
20 
21 import org.apache.qetest.CheckService;
22 import org.apache.qetest.Logger;
23 import org.apache.qetest.xsl.StylesheetDatalet;
24 import org.apache.qetest.xsl.TestableExtension;
25 import org.apache.qetest.xsl.XHTFileCheckService;
26 
27 import java.io.File;
28 import java.util.Calendar;
29 import java.util.Date;
30 import java.util.Hashtable;
31 
32 /**
33  * Extension for testing xml-xalan/samples/extensions.
34  */
35 public class javaSample3 extends TestableExtension
36 {
37 
38     /** Extension method called from stylesheet.  */
getDate(String year, String month, String day)39     public static Date getDate(String year, String month, String day)
40     {
41         // Bump up counter for later validation in postCheck
42         counter++;
43         Calendar c = Calendar.getInstance();
44         // Convert each argument to int.
45         c.set(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day));
46         return c.getTime();
47     }
48 
49 
50     //// Implementations of TestableExtension
51     /** Simple counter of number of times called.  */
52     private static int counter = 0;
53 
54 
55     /**
56      * Perform and log any pre-transformation info.
57      * @return true if OK; false if any fatal error occoured
58      * @param datalet Datalet of current stylesheet test
59      */
preCheck(Logger logger, StylesheetDatalet datalet)60     public static boolean preCheck(Logger logger, StylesheetDatalet datalet)
61     {
62         logger.logMsg(Logger.INFOMSG, "javaSample3.preCheck; counter=" + counter);
63         return true;
64     }
65 
66 
67     /**
68      * Perform and log any post-transformation info.
69      *
70      * The extension should validate that it's extension was
71      * properly called; we also validate output file.
72      *
73      * @param logger Logger to dump any info to
74      * @param datalet Datalet of current stylesheet test
75      */
postCheck(Logger logger, StylesheetDatalet datalet)76     public static void postCheck(Logger logger, StylesheetDatalet datalet)
77     {
78         // Verify that we've been called at least once
79         if (counter > 0)
80             logger.checkPass("javaSample3 has been called " + counter + " times");
81         else
82             logger.checkFail("javaSample3 has not been called");
83 
84         // We also validate the output file the normal way
85         CheckService fileChecker = (CheckService)datalet.options.get("fileCheckerImpl");
86         // Supply default value
87         if (null == fileChecker)
88             fileChecker = new XHTFileCheckService();
89         if (Logger.PASS_RESULT
90             != fileChecker.check(logger,
91                                  new File(datalet.outputName),
92                                  new File(datalet.goldName),
93                                  "Extension test of " + datalet.getDescription())
94            )
95         {
96             // Log a custom element with all the file refs first
97             // Closely related to viewResults.xsl select='fileref"
98             //@todo check that these links are valid when base
99             //  paths are either relative or absolute!
100             Hashtable attrs = new Hashtable();
101             attrs.put("idref", (new File(datalet.inputName)).getName());
102             attrs.put("inputName", datalet.inputName);
103             attrs.put("xmlName", datalet.xmlName);
104             attrs.put("outputName", datalet.outputName);
105             attrs.put("goldName", datalet.goldName);
106             logger.logElement(Logger.STATUSMSG, "fileref", attrs, "Extension test file references");
107         }
108     }
109 
110 
111     /**
112      * Description of what this extension does.
113      * @return String description of extension
114      */
getDescription()115     public static String getDescription()
116     {
117         return "getDate() returns date for ints";
118     }
119 }
120 
121