<commit_before>/*
 * jlib - Open Source Java Library
 *
 *     www.jlib.org
 *
 *
 *     Copyright 2005-2015 <NAME>
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 */

package org.jlib.basefunctions.apachecommons.tostring;

import org.apache.commons.lang3.builder.ToStringStyle;

import org.jlib.core.classinstance.ClassInstanceService;
import org.jlib.core.classinstance.ClassInstantiationException;

import static java.lang.String.format;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class IdentifierOrClassNameToStringStyleSupplierTest {

    public static final String STYLE_ID = "MY_STYLE";
    @SuppressWarnings("serial")
    private static final ToStringStyle STYLE = new ToStringStyle() {};
    private static final String CLASS_NAME = "org.jlib.i.do.not.Exist";

    private IdentifierOrClassNameToStringStyleSupplier configurableSupplier;

    @Mock
    private NamedToStringStyleSupplier namedToStringStyleSupplier;

    @Mock
    private ClassInstanceService instanceService;

    @Before
    public void initializeStyleSupplier() {
        configurableSupplier = new IdentifierOrClassNameToStringStyleSupplier();
        configurableSupplier.setNamedStyleSupplier(namedToStringStyleSupplier);
        configurableSupplier.setInstanceService(instanceService);
    }

    @Test
    public void styleOfMappedIdentifierShouldBeReturned() {

        // given
        when(namedToStringStyleSupplier.get(STYLE_ID)).thenReturn(of(STYLE));

        // when
        configurableSupplier.setIdentifierOrClassName(STYLE_ID);
        final ToStringStyle style = configurableSupplier.get();

        // then
        verify(namedToStringStyleSupplier).get(STYLE_ID);
        verifyNoMoreInteractions(namedToStringStyleSupplier);

        verifyNoMoreInteractions(instanceService);

        assertThat(style).isSameAs(STYLE);
    }

    @Test
    public void instantiatedStyleOfClassNameShouldBeRetrurned()
    throws Exception {

        // given
        when(namedToStringStyleSupplier.get(CLASS_NAME)).thenReturn(empty());
        when(instanceService.instanceOf(CLASS_NAME, ToStringStyle.class)).thenReturn(STYLE);

        // when
        configurableSupplier.setIdentifierOrClassName(CLASS_NAME);
        final ToStringStyle style = configurableSupplier.get();

        // then
        verify(namedToStringStyleSupplier).get(CLASS_NAME);
        verifyNoMoreInteractions(namedToStringStyleSupplier);

        verify(instanceService).instanceOf(CLASS_NAME, ToStringStyle.class);
        verifyNoMoreInteractions(instanceService);

        assertThat(style).isSameAs(STYLE);
    }

    @Test
    public void notInstantiatableClassNameShouldFailMappingAndThrowException()
    throws Exception {

        try {<commit_msg>suppressing implicit varargs array creation warning<commit_after>/*
 * jlib - Open Source Java Library
 *
 *     www.jlib.org
 *
 *
 *     Copyright 2005-2015 <NAME>
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 */

package org.jlib.basefunctions.apachecommons.tostring;

import org.apache.commons.lang3.builder.ToStringStyle;

import org.jlib.core.classinstance.ClassInstanceService;
import org.jlib.core.classinstance.ClassInstantiationException;

import static java.lang.String.format;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class IdentifierOrClassNameToStringStyleSupplierTest {

    public static final String STYLE_ID = "MY_STYLE";
    @SuppressWarnings("serial")
    private static final ToStringStyle STYLE = new ToStringStyle() {};
    private static final String CLASS_NAME = "org.jlib.i.do.not.Exist";

    private IdentifierOrClassNameToStringStyleSupplier configurableSupplier;

    @Mock
    private NamedToStringStyleSupplier namedToStringStyleSupplier;

    @Mock
    private ClassInstanceService instanceService;

    @Before
    public void initializeStyleSupplier() {
        configurableSupplier = new IdentifierOrClassNameToStringStyleSupplier();
        configurableSupplier.setNamedStyleSupplier(namedToStringStyleSupplier);
        configurableSupplier.setInstanceService(instanceService);
    }

    @Test
    public void styleOfMappedIdentifierShouldBeReturned() {

        // given
        when(namedToStringStyleSupplier.get(STYLE_ID)).thenReturn(of(STYLE));

        // when
        configurableSupplier.setIdentifierOrClassName(STYLE_ID);
        final ToStringStyle style = configurableSupplier.get();

        // then
        verify(namedToStringStyleSupplier).get(STYLE_ID);
        verifyNoMoreInteractions(namedToStringStyleSupplier);

        verifyNoMoreInteractions(instanceService);

        assertThat(style).isSameAs(STYLE);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void instantiatedStyleOfClassNameShouldBeRetrurned()
    throws Exception {

        // given
        when(namedToStringStyleSupplier.get(CLASS_NAME)).thenReturn(empty());
        when(instanceService.instanceOf(CLASS_NAME, ToStringStyle.class)).thenReturn(STYLE);

        // when
        configurableSupplier.setIdentifierOrClassName(CLASS_NAME);
        final ToStringStyle style = configurableSupplier.get();

        // then
        verify(namedToStringStyleSupplier).get(CLASS_NAME);
        verifyNoMoreInteractions(namedToStringStyleSupplier);

        verify(instanceService).instanceOf(CLASS_NAME, ToStringStyle.class);
        verifyNoMoreInteractions(instanceService);

        assertThat(style).isSameAs(STYLE);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void notInstantiatableClassNameShouldFailMappingAndThrowException()
    throws Exception {

        try {
{'fraction_non_alphanumeric': 0.08085330524097972, 'fraction_numerical': 0.003423755596523571, 'mean_word_length': 3.9382314694408325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '147073', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2031, 'n_words': 449}