Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Commit

Permalink
Add ClassPathScanner tests
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt authored and vorburger committed Oct 10, 2018
1 parent 08fe84d commit 01213f8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright © 2018 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.infrautils.inject.tests;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mock;

import com.google.inject.Binder;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.binder.ScopedBindingBuilder;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.opendaylight.infrautils.inject.ClassPathScanner;

public class ClassPathScannerTest {
private final TestBinder testBinder = mock(TestBinder.class, Mockito.CALLS_REAL_METHODS);

@Before
public void setup() {
new ClassPathScanner("org.opendaylight.infrautils.inject.tests").bind(testBinder,
ClassPathScannerTestTopInterface.class);
}

@Test
public void verifyImplementationBinding() {
assertEquals(ClassPathScannerTestImplementation.class,
testBinder.getImplementation(ClassPathScannerTestTopInterface.class));
}

private abstract static class TestBinder implements Binder {
private Map<Class<?>, Class<?>> bindings;

private <T> void storeBinding(Class<T> type, Class<? extends T> implementation) {
if (bindings == null) {
bindings = new HashMap<>();
}
bindings.put(type, implementation);
}

<T> Class<? extends T> getImplementation(Class<T> type) {
return (Class<? extends T>) bindings.get(type);
}

@Override
public <T> AnnotatedBindingBuilder<T> bind(Class<T> type) {
TestAnnotatedBindingBuilder builder = mock(TestAnnotatedBindingBuilder.class, CALLS_REAL_METHODS);
builder.setType(type);
builder.setBinder(this);
return builder;
}

private abstract static class TestAnnotatedBindingBuilder<T> implements AnnotatedBindingBuilder<T> {
private Class<T> type;
private TestBinder binder;

@Nullable
@Override
public ScopedBindingBuilder to(Class<? extends T> implementation) {
binder.storeBinding(type, implementation);
return null;
}

void setType(Class<T> type) {
this.type = type;
}

void setBinder(TestBinder binder) {
this.binder = binder;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright © 2018 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.infrautils.inject.tests;

import javax.inject.Singleton;

@Singleton
public class ClassPathScannerTestImplementation implements ClassPathScannerTestTopInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright © 2018 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.infrautils.inject.tests;

public interface ClassPathScannerTestTopInterface {
}

0 comments on commit 01213f8

Please sign in to comment.