ModuleInfoBackedContext cache
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / AbstractSchemaAwareTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter.test;
9
10 import com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Set;
15 import org.junit.Before;
16 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
17 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
18 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
19 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21
22 public abstract class AbstractSchemaAwareTest {
23     private static final LoadingCache<Set<YangModuleInfo>, SchemaContext> SCHEMA_CONTEXT_CACHE =
24             CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<YangModuleInfo>, SchemaContext>() {
25                 @Override
26                 public SchemaContext load(final Set<YangModuleInfo> key) {
27                     return ModuleInfoBackedContext.cacheContext(
28                         GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(), ImmutableSet.copyOf(key))
29                             .tryToCreateSchemaContext().get();
30                 }
31             });
32
33     @Before
34     public final void setup() throws Exception {
35         setupWithSchema(getSchemaContext());
36     }
37
38     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
39         return BindingReflections.cacheModuleInfos(Thread.currentThread().getContextClassLoader());
40     }
41
42     protected SchemaContext getSchemaContext() throws Exception {
43         return SCHEMA_CONTEXT_CACHE.getUnchecked(getModuleInfos());
44     }
45
46     /**
47      * Setups test with Schema context.
48      *
49      * @param context schema context
50      */
51     protected abstract void setupWithSchema(SchemaContext context);
52 }