Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / AbstractMockedModule.java
1 /*
2  * Copyright (c) 2013, 2017 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.controller.config.manager.impl;
9
10 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
11 import org.opendaylight.controller.config.api.ModuleIdentifier;
12 import org.opendaylight.controller.config.spi.Module;
13
14 public abstract class AbstractMockedModule implements Module {
15
16     protected final AutoCloseable instance;
17     private final ModuleIdentifier id;
18
19     protected abstract AutoCloseable prepareMockedInstance() throws Exception;
20
21     @SuppressWarnings("IllegalCatch")
22     public AbstractMockedModule(final DynamicMBeanWithInstance old, final ModuleIdentifier id) {
23         if (old != null) {
24             instance = old.getInstance();
25         } else {
26             try {
27                 instance = prepareMockedInstance();
28             } catch (final Exception e) {
29                 throw new RuntimeException(e);
30             }
31         }
32
33         this.id = id == null ? new ModuleIdentifier(getClass().getCanonicalName(), "mock") : id;
34     }
35
36     @Override
37     public boolean canReuse(final Module oldModule) {
38         return instance != null;
39     }
40
41     @Override
42     public void validate() {
43     }
44
45     @Override
46     public AutoCloseable getInstance() {
47         return instance;
48     }
49
50     @Override
51     public ModuleIdentifier getIdentifier() {
52         return id;
53     }
54 }