Skip to content

Commit 6e72f5c

Browse files
royteeuwenRoy Teeuwen
andauthored
GROOVY-11578: provide default FastStringService in case of service loader failure (#2330)
Co-authored-by: Roy Teeuwen <roy.teeuwen@kbc.be>
1 parent c46a124 commit 6e72f5c

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/FastStringUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static FastStringService loadService() {
4747
}
4848
}
4949
}
50-
return found;
50+
return found != null ? found : new DefaultFastStringService();
5151
}
5252
}
5353

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. 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,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.groovy.json.internal
20+
21+
import groovy.test.GroovyTestCase
22+
23+
class FastStringUtilsTest extends GroovyTestCase {
24+
25+
static class NoServiceLoaderGroovyClassLoader extends GroovyClassLoader {
26+
27+
@Override
28+
Enumeration<URL> getResources(String name) throws IOException {
29+
if (name == "META-INF/services/org.apache.groovy.json.FastStringServiceFactory") {
30+
return Collections.emptyEnumeration();
31+
}
32+
return super.getResources(name);
33+
}
34+
}
35+
36+
void testToCharArray() {
37+
char[] expected = ["t", "e", "s", "t"]
38+
assertEquals(expected, FastStringUtils.toCharArray("test"))
39+
}
40+
41+
void testToCharArrayWithNoServiceLoaderUsedTheDefaultStringService() {
42+
ClassLoader previous = Thread.currentThread().getContextClassLoader()
43+
try {
44+
// Making sure that our class loader is used in the ServiceLoader class
45+
def loader = new NoServiceLoaderGroovyClassLoader();
46+
Thread.currentThread().setContextClassLoader(loader)
47+
def shell = new GroovyShell(loader)
48+
def result = shell.evaluate('''
49+
import org.apache.groovy.json.internal.FastStringUtils
50+
FastStringUtils.toCharArray("json")
51+
''')
52+
assert result == ['j', 's', 'o', 'n'] as char[]
53+
54+
} finally {
55+
Thread.currentThread().setContextClassLoader(previous)
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)