summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2011-10-19 12:12:15 +0400
committerEvgeny Mandrikov <mandrikov@gmail.com>2011-10-19 12:58:45 +0400
commit4f733b3e0b744ef756eb0d203b33aef001a4a355 (patch)
tree5e167fee0a567b5f6595a0ac978a3ef487eec261 /plugins
parent2300dc6b06275d5e6104abefcec23f490427cf12 (diff)
downloadsonarqube-4f733b3e0b744ef756eb0d203b33aef001a4a355.tar.gz
sonarqube-4f733b3e0b744ef756eb0d203b33aef001a4a355.zip
SONAR-2892 Add more tests and javadocs
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/FileSystemLoader.java13
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/JarLoader.java11
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/Loader.java23
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/SquidClassLoader.java23
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/FileSystemLoaderTest.java91
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/JarLoaderTest.java59
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/SquidClassLoaderTest.java45
7 files changed, 239 insertions, 26 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/FileSystemLoader.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/FileSystemLoader.java
index ded94e76be9..de4460276ae 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/FileSystemLoader.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/FileSystemLoader.java
@@ -30,13 +30,19 @@ import org.apache.commons.io.IOUtils;
class FileSystemLoader implements Loader {
- private final File baseDir;
+ private File baseDir;
public FileSystemLoader(File baseDir) {
+ if (baseDir == null) {
+ throw new IllegalArgumentException("baseDir can't be null");
+ }
this.baseDir = baseDir;
}
public URL findResource(String name) {
+ if (baseDir == null) {
+ throw new IllegalStateException("Loader closed");
+ }
File file = new File(baseDir, name);
if (file.exists() && file.isFile()) {
try {
@@ -49,6 +55,9 @@ class FileSystemLoader implements Loader {
}
public byte[] loadBytes(String name) {
+ if (baseDir == null) {
+ throw new IllegalStateException("Loader closed");
+ }
File file = new File(baseDir, name);
if (!file.exists()) {
return null;
@@ -65,7 +74,7 @@ class FileSystemLoader implements Loader {
}
public void close() {
- // nothing to do
+ baseDir = null;
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/JarLoader.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/JarLoader.java
index e1d7f53662a..5f6fa7d922a 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/JarLoader.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/JarLoader.java
@@ -36,7 +36,13 @@ class JarLoader implements Loader {
private final JarFile jarFile;
private final URL jarUrl;
+ /**
+ * @throws IOException if an I/O error has occurred
+ */
public JarLoader(File file) throws IOException {
+ if (file == null) {
+ throw new IllegalArgumentException("file can't be null");
+ }
jarFile = new JarFile(file);
jarUrl = new URL("jar", "", -1, file.getAbsolutePath() + "!/");
}
@@ -100,10 +106,7 @@ class JarLoader implements Loader {
@Override
public InputStream getInputStream() throws IOException {
- if (jarFile != null) {
- return jarFile.getInputStream(entry);
- }
- throw new IOException("JarLoader has been closed");
+ return jarFile.getInputStream(entry);
}
};
}
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/Loader.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/Loader.java
index dcb521d890a..1d19a9da7c4 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/Loader.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/Loader.java
@@ -21,12 +21,35 @@ package org.sonar.java.bytecode.loader;
import java.net.URL;
+/**
+ * Specifies resource loading behavior.
+ */
interface Loader {
+ /**
+ * Finds the resource with the given name.
+ *
+ * @param name resource name
+ * @return an <tt>URL</tt> object for reading the resource, or
+ * <tt>null</tt> if the resource could not be found
+ * @throws IllegalStateException if loader has been closed
+ */
URL findResource(String name);
+ /**
+ * Loads bytes of the resource with the given name.
+ *
+ * @param name resource name
+ * @return an array of <tt>byte</tt>s, or
+ * <tt>null</tt> if the resource could not be found or could not be loaded for some reason
+ * @throws IllegalStateException if loader has been closed
+ */
byte[] loadBytes(String name);
+ /**
+ * Closes this loader, so that it can no longer be used to load new resources.
+ * If loader is already closed, then invoking this method has no effect.
+ */
void close();
}
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/SquidClassLoader.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/SquidClassLoader.java
index d7bb1ef9943..783e227e6cf 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/SquidClassLoader.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/loader/SquidClassLoader.java
@@ -19,6 +19,7 @@
*/
package org.sonar.java.bytecode.loader;
+import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.URL;
@@ -27,7 +28,12 @@ import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
-public class SquidClassLoader extends ClassLoader {
+import com.google.common.collect.Iterators;
+
+/**
+ * Class loader, which is able to load classes from a list of JAR files and directories.
+ */
+public class SquidClassLoader extends ClassLoader implements Closeable {
private final List<Loader> loaders;
@@ -79,9 +85,22 @@ public class SquidClassLoader extends ClassLoader {
@Override
protected Enumeration<URL> findResources(String name) throws IOException {
- throw new UnsupportedOperationException();
+ List<URL> result = new ArrayList<URL>();
+ for (Loader loader : loaders) {
+ URL url = loader.findResource(name);
+ if (url != null) {
+ result.add(url);
+ }
+ }
+ return Iterators.asEnumeration(result.iterator());
}
+ /**
+ * Closes this class loader, so that it can no longer be used to load new classes or resources.
+ * Any classes or resources that are already loaded, are still accessible.
+ *
+ * If class loader is already closed, then invoking this method has no effect.
+ */
public void close() {
for (Loader loader : loaders) {
loader.close();
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/FileSystemLoaderTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/FileSystemLoaderTest.java
new file mode 100644
index 00000000000..177a28d2e35
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/FileSystemLoaderTest.java
@@ -0,0 +1,91 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.java.bytecode.loader;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.startsWith;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.net.URL;
+
+import org.junit.Test;
+import org.sonar.java.ast.SquidTestUtils;
+
+public class FileSystemLoaderTest {
+
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldThrowIllegalArgumentException() throws Exception {
+ new FileSystemLoader(null);
+ }
+
+ @Test
+ public void testFindResource() throws Exception {
+ File dir = SquidTestUtils.getFile("/bytecode/bin/");
+ FileSystemLoader loader = new FileSystemLoader(dir);
+
+ assertThat(loader.findResource("notfound"), nullValue());
+
+ URL url = loader.findResource("tags/TagName.class");
+ assertThat(url, notNullValue());
+ assertThat(url.toString(), allOf(startsWith("file:"), endsWith("TagName.class")));
+
+ loader.close();
+
+ try {
+ loader.findResource("tags/TagName.class");
+ fail();
+ } catch (IllegalStateException e) {
+ // ok
+ }
+ }
+
+ @Test
+ public void testLoadBytes() throws Exception {
+ File dir = SquidTestUtils.getFile("/bytecode/bin/");
+ FileSystemLoader loader = new FileSystemLoader(dir);
+
+ assertThat(loader.loadBytes("notfound"), nullValue());
+
+ assertThat(loader.loadBytes("tags/TagName.class"), notNullValue());
+
+ loader.close();
+
+ try {
+ loader.loadBytes("tags/TagName.class");
+ fail();
+ } catch (IllegalStateException e) {
+ // ok
+ }
+ }
+
+ @Test
+ public void closeCanBeCalledMultipleTimes() throws Exception {
+ File dir = SquidTestUtils.getFile("/bytecode/bin/");
+ FileSystemLoader loader = new FileSystemLoader(dir);
+ loader.close();
+ loader.close();
+ }
+
+}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/JarLoaderTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/JarLoaderTest.java
index efde0f4d367..e43e00961b8 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/JarLoaderTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/JarLoaderTest.java
@@ -19,12 +19,12 @@
*/
package org.sonar.java.bytecode.loader;
-import static org.hamcrest.Matchers.allOf;
-import static org.hamcrest.Matchers.endsWith;
-import static org.hamcrest.Matchers.hasItem;
-import static org.hamcrest.Matchers.startsWith;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
@@ -35,12 +35,20 @@ import org.sonar.java.ast.SquidTestUtils;
public class JarLoaderTest {
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldThrowIllegalArgumentException() throws Exception {
+ new JarLoader(null);
+ }
+
@Test
public void testFindResource() throws Exception {
File jar = SquidTestUtils.getFile("/bytecode/lib/hello.jar");
- JarLoader jarLoader = new JarLoader(jar);
+ JarLoader loader = new JarLoader(jar);
+
+ assertThat(loader.findResource("notfound"), nullValue());
- URL url = jarLoader.findResource("META-INF/MANIFEST.MF");
+ URL url = loader.findResource("META-INF/MANIFEST.MF");
+ assertThat(url, notNullValue());
assertThat(url.toString(), allOf(startsWith("jar:"), endsWith("hello.jar!/META-INF/MANIFEST.MF")));
InputStream is = url.openStream();
@@ -50,7 +58,44 @@ public class JarLoaderTest {
IOUtils.closeQuietly(is);
}
- jarLoader.close();
+ loader.close();
+
+ try {
+ loader.findResource("META-INF/MANIFEST.MF");
+ fail();
+ } catch (IllegalStateException e) {
+ // ok
+ }
+ }
+
+ @Test
+ public void testLoadBytes() throws Exception {
+ File jar = SquidTestUtils.getFile("/bytecode/lib/hello.jar");
+ JarLoader loader = new JarLoader(jar);
+
+ assertThat(loader.loadBytes("notfound"), nullValue());
+
+ byte[] bytes = loader.loadBytes("META-INF/MANIFEST.MF");
+ assertThat(bytes, notNullValue());
+ ByteArrayInputStream is = new ByteArrayInputStream(bytes);
+ assertThat(IOUtils.readLines(is), hasItem("Manifest-Version: 1.0"));
+
+ loader.close();
+
+ try {
+ loader.loadBytes("META-INF/MANIFEST.MF");
+ fail();
+ } catch (IllegalStateException e) {
+ // ok
+ }
+ }
+
+ @Test
+ public void closeCanBeCalledMultipleTimes() throws Exception {
+ File jar = SquidTestUtils.getFile("/bytecode/lib/hello.jar");
+ JarLoader loader = new JarLoader(jar);
+ loader.close();
+ loader.close();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/SquidClassLoaderTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/SquidClassLoaderTest.java
index df469136767..4b726f58227 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/SquidClassLoaderTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/loader/SquidClassLoaderTest.java
@@ -20,17 +20,23 @@
package org.sonar.java.bytecode.loader;
import static org.hamcrest.CoreMatchers.nullValue;
-import static org.hamcrest.core.IsNot.not;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.io.File;
+import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
+import java.util.List;
import org.junit.Test;
import org.sonar.java.ast.SquidTestUtils;
+import com.google.common.collect.Iterators;
+import com.google.common.collect.Lists;
+
public class SquidClassLoaderTest {
/**
@@ -47,8 +53,8 @@ public class SquidClassLoaderTest {
} catch (ClassNotFoundException e) {
// ok
}
- assertThat(classLoader.loadClass("java.lang.Integer"), not(nullValue()));
- assertThat(classLoader.getResource("java/lang/Integer.class"), not(nullValue()));
+ assertThat(classLoader.loadClass("java.lang.Integer"), notNullValue());
+ assertThat(classLoader.getResource("java/lang/Integer.class"), notNullValue());
}
@Test
@@ -56,8 +62,10 @@ public class SquidClassLoaderTest {
File jar = SquidTestUtils.getFile("/bytecode/lib/hello.jar");
SquidClassLoader classLoader = new SquidClassLoader(Arrays.asList(jar));
- assertThat(classLoader.loadClass("org.sonar.tests.Hello"), not(nullValue()));
- assertThat(classLoader.getResource("org/sonar/tests/Hello.class"), not(nullValue()));
+ assertThat(classLoader.loadClass("org.sonar.tests.Hello"), notNullValue());
+ assertThat(classLoader.getResource("org/sonar/tests/Hello.class"), notNullValue());
+ List<URL> resources = Lists.newArrayList(Iterators.forEnumeration(classLoader.findResources("org/sonar/tests/Hello.class")));
+ assertThat(resources.size(), is(1));
try {
classLoader.loadClass("foo.Unknown");
fail();
@@ -83,8 +91,10 @@ public class SquidClassLoaderTest {
File dir = SquidTestUtils.getFile("/bytecode/bin/");
SquidClassLoader classLoader = new SquidClassLoader(Arrays.asList(dir));
- assertThat(classLoader.loadClass("tags.TagName"), not(nullValue()));
- assertThat(classLoader.getResource("tags/TagName.class"), not(nullValue()));
+ assertThat(classLoader.loadClass("tags.TagName"), notNullValue());
+ assertThat(classLoader.getResource("tags/TagName.class"), notNullValue());
+ List<URL> resources = Lists.newArrayList(Iterators.forEnumeration(classLoader.findResources("tags/TagName.class")));
+ assertThat(resources.size(), is(1));
try {
classLoader.loadClass("tags.Unknown");
fail();
@@ -95,10 +105,23 @@ public class SquidClassLoaderTest {
classLoader.close();
}
- @Test(expected = UnsupportedOperationException.class)
- public void findResourcesNotSupported() throws Exception {
- SquidClassLoader classLoader = new SquidClassLoader(Collections.EMPTY_LIST);
- classLoader.findResources("");
+ @Test
+ public void testFindResources() throws Exception {
+ File dir = SquidTestUtils.getFile("/bytecode/bin/");
+ SquidClassLoader classLoader = new SquidClassLoader(Arrays.asList(dir, dir));
+
+ List<URL> resources = Lists.newArrayList(Iterators.forEnumeration(classLoader.findResources("tags/TagName.class")));
+ assertThat(resources.size(), is(2));
+
+ classLoader.close();
+ }
+
+ @Test
+ public void closeCanBeCalledMultipleTimes() throws Exception {
+ File jar = SquidTestUtils.getFile("/bytecode/lib/hello.jar");
+ SquidClassLoader classLoader = new SquidClassLoader(Arrays.asList(jar));
+ classLoader.close();
+ classLoader.close();
}
}