/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.infra.spi.ordered;

import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.fixture.FixtureCustomInterface;
import org.apache.shardingsphere.infra.spi.fixture.FixtureCustomInterfaceImpl;
import org.apache.shardingsphere.infra.spi.fixture.OrderedSPIFixture;
import org.junit.Before;
import org.junit.Test;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public final class OrderedSPIRegistryTest {
    
    @Before
    public void init() {
        ShardingSphereServiceLoader.register(OrderedSPIFixture.class);
    }
    
    @Test
    public void assertGetRegisteredServicesByClass() {
        Collection<FixtureCustomInterface> customInterfaceCollection = new LinkedList<>();
        customInterfaceCollection.add(new FixtureCustomInterfaceImpl());
        Collection<Class<?>> collection = customInterfaceCollection.stream().map(Object::getClass).collect(Collectors.toList());
        Map<Class<?>, OrderedSPIFixture> actual = OrderedSPIRegistry.getRegisteredServicesByClass(collection, OrderedSPIFixture.class);
        assertThat(actual.size(), is(1));
    }
    
    @Test
    public void assertGetRegisteredServices() {
        Collection<FixtureCustomInterface> collection = new LinkedList<>();
        collection.add(new FixtureCustomInterfaceImpl());
        Map<FixtureCustomInterface, OrderedSPIFixture> actual = OrderedSPIRegistry.getRegisteredServices(collection, OrderedSPIFixture.class);
        assertThat(actual.size(), is(1));
    }
}

@@ -850,16 +850,44 @@
 dered;%0A%0A
+import lombok.SneakyThrows;%0A
 import o
@@ -1189,45 +1189,369 @@
 org.
-junit.Before;%0Aimport org.junit.Test;%0A
+apache.shardingsphere.infra.spi.fixture.OrderedSPIFixtureImpl;%0Aimport org.apache.shardingsphere.infra.spi.ordered.cache.CachedOrderedServices;%0Aimport org.apache.shardingsphere.infra.spi.ordered.cache.OrderedServicesCache;%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport java.lang.reflect.Field;%0Aimport java.lang.reflect.Modifier;
 %0Aimp
@@ -1576,16 +1576,48 @@
 ection;%0A
+import java.util.LinkedHashMap;%0A
 import j
@@ -1655,24 +1655,98 @@
 a.util.Map;%0A
+import java.util.Optional;%0Aimport java.util.concurrent.ConcurrentHashMap;%0A
 import java.
@@ -1906,118 +1906,1165 @@
 t %7B%0A
+%0A
     
-%0A    @Before%0A    public void init() %7B%0A        ShardingSphereServiceLoader.register(OrderedSPIFixture.class
+private Collection%3CFixtureCustomInterface%3E customInterfaceCollection;%0A%0A    private FixtureCustomInterface fixtureCustomInterface;%0A%0A    private OrderedSPIFixture cacheOrderedSPIFixture;%0A%0A    @Before%0A    public void init() %7B%0A        ShardingSphereServiceLoader.register(OrderedSPIFixture.class);%0A%0A        customInterfaceCollection = new LinkedList%3C%3E();%0A        fixtureCustomInterface = new FixtureCustomInterfaceImpl();%0A        customInterfaceCollection.add(fixtureCustomInterface);%0A        cacheOrderedSPIFixture = new OrderedSPIFixtureImpl();%0A        Map%3CFixtureCustomInterface, OrderedSPIFixture%3E result = new LinkedHashMap%3C%3E(customInterfaceCollection.size(), 1);%0A        result.put(fixtureCustomInterface, cacheOrderedSPIFixture);%0A        OrderedServicesCache.cacheServices(customInterfaceCollection, OrderedSPIFixture.class, result);%0A    %7D%0A%0A    @Test%0A    public void assertGetRegisteredServicesByCache() %7B%0A        Optional%3CCachedOrderedServices%3E actual = OrderedServicesCache.findCachedServices(customInterfaceCollection, OrderedSPIFixture.class);%0A        assertThat(cacheOrderedSPIFixture, is(actual.get().getServices().get(fixtureCustomInterface))
 );%0A 
@@ -4003,10 +4003,427 @@
 ;%0A    %7D%0A
+%0A    @After%0A    @SneakyThrows%0A    public void clean() %7B%0A        Field field = OrderedServicesCache.class.getDeclaredField(%22CACHED_SERVICES%22);%0A        field.setAccessible(true);%0A        Field modifiers = Field.class.getDeclaredField(%22modifiers%22);%0A        modifiers.setAccessible(true);%0A        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);%0A        field.set(null, new ConcurrentHashMap%3C%3E());%0A    %7D%0A%0A
 %7D%0A

add test cases for OrderedServicesCache (#9824)