|
| 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