Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tests-for-constructor-classes #1803

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions core/test/com/google/inject/internal/ConstructionContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.google.inject.internal;

import org.junit.rules.Timeout;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.hamcrest.Matchers.is;

public class ConstructionContextTest {

@Rule()
public Timeout timeoutRule = Timeout.seconds(5);

private final InjectorImpl.InjectorOptions injectorImplInjectorOptionsMock = mock(InjectorImpl.InjectorOptions.class);

@Rule()
public ExpectedException thrown = ExpectedException.none();

//Sapient generated method id: ${fd29cd0f-f5b1-30b4-8cda-768672f68aba}, hash: E66F5CF26FE199A2F2B2C170E4932242
@Test()
public void removeCurrentReferenceTest() {
//Arrange Statement(s)
ConstructionContext<Object> target = new ConstructionContext<>();
//Act Statement(s)
target.removeCurrentReference();
//Assert statement(s)
assertThat(target.getCurrentReference(), is(nullValue()));
}

//Sapient generated method id: ${5af195be-742d-303b-b6b2-671a9ad7deb4}, hash: F27F08A7B477319B751779BD074389FA
@Test()
public void startConstructionTest() {
//Arrange Statement(s)
ConstructionContext<Object> target = new ConstructionContext<>();
//Act Statement(s)
target.startConstruction();
//Assert statement(s)
assertThat(target.isConstructing(), equalTo(Boolean.TRUE));
}

//Sapient generated method id: ${28013cdd-b521-384f-a2bb-6f2e5d85212e}, hash: DE26EFA9360DD0B6BB3AD7E932E207A3
@Test()
public void finishConstructionTest() {
//Arrange Statement(s)
ConstructionContext<Object> target = new ConstructionContext<>();
//Act Statement(s)
target.finishConstruction();
//Assert statement(s)
assertThat(target.isConstructing(), equalTo(Boolean.FALSE));
}

//Sapient generated method id: ${55d4b4e7-0019-356b-b29b-19c11b0be872}, hash: E622E7100DD2C222C169C3EC6E03DB04
@Test()
public void setProxyDelegatesWhenInvocationHandlersIsNull() {
/* Branches:
* (invocationHandlers != null) : false
*/
//Arrange Statement(s)
ConstructionContext<Object> target = new ConstructionContext<>();
Object object = new Object();
//Act Statement(s)
target.setProxyDelegates(object);
}
}
172 changes: 172 additions & 0 deletions core/test/com/google/inject/internal/ConstructorInjectorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package com.google.inject.internal;

import org.junit.rules.Timeout;
import org.junit.Rule;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;

import org.junit.rules.ExpectedException;

import java.util.HashSet;

import com.google.inject.Key;

import java.lang.reflect.Constructor;

import com.google.inject.TypeLiteral;

import java.util.Set;

import com.google.inject.spi.Dependency;
import org.mockito.MockedStatic;
import com.google.inject.spi.InjectionPoint;

import static org.mockito.Mockito.doNothing;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mockStatic;

public class ConstructorInjectorTest {

@Rule()
public Timeout timeoutRule = Timeout.seconds(5);

private final ConstructionProxy<java.lang.Object> constructionProxyMock = mock(ConstructionProxy.class, "ConstructionProxy<Object> constructionProxy");

private final ConstructionContext<java.lang.Object> constructionContextMock = mock(ConstructionContext.class);

private final Constructor constructorMock = mock(Constructor.class);

private final InternalContext contextMock = mock(InternalContext.class);

private final Dependency<?> dependencyMock = mock(Dependency.class);

private final InjectorImpl.InjectorOptions injectorImplInjectorOptionsMock = mock(InjectorImpl.InjectorOptions.class);

private final Key keyMock = mock(Key.class);

private final MembersInjectorImpl<java.lang.Object> membersInjectorImplMock = mock(MembersInjectorImpl.class);

private final ProvisionListenerStackCallback<java.lang.Object> provisionListenerStackCallbackMock = mock(ProvisionListenerStackCallback.class);

private final TypeLiteral typeLiteralMock = mock(TypeLiteral.class);

@Rule()
public ExpectedException thrown = ExpectedException.none();

//Sapient generated method id: ${49f22cac-3604-3497-90db-d26edb0e7397}, hash: DCD02567C05D90BE46F869ECA4B0F33A
@Test()
public void constructWhenConstructionContextIsConstructing() throws InternalProvisionException, InvocationTargetException {
/* Branches:
* (constructionContext.isConstructing()) : true
*/
//Arrange Statement(s)
doReturn(injectorImplInjectorOptionsMock).when(contextMock).getInjectorOptions();
doReturn(typeLiteralMock).when(keyMock).getTypeLiteral();
doReturn(Object.class).when(typeLiteralMock).getRawType();
Set<InjectionPoint> injectionPointSet = new HashSet<>();
SingleParameterInjector<?>[] singleParameterInjectorArray = new SingleParameterInjector[]{};
ConstructorInjector<java.lang.Object> target = new ConstructorInjector(injectionPointSet, constructionProxyMock, singleParameterInjectorArray, membersInjectorImplMock);
doReturn(constructionContextMock).when(contextMock).getConstructionContext(target);
doReturn(true).when(constructionContextMock).isConstructing();
java.lang.Object object = new java.lang.Object();
doReturn(object).when(constructionContextMock).createProxy(injectorImplInjectorOptionsMock, java.lang.Object.class);
Dependency dependency = Dependency.get(keyMock);
//Act Statement(s)
java.lang.Object result = target.construct(contextMock, dependency, provisionListenerStackCallbackMock);
//Assert statement(s)
assertThat(result, equalTo(object));
verify(contextMock).getInjectorOptions();
verify(keyMock).getTypeLiteral();
verify(typeLiteralMock).getRawType();
verify(contextMock).getConstructionContext(target);
verify(constructionContextMock).isConstructing();
verify(constructionContextMock).createProxy(injectorImplInjectorOptionsMock, java.lang.Object.class);
}

//Sapient generated method id: ${4d517ada-89b7-3a9e-9289-6ee0971952e5}, hash: F273C0D0DD99D49493C6E61964D4030A
@Test()
public void constructWhenContextGetInjectorOptionsNotDisableCircularProxies() throws InternalProvisionException, InvocationTargetException {
/* Branches:
* (constructionContext.isConstructing()) : false
* (t != null) : true
* (context.getInjectorOptions().disableCircularProxies) : false
*/
//Arrange Statement(s)
doReturn(injectorImplInjectorOptionsMock).when(contextMock).getInjectorOptions();
Set<InjectionPoint> injectionPointSet = new HashSet<>();
SingleParameterInjector<?>[] singleParameterInjectorArray = new SingleParameterInjector[]{};
ConstructorInjector<java.lang.Object> target = new ConstructorInjector(injectionPointSet, constructionProxyMock, singleParameterInjectorArray, membersInjectorImplMock);
doReturn(constructionContextMock).when(contextMock).getConstructionContext(target);
doReturn(false).when(constructionContextMock).isConstructing();
java.lang.Object object = new java.lang.Object();
doReturn(object).when(constructionContextMock).getCurrentReference();
//Act Statement(s)
java.lang.Object result = target.construct(contextMock, dependencyMock, provisionListenerStackCallbackMock);
//Assert statement(s)
assertThat(result, equalTo(object));
verify(contextMock).getInjectorOptions();
verify(contextMock).getConstructionContext(target);
verify(constructionContextMock).isConstructing();
verify(constructionContextMock).getCurrentReference();
}

//Sapient generated method id: ${5518bfd1-919d-3a21-9890-4da022deb59b}, hash: A540D7A50620E5161323DA156AC70484
@Test()
public void constructWhenProvisionCallbackIsNull() throws InternalProvisionException, InvocationTargetException {
/* Branches:
* (constructionContext.isConstructing()) : false
* (t != null) : false
* (provisionCallback == null) : true
*/
//Arrange Statement(s)
MembersInjectorImpl<java.lang.Object> localMembersInjectorMock = mock(MembersInjectorImpl.class);
InternalContext internalContextMock = mock(InternalContext.class);
try (MockedStatic<SingleParameterInjector> singleParameterInjector = mockStatic(SingleParameterInjector.class)) {
java.lang.Object object = new java.lang.Object();
doNothing().when(localMembersInjectorMock).injectMembers(object, internalContextMock, false);
doNothing().when(localMembersInjectorMock).notifyListeners(object);
java.lang.Object[] objectArray = new java.lang.Object[]{};
SingleParameterInjector<?>[] singleParameterInjectorArray = new SingleParameterInjector[]{};
singleParameterInjector.when(() -> SingleParameterInjector.getAll(internalContextMock, singleParameterInjectorArray)).thenReturn(objectArray);
Set<InjectionPoint> injectionPointSet = new HashSet<>();
ConstructorInjector<java.lang.Object> target = new ConstructorInjector(injectionPointSet, constructionProxyMock, singleParameterInjectorArray, localMembersInjectorMock);
doReturn(object).when(constructionProxyMock).newInstance(objectArray);
doReturn(constructionContextMock).when(internalContextMock).getConstructionContext(target);
doReturn(false).when(constructionContextMock).isConstructing();
doReturn(null).when(constructionContextMock).getCurrentReference();
doNothing().when(constructionContextMock).startConstruction();
doNothing().when(constructionContextMock).setProxyDelegates(object);
doNothing().when(constructionContextMock).finishConstruction();
doNothing().when(constructionContextMock).setCurrentReference(object);
doNothing().when(constructionContextMock).removeCurrentReference();
ProvisionListenerStackCallback<java.lang.Object> provisionListenerStackCallback = null;
//Act Statement(s)
java.lang.Object result = target.construct(internalContextMock, dependencyMock, provisionListenerStackCallback);
//Assert statement(s)
assertThat(result, equalTo(object));
verify(localMembersInjectorMock).injectMembers(object, internalContextMock, false);
verify(localMembersInjectorMock).notifyListeners(object);
singleParameterInjector.verify(() -> SingleParameterInjector.getAll(internalContextMock, singleParameterInjectorArray), atLeast(1));
verify(constructionProxyMock).newInstance(objectArray);
verify(internalContextMock).getConstructionContext(target);
verify(constructionContextMock).isConstructing();
verify(constructionContextMock).getCurrentReference();
verify(constructionContextMock).startConstruction();
verify(constructionContextMock).setProxyDelegates(object);
verify(constructionContextMock, times(2)).finishConstruction();
verify(constructionContextMock).setCurrentReference(object);
verify(constructionContextMock).removeCurrentReference();
}
}
}
32 changes: 32 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,38 @@ See the Apache License Version 2.0 for the specific language governing permissio
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>

<!-- byte-buddy is an optional dependency. Mockito brings it, but an old version can cause problems.
We need at least 1.12.9 -->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.14.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>


</dependencies>

<build>
Expand Down