| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveutils.test; |
| 16 | |
| 17 | import java.net.URL; |
| 18 | import java.util.Locale; |
| 19 | |
| 20 | import org.apache.hivemind.ClassResolver; |
| 21 | import org.apache.hivemind.Registry; |
| 22 | import org.apache.hivemind.Resource; |
| 23 | import org.apache.hivemind.impl.DefaultClassResolver; |
| 24 | import org.apache.hivemind.impl.RegistryBuilder; |
| 25 | import org.apache.hivemind.impl.XmlModuleDescriptorProvider; |
| 26 | import org.apache.hivemind.util.URLResource; |
| 27 | |
| 28 | /** |
| 29 | * Utility class that integration test cases can inherit in order to have better |
| 30 | * support for HiveMind tests. |
| 31 | * This code was just excerpted from HiveMind <code>HiveMindTestCase</code>. |
| 32 | * |
| 33 | * @author Howard Lewis Ship |
| 34 | * @author Jean-Francois Poilpret |
| 35 | */ |
| 36 | public class HivemindIntegrationTestCase extends HivemindUnitTestCase |
| 37 | { |
| 38 | /** |
| 39 | * Returns the given file as a <code>Resource</code> from the classpath. |
| 40 | * Typically, this is to find files in the same folder as the invoking class. |
| 41 | */ |
| 42 | protected Resource getResource(String file) |
| 43 | { |
| 44 | URL url = getClass().getResource(file); |
| 45 | if (url == null) |
| 46 | { |
| 47 | throw new NullPointerException("No resource named '" + file + "'."); |
| 48 | } |
| 49 | return new URLResource(url); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Convienience method for invoking |
| 54 | * {@link #buildFrameworkRegistry(String[])} with only a single file. |
| 55 | */ |
| 56 | protected Registry buildFrameworkRegistry(String file) throws Exception |
| 57 | { |
| 58 | return buildFrameworkRegistry(new String[] {file}); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Builds a minimal registry, containing only the specified files, plus |
| 63 | * the master module descriptor (ie, those visible on the classpath). |
| 64 | * Files are resolved using {@link #getResource(String)}. |
| 65 | */ |
| 66 | protected Registry buildFrameworkRegistry(String[] files) throws Exception |
| 67 | { |
| 68 | ClassResolver resolver = new DefaultClassResolver(); |
| 69 | RegistryBuilder builder = new RegistryBuilder(); |
| 70 | for (int i = 0; i < files.length; i++) |
| 71 | { |
| 72 | Resource resource = getResource(files[i]); |
| 73 | builder.addModuleDescriptorProvider( |
| 74 | new XmlModuleDescriptorProvider(resolver, resource)); |
| 75 | } |
| 76 | builder.addDefaultModuleDescriptorProvider(); |
| 77 | return builder.constructRegistry(Locale.getDefault()); |
| 78 | } |
| 79 | } |