<commit_before>/**
 * Copyright (C) 2011 Akiban Technologies Inc.
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 */

package com.akiban.server.service.servicemanager;

import org.junit.Test;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>Tests the startup of various services. Here's the dependency graph:
 * <pre>
 *        A   <---+
 *      ↙   ↘     ¦
 *     B     C    ¦
 *  ↙  ↓     ↓    ¦
 * E   D     ↓    ¦
 *  ↖   ↘   ↙     ¦
 *    ← ← F - - - +
 * </pre>
 * </p>
 * <p>The dotted line from F to A represents a circular dependency. We don't allow this, so most tests use an
 * implementation of F that does <em>not</em> depend on A. The one that does, we're looking for an exception.</p>
 */
public final class GuicerDITest {

    @Test
    public void nonCircularFromA() {
        standardTester()
                .startAndStop(A.class)
                .check(AImpl.class, BImpl.class, CImpl.class, EImpl.class, DImpl.class, FImpl.class)
                .checkDependencies(AImpl.class, BImpl.class, DImpl.class, CImpl.class, FImpl.class, EImpl.class)
                .checkDependencies(BImpl.class, DImpl.class, FImpl.class, EImpl.class)
                .checkDependencies(CImpl.class, FImpl.class, EImpl.class)
                .checkDependencies(DImpl.class, FImpl.class, EImpl.class)
                .checkDependencies(EImpl.class)
                .checkDependencies(FImpl.class, EImpl.class)
        ;
    }
    
    @Test
    public void nonCircularFromF() {
        standardTester()
                .startAndStop(F.class)
                .check(EImpl.class, FImpl.class)
                .checkDependencies(EImpl.class)
                .checkDependencies(FImpl.class, EImpl.class)
        ;
    }


    @Test
    public void nonCircularFromE() {
        standardTester()
                .startAndStop(E.class)
                .check(EImpl.class)
        ;
    }

    @Test(expected = CircularDependencyException.class)
    public void circular() {
        standardTester()
                .bind(F.class, CircularF.class)
                .check()
                .startAndStop(F.class);
    }
    
    @Test
    public void fieldInjection() {
        new GuiceInjectionTester()
                .bind(FieldInjectionA.class, FieldInjectionAImpl.class)
                .bind(FieldInjectionB.class, FieldInjectionBImpl.class)
                .startAndStop(FieldInjectionA.class)
                .check(FieldInjectionAImpl.class, FieldInjectionBImpl.class)
                .checkDependencies(FieldInjectionAImpl.class, FieldInjectionBImpl.class);
    }

    @Test
    public void methodInjection() {
        new GuiceInjectionTester()
                .bind(MethodInjectionA.class, MethodInjectionAImpl.class)
                .bind(MethodInjectionB.class, MethodInjectionBImpl.class)
                .startAndStop(MethodInjectionA.class)
                .check(MethodInjectionAImpl.class, MethodInjectionBImpl.class)
                .checkDependencies(MethodInjectionAImpl.class, MethodInjectionBImpl.class);
    }

    private static GuiceInjectionTester standardTester() {
        return new GuiceInjectionTester()<commit_msg>Modifying test slightly, so that we verify that a CircularDependencyException prevents any services from being started/stopped<commit_after>/**
 * Copyright (C) 2011 <NAME> Inc.
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 */

package com.akiban.server.service.servicemanager;

import org.junit.Test;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.fail;

/**
 * <p>Tests the startup of various services. Here's the dependency graph:
 * <pre>
 *        A   <---+
 *      ↙   ↘     ¦
 *     B     C    ¦
 *  ↙  ↓     ↓    ¦
 * E   D     ↓    ¦
 *  ↖   ↘   ↙     ¦
 *    ← ← F - - - +
 * </pre>
 * </p>
 * <p>The dotted line from F to A represents a circular dependency. We don't allow this, so most tests use an
 * implementation of F that does <em>not</em> depend on A. The one that does, we're looking for an exception.</p>
 */
public final class GuicerDITest {

    @Test
    public void nonCircularFromA() {
        standardTester()
                .startAndStop(A.class)
                .check(AImpl.class, BImpl.class, CImpl.class, EImpl.class, DImpl.class, FImpl.class)
                .checkDependencies(AImpl.class, BImpl.class, DImpl.class, CImpl.class, FImpl.class, EImpl.class)
                .checkDependencies(BImpl.class, DImpl.class, FImpl.class, EImpl.class)
                .checkDependencies(CImpl.class, FImpl.class, EImpl.class)
                .checkDependencies(DImpl.class, FImpl.class, EImpl.class)
                .checkDependencies(EImpl.class)
                .checkDependencies(FImpl.class, EImpl.class)
        ;
    }
    
    @Test
    public void nonCircularFromF() {
        standardTester()
                .startAndStop(F.class)
                .check(EImpl.class, FImpl.class)
                .checkDependencies(EImpl.class)
                .checkDependencies(FImpl.class, EImpl.class)
        ;
    }


    @Test
    public void nonCircularFromE() {
        standardTester()
                .startAndStop(E.class)
                .check(EImpl.class)
        ;
    }

    @Test(expected = CircularDependencyException.class)
    public void circular() {
        GuiceInjectionTester tester = standardTester().bind(F.class, CircularF.class).check();
        try {
            tester.startAndStop(F.class);
            fail("expected a CircularDependencyException");
        } catch (CircularDependencyException e) {
            tester.check();
            throw e;
        }
    }
    
    @Test
    public void fieldInjection() {
        new GuiceInjectionTester()
                .bind(FieldInjectionA.class, FieldInjectionAImpl.class)
                .bind(FieldInjectionB.class, FieldInjectionBImpl.class)
                .startAndStop(FieldInjectionA.class)
                .check(FieldInjectionAImpl.class, FieldInjectionBImpl.class)
                .checkDependencies(FieldInjectionAImpl.class, FieldInjectionBImpl.class);
    }

    @Test
    public void methodInjection() {
        new GuiceInjectionTester()
                .bind(MethodInjectionA.class, MethodInjectionAImpl.class)
                .bind(MethodInjectionB.class, MethodInjectionBImpl.class)
                .startAndStop(MethodInjectionA.class)
                .check(MethodInjectionAImpl.class, MethodInjectionBImpl.class)
                .checkDependencies(MethodInjectionAImpl.class, MethodInjectionBImpl.class);
    }

    private static GuiceInjectionTester standardTester() {
        return new GuiceInjectionTester()
{'fraction_non_alphanumeric': 0.09441183975503956, 'fraction_numerical': 0.0012758356723653994, 'mean_word_length': 2.2950819672131146, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2278518', 'n_tokens_mistral': 2328, 'n_tokens_neox': 2229, 'n_words': 592}