| 1 |
/* |
| 2 |
* JBoss, Home of Professional Open Source. |
| 3 |
* Copyright 2014 Red Hat, Inc., and individual contributors |
| 4 |
* as indicated by the @author tags. |
| 5 |
* |
| 6 |
* Licensed 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 |
package org.jboss.modules; |
| 20 |
|
| 21 |
import static org.jboss.modules.util.Util.readBytes; |
| 22 |
import static org.junit.Assert.assertArrayEquals; |
| 23 |
import static org.junit.Assert.assertEquals; |
| 24 |
import static org.junit.Assert.assertFalse; |
| 25 |
import static org.junit.Assert.assertNotNull; |
| 26 |
import static org.junit.Assert.assertNull; |
| 27 |
import static org.junit.Assert.assertTrue; |
| 28 |
|
| 29 |
import java.net.URL; |
| 30 |
import java.util.Collection; |
| 31 |
import java.util.Collections; |
| 32 |
import java.util.HashSet; |
| 33 |
import java.util.Iterator; |
| 34 |
import java.util.Set; |
| 35 |
|
| 36 |
import org.jboss.modules.filter.PathFilter; |
| 37 |
import org.jboss.modules.filter.PathFilters; |
| 38 |
import org.junit.Before; |
| 39 |
import org.junit.Test; |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Abstract Test Case used as the base for all resource loader tests. |
| 44 |
* |
| 45 |
* @author John Bailey |
| 46 |
*/ |
| 47 |
public abstract class AbstractResourceLoaderTestCase extends AbstractModuleTestCase { |
| 48 |
|
| 49 |
protected ResourceLoader loader; |
| 50 |
|
| 51 |
@Before |
| 52 |
public void setupLoader() throws Exception { |
| 53 |
loader = createLoader(PathFilters.acceptAll()); |
| 54 |
} |
| 55 |
|
| 56 |
protected abstract ResourceLoader createLoader(final PathFilter exportFilter) throws Exception; |
| 57 |
protected abstract void assertResource(final Resource resource, final String fileName) throws Exception; |
| 58 |
|
| 59 |
@Test |
| 60 |
public void testBasicResource() throws Exception { |
| 61 |
Resource resource = loader.getResource("/test.txt"); |
| 62 |
assertNotNull(resource); |
| 63 |
assertResource(resource, "test.txt"); |
| 64 |
resource = loader.getResource("/nested/nested.txt"); |
| 65 |
assertNotNull(resource); |
| 66 |
assertResource(resource, "nested/nested.txt"); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void testMissingResource() throws Exception { |
| 71 |
Resource resource = loader.getResource("/test-bogus.txt"); |
| 72 |
assertNull(resource); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void testIndexPaths() throws Exception { |
| 77 |
final Collection<String> paths = loader.getPaths(); |
| 78 |
assertFalse(paths.isEmpty()); |
| 79 |
|
| 80 |
assertTrue(paths.contains("")); |
| 81 |
assertTrue(paths.contains("META-INF")); |
| 82 |
assertTrue(paths.contains("nested")); |
| 83 |
assertTrue(paths.contains("org")); |
| 84 |
assertTrue(paths.contains("org/jboss")); |
| 85 |
assertTrue(paths.contains("org/jboss/modules")); |
| 86 |
assertTrue(paths.contains("org/jboss/modules/test")); |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void testGetClassSpec() throws Exception { |
| 91 |
ClassSpec spec = loader.getClassSpec(Module.fileNameOfClass("org.jboss.modules.test.TestClass")); |
| 92 |
assertNotNull(spec); |
| 93 |
byte[] bytes = spec.getBytes(); |
| 94 |
|
| 95 |
final URL classResource = getClass().getClassLoader().getResource("org/jboss/modules/test/TestClass.class"); |
| 96 |
final byte[] expectedBytes = readBytes(classResource.openStream()); |
| 97 |
assertArrayEquals(expectedBytes, bytes); |
| 98 |
} |
| 99 |
|
| 100 |
@Test |
| 101 |
public void testMissingClassSpec() throws Exception { |
| 102 |
ClassSpec spec = loader.getClassSpec(Module.fileNameOfClass("org.jboss.modules.test.BogusClass")); |
| 103 |
assertNull(spec); |
| 104 |
} |
| 105 |
|
| 106 |
@Test |
| 107 |
public void testGetPackageSpec() throws Exception { |
| 108 |
PackageSpec spec = loader.getPackageSpec("org.jboss.modules.test"); |
| 109 |
assertNotNull(spec); |
| 110 |
|
| 111 |
assertEquals("JBoss Modules Test Classes", spec.getSpecTitle()); |
| 112 |
assertEquals("0.1", spec.getSpecVersion()); |
| 113 |
assertEquals("JBoss", spec.getSpecVendor()); |
| 114 |
assertEquals("org.jboss.modules.test", spec.getImplTitle()); |
| 115 |
assertEquals("1.0", spec.getImplVersion()); |
| 116 |
assertEquals("JBoss", spec.getImplVendor()); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void testMissingPackageSpec() throws Exception { |
| 121 |
PackageSpec spec = loader.getPackageSpec("org.jboss.modules.bogus"); |
| 122 |
assertNotNull(spec); |
| 123 |
|
| 124 |
assertEquals("MODULES-89", spec.getSpecTitle()); |
| 125 |
assertNull(spec.getSpecVersion()); |
| 126 |
assertNull(spec.getSpecVendor()); |
| 127 |
assertNull(spec.getImplTitle()); |
| 128 |
assertNull(spec.getImplVersion()); |
| 129 |
assertNull(spec.getImplVendor()); |
| 130 |
} |
| 131 |
|
| 132 |
@Test |
| 133 |
public void testIterateResourcesRootRecursive() throws Exception { |
| 134 |
Set<String> expected = new HashSet<String>(); |
| 135 |
expected.add("test.txt"); |
| 136 |
expected.add("nested/nested.txt"); |
| 137 |
expected.add("org/jboss/modules/test/TestClass.class"); |
| 138 |
expected.add("META-INF/MANIFEST.MF"); |
| 139 |
assertEquals(expected, getResourceNames("/", true)); |
| 140 |
assertEquals(expected, getResourceNames("", true)); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void testIterateResourcesRoot() throws Exception { |
| 145 |
Set<String> expected = new HashSet<String>(); |
| 146 |
expected.add("test.txt"); |
| 147 |
assertEquals(expected, getResourceNames("/", false)); |
| 148 |
assertEquals(expected, getResourceNames("", false)); |
| 149 |
} |
| 150 |
|
| 151 |
@Test |
| 152 |
public void testIterateResourcesNested() throws Exception { |
| 153 |
Set<String> expected = new HashSet<String>(); |
| 154 |
expected.add("nested/nested.txt"); |
| 155 |
assertEquals(expected, getResourceNames("/nested", true)); |
| 156 |
assertEquals(expected, getResourceNames("/nested", false)); |
| 157 |
assertEquals(expected, getResourceNames("nested", true)); |
| 158 |
assertEquals(expected, getResourceNames("nested", false)); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void testIterateResourcesClasses() throws Exception { |
| 163 |
Set<String> expected = new HashSet<String>(); |
| 164 |
expected.add("org/jboss/modules/test/TestClass.class"); |
| 165 |
assertEquals(expected, getResourceNames("/org/jboss/modules", true)); |
| 166 |
assertEquals(expected, getResourceNames("org/jboss/modules", true)); |
| 167 |
expected = Collections.<String>emptySet(); |
| 168 |
assertEquals(expected, getResourceNames("/org/jboss/modules", false)); |
| 169 |
assertEquals(expected, getResourceNames("org/jboss/modules", false)); |
| 170 |
} |
| 171 |
|
| 172 |
private Set<String> getResourceNames(String startPath, boolean recursive) { |
| 173 |
Set<String> result = new HashSet<String>(); |
| 174 |
IterableResourceLoader itloader = (IterableResourceLoader) loader; |
| 175 |
Iterator<Resource> itres = itloader.iterateResources(startPath, recursive); |
| 176 |
while(itres.hasNext()) { |
| 177 |
result.add(itres.next().getName()); |
| 178 |
} |
| 179 |
return result; |
| 180 |
} |
| 181 |
} |