Skip to content
Merged
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
2 changes: 0 additions & 2 deletions recipe.cake
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ BuildParameters.SetParameters(
shouldRunDotNetCorePack: true,
preferredBuildProviderType: BuildProviderType.GitHubActions,
twitterMessage: standardNotificationMessage,
shouldRunCoveralls: false, // no tests, currently
shouldRunCodecov: false,
shouldRunIntegrationTests: false);

BuildParameters.PrintParameters(Context);
Expand Down
124 changes: 124 additions & 0 deletions src/JavaVersionSwitcher.Tests/ConfigurationServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JavaVersionSwitcher.Tests.Fixtures;
using Shouldly;
using Xunit;

namespace JavaVersionSwitcher.Tests
{
public class ConfigurationServiceTests
{
[Fact]
public async Task SetConfiguration_throws_on_wrong_provider()
{
// arrange
using var fixture = new ConfigurationServiceFixture();
const string providerName = "non-existent-provider";

// act
// ReSharper disable once AccessToDisposedClosure
async Task Act() => await fixture.Service.SetConfiguration(providerName, null, null);

// assert
(await Should.ThrowAsync<KeyNotFoundException>((Func<Task>)Act))
.Message
.ShouldSatisfyAllConditions(
m => m.ShouldStartWith("No ConfigurationProvider"),
m => m.ShouldContain(providerName));
}

[Fact]
public async Task SetConfiguration_throws_on_wrong_setting()
{
// arrange
const string providerName = "provider";
using var fixture = new ConfigurationServiceFixture();
fixture.WithConfigurationProvider(providerName);
const string setting = "non-existent-setting";

// act'
// ReSharper disable once AccessToDisposedClosure
async Task Act() => await fixture.Service.SetConfiguration(providerName, setting, null);

// assert
(await Should.ThrowAsync<KeyNotFoundException>((Func<Task>)Act))
.Message
.ShouldSatisfyAllConditions(
m => m.ShouldStartWith("No Configuration with the name"),
m => m.ShouldContain(setting));
}

[Fact]
public async Task SetConfiguration_writes_value_to_xml()
{
// arrange
const string providerName = "pName";
const string settingsName = "settingsName";
const string value = "a value";
using var fixture = new ConfigurationServiceFixture();
fixture.WithConfigurationProvider(providerName, settingsName);

// act'
await fixture.Service.SetConfiguration(providerName, settingsName, value);

// assert
var xml = fixture.ReadXml(providerName, settingsName);
xml.Value.ShouldBe(value);
}

[Fact]
public async Task GetConfiguration_returns_empty_for_not_set_setting()
{
// arrange
const string providerName = "pName";
const string settingsName = "settingsName";
using var fixture = new ConfigurationServiceFixture();
fixture.WithConfigurationProvider(providerName, settingsName);

// act'
var actual = await fixture.Service.GetConfiguration(providerName, settingsName);

// assert
actual.ShouldBe(string.Empty);
}

[Fact]
public async Task GetConfiguration_returns_the_value_from_xml()
{
// arrange
const string providerName = "pName";
const string settingsName = "settingsName";
const string expected = "some value";
using var fixture = new ConfigurationServiceFixture();
fixture.WithConfigurationProvider(providerName, settingsName);
fixture.EnsureSetting(providerName, settingsName, expected);

// act'
var actual = await fixture.Service.GetConfiguration(providerName, settingsName);

// assert
actual.ShouldBe(expected);
}

[Fact]
public async Task SetConfiguration_removes_empty_settings()
{
// arrange
const string providerName = "pName";
const string settingsName = "settingsName";
using var fixture = new ConfigurationServiceFixture();
fixture.WithConfigurationProvider(providerName, settingsName);
fixture.EnsureSetting(providerName, settingsName, "some value");

// act'
await fixture.Service.SetConfiguration(providerName, settingsName, null);

// assert
var xml = fixture.ReadXml();
xml.Parent.ShouldBeNull("this should be the root node.");
xml.Elements().Count().ShouldBe(0);
}
}
}
58 changes: 58 additions & 0 deletions src/JavaVersionSwitcher.Tests/Fixtures/CommandFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using JavaVersionSwitcher.Adapters;
using JavaVersionSwitcher.Logging;
using JavaVersionSwitcher.Services;
using JavaVersionSwitcher.Tests.TestImplementations;
using Moq;
using SimpleInjector;
using Spectre.Console;
using Spectre.Console.Cli;
using Spectre.Console.Testing;

namespace JavaVersionSwitcher.Tests.Fixtures
{
public class CommandFixture
{
public TestConsole Console => new TestConsole();

public Logger Logger => new Logger();

public TestConfigurationService ConfigurationService => new TestConfigurationService();

public Mock<IJavaHomeAdapter> JavaHomeAdapter => new Mock<IJavaHomeAdapter>();

public Mock<IPathAdapter> PathAdapter => new Mock<IPathAdapter>();

public Mock<IJavaInstallationsAdapter> JavaInstallationsAdapter => new Mock<IJavaInstallationsAdapter>();

private ITypeRegistrar BuildRegistrar()
{
var container = new Container();
container.RegisterInstance<ILogger>(Logger);
container.RegisterInstance<IConfigurationService>(ConfigurationService);
container.RegisterInstance(JavaHomeAdapter.Object);
container.RegisterInstance(PathAdapter.Object);
container.RegisterInstance(JavaInstallationsAdapter.Object);

container.Register<JavaInstallationsAdapterConfigurationProvider>(Lifestyle.Singleton);

container.Collection.Register<IConfigurationProvider>(
new[]
{
typeof(JavaInstallationsAdapterConfigurationProvider),
},
Lifestyle.Singleton);

return new SimpleInjectorRegistrar(container);
}

public int Run(params string[] args)
{
AnsiConsole.Console = Console;
var registrar = BuildRegistrar();
var app = new CommandApp(registrar);
app.Configure(Program.ConfigureApp);

return app.Run(args);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using JavaVersionSwitcher.Adapters;
using JavaVersionSwitcher.Services;
using JetBrains.Annotations;
using Moq;
using Shouldly;

namespace JavaVersionSwitcher.Tests.Fixtures
{
public class ConfigurationServiceFixture : IDisposable
{
private readonly List<IConfigurationProvider> _configurationProviders = new List<IConfigurationProvider>();
private readonly Mock<IStorageAdapter> _storageAdapter;
private readonly string _tmpFile;

public ConfigurationServiceFixture()
{
_tmpFile = Path.GetTempFileName()+".xml";
_storageAdapter = new Mock<IStorageAdapter>();
_storageAdapter.Setup(x => x.ConfigurationFilePath).Returns(_tmpFile);
}

public ConfigurationService Service => new ConfigurationService(_configurationProviders, _storageAdapter.Object);

public void WithConfigurationProvider(string providerName, params string[] settings)
{
var configurationProvider = new Mock<IConfigurationProvider>();
configurationProvider.Setup(x => x.ProviderName).Returns(providerName);
configurationProvider.Setup(x => x.Settings).Returns(settings);

_configurationProviders.Add(configurationProvider.Object);
}

public void EnsureSetting([NotNull]string providerName, [NotNull]string setting, string value)
{
var doc = new XDocument();
doc.Add(ReadXml());
var root = doc.Root;

var providerElm = root!.Elements(providerName).SingleOrDefault();
if (providerElm == null)
{
providerElm = new XElement(providerName);
root.Add(providerElm);
}

var settingElm = providerElm.Elements(setting).SingleOrDefault();
if (settingElm == null)
{
settingElm = new XElement(setting);
providerElm.Add(settingElm);
}

settingElm.Value = value;
doc.Save(_tmpFile);
}

public XElement ReadXml(string providerName = null, string setting = null)
{
if (!File.Exists(_tmpFile))
{
return new XElement("temp-settings");
}

var xml = XDocument.Load(_tmpFile);
if (providerName == null)
{
return xml.Root;
}

var providerElm = xml.Root!.Elements(providerName).SingleOrDefault();
providerElm.ShouldNotBeNull("a provider element should have been created.");
var settingElm = providerElm.Elements(setting).SingleOrDefault();
if (setting == null)
{
return providerElm;
}

settingElm.ShouldNotBeNull("a settings element should have been created.");
return settingElm;
}

public void Dispose()
{
GC.SuppressFinalize(this);
if (_tmpFile != null && File.Exists(_tmpFile))
{
File.Delete(_tmpFile);
}
}
}
}
20 changes: 20 additions & 0 deletions src/JavaVersionSwitcher.Tests/GetConfigCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Xunit;

namespace JavaVersionSwitcher.Tests
{
public class GetConfigCommandTests
{
[Fact]
public void Can_Set_CacheTimeout_Configuration()
{
/*
var fixture = new CommandFixture();

var result = fixture.Run("config", "set", "cache", "timeout", "12");

result.ShouldBe(0);
fixture.ConfigurationService.Configuration["cache"]["timeout"].ShouldBe("12");
*/
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="Shouldly" Version="4.0.3" />
<PackageReference Include="Spectre.Console.Testing" Version="0.40.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
45 changes: 45 additions & 0 deletions src/JavaVersionSwitcher.Tests/LoggerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using JavaVersionSwitcher.Logging;
using Shouldly;
using Spectre.Console;
using Spectre.Console.Testing;
using Xunit;

namespace JavaVersionSwitcher.Tests
{
public class LoggerTests
{
private readonly TestConsole _console = new TestConsole();
private readonly ILogger _logger = new Logger();

public LoggerTests()
{
AnsiConsole.Console = _console;
}

[Fact]
public void Writes_warning_with_prefix()
{
_logger.LogWarning("test");

_console.Output.ShouldStartWith("WARNING:");
}

[Fact]
public void Writes_verbose_when_verbose_is_set()
{
_logger.PrintVerbose = true;
_logger.LogVerbose("test");

_console.Output.ShouldBe("test\n");
}

[Fact]
public void Writes_nothing_when_verbose_is_not_set()
{
_logger.PrintVerbose = false;
_logger.LogVerbose("test");

_console.Output.ShouldBe(string.Empty);
}
}
}
4 changes: 1 addition & 3 deletions src/JavaVersionSwitcher.Tests/PathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
using System.Linq;
using System.Threading.Tasks;
using JavaVersionSwitcher.Adapters;
using JavaVersionSwitcher.Logging;
using Moq;
using Shouldly;
using Xunit;

namespace JavaVersionSwitcher.Tests
{
public class PathTests
{
private readonly PathAdapter _adapter = new PathAdapter(new Mock<ILogger>().Object);
private readonly PathAdapter _adapter = new PathAdapter();

[Fact]
public async Task Can_Set_per_process()
Expand Down
Loading