<commit_before>﻿using System;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Xml.Linq;
using Common.Logging;
using HealthCheck.Framework;
using Moq;
using Xunit;

namespace HealthCheck
{
    [SuppressMessage(
         "StyleCop.CSharp.DocumentationRules",
         "SA1600:ElementsMustBeDocumented",
         Justification = "Test Suites do not need XML Documentation.")]
    public class ComponentFactoryTests
    {
        public ComponentFactoryTests()
        {
            // Need to copy the Unit Test DLL so that MEF can find the exports below

            if (!Directory.Exists("plugins"))
            {
                Directory.CreateDirectory("plugins");
            }

            File.Copy("UnitTests.dll", "plugins\\TestPlugin.dll", true);
        }

        [Fact]
        public void GetPlugin_Should_BeAbleToInstantiateTwoNonSharedInstances()
        {
            // Arrange
            var factory = new ComponentFactory();

            // Act
            var plugin1 = factory.GetPlugin("ContractName1");
            var plugin2 = factory.GetPlugin("ContractName1");

            plugin1.Name = "Plugin1";
            plugin2.Name = "Plugin2";
<commit_msg>Added unit test for listeners<commit_after>﻿using System;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Xml.Linq;
using Common.Logging;
using HealthCheck.Framework;
using Moq;
using Xunit;

namespace HealthCheck
{
    [SuppressMessage(
         "StyleCop.CSharp.DocumentationRules",
         "SA1600:ElementsMustBeDocumented",
         Justification = "Test Suites do not need XML Documentation.")]
    public class ComponentFactoryTests
    {
        public ComponentFactoryTests()
        {
            // Need to copy the Unit Test DLL so that MEF can find the exports below

            if (!Directory.Exists("plugins"))
            {
                Directory.CreateDirectory("plugins");
            }

            File.Copy("UnitTests.dll", "plugins\\TestPlugin.dll", true);
        }

        [Fact]
        public void GetListener_Should_BeAbleToInstantiateTwoNonSharedInstances()
        {
            // Arrange
            var factory = new ComponentFactory();

            // Act
            var listener1 = factory.GetListener("Listen1");
            var listener2 = factory.GetListener("Listen1");

            listener1.Threshold = CheckResult.Success;
            listener2.Threshold = CheckResult.Error;

            // Assert
            Assert.NotEqual(listener1.Threshold, listener2.Threshold);
        }

        [Fact]
        public void GetListener_Should_BeAbleToInstantiateTwoSharedInstances()
        {
            // Arrange
            var factory = new ComponentFactory();

            // Act
            var listener1 = factory.GetListener("Listen2");
            var listener2 = factory.GetListener("Listen2");

            // Assert
            Assert.Same(listener1, listener2);
        }

        [Fact]
        public void GetListener_Should_InstantiateTheNamedPlugin_When_AskedByContractName()
        {
            // Arrange
            var factory = new ComponentFactory();

            // Act
            var listener = factory.GetListener("Listen1");

            // Assert
            Assert.IsType<DummyListener1>(listener);
        }

        [Fact]
        public void GetListener_Should_LoadNamedInstance_When_SpecifyingByName()
        {
            // Arrange
            var factory = new ComponentFactory();

            // Act
            var listener1 = factory.GetListener("Listen1");
            var listener2 = factory.GetListener("Listen2");

            // Assert
            Assert.IsType<DummyListener1>(listener1);
            Assert.IsType<DummyListener2>(listener2);
        }

        [Fact]
        public void GetListener_Should_ReturnNull_When_ListenerTypeDoesNotExist()
        {
            // Arrange
            var factory = new ComponentFactory();

            // Act
            var listener = factory.GetListener("NonExistentListener");

            // Assert
            Assert.Null(listener);
        }

        [Fact]
        public void GetListener_Should_ThrowInvalidArgumentException_When_TypeNameIsEmpty()
        {
            // Arrange
            var factory = new ComponentFactory();

            // Act & Assert
            Assert.Throws<ArgumentNullException>(() =>
            {
                var listener = factory.GetListener(String.Empty);
            });
        }

        [Fact]
        public void GetPlugin_Should_BeAbleToInstantiateTwoNonSharedInstances()
        {
            // Arrange
            var factory = new ComponentFactory();

            // Act
            var plugin1 = factory.GetPlugin("ContractName1");
            var plugin2 = factory.GetPlugin("ContractName1");

            plugin1.Name = "Plugin1";
            plugin2.Name = "Plugin2";

{'fraction_non_alphanumeric': 0.08843674456083804, 'fraction_numerical': 0.009669621273166801, 'mean_word_length': 1.9240282685512367, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4534034', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1209, 'n_words': 264}