-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
1,846 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/JavaVersionSwitcher.Tests/ConfigurationServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/JavaVersionSwitcher.Tests/Fixtures/ConfigurationServiceFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
*/ | ||
} | ||
} | ||
} |
Oops, something went wrong.