1a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes/* 2e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * Licensed to the Apache Software Foundation (ASF) under one or more 3e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * contributor license agreements. See the NOTICE file distributed with 4e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * this work for additional information regarding copyright ownership. 5e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0 6e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * (the "License"); you may not use this file except in compliance with 7e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * the License. You may obtain a copy of the License at 8a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes * 9e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 10a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes * 11e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * Unless required by applicable law or agreed to in writing, software 12e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 13e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * See the License for the specific language governing permissions and 15e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * limitations under the License. 16e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes */ 17e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes 18e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughespackage tests.support.resource; 19e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes 20e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport java.io.File; 21e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport java.io.FileNotFoundException; 22e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport java.io.FileOutputStream; 23e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport java.io.IOException; 24e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport java.io.InputStream; 25e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport java.net.MalformedURLException; 26e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport java.net.URISyntaxException; 27e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport java.net.URL; 28e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes 29e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport tests.support.Support_Configuration; 30e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes 31e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughespublic class Support_Resources { 32e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes 33a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes public static final String RESOURCE_PACKAGE = "/tests/resources/"; 34a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes 35a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes public static final String RESOURCE_PACKAGE_NAME = "tests.resources"; 36a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes 37a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes public static InputStream getStream(String name) { 38a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes // System.err.println("getResourceAsStream(" + RESOURCE_PACKAGE + name + ")"); 39a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes return Support_Resources.class.getResourceAsStream(RESOURCE_PACKAGE + name); 40a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 41a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes 42a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes public static String getURL(String name) { 43a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes String folder = null; 44a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes String fileName = name; 45a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes File resources = createTempFolder(); 46a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes int index = name.lastIndexOf("/"); 47a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes if (index != -1) { 48a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes folder = name.substring(0, index); 49a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes name = name.substring(index + 1); 50a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 51a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes copyFile(resources, folder, name); 52a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes URL url = null; 53a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes String resPath = resources.toString(); 54a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') { 55e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes resPath = resPath.substring(1); 56e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes } 57a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes try { 58a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes url = new URL("file:/" + resPath + "/" + fileName); 59a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } catch (MalformedURLException e) { 60a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes throw new RuntimeException(e); 61a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 62a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes return url.toString(); 63a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 64a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes 65a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes public static File createTempFolder() { 66a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes File folder = null; 67a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes try { 68a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes folder = File.createTempFile("hyts_resources", "", null); 69a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes folder.delete(); 70a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes folder.mkdirs(); 71a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } catch (IOException e) { 72a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes throw new RuntimeException(e); 73a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 74a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes folder.deleteOnExit(); 75a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes return folder; 76a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 77a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes 78a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes public static void copyFile(File root, String folder, String file) { 79a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes File f; 80a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes if (folder != null) { 81a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes f = new File(root.toString() + "/" + folder); 82a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes if (!f.exists()) { 83a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes f.mkdirs(); 84a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes f.deleteOnExit(); 85a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 86a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } else { 87e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes f = root; 88e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes } 89e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes 90a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes String src = folder == null ? file : folder + "/" + file; 91a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes InputStream in = Support_Resources.getStream(src); 92a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes try { 93a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes File dst = new File(f.toString() + "/" + file); 94a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes copyLocalFileTo(dst, in); 95a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } catch (Exception e) { 96a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes throw new RuntimeException("copyFile failed: root=" + root + " folder=" + folder + " file=" + file + " (src=" + src + ")", e); 97a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 98a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 99a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes 100a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes public static File createTempFile(String suffix) throws IOException { 101a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes return File.createTempFile("hyts_", suffix, null); 102a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 103a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes 104a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes public static void copyLocalFileTo(File dest, InputStream in) throws IOException { 105a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes if (!dest.exists()) { 106a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes FileOutputStream out = new FileOutputStream(dest); 107a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes int result; 108a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes byte[] buf = new byte[4096]; 109a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes while ((result = in.read(buf)) != -1) { 110e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes out.write(buf, 0, result); 111e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes } 112a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes in.close(); 113a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes out.close(); 114a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes dest.deleteOnExit(); 115a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 116a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 117a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes 118a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes public static File getExternalLocalFile(String url) throws IOException, MalformedURLException { 119a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes File resources = createTempFolder(); 120a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes InputStream in = new URL(url).openStream(); 121a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes File temp = new File(resources.toString() + "/local.tmp"); 122a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes copyLocalFileTo(temp, in); 123a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes return temp; 124a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 125a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes 126a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes public static String getResourceURL(String resource) { 127a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes return "http://" + Support_Configuration.TestResources + resource; 128a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes } 129e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes 130e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes /** 131e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * Util method to load resource files 132a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes * 133e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * @param name - name of resource file 134e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * @return - resource input stream 135e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes */ 136e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes public static InputStream getResourceStream(String name) { 137a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(name); 138e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes if (is == null) { 139e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes throw new RuntimeException("Failed to load resource: " + name); 140e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes } 141e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes return is; 142e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes } 143a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes 144e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes /** 145e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * Util method to get absolute path to resource file 146a0696ae076ed7ea66387057c7a1b34d42e10f140Elliott Hughes * 147e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * @param name - name of resource file 148e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * @return - path to resource 149e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes */ 150e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes public static String getAbsoluteResourcePath(String name) { 151e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes URL url = ClassLoader.getSystemClassLoader().getResource(name); 152e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes if (url == null) { 153e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes throw new RuntimeException("Failed to load resource: " + name); 154e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes } 155e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes 156e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes try { 157e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes return new File(url.toURI()).getAbsolutePath(); 158e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes } catch (URISyntaxException e) { 159e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes throw new RuntimeException("Failed to load resource: " + name); 160e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes } 161e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes } 162e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes} 163