1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package tests.support.resource;
19 
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.net.MalformedURLException;
28 import java.net.URL;
29 import tests.support.Support_Configuration;
30 
31 public class Support_Resources {
32 
33     public static final String RESOURCE_PACKAGE = "/tests/resources/";
34 
35     public static final String RESOURCE_PACKAGE_NAME = "tests.resources";
36 
getStream(String name)37     public static InputStream getStream(String name) {
38         // If we have the resources packaged up in our jar file, get them that way.
39         String path = RESOURCE_PACKAGE + name;
40         InputStream result = Support_Resources.class.getResourceAsStream(path);
41         if (result != null) {
42             return result;
43         }
44         // Otherwise, if we're in an Android build tree, get the files directly.
45         String ANDROID_BUILD_TOP = System.getenv("ANDROID_BUILD_TOP");
46         if (ANDROID_BUILD_TOP != null) {
47             File resource = new File(ANDROID_BUILD_TOP + "/libcore/support/src/test/java" + path);
48             if (resource.exists()) {
49                 try {
50                     return new FileInputStream(resource);
51                 } catch (IOException ex) {
52                     throw new IllegalArgumentException("Couldn't open: " + resource, ex);
53                 }
54             }
55         }
56         throw new IllegalArgumentException("No such resource: " + path);
57     }
58 
getURL(String name)59     public static String getURL(String name) {
60         String folder = null;
61         String fileName = name;
62         File resources = createTempFolder();
63         int index = name.lastIndexOf("/");
64         if (index != -1) {
65             folder = name.substring(0, index);
66             name = name.substring(index + 1);
67         }
68         copyFile(resources, folder, name);
69         String resPath = resources.toString();
70         if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') {
71             resPath = resPath.substring(1);
72         }
73         String urlSpec = "file:/" + resPath + "/" + fileName;
74         URL url;
75         try {
76             url = new URL(urlSpec);
77         } catch (MalformedURLException e) {
78             throw new RuntimeException("Unable to create url: " + urlSpec, e);
79         }
80         return url.toString();
81     }
82 
createTempFolder()83     public static File createTempFolder() {
84 
85         File folder = null;
86         try {
87             folder = File.createTempFile("hyts_resources", "", null);
88             folder.delete();
89             folder.mkdirs();
90         } catch (IOException e) {
91             throw new RuntimeException("Unable to create temp folder: " + folder, e);
92         }
93         folder.deleteOnExit();
94         return folder;
95     }
96 
copyFile(File root, String folder, String file)97     public static File copyFile(File root, String folder, String file) {
98         File f;
99         if (folder != null) {
100             f = new File(root.toString() + "/" + folder);
101             if (!f.exists()) {
102                 f.mkdirs();
103                 f.deleteOnExit();
104             }
105         } else {
106             f = root;
107         }
108 
109         File dest = new File(f.toString() + "/" + file);
110 
111         String resourceName = (folder == null ? file : folder + "/" + file);
112         InputStream in = Support_Resources.getStream(resourceName);
113         try {
114             copyLocalFileto(dest, in);
115         } catch (IOException e) {
116             throw new RuntimeException(
117                 "Unable to copy file from resource " + resourceName + " to file " + dest, e);
118         }
119 
120         return dest;
121     }
122 
createTempFile(String suffix)123     public static File createTempFile(String suffix) throws IOException {
124         return File.createTempFile("hyts_", suffix, null);
125     }
126 
copyLocalFileto(File dest, InputStream in)127     public static void copyLocalFileto(File dest, InputStream in) throws IOException {
128         if (!dest.exists()) {
129             FileOutputStream out = new FileOutputStream(dest);
130             copy(in, out);
131             out.close();
132             dest.deleteOnExit();
133         }
134         in.close();
135     }
136 
copy(InputStream in, OutputStream out)137     private static int copy(InputStream in, OutputStream out) throws IOException {
138         int total = 0;
139         byte[] buffer = new byte[8192];
140         int c;
141         while ((c = in.read(buffer)) != -1) {
142             total += c;
143             out.write(buffer, 0, c);
144         }
145         return total;
146     }
147 
getExternalLocalFile(String url)148     public static File getExternalLocalFile(String url) throws IOException {
149         File resources = createTempFolder();
150         InputStream in = new URL(url).openStream();
151         File temp = new File(resources.toString() + "/local.tmp");
152         copyLocalFileto(temp, in);
153         return temp;
154     }
155 
getResourceURL(String resource)156     public static String getResourceURL(String resource) {
157         return "http://" + Support_Configuration.TestResources + resource;
158     }
159 
160     /**
161      * Util method to load resource files
162      *
163      * @param name - name of resource file
164      * @return - resource input stream
165      */
getResourceStream(String name)166     public static InputStream getResourceStream(String name) {
167         InputStream is = Support_Resources.class.getResourceAsStream(name);
168 
169         if (is == null) {
170             name = RESOURCE_PACKAGE + name;
171             is = Support_Resources.class.getResourceAsStream(name);
172             if (is == null) {
173                 throw new RuntimeException("Failed to load resource: " + name);
174             }
175         }
176 
177         return is;
178     }
179 
resourceToTempFile(String path)180     public static File resourceToTempFile(String path) throws IOException {
181         File f = File.createTempFile("out", ".xml");
182         f.deleteOnExit();
183         FileOutputStream out = new FileOutputStream(f);
184 
185         InputStream xml = Support_Resources.class.getResourceAsStream(path);
186         int b;
187         while ((b = xml.read()) != -1) {
188             out.write(b);
189         }
190         out.flush();
191         out.close();
192         xml.close();
193         return f;
194     }
195 
copyLocalFileTo(File dest, InputStream in)196     public static void copyLocalFileTo(File dest, InputStream in) throws IOException {
197         if (!dest.exists()) {
198             FileOutputStream out = new FileOutputStream(dest);
199             int result;
200             byte[] buf = new byte[4096];
201             while ((result = in.read(buf)) != -1) {
202                 out.write(buf, 0, result);
203             }
204             in.close();
205             out.close();
206             dest.deleteOnExit();
207         }
208     }
209 }
210