Make LazySerializedDOMNotification a DOMEvent
[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.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;
20
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>>() {
24                 @Override
25                 public Set<YangModuleInfo> load(final ClassLoader key) {
26                     return BindingReflections.loadModuleInfos(key);
27                 }
28             });
29     private static final LoadingCache<Set<YangModuleInfo>, SchemaContext> SCHEMA_CONTEXT_CACHE =
30             CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<YangModuleInfo>, SchemaContext>() {
31                 @Override
32                 public SchemaContext load(final Set<YangModuleInfo> key) {
33                     final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
34                     moduleContext.addModuleInfos(key);
35                     return moduleContext.tryToCreateSchemaContext().get();
36                 }
37             });
38
39     @Before
40     public final void setup() throws Exception {
41         setupWithSchema(getSchemaContext());
42     }
43
44     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
45         return MODULE_INFO_CACHE.getUnchecked(Thread.currentThread().getContextClassLoader());
46     }
47
48     protected SchemaContext getSchemaContext() throws Exception {
49         // ImmutableSet guarantees non-null
50         return SCHEMA_CONTEXT_CACHE.getUnchecked(ImmutableSet.copyOf(getModuleInfos()));
51     }
52
53     /**
54      * Setups test with Schema context.
55      *
56      * @param context schema context
57      */
58     protected abstract void setupWithSchema(SchemaContext context);
59 }