You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ni...@apache.org on 2013/07/04 20:35:33 UTC
svn commit: r1499839 -
/tika/trunk/tika-core/src/test/java/org/apache/tika/ResourceLoggingClassLoader.java
Author: nick
Date: Thu Jul 4 18:35:33 2013
New Revision: 1499839
URL: http://svn.apache.org/r1499839
Log:
Helper class for unit testing TIKA-1145 - Test classloader that logs the resources it loads
Added:
tika/trunk/tika-core/src/test/java/org/apache/tika/ResourceLoggingClassLoader.java
Added: tika/trunk/tika-core/src/test/java/org/apache/tika/ResourceLoggingClassLoader.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/ResourceLoggingClassLoader.java?rev=1499839&view=auto
==============================================================================
--- tika/trunk/tika-core/src/test/java/org/apache/tika/ResourceLoggingClassLoader.java (added)
+++ tika/trunk/tika-core/src/test/java/org/apache/tika/ResourceLoggingClassLoader.java Thu Jul 4 18:35:33 2013
@@ -0,0 +1,82 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * A wrapper around a {@link ClassLoader} that logs all
+ * the Resources loaded through it.
+ * Used to check that a specific ClassLoader was used
+ * when unit testing
+ */
+public class ResourceLoggingClassLoader extends ClassLoader {
+ private Map<String,List<URL>> loadedResources = new HashMap<String, List<URL>>();
+
+ public ResourceLoggingClassLoader(ClassLoader realClassloader) {
+ super(realClassloader);
+ }
+
+ private List<URL> fetchRecord(String name) {
+ List<URL> alreadyLoaded = loadedResources.get(name);
+ if (alreadyLoaded == null) {
+ alreadyLoaded = new ArrayList<URL>();
+ loadedResources.put(name, alreadyLoaded);
+ }
+ return alreadyLoaded;
+ }
+
+ @Override
+ public URL getResource(String name) {
+ URL resource = super.getResource(name);
+ List<URL> alreadyLoaded = fetchRecord(name);
+ alreadyLoaded.add(resource);
+ return resource;
+ }
+
+ @Override
+ public Enumeration<URL> getResources(String name) throws IOException {
+ Enumeration<URL> resources = super.getResources(name);
+ List<URL> alreadyLoaded = fetchRecord(name);
+
+ // Need to copy as we record
+ List<URL> these = Collections.list(resources);
+ alreadyLoaded.addAll(these);
+
+ // Return our copy
+ return Collections.enumeration(these);
+ }
+
+ public List<URL> getLoadedResources(String resourceName) {
+ List<URL> resources = loadedResources.get(resourceName);
+ if (resources == null) return Collections.emptyList();
+ return Collections.unmodifiableList(resources);
+ }
+ public Map<String,List<URL>> getLoadedResources() {
+ return Collections.unmodifiableMap(loadedResources);
+ }
+ public void resetLoadedResources() {
+ loadedResources.clear();
+ }
+}