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<Set<YangModuleInfo>, SchemaContext> SCHEMA_CONTEXT_CACHE =
23 CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<YangModuleInfo>, SchemaContext>() {
25 public SchemaContext load(final Set<YangModuleInfo> key) {
26 final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
27 moduleContext.addModuleInfos(key);
28 return moduleContext.tryToCreateSchemaContext().get();
33 public final void setup() throws Exception {
34 setupWithSchema(getSchemaContext());
37 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
38 return BindingReflections.cacheModuleInfos(Thread.currentThread().getContextClassLoader());
41 protected SchemaContext getSchemaContext() throws Exception {
42 // ImmutableSet guarantees non-null
43 return SCHEMA_CONTEXT_CACHE.getUnchecked(ImmutableSet.copyOf(getModuleInfos()));
47 * Setups test with Schema context.
49 * @param context schema context
51 protected abstract void setupWithSchema(SchemaContext context);