Skip to content

Commit

Permalink
Merge pull request #14 from TomasHofman/JBEE-258-3.0.x-new
Browse files Browse the repository at this point in the history
[JBEE-258] FactoryFinderCache does not properly handle comments in service
  • Loading branch information
scottmarlow committed Jan 24, 2023
2 parents 4d9f03a + 8ca9d3b commit f634925
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 1 deletion.
28 changes: 27 additions & 1 deletion api/src/main/java/org/jboss/el/cache/FactoryFinderCache.java
Expand Up @@ -96,7 +96,11 @@ public static String loadImplementationClassName(final String factoryId, final C
BufferedReader rd =
new BufferedReader(new InputStreamReader(is, "UTF-8"));

String factoryClassName = rd.readLine();
String factoryClassName;
do {
factoryClassName = sanitize(rd.readLine());
} while (factoryClassName != null && factoryClassName.isEmpty());

rd.close();

if (factoryClassName != null &&
Expand All @@ -115,6 +119,28 @@ public static String loadImplementationClassName(final String factoryId, final C
return null;
}

static String sanitize(String line) {
if (line == null) {
return null;
}

// strip comment
int idx = line.indexOf('#');
if (idx >= 0) {
line = line.substring(0, idx);
}

// strip spaces and tabs
while (line.startsWith(" ") || line.startsWith("\t")) {
line = line.substring(1);
}
while (line.endsWith(" ") || line.endsWith("\t")) {
line = line.substring(0, line.length() - 1);
}

return line;
}

private static class CacheKey {
private final ClassLoader loader;
private final String className;
Expand Down
@@ -0,0 +1,39 @@
package org.jboss.el.cache;

import org.junit.Assert;
import org.junit.Test;

public class FactoryFinderCacheTestCase {

@Test
public void testServiceFileWithComment() {
String className = FactoryFinderCache.loadImplementationClassName("org.jboss.el.cache.TestService-comment",
this.getClass().getClassLoader());
Assert.assertEquals("org.jboss.el.cache.TestServiceImpl", className);
}

@Test
public void testEmptyServiceFile() {
String className = FactoryFinderCache.loadImplementationClassName("org.jboss.el.cache.TestService-empty",
this.getClass().getClassLoader());
Assert.assertNull(className);
}

@Test
public void testServiceFile() {
String className = FactoryFinderCache.loadImplementationClassName("org.jboss.el.cache.TestService",
this.getClass().getClassLoader());
Assert.assertEquals("org.jboss.el.cache.TestServiceImpl", className);
}

@Test
public void testSanitizeMethod() {
Assert.assertEquals("ClassName", FactoryFinderCache.sanitize("ClassName"));
Assert.assertEquals("ClassName", FactoryFinderCache.sanitize(" ClassName "));
Assert.assertEquals("ClassName", FactoryFinderCache.sanitize("\tClassName\t"));
Assert.assertEquals("ClassName", FactoryFinderCache.sanitize(" \tClassName\t # comment..."));
Assert.assertEquals("", FactoryFinderCache.sanitize("# only comment..."));
Assert.assertEquals("", FactoryFinderCache.sanitize(""));
Assert.assertNull(FactoryFinderCache.sanitize(null));
}
}
5 changes: 5 additions & 0 deletions api/src/test/java/org/jboss/el/cache/TestService.java
@@ -0,0 +1,5 @@
package org.jboss.el.cache;

public interface TestService {
void execute();
}
9 changes: 9 additions & 0 deletions api/src/test/java/org/jboss/el/cache/TestServiceImpl.java
@@ -0,0 +1,9 @@
package org.jboss.el.cache;

public class TestServiceImpl implements TestService {

@Override
public void execute() {
// pass
}
}
@@ -0,0 +1 @@
org.jboss.el.cache.TestServiceImpl
@@ -0,0 +1,2 @@
# Comment
org.jboss.el.cache.TestServiceImpl
Empty file.

0 comments on commit f634925

Please sign in to comment.