2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter.test;
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;
15 import org.junit.Before;
16 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
17 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
18 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 public abstract class AbstractSchemaAwareTest {
22 private static final LoadingCache<ClassLoader, Set<YangModuleInfo>> MODULE_INFO_CACHE = CacheBuilder.newBuilder()
23 .weakKeys().weakValues().build(new CacheLoader<ClassLoader, Set<YangModuleInfo>>() {
25 public Set<YangModuleInfo> load(final ClassLoader key) {
26 return BindingReflections.loadModuleInfos(key);
29 private static final LoadingCache<Set<YangModuleInfo>, SchemaContext> SCHEMA_CONTEXT_CACHE =
30 CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<YangModuleInfo>, SchemaContext>() {
32 public SchemaContext load(final Set<YangModuleInfo> key) {
33 final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
34 moduleContext.addModuleInfos(key);
35 return moduleContext.tryToCreateSchemaContext().get();
40 public final void setup() throws Exception {
41 setupWithSchema(getSchemaContext());
44 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
45 return MODULE_INFO_CACHE.getUnchecked(Thread.currentThread().getContextClassLoader());
48 protected SchemaContext getSchemaContext() throws Exception {
49 // ImmutableSet guarantees non-null
50 return SCHEMA_CONTEXT_CACHE.getUnchecked(ImmutableSet.copyOf(getModuleInfos()));
54 * Setups test with Schema context.
56 * @param context schema context
58 protected abstract void setupWithSchema(SchemaContext context);