xref: /aosp_15_r20/external/apache-xml/test/tests/extensions/java/javaSample4.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.Hashtable;
29 
30 /**
31  * Extension for testing xml-xalan/samples/extensions.
32  */
33 public class javaSample4 extends TestableExtension
34 {
35     static Hashtable counters = new Hashtable ();
36 
37     /** Simple extension method to setup hashtable.  */
init(org.apache.xalan.extensions.XSLProcessorContext context, org.w3c.dom.Element elem)38     public void init(org.apache.xalan.extensions.XSLProcessorContext context,
39                        org.w3c.dom.Element elem)
40     {
41         counter++; // every method call increments plain counter
42         String name = elem.getAttribute("name");
43         String value = elem.getAttribute("value");
44         int val;
45         try
46         {
47             val = Integer.parseInt (value);
48         }
49         catch (NumberFormatException e)
50         {
51             e.printStackTrace ();
52             val = 0;
53         }
54         counters.put (name, new Integer (val));
55     }
56 
57 
58     /** Simple extension method to get a value from the hashtable.  */
read(String name)59     public int read(String name)
60     {
61         counter++; // every method call increments plain counter
62         Integer cval = (Integer)counters.get(name);
63         return (cval == null) ? 0 : cval.intValue();
64     }
65 
66 
67     /** Simple extension method to increment a value in the hashtable.  */
incr(org.apache.xalan.extensions.XSLProcessorContext context, org.w3c.dom.Element elem)68     public void incr(org.apache.xalan.extensions.XSLProcessorContext context,
69                      org.w3c.dom.Element elem)
70     {
71         counter++; // every method call increments plain counter
72         String name = elem.getAttribute("name");
73         Integer cval = (Integer) counters.get(name);
74         int nval = (cval == null) ? 0 : (cval.intValue () + 1);
75         counters.put (name, new Integer (nval));
76     }
77 
78 
79     //// Implementations of TestableExtension
80     /** Plain counter of number of times called.  */
81     private static int counter = 0;
82 
83 
84     /**
85      * Perform and log any pre-transformation info.
86      * @return true if OK; false if any fatal error occoured
87      * @param datalet Datalet of current stylesheet test
88      */
preCheck(Logger logger, StylesheetDatalet datalet)89     public static boolean preCheck(Logger logger, StylesheetDatalet datalet)
90     {
91         logger.logMsg(Logger.INFOMSG, "javaSample4.preCheck; counter=" + counter);
92         return true;
93     }
94 
95 
96     /**
97      * Perform and log any post-transformation info.
98      *
99      * The extension should validate that it's extension was
100      * properly called; we also validate output file.
101      *
102      * @param logger Logger to dump any info to
103      * @param datalet Datalet of current stylesheet test
104      */
postCheck(Logger logger, StylesheetDatalet datalet)105     public static void postCheck(Logger logger, StylesheetDatalet datalet)
106     {
107         // Dump out our hashtable for user analysis
108         logger.logHashtable(Logger.STATUSMSG, counters, "javaSample4.postCheck() counters");
109 
110         // Verify that we've been called at least once
111         //@todo update to verify specific number of calls and hash entries
112         if (counter > 0)
113             logger.checkPass("javaSample4 has been called " + counter + " times");
114         else
115             logger.checkFail("javaSample4 has not been called");
116 
117         // We also validate the output file the normal way
118         CheckService fileChecker = (CheckService)datalet.options.get("fileCheckerImpl");
119         // Supply default value
120         if (null == fileChecker)
121             fileChecker = new XHTFileCheckService();
122         if (Logger.PASS_RESULT
123             != fileChecker.check(logger,
124                                  new File(datalet.outputName),
125                                  new File(datalet.goldName),
126                                  "Extension test of " + datalet.getDescription())
127            )
128         {
129             // Log a custom element with all the file refs first
130             // Closely related to viewResults.xsl select='fileref"
131             //@todo check that these links are valid when base
132             //  paths are either relative or absolute!
133             Hashtable attrs = new Hashtable();
134             attrs.put("idref", (new File(datalet.inputName)).getName());
135             attrs.put("inputName", datalet.inputName);
136             attrs.put("xmlName", datalet.xmlName);
137             attrs.put("outputName", datalet.outputName);
138             attrs.put("goldName", datalet.goldName);
139             logger.logElement(Logger.STATUSMSG, "fileref", attrs, "Extension test file references");
140         }
141     }
142 
143 
144     /**
145      * Description of what this extension does.
146      * @return String description of extension
147      */
getDescription()148     public static String getDescription()
149     {
150         return "Simple hashtable lookup and counter";
151     }
152 }
153