|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * 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 | + |
| 20 | +package co.elastic.clients.elasticsearch.json; |
| 21 | + |
| 22 | +import co.elastic.clients.json.JsonpUtils; |
| 23 | +import co.elastic.clients.util.AllowForbiddenApis; |
| 24 | +import jakarta.json.JsonException; |
| 25 | +import jakarta.json.spi.JsonProvider; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.net.URL; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.Enumeration; |
| 32 | + |
| 33 | +public class JsonpUtilsTest extends Assert { |
| 34 | + |
| 35 | + @Test |
| 36 | + @AllowForbiddenApis("Testing JsonpUtil.provider()") |
| 37 | + public void testProviderLoading() { |
| 38 | + // See https://github.com/elastic/elasticsearch-java/issues/163 |
| 39 | + |
| 40 | + // Create an empty non-delegating classloader and set it as the context classloader. It simulates a |
| 41 | + // plugin system that doesn't set the context classloader to the plugins classloader. |
| 42 | + ClassLoader emptyLoader = new ClassLoader() { |
| 43 | + @Override |
| 44 | + public Enumeration<URL> getResources(String name) { |
| 45 | + return Collections.emptyEnumeration(); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + ClassLoader savedLoader = Thread.currentThread().getContextClassLoader(); |
| 50 | + try { |
| 51 | + Thread.currentThread().setContextClassLoader(emptyLoader); |
| 52 | + |
| 53 | + assertThrows(JsonException.class, () -> { |
| 54 | + assertNotNull(JsonProvider.provider()); |
| 55 | + }); |
| 56 | + |
| 57 | + assertNotNull(JsonpUtils.provider()); |
| 58 | + |
| 59 | + } finally { |
| 60 | + Thread.currentThread().setContextClassLoader(savedLoader); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments