Skip to content

Commit

Permalink
Add ResolverProviderTest unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouncheck committed Sep 9, 2024
1 parent fc38765 commit eb3faa2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.datastax.oss.driver.internal.core.resolver;

import com.datastax.oss.driver.internal.core.resolver.defaultResolver.DefaultResolverFactory;
import com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting;

/**
* Entry point for driver components to getting the {@link Resolver} instances. By default returns
Expand Down Expand Up @@ -45,4 +46,10 @@ public static synchronized void setDefaultResolverFactory(
alreadySet = true;
defaultResolverFactoryImpl = resolverFactoryImpl;
}

@VisibleForTesting
static synchronized void resetBooleansForTesting() {
alreadyInUse = false;
alreadySet = false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.datastax.oss.driver.internal.core.resolver;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import com.datastax.oss.driver.internal.core.resolver.mockResolver.MockResolverFactory;
import org.junit.Before;
import org.junit.Test;

public class ResolverProviderTest {

@Before
public void resetState() {
ResolverProvider.resetBooleansForTesting();
}

@Test(expected = IllegalStateException.class)
public void should_not_allow_setting_twice() {
try {
MockResolverFactory first = new MockResolverFactory();
MockResolverFactory second = new MockResolverFactory();
ResolverProvider.setDefaultResolverFactory(first);
ResolverProvider.setDefaultResolverFactory(second);
} catch (IllegalStateException ex) {
assertThat(ex.getMessage())
.isEqualTo(
"Cannot change default resolver factory: this method has already been called. You can set "
+ "default resolver factory only once.");
throw ex;
}
}

@Test(expected = IllegalStateException.class)
public void should_not_allow_setting_once_in_use() {
try {
ResolverProvider.getResolver(ResolverProviderTest.class);
MockResolverFactory first = new MockResolverFactory();
ResolverProvider.setDefaultResolverFactory(first);
} catch (IllegalStateException ex) {
assertThat(ex.getMessage())
.isEqualTo(
"Cannot change default resolver factory: ResolverProvider has already returned an instance of a "
+ "Resolver to use. Default resolver factory needs to be set up before first use by any class.");
throw ex;
}
}

@Test
public void should_allow_setting_once() {
MockResolverFactory first = new MockResolverFactory();
ResolverProvider.setDefaultResolverFactory(first);
}
}

0 comments on commit eb3faa2

Please sign in to comment.