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 * $Id$ 20 */ 21 package org.apache.qetest.xalanj2; 22 23 import android.platform.test.annotations.FlakyTest; 24 import org.apache.qetest.FileBasedTest; 25 import org.apache.xml.utils.SystemIDResolver; 26 import org.junit.Test; 27 28 import java.nio.file.Paths; 29 30 /** 31 * Functionality/system/integration tests for SystemIDResolver. 32 * 33 * Very simple coverage test. 34 * 35 * @author shane_curcuru@us.ibm.com, 36 * Joe Kesselman 37 * @version $Id$ 38 */ 39 public class SystemIDResolverAPITest extends FileBasedTest 40 { 41 /** Just initialize test name, comment, numTestCases. */ SystemIDResolverAPITest()42 public SystemIDResolverAPITest() 43 { 44 numTestCases = 1; // REPLACE_num 45 testName = "SystemIDResolverAPITest"; 46 testComment = "Functionality/system/integration tests for SystemIDResolver"; 47 } 48 49 /** Separator for hierarchical URLs /. */ 50 private static final char URL_SEP = '/'; 51 52 /** Default file:// scheme header. */ 53 private static final String FILE_SCHEME = "file://"; 54 55 /** 56 * Test strings and expected data for getAbsoluteURIFromRelative(String uri). 57 * NOTE: We really need a more definitive and thorough set of 58 * test strings: this requires really reading a number of RFC's... 59 */ 60 protected static String[][] ABS_URI_FROM_REL = 61 { /* Assumption: test prepends expected user.dir stuff */ 62 { "foo.out", "foo.out" }, 63 { "bar/foo.out", "bar/foo.out" }, 64 { "bar\\foo.out", "bar/foo.out" }, 65 { "foo.out", "foo.out" } 66 }; 67 68 /** Test strings and expected data for getAbsoluteURI(String url) 69 * assuming they're relative paths from a baseURL of file:///'user.dir'. 70 * NOTE: We really need a more definitive and thorough set of 71 * test strings: this requires really reading a number of RFC's... 72 */ 73 protected static String[][] ABS_URI_REL = 74 { /* Assumption: test prepends expected user.dir stuff */ 75 { "foo.out", "foo.out" }, 76 { "bar/foo.out", "bar/foo.out" }, 77 { "bar\\foo.out", "bar/foo.out" }, 78 { "foo.out", "foo.out" } 79 }; 80 81 /** Test strings and expected data for getAbsoluteURI(String url) 82 * assuming they're absolute paths with their own scheme:. 83 * NOTE: We really need a more definitive and thorough set of 84 * test strings: this requires really reading a number of RFC's... 85 */ 86 protected static String[][] ABS_URI_ABS = 87 { 88 { "http://server.com/foo.out", "http://server.com/foo.out" }, 89 { "http://server.com/bar/foo.out", "http://server.com/bar/foo.out" }, 90 { "http://server.com/bar/foo.out#fragment", "http://server.com/bar/foo.out#fragment" }, 91 { "http://server.com/bar/foo/?param=value&name=value", "http://server.com/bar/foo/?param=value&name=value" }, 92 { "http://127.0.0.1/bar/foo.out#fragment", "http://127.0.0.1/bar/foo.out#fragment" }, 93 { "file://server.com/bar/foo.out", "file://server.com/bar/foo.out" } 94 }; 95 96 /** Test strings and expected data for getAbsoluteURI(String urlString, String base). */ 97 protected static String[][] ABS_URI_WITH_BASE = 98 { /* "urlString", "base", "expected result" */ 99 { "foo.out", "file:///bar", "file:///foo.out" }, /* Note that trailing /bar is dropped since it's effectively a file reference */ 100 { "foo.out", "file:///bar/", "file:///bar/foo.out" }, 101 { "foo.out", "http://server.com/bar", "http://server.com/foo.out" }, 102 { "foo.out", "http://server.com/bar/", "http://server.com/bar/foo.out" }, 103 { "../foo.out", "http://server.com/bar/", "http://server.com/foo.out" } 104 }; 105 106 107 /** 108 * Using our data set of URLs, try various resolver calls. 109 * 110 * @return false if we should abort the test; true otherwise 111 */ testCase1()112 public boolean testCase1() 113 { 114 reporter.testCaseInit("Using our data set of URLs, try various resolver calls"); 115 116 try 117 { 118 String prevUserDir = System.getProperty("user.dir"); 119 String baseURL = ((Paths.get(prevUserDir)).toUri()).toString(); 120 121 reporter.logStatusMsg("user.dir baseURI is: " + baseURL); 122 123 reporter.logTraceMsg("Now testing getAbsoluteURIFromRelative..."); 124 for (int i = 0; i < ABS_URI_FROM_REL.length; i++) 125 { 126 String val = SystemIDResolver.getAbsoluteURIFromRelative(ABS_URI_FROM_REL[i][0]); 127 // Automatically prepend the baseURI to expected data 128 reporter.check(val, baseURL + ABS_URI_FROM_REL[i][1], "getAbsoluteURIFromRelative(" + ABS_URI_FROM_REL[i][0] + ") = " + val); 129 } 130 131 reporter.logTraceMsg("Now testing getAbsoluteURI with relative paths and default baseURL"); 132 for (int i = 0; i < ABS_URI_REL.length; i++) 133 { 134 String val = SystemIDResolver.getAbsoluteURI(ABS_URI_REL[i][0]); 135 // Automatically prepend the baseURI to expected data 136 reporter.check(val, baseURL + ABS_URI_REL[i][1], "getAbsoluteURI(" + ABS_URI_REL[i][0] + ") = " + val); 137 } 138 139 reporter.logTraceMsg("Now testing getAbsoluteURI with absolute paths"); 140 for (int i = 0; i < ABS_URI_ABS.length; i++) 141 { 142 String val = SystemIDResolver.getAbsoluteURI(ABS_URI_ABS[i][0]); 143 reporter.check(val, ABS_URI_ABS[i][1], "getAbsoluteURI(" + ABS_URI_ABS[i][0] + ") = " + val); 144 } 145 146 reporter.logTraceMsg("Now testing getAbsoluteURI with a user-supplied baseURL"); 147 for (int i = 0; i < ABS_URI_WITH_BASE.length; i++) 148 { 149 String val = SystemIDResolver.getAbsoluteURI(ABS_URI_WITH_BASE[i][0], ABS_URI_WITH_BASE[i][1]); 150 reporter.check(val, ABS_URI_WITH_BASE[i][2], "getAbsoluteURI(" + ABS_URI_WITH_BASE[i][0] 151 + ", " + ABS_URI_WITH_BASE[i][1] + ") = " + val); 152 } 153 } 154 catch (Throwable t) 155 { 156 reporter.logThrowable(reporter.ERRORMSG, t, "testCase1 threw"); 157 reporter.checkFail("testCase1 threw: " + t.toString()); 158 } 159 160 reporter.testCaseClose(); 161 return true; 162 } 163 164 165 /** 166 * Convenience method to print out usage information - update if needed. 167 * @return String denoting usage of this test class 168 */ usage()169 public String usage() 170 { 171 return ("Common [optional] options supported by SystemIDResolverAPITest:\n" 172 + super.usage()); // Grab our parent classes usage as well 173 } 174 175 176 /** 177 * Main method to run test from the command line - can be left alone. 178 * @param args command line argument array 179 */ main(String[] args)180 public static void main(String[] args) 181 { 182 SystemIDResolverAPITest app = new SystemIDResolverAPITest(); 183 app.doMain(args); 184 } 185 186 // Android-added: Run main method as a JUnit test case. 187 @FlakyTest(bugId = 292520220) 188 @Test main()189 public void main() { 190 main(new String[0]); 191 } 192 } 193