001/* 002 * Copyright 2002-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.test.context.util; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.springframework.core.io.Resource; 023import org.springframework.core.io.ResourceLoader; 024import org.springframework.core.io.support.ResourcePatternUtils; 025import org.springframework.util.ClassUtils; 026import org.springframework.util.ResourceUtils; 027import org.springframework.util.StringUtils; 028 029/** 030 * Utility methods for working with resources within the <em>Spring TestContext 031 * Framework</em>. Mainly for internal use within the framework. 032 * 033 * @author Sam Brannen 034 * @author Tadaya Tsuyukubo 035 * @since 4.1 036 * @see org.springframework.util.ResourceUtils 037 * @see org.springframework.core.io.Resource 038 * @see org.springframework.core.io.ClassPathResource 039 * @see org.springframework.core.io.FileSystemResource 040 * @see org.springframework.core.io.UrlResource 041 * @see org.springframework.core.io.ResourceLoader 042 */ 043public abstract class TestContextResourceUtils { 044 045 private static final String SLASH = "/"; 046 047 048 private TestContextResourceUtils() { 049 /* prevent instantiation */ 050 } 051 052 /** 053 * Convert the supplied paths to classpath resource paths. 054 * 055 * <p>For each of the supplied paths: 056 * <ul> 057 * <li>A plain path — for example, {@code "context.xml"} — will 058 * be treated as a classpath resource that is relative to the package in 059 * which the specified class is defined. 060 * <li>A path starting with a slash will be treated as an absolute path 061 * within the classpath, for example: {@code "/org/example/schema.sql"}. 062 * <li>A path which is prefixed with a URL protocol (e.g., 063 * {@link ResourceUtils#CLASSPATH_URL_PREFIX classpath:}, 064 * {@link ResourceUtils#FILE_URL_PREFIX file:}, {@code http:}, etc.) will be 065 * {@link StringUtils#cleanPath cleaned} but otherwise unmodified. 066 * 067 * @param clazz the class with which the paths are associated 068 * @param paths the paths to be converted 069 * @return a new array of converted resource paths 070 * @see #convertToResources 071 */ 072 public static String[] convertToClasspathResourcePaths(Class<?> clazz, String... paths) { 073 String[] convertedPaths = new String[paths.length]; 074 for (int i = 0; i < paths.length; i++) { 075 String path = paths[i]; 076 if (path.startsWith(SLASH)) { 077 convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path; 078 } 079 else if (!ResourcePatternUtils.isUrl(path)) { 080 convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + SLASH 081 + StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + SLASH + path); 082 } 083 else { 084 convertedPaths[i] = StringUtils.cleanPath(path); 085 } 086 } 087 return convertedPaths; 088 } 089 090 /** 091 * Convert the supplied paths to an array of {@link Resource} handles using 092 * the given {@link ResourceLoader}. 093 * 094 * @param resourceLoader the {@code ResourceLoader} to use to convert the paths 095 * @param paths the paths to be converted 096 * @return a new array of resources 097 * @see #convertToResourceList(ResourceLoader, String...) 098 * @see #convertToClasspathResourcePaths 099 */ 100 public static Resource[] convertToResources(ResourceLoader resourceLoader, String... paths) { 101 List<Resource> list = convertToResourceList(resourceLoader, paths); 102 return list.toArray(new Resource[list.size()]); 103 } 104 105 /** 106 * Convert the supplied paths to a list of {@link Resource} handles using 107 * the given {@link ResourceLoader}. 108 * 109 * @param resourceLoader the {@code ResourceLoader} to use to convert the paths 110 * @param paths the paths to be converted 111 * @return a new list of resources 112 * @since 4.2 113 * @see #convertToResources(ResourceLoader, String...) 114 * @see #convertToClasspathResourcePaths 115 */ 116 public static List<Resource> convertToResourceList(ResourceLoader resourceLoader, String... paths) { 117 List<Resource> list = new ArrayList<Resource>(); 118 for (String path : paths) { 119 list.add(resourceLoader.getResource(path)); 120 } 121 return list; 122 } 123 124}