Mercurial > silverbladetech
changeset 43:aef06698d9e2 Test
Added the library tests
line wrap: on
line diff
--- a/.hgignore Tue Apr 03 23:32:40 2012 +0100 +++ b/.hgignore Wed Apr 04 19:20:20 2012 +0100 @@ -5,4 +5,6 @@ *.csproj.user */obj/* */_ReSharper* -Stocks/packages/* +*.DotSettings.user +*/packages/* +*.stats
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/.nuget/NuGet.Config Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<configuration> + <solution> + <add key="disableSourceControlIntegration" value="true" /> + </solution> +</configuration> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/.nuget/NuGet.targets Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,71 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir> + + <!-- Windows specific commands --> + <NuGetToolsPath Condition=" '$(OS)' == 'Windows_NT'">$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath> + <PackagesConfig Condition=" '$(OS)' == 'Windows_NT'">$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig> + <PackagesDir Condition=" '$(OS)' == 'Windows_NT'">$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir> + + <!-- We need to launch nuget.exe with the mono command if we're not on windows --> + <NuGetToolsPath Condition=" '$(OS)' != 'Windows_NT'">$(SolutionDir).nuget</NuGetToolsPath> + <PackagesConfig Condition=" '$(OS)' != 'Windows_NT' ">packages.config</PackagesConfig> + <PackagesDir Condition=" '$(OS)' != 'Windows_NT'">$(SolutionDir)packages</PackagesDir> + + <!-- NuGet command --> + <NuGetExePath>$(NuGetToolsPath)\nuget.exe</NuGetExePath> + <NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand> + <NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand> + + <PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir> + + <!-- Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config --> + <PackageSources>""</PackageSources> + + <!-- Enable the restore command to run before builds --> + <RestorePackages Condition="$(RestorePackages) == ''">false</RestorePackages> + + <!-- Property that enables building a package from a project --> + <BuildPackage Condition="$(BuildPackage) == ''">false</BuildPackage> + + <!-- Commands --> + <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source $(PackageSources) -o "$(PackagesDir)"</RestoreCommand> + <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand> + + <!-- Make the build depend on restore packages --> + <BuildDependsOn Condition="$(RestorePackages) == 'true'"> + RestorePackages; + $(BuildDependsOn); + </BuildDependsOn> + + <!-- Make the build depend on restore packages --> + <BuildDependsOn Condition="$(BuildPackage) == 'true'"> + $(BuildDependsOn); + BuildPackage; + </BuildDependsOn> + </PropertyGroup> + + <Target Name="CheckPrerequisites"> + <!-- Raise an error if we're unable to locate nuget.exe --> + <Error Condition="!Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" /> + </Target> + + <Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites"> + <Exec Command="$(RestoreCommand)" + Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" /> + + <Exec Command="$(RestoreCommand)" + LogStandardErrorAsError="true" + Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" /> + </Target> + + <Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites" Condition=" '$(OS)' != 'Windows_NT' "> + <Exec Command="$(BuildCommand)" + Condition=" '$(OS)' != 'Windows_NT' " /> + + <Exec Command="$(BuildCommand)" + LogStandardErrorAsError="true" + Condition=" '$(OS)' == 'Windows_NT' " /> + </Target> +</Project> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/BookTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,42 @@ +using System; +using LibrarySystem.Core; +using LibrarySystem.Interfaces; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + class BookTests + { + [Test] + public void Create_WithNullIdAndNullPublication_ShouldThrowArgumentNullException() + { + Assert.Throws<ArgumentNullException>(() => Book.Create(null, null)); + } + + [Test] + public void Create_WithNullIdAndValidPublication_ShouldThrowArgumentNullException() + { + var publisher = Publisher.Create("dummy"); + var publication = Publication.Create("dummy", "dummy", publisher, "dummy"); + Assert.Throws<ArgumentNullException>(() => Book.Create(null, publication)); + } + + [Test] + public void Create_WithNullPublication_ShouldThrowArgumentNullException() + { + var validId = "1"; + Assert.Throws<ArgumentNullException>(() => Book.Create(validId, null)); + } + + [Test] + public void Create_WithValidParametersPublication_ShouldReturnBook() + { + var publisher = Publisher.Create("dummy"); + var publication = Publication.Create("dummy","dummy", publisher, "dummy"); + var validId = "1"; + IBook book = Book.Create(validId, publication); + Assert.NotNull(book); + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/LibraryTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,374 @@ +using System; +using System.Collections.Generic; +using LibrarySystem.Core; +using LibrarySystem.Interfaces; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + public class LibraryTests + { + #region Factory methods to setup fixed scenario for testing + + // Factory that creates: + // A library object with 10 books (2 x Lord of the Flies, 3 x 1984, 5 x Brave New World) + private ILibrary CreateLibraryScenario() + { + #region Publisher + + IPublisher penguin = Publisher.Create("Penguin"); + + #endregion + + #region Publications + + IPublication lordOfTheFlies = Publication.Create("William Golding", "0140283331", penguin, "Lord of the Flies"); + + IPublication nineteenEightyFour = Publication.Create("George Orwell", "0452284236", penguin, "Nineteen Eighty-Four"); + + IPublication braveNewWorld = Publication.Create("Aldous Huxley", "0060850523", penguin, "Brave New World"); + + #endregion + + #region Books + + // Lord of the flies + IBook book1 = Book.Create("0000001", lordOfTheFlies); + IBook book2 = Book.Create("0000002", lordOfTheFlies); + IBook book3 = Book.Create("0000003", lordOfTheFlies); + + // 1984 + IBook book4 = Book.Create("0000004", nineteenEightyFour); + IBook book5 = Book.Create("0000005", nineteenEightyFour); + + // Brave new world + IBook book6 = Book.Create("0000006", braveNewWorld); + IBook book7 = Book.Create("0000007", braveNewWorld); + IBook book8 = Book.Create("0000008", braveNewWorld); + IBook book9 = Book.Create("0000009", braveNewWorld); + IBook book10 = Book.Create("0000010", braveNewWorld); + + #endregion + + #region Library + + ILibrary library = Library.Create("Leicester Square"); + library.AddStock(book1, book2, book3, book4, book5, book6, book7, book8, book9, book10); + + #endregion + + return library; + } + + public IBook CreateBook(string bookId = "dummy") + { + IPublisher publisher = Publisher.Create("dummy"); + IPublication publication = Publication.Create("dummy", "dummy", publisher, "dummy"); + IBook book = Book.Create(bookId, publication); + return book; + } + + #endregion + + #region Create + + [Test] + public void Create_WithNullLocation_ThrowsArgumentNullException() + { + Assert.Throws<ArgumentNullException>(() => Library.Create(null)); + } + + #endregion + + #region AddStock + + [Test] + public void AddStock_WithNullBook_ShouldThrowArgumentNullException() + { + var library = CreateLibraryScenario(); + Assert.Throws<ArgumentNullException>(() => library.AddStock(null)); + } + + [Test] + public void AddStock_WithOneValidBookAndOneNullBook_ShouldThrowArgumentNullException() + { + var library = CreateLibraryScenario(); + var validBook = CreateBook(); + Assert.Throws<ArgumentNullException>(() => library.AddStock(validBook, null)); + } + + [Test] + public void AddStock_WithValidBook_ShouldIncreaseStockListCountByOne() + { + //arrange + var library = CreateLibraryScenario(); + var expected = library.StockList.Count + 1; + var dummyBook = CreateBook(); + + //act + library.AddStock(dummyBook); + var actual = library.StockList.Count; + + //assert + Assert.AreEqual(expected, actual); + } + + [Test] + public void AddStock_WithBookIdAlreadyInStock_ShouldThrowException() + { + //arrange + var library = CreateLibraryScenario(); + var idThatIsAlreadyInStock = "0000001"; + var dummyBook = CreateBook(idThatIsAlreadyInStock); + + //act + Assert.Throws<Exception>( + () => library.AddStock(dummyBook), + string.Format("Book with {0} is already in stock", idThatIsAlreadyInStock)); + } + + [Test] + public void AddStock_WithValidBook_ShouldMakeBookAvailable() + { + //arrange + var library = CreateLibraryScenario(); + var id = "0000001"; + var dummyBook = CreateBook(); + + //act + library.AddStock(dummyBook); + var lookupBook = ((List<IStock>) library.StockList).Find(s => s.Book.Id == id); + var actual = lookupBook.IsAvailable; + + //assert + Assert.IsTrue(actual); + } + + #endregion + + #region IsBookAvailableByISBN + + [Test] + public void IsBookAvailableByISBN_WithNullISBN_ShouldThrowArgumentNullExpection() + { + var library = CreateLibraryScenario(); + Assert.Throws<ArgumentNullException>(() => library.IsBookAvailableByISBN(null)); + } + + [Test] + public void IsBookAvailableByISBN_WithValidISBN_ShouldReturnTrue() + { + var library = CreateLibraryScenario(); + + // Lord of the flies + var validISBN = "0140283331"; + + var actual = library.IsBookAvailableByISBN(validISBN); + + Assert.IsTrue(actual); + } + + [Test] + public void IsBookAvailableByISBN_WithUnknownISBN_ShouldReturnFalse() + { + var library = CreateLibraryScenario(); + + // rubbish data + var unknownISBN = "X"; + + var actual = library.IsBookAvailableByISBN(unknownISBN); + + Assert.IsFalse(actual); + } + + #endregion + + #region FindBooks + + [Test] + public void FindBooks_WhenNullSearchString_ShouldThrowArgumentNullExpection() + { + var library = CreateLibraryScenario(); + Assert.Throws<ArgumentNullException>(() => library.FindBooks(null)); + } + + + [Test] + public void FindBooks_WithValidSearchTitleString_ShouldReturnCorrectNumberOfBooks() + { + var library = CreateLibraryScenario(); + + // good search string + var searchString = "Flies"; + + // number of Lord of the Flies books expected + var expected = 3; + var actual = library.FindBooks(searchString).Count; + + Assert.AreEqual(expected, actual); + } + + [Test] + public void FindBooks_WithValidSearchTitleStringLowerCase_ShouldReturnCorrectNumberOfBooks() + { + var library = CreateLibraryScenario(); + + // good search string + var searchString = "flies"; + + // number of Lord of the Flies books expected + var expected = 3; + var actual = library.FindBooks(searchString).Count; + + Assert.AreEqual(expected, actual); + } + + [Test] + public void FindBooks_WithValidSearchStringMultiplePublications_ShouldReturnCorrectNumberOfBooks() + { + var library = CreateLibraryScenario(); + + // good search string + var searchString = "e"; + + // number of Lord of the Flies books expected + var expected = 10; + var actual = library.FindBooks(searchString).Count; + + Assert.AreEqual(expected, actual); + } + + [Test] + public void FindBooks_WithValidSearchAuthorString_ShouldReturnCorrectNumberOfBooks() + { + var library = CreateLibraryScenario(); + + // number of Brave New World books expected + var expected = 5; + var searchString = "ldous"; + + var actual = library.FindBooks(searchString).Count; + + Assert.AreEqual(expected, actual); + } + + [Test] + public void FindBooks_WithInvalidSearchString_ShouldBeEmpty() + { + var library = CreateLibraryScenario(); + + // rubbish data + var searchString = "XYZ"; + + var actual = library.FindBooks(searchString); + + Assert.IsEmpty(actual); + } + + #endregion + + #region GetBookById + + [Test] + public void GetBookById_WhenNullId_ShouldThrowArgumentNullExpection() + { + var library = CreateLibraryScenario(); + Assert.Throws<ArgumentNullException>(() => library.GetBookById(null)); + } + + [Test] + public void GetBookById_WhenValidId_ShouldReturnBook() + { + var library = CreateLibraryScenario(); + var validBookId = "0000001"; + var book = library.GetBookById(validBookId); + Assert.IsNotNull(book); + } + + [Test] + public void GetBookById_WhenValidId_ShouldReturnNull() + { + var library = CreateLibraryScenario(); + var invalidBookId = "X"; + var book = library.GetBookById(invalidBookId); + Assert.IsNull(book); + } + + #endregion + + #region LendBook + + [Test] + public void LendBook_ShouldMakeBookInLibraryStockListUnavailable() + { + //arrange + var library = CreateLibraryScenario(); + var person = CreatePerson(); + var bookId = "0000001"; + + // act + library.LendBook(person, bookId); + var lookupBook = ((List<IStock>) library.StockList).Find(s => s.Book.Id == bookId); + var actual = lookupBook.IsAvailable; + + // assert + Assert.IsFalse(actual); + } + + private IPerson CreatePerson() + { + var person = Person.Create("dummy", "dummy"); + return person; + } + + [Test] + public void LendBook_WithNullPerson_ShouldThrowArgumentNullException() + { + //arrange + var library = CreateLibraryScenario(); + var id = "dummy"; + + // act & assert + Assert.Throws<ArgumentNullException>(() => library.LendBook(null, id)); + } + + [Test] + public void LendBook_WithNullBook_ShouldThrowArgumentNullException() + { + //arrange + var library = CreateLibraryScenario(); + var person = CreatePerson(); + + // act & assert + Assert.Throws<ArgumentNullException>(() => library.LendBook(person, null)); + } + + [Test] + public void LendBook_WithNullBookAndNullPerson_ShouldThrowArgumentNullException() + { + //arrange + var library = CreateLibraryScenario(); + + // act & assert + Assert.Throws<ArgumentNullException>(() => library.LendBook(null, null)); + } + + [Test] + public void LendBook_WithUnknownBook_ShouldThrowArgumentException() + { + //arrange + var library = CreateLibraryScenario(); + var person = CreatePerson(); + var id = "dummy"; + + // act & assert + Assert.Throws<Exception>( + () => library.LendBook(person, id), + string.Format("Book id {0} is not in stock", id)); + } + + #endregion + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/PersonTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,35 @@ +using System; +using LibrarySystem.Core; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + class PersonTests + { + [Test] + public void Create_WithValidParams_ShouldReturnPerson() + { + var person = Person.Create("dummy", "dummy"); + Assert.IsNotNull(person); + } + + [Test] + public void Create_WithNullAddress_ShouldThrowAgrumentNullException() + { + Assert.Throws<ArgumentNullException>(() => Person.Create(null, "dummy")); + } + + [Test] + public void Create_WithNullName_ShouldThrowAgrumentNullException() + { + Assert.Throws<ArgumentNullException>(() => Person.Create("dummy", null)); + } + + [Test] + public void Create_WithNullAddressAndName_ShouldThrowAgrumentNullException() + { + Assert.Throws<ArgumentNullException>(() => Person.Create(null, null)); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/PublicationTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,45 @@ +using System; +using LibrarySystem.Core; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + class PublicationTests + { + [Test] + public void Create_WithValidParams_ShouldReturnPublication() + { + var publisher = Publisher.Create("dummy"); + var publication = Publication.Create("dummy","dummy", publisher, "dummy"); + Assert.IsNotNull(publication); + } + + [Test] + public void Create_WithNullAuthor_ShouldThrowAgrumentNullException() + { + var publisher = Publisher.Create("dummy"); + Assert.Throws<ArgumentNullException>(() => Publication.Create(null, "dummy", publisher, "dummy")); + } + + [Test] + public void Create_WithNullISBN_ShouldThrowAgrumentNullException() + { + var publisher = Publisher.Create("dummy"); + Assert.Throws<ArgumentNullException>(() => Publication.Create("dummy", null, publisher, "dummy")); + } + + [Test] + public void Create_WithNullPublisher_ShouldThrowAgrumentNullException() + { + Assert.Throws<ArgumentNullException>(() => Publication.Create("dummy","dummy", null, "dummy")); + } + + [Test] + public void Create_WithNullTitle_ShouldThrowAgrumentNullException() + { + var publisher = Publisher.Create("dummy"); + Assert.Throws<ArgumentNullException>(() => Publication.Create("dummy", "dummy", publisher, null)); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/PublisherTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,23 @@ +using System; +using LibrarySystem.Core; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + class PublisherTests + { + [Test] + public void Create_WithValidName_ShouldReturnPublisher() + { + var publisher = Publisher.Create("dummy"); + Assert.IsNotNull(publisher); + } + + [Test] + public void Create_WithNullName_ShouldThrowAgrumentNullException() + { + Assert.Throws<ArgumentNullException>(() => Publisher.Create(null)); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/StockTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,26 @@ +using System; +using LibrarySystem.Core; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + class StockTests + { + [Test] + public void Create_WithValidName_ShouldReturnPublisher() + { + var publisher = Publisher.Create("dummy"); + var publication = Publication.Create("dummy","dummy", publisher, "dummy"); + var book = Book.Create("dummy", publication); + var stock = Stock.Create(book, true); + Assert.IsNotNull(stock); + } + + [Test] + public void Create_WithNullBook_ShouldThrowAgrumentNullException() + { + Assert.Throws<ArgumentNullException>(() => Stock.Create(null, true)); + } + } +}
--- a/Library/LibrarySystem.Tests/LibrarySystem.Tests.csproj Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem.Tests/LibrarySystem.Tests.csproj Wed Apr 04 19:20:20 2012 +0100 @@ -12,6 +12,8 @@ <AssemblyName>LibrarySystem.Tests</AssemblyName> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> + <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\Library\</SolutionDir> + <RestorePackages>true</RestorePackages> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> @@ -38,8 +40,13 @@ <Reference Include="System.Core" /> </ItemGroup> <ItemGroup> - <Compile Include="LibrarySystemTests.cs" /> + <Compile Include="Core\BookTests.cs" /> + <Compile Include="Core\LibraryTests.cs" /> + <Compile Include="Core\PersonTests.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="Core\PublicationTests.cs" /> + <Compile Include="Core\PublisherTests.cs" /> + <Compile Include="Core\StockTests.cs" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\LibrarySystem\LibrarySystem.csproj"> @@ -51,6 +58,7 @@ <None Include="packages.config" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Import Project="$(SolutionDir)\.nuget\nuget.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. <Target Name="BeforeBuild">
--- a/Library/LibrarySystem.Tests/LibrarySystemTests.cs Tue Apr 03 23:32:40 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,204 +0,0 @@ -using LibrarySystem.Core; -using LibrarySystem.Interfaces; -using NUnit.Framework; - -namespace LibrarySystem.Tests -{ - [TestFixture] - public class LibraryTests - { - /* - * Factory that creates: - * Library with 10 books (2 x Lord of the Flies, 3 x 1984, 5 x Brave New World) - */ - private ILibrary CreateScenario() - { - #region Publisher - - IPublisher penguin = new Publisher() {Name = "Penguin"}; - - #endregion - - #region Publications - - IPublication lordOfTheFlies = new Publication() - { - Author = "William Golding", - ISBN = "0140283331", - Publisher = penguin, - Title = "Lord of the Flies" - }; - - IPublication nineteenEightyFour = new Publication() - { - Author = "George Orwell", - ISBN = "0452284236", - Title = "Nineteen Eighty-Four" - }; - - IPublication braveNewWorld = new Publication() - { - Author = "Aldous Huxley", - ISBN = "0060850523", - Title = "Brave New World" - }; - - #endregion - - #region Books - - // Lord of the flies - IBook book1 = new Book(lordOfTheFlies) - {Id = "0000001", CurrentFee = 1.25m}; - IBook book2 = new Book(lordOfTheFlies) - {Id = "0000002", CurrentFee = 1.25m}; - IBook book3 = new Book(lordOfTheFlies) - {Id = "0000003", CurrentFee = 1.25m}; - - // 1984 - IBook book4 = new Book(nineteenEightyFour) - {Id = "0000004", CurrentFee = 1.5m}; - IBook book5 = new Book(nineteenEightyFour) - {Id = "0000005", CurrentFee = 1.5m}; - - // Brave new world - IBook book6 = new Book(braveNewWorld) - {Id = "0000006", CurrentFee = 1.8m}; - IBook book7 = new Book(braveNewWorld) - {Id = "0000007", CurrentFee = 1.8m}; - IBook book8 = new Book(braveNewWorld) - {Id = "0000008", CurrentFee = 1.8m}; - IBook book9 = new Book(braveNewWorld) - {Id = "0000009", CurrentFee = 1.8m}; - IBook book10 = new Book(braveNewWorld) - {Id = "0000010", CurrentFee = 1.8m}; - - #endregion - - #region Library - - ILibrary library = new Library(); - library.Location = "Leicester Square"; - library.AddStock(book1, book2, book3, book4, book5, book6, book7, book8, book9, book10); - - #endregion - - return library; - } - - [Test] - public void AddStockShouldIncreaseStockListCountByOne() - { - //arrange - var library = new Library(); - var expected = library.StockList.Count + 1; - var dummyBook = new Book(new Publication()); - - //act - library.AddStock(dummyBook); - var actual = library.StockList.Count; - - //assert - Assert.AreEqual(expected, actual); - } - - [Test] - public void SearchForAvailableBookByIBSNShouldReturnTrue() - { - var library = CreateScenario(); - - // Lord of the flies - var validISBN = "0140283331"; - - var actual = library.IsBookAvailableByISBN(validISBN); - - Assert.IsTrue(actual); - } - - [Test] - public void SearchForUnavailableBookByIBSNShouldReturnFalse() - { - var library = CreateScenario(); - - // rubbish data - var invalidISBN = "X"; - - var actual = library.IsBookAvailableByISBN(invalidISBN); - - Assert.IsFalse(actual); - } - - [Test] - public void FindBooksByValidSearchTitleString() - { - var library = CreateScenario(); - - // good search string - var searchString = "Flies"; - - // number of Lord of the Flies books expected - var expected = 3; - var actual = library.FindBooks(searchString).Count; - - Assert.AreEqual(expected, actual); - } - - - [Test] - public void FindBooksByValidSearchTitleStringLowerCase() - { - var library = CreateScenario(); - - // good search string - var searchString = "flies"; - - // number of Lord of the Flies books expected - var expected = 3; - var actual = library.FindBooks(searchString).Count; - - Assert.AreEqual(expected, actual); - } - - [Test] - public void FindBooksByValidSearchStringMultiplePublications() - { - var library = CreateScenario(); - - // good search string - var searchString = "e"; - - // number of Lord of the Flies books expected - var expected = 10; - var actual = library.FindBooks(searchString).Count; - - Assert.AreEqual(expected, actual); - } - - [Test] - public void FindBooksByValidSearchAuthorString() - { - var library = CreateScenario(); - - // number of Brave New World books expected - var expected = 5; - var searchString = "ldous"; - - var actual = library.FindBooks(searchString).Count; - - Assert.AreEqual(expected, actual); - } - - [Test] - public void FindBooksByInvalidSearchString() - { - var library = CreateScenario(); - - // rubbish data - var searchString = "XYZ"; - - var actual = library.FindBooks(searchString); - - Assert.IsEmpty(actual); - } - } -}
--- a/Library/LibrarySystem/Core/Book.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Core/Book.cs Wed Apr 04 19:20:20 2012 +0100 @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System; using LibrarySystem.Interfaces; namespace LibrarySystem.Core @@ -10,16 +10,25 @@ public IPublisher Publisher { get; set; } public string Title { get; set; } public string Id { get; set; } - public decimal CurrentFee { get; set; } - public IList<IRental> RentalHistory { get; set; } - public Book(IPublication publication) + private Book() + { + + } + + public static IBook Create(string id, IPublication publication) { - Author = publication.Author; - ISBN = publication.ISBN; - Title = publication.Title; - Publisher = publication.Publisher; - RentalHistory = new List<IRental>(0); + if (id == null || publication == null) + throw new ArgumentNullException(); + + return new Book() + { + Author = publication.Author, + Id = id, + ISBN = publication.ISBN, + Title = publication.Title, + Publisher = publication.Publisher + }; } } }
--- a/Library/LibrarySystem/Core/Library.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Core/Library.cs Wed Apr 04 19:20:20 2012 +0100 @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using LibrarySystem.Interfaces; @@ -6,30 +7,55 @@ { public class Library : ILibrary { - public string Location { get; set; } - public IList<IStock> StockList { get; set; } + public string Location { get; private set; } + public IList<IStock> StockList { get; private set; } + + private Library() + { + } - public Library() + public static ILibrary Create(string location) { - StockList = new List<IStock>(0); + if (location == null) + throw new ArgumentNullException(); + + return new Library() {Location = location, StockList = new List<IStock>(0)}; } public void AddStock(params IBook[] books) { + if (books == null) + throw new ArgumentNullException(); + var stock = new List<IStock>(StockList.Count + books.Length); stock.AddRange(StockList); foreach (var book in books) { - stock.Add(new Stock() { Book = book, IsAvailable = true }); + if (book == null) + throw new ArgumentNullException(); + + ThrowExceptionIfBookIsAlreadyInStock(book); + + stock.Add(Stock.Create(book, true)); } StockList = stock; } + private void ThrowExceptionIfBookIsAlreadyInStock(IBook book) + { + var lookup = ((List<IStock>) StockList).Find(s => s.Book.Id == book.Id); + if (lookup != null) + throw new Exception(string.Format("Book id {0} is already in stock", book.Id)); + } + public bool IsBookAvailableByISBN(string isbn) { + if (isbn == null) + throw new ArgumentNullException(); + var qry = from books in StockList where books.Book.ISBN == isbn & books.IsAvailable @@ -40,6 +66,9 @@ public IList<IStock> FindBooks(string searchString) { + if (searchString == null) + throw new ArgumentNullException(); + searchString = searchString.ToLower(); var qry = from books in StockList @@ -50,9 +79,38 @@ return qry.ToList(); } - public void LendBook(IPerson person, IBook book) + public IBook GetBookById(string id) { - throw new System.NotImplementedException(); + if (id == null) + throw new ArgumentNullException(); + + id = id.ToLower(); + + var qry = from books in StockList + where books.Book.Id.ToLower() == id + select books.Book; + + return qry.FirstOrDefault(); + } + + + public void LendBook(IPerson person, string id) + { + if (person == null || id == null) + throw new ArgumentNullException(); + + var stock = ThrowExceptionIfBookIsNotInStock(id); + stock.IsAvailable = false; + + // FUTURE: register with person that they have borrowed the book + } + + private IStock ThrowExceptionIfBookIsNotInStock(string id) + { + var lookup = ((List<IStock>) StockList).Find(s => s.Book.Id == id); + if (lookup == null) + throw new Exception(string.Format("Book with id {0} is not in stock", id)); + return lookup; } } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem/Core/Person.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,24 @@ +using System; +using LibrarySystem.Interfaces; + +namespace LibrarySystem.Core +{ + public class Person : IPerson + { + public string Address { get; private set; } + public string Name { get; private set; } + + private Person() + { + + } + + public static IPerson Create(string address, string name) + { + if (address == null || name == null) + throw new ArgumentNullException(); + + return new Person() { Address = address, Name = name}; + } + } +}
--- a/Library/LibrarySystem/Core/Publication.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Core/Publication.cs Wed Apr 04 19:20:20 2012 +0100 @@ -1,13 +1,33 @@ -using LibrarySystem.Interfaces; +using System; +using LibrarySystem.Interfaces; namespace LibrarySystem.Core { public class Publication : IPublication { - public string Author { get; set; } - public string ISBN { get; set; } - public IPublisher Publisher { get; set; } - public string Title { get; set; } + public string Author { get; private set; } + public string ISBN { get; private set; } + public IPublisher Publisher { get; private set; } + public string Title { get; private set; } + + private Publication() + { + + } + + public static IPublication Create(string author, string isbn, IPublisher publisher, string title) + { + if (author == null || isbn == null || publisher == null || title == null) + throw new ArgumentNullException(); + + return new Publication() + { + Author = author, + ISBN = isbn, + Publisher = publisher, + Title = title + }; + } } }
--- a/Library/LibrarySystem/Core/Publisher.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Core/Publisher.cs Wed Apr 04 19:20:20 2012 +0100 @@ -1,9 +1,23 @@ -using LibrarySystem.Interfaces; +using System; +using LibrarySystem.Interfaces; namespace LibrarySystem.Core { public class Publisher : IPublisher { - public string Name { get; set; } + public string Name { get; private set; } + + private Publisher() + { + + } + + public static IPublisher Create(string name) + { + if (name == null) + throw new ArgumentNullException(); + + return new Publisher() { Name = name }; + } } }
--- a/Library/LibrarySystem/Core/Rental.cs Tue Apr 03 23:32:40 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -using System; -using LibrarySystem.Interfaces; - -namespace LibrarySystem.Core -{ - public class Rental : IRental - { - public DateTime ActualCheckIn { get; set; } - public IBook Book { get; set; } - public DateTime CheckOut { get; set; } - public DateTime ExpectedCheckIn { get; set; } - public decimal Fee { get; set; } - public IPerson Person { get; set; } - } -}
--- a/Library/LibrarySystem/Core/Stock.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Core/Stock.cs Wed Apr 04 19:20:20 2012 +0100 @@ -1,10 +1,24 @@ -using LibrarySystem.Interfaces; +using System; +using LibrarySystem.Interfaces; namespace LibrarySystem.Core { public class Stock : IStock { + public IBook Book { get; private set; } public bool IsAvailable { get; set; } - public IBook Book { get; set; } + + private Stock() + { + + } + + public static IStock Create(IBook book, bool isAvailable) + { + if (book == null) + throw new ArgumentNullException(); + + return new Stock() {Book = book, IsAvailable = isAvailable}; + } } }
--- a/Library/LibrarySystem/Diagrams/Overview.cd Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Diagrams/Overview.cd Wed Apr 04 19:20:20 2012 +0100 @@ -1,12 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> <ClassDiagram MajorVersion="1" MinorVersion="1"> - <Interface Name="LibrarySystem.Interfaces.IRental"> - <Position X="5.25" Y="3" Width="1.5" /> - <TypeIdentifier> - <HashCode>AIAAAAAAAAAABABgACAAAAAAAgAAAAAAAAAAAAAAAAA=</HashCode> - <FileName>Interfaces\IRental.cs</FileName> - </TypeIdentifier> - </Interface> <Interface Name="LibrarySystem.Interfaces.IPublication"> <Position X="3.5" Y="1" Width="1.5" /> <TypeIdentifier> @@ -17,28 +10,28 @@ <Interface Name="LibrarySystem.Interfaces.IBook"> <Position X="5.25" Y="1" Width="1.5" /> <TypeIdentifier> - <HashCode>AAACAAAAABAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAA=</HashCode> + <HashCode>AAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode> <FileName>Interfaces\IBook.cs</FileName> </TypeIdentifier> </Interface> <Interface Name="LibrarySystem.Interfaces.IStock"> - <Position X="7" Y="1" Width="2.25" /> + <Position X="5.25" Y="2.5" Width="1.5" /> <TypeIdentifier> <HashCode>AAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAIAAAAA=</HashCode> <FileName>Interfaces\IStock.cs</FileName> </TypeIdentifier> </Interface> <Interface Name="LibrarySystem.Interfaces.ILibrary"> - <Position X="9.5" Y="1" Width="1.5" /> + <Position X="7" Y="1" Width="1.5" /> <TypeIdentifier> - <HashCode>AAAAAAAAAAAAAAAAAAIAgAAEAAAAAAAAAAIAABAAAgA=</HashCode> + <HashCode>AAAAAAAAAAAAAAAAAAIAgAAEAAAAgAAAAAIAABAAAgA=</HashCode> <FileName>Interfaces\ILibrary.cs</FileName> </TypeIdentifier> </Interface> <Interface Name="LibrarySystem.Interfaces.IPerson"> - <Position X="7.25" Y="3" Width="1.5" /> + <Position X="8.75" Y="1" Width="1.5" /> <TypeIdentifier> - <HashCode>AAAAAAAABAAAAAAAAAAAAAQAAAAAAIACAAAAAAAAAAA=</HashCode> + <HashCode>AAAAAAAAAAAAAAAAAAAAAAQAAAAAAAACAAAAAAAAAAA=</HashCode> <FileName>Interfaces\IPerson.cs</FileName> </TypeIdentifier> </Interface>
--- a/Library/LibrarySystem/Interfaces/IBook.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Interfaces/IBook.cs Wed Apr 04 19:20:20 2012 +0100 @@ -1,11 +1,7 @@ -using System.Collections.Generic; - -namespace LibrarySystem.Interfaces +namespace LibrarySystem.Interfaces { public interface IBook : IPublication { - string Id { get; set; } - decimal CurrentFee { get; set; } - IList<IRental> RentalHistory { get; set; } + string Id { get; } } } \ No newline at end of file
--- a/Library/LibrarySystem/Interfaces/ILibrary.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Interfaces/ILibrary.cs Wed Apr 04 19:20:20 2012 +0100 @@ -4,12 +4,13 @@ { public interface ILibrary { - string Location { get; set; } - IList<IStock> StockList { get; set; } + string Location { get; } + IList<IStock> StockList { get; } void AddStock(params IBook[] books); bool IsBookAvailableByISBN(string isbn); IList<IStock> FindBooks(string searchString); - void LendBook(IPerson person, IBook book); + IBook GetBookById(string id); + void LendBook(IPerson person, string id); } } \ No newline at end of file
--- a/Library/LibrarySystem/Interfaces/IPerson.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Interfaces/IPerson.cs Wed Apr 04 19:20:20 2012 +0100 @@ -1,13 +1,9 @@ -using System.Collections.Generic; -using LibrarySystem.Core; - + namespace LibrarySystem.Interfaces { public interface IPerson { - string Address { get; set; } - decimal CurrentBalance { get; set; } - IList<Rental> RentalHistory { get; set; } - string Name { get; set; } + string Address { get; } + string Name { get; } } } \ No newline at end of file
--- a/Library/LibrarySystem/Interfaces/IPublication.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Interfaces/IPublication.cs Wed Apr 04 19:20:20 2012 +0100 @@ -2,9 +2,9 @@ { public interface IPublication { - string Author { get; set; } - string ISBN { get; set; } - IPublisher Publisher { get; set; } - string Title { get; set; } + string Author { get; } + string ISBN { get; } + IPublisher Publisher { get; } + string Title { get; } } } \ No newline at end of file
--- a/Library/LibrarySystem/Interfaces/IPublisher.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Interfaces/IPublisher.cs Wed Apr 04 19:20:20 2012 +0100 @@ -2,6 +2,6 @@ { public interface IPublisher { - string Name { get; set; } + string Name { get; } } } \ No newline at end of file
--- a/Library/LibrarySystem/Interfaces/IRental.cs Tue Apr 03 23:32:40 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -using System; - -namespace LibrarySystem.Interfaces -{ - public interface IRental - { - DateTime ActualCheckIn { get; set; } - IBook Book { get; set; } - DateTime CheckOut { get; set; } - DateTime ExpectedCheckIn { get; set; } - Decimal Fee { get; set; } - IPerson Person { get; set; } - } -} \ No newline at end of file
--- a/Library/LibrarySystem/Interfaces/IStock.cs Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/Interfaces/IStock.cs Wed Apr 04 19:20:20 2012 +0100 @@ -3,6 +3,6 @@ public interface IStock { bool IsAvailable { get; set; } - IBook Book { get; set; } + IBook Book { get; } } } \ No newline at end of file
--- a/Library/LibrarySystem/LibrarySystem.csproj Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem/LibrarySystem.csproj Wed Apr 04 19:20:20 2012 +0100 @@ -12,6 +12,8 @@ <AssemblyName>LibrarySystem</AssemblyName> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> + <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\Library\</SolutionDir> + <RestorePackages>true</RestorePackages> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> @@ -36,7 +38,7 @@ </ItemGroup> <ItemGroup> <Compile Include="Core\Book.cs" /> - <Compile Include="Interfaces\IRental.cs" /> + <Compile Include="Core\Person.cs" /> <Compile Include="Interfaces\IBook.cs" /> <Compile Include="Interfaces\ILibrary.cs" /> <Compile Include="Interfaces\IPerson.cs" /> @@ -47,13 +49,13 @@ <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Core\Publication.cs" /> <Compile Include="Core\Publisher.cs" /> - <Compile Include="Core\Rental.cs" /> <Compile Include="Core\Stock.cs" /> </ItemGroup> <ItemGroup> <None Include="Diagrams\Overview.cd" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Import Project="$(SolutionDir)\.nuget\nuget.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. <Target Name="BeforeBuild">