Skip to content
Browse files

-Added save/load of project to disk

-Marked model obejcts as serializable (via DataContract/Member attribs)
-Added binding validator tests for ProjectView
-Added tests for project load/save
  • Loading branch information...
1 parent 8e91910 commit 6b78c3cb16a87178430b8862c74e5fb1196ddef1 @HEskandari HEskandari committed Aug 15, 2010
View
1 Rhino.Licensing.AdminTool.Tests/Rhino.Licensing.AdminTool.Tests.csproj
@@ -96,6 +96,7 @@
<Compile Include="Models\ProductModelTests.cs" />
<Compile Include="Models\ProjectModelTests.cs" />
<Compile Include="Services\DialogServiceTests.cs" />
+ <Compile Include="Services\ProjectServiceTests.cs" />
<Compile Include="ViewModels\AboutViewModelTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModels\DialogViewModelTests.cs" />
View
60 Rhino.Licensing.AdminTool.Tests/Services/ProjectServiceTests.cs
@@ -0,0 +1,60 @@
+using System;
+using System.IO;
+using Rhino.Licensing.AdminTool.Model;
+using Rhino.Licensing.AdminTool.Services;
+using Xunit;
+
+namespace Rhino.Licensing.AdminTool.Tests.Services
+{
+ public class ProjectServiceTests
+ {
+ [Fact]
+ public void Can_Save_Project_Graph()
+ {
+ var p = CreateNewProject();
+ var service = new ProjectService() as IProjectService;
+ var fileInfo = new FileInfo(Path.GetTempFileName());
+
+ service.Save(p, fileInfo);
+
+ var reader = fileInfo.OpenText();
+ var content = reader.ReadToEnd();
+
+ Assert.NotNull(content);
+ Assert.NotEmpty(content);
+ }
+
+ [Fact(Skip = "Won't run due to reported bug in Caliburn")]
+ public void Can_Load_Project_Graph()
+ {
+ var p = CreateNewProject();
+ var service = new ProjectService() as IProjectService;
+ var fileInfo = new FileInfo(Path.GetTempFileName());
+
+ service.Save(p, fileInfo);
+
+ var project = service.Load(fileInfo);
+
+ Assert.NotNull(project);
+ Assert.NotNull(project.Product);
+ Assert.Equal("New Project", project.Name);
+ Assert.Equal("Rhino Mocks", project.Product.Name);
+ Assert.Equal("Private Key", project.Product.PrivateKey);
+ Assert.Equal("Public Key", project.Product.PublicKey);
+ }
+
+ private Project CreateNewProject()
+ {
+ return new Project
+ {
+ Name = "New Project",
+ Product = new Product
+ {
+ Name = "Rhino Mocks",
+ PrivateKey = "Private Key",
+ PublicKey = "Public Key"
+ }
+ };
+ }
+ }
+}
View
2 Rhino.Licensing.AdminTool.Tests/ViewModels/AboutViewModelTests.cs
@@ -10,7 +10,7 @@ namespace Rhino.Licensing.AdminTool.Tests.ViewModels
public class AboutViewModelTests
{
[Fact]
- public void Version_Is_Bound()
+ public void Properties_Are_Bound()
{
var validator = Validator.For<AboutView, AboutViewModel>()
.Validate();
View
27 Rhino.Licensing.AdminTool.Tests/ViewModels/ProjectViewModelTests.cs
@@ -1,6 +1,9 @@
+using System.IO;
+using Caliburn.Testability;
using Rhino.Licensing.AdminTool.Model;
using Rhino.Licensing.AdminTool.Services;
using Rhino.Licensing.AdminTool.ViewModels;
+using Rhino.Licensing.AdminTool.Views;
using Rhino.Mocks;
using Xunit;
using Caliburn.Testability.Extensions;
@@ -35,6 +38,17 @@ public void Fires_PropertyChange_Notification()
}
[Fact]
+ public void Properties_Are_Bound()
+ {
+ var validator = Validator.For<ProjectView, ProjectViewModel>()
+ .Validate();
+
+ validator.AssertWasBound(x => x.CurrentProject.Product.Name);
+ validator.AssertWasBound(x => x.CurrentProject.Product.PrivateKey);
+ validator.AssertWasBound(x => x.CurrentProject.Product.PublicKey);
+ }
+
+ [Fact]
public void Can_Not_Save_If_Name_Is_Not_Provided()
{
var vm = CreateViewModel();
@@ -81,13 +95,20 @@ public void Will_Proceed_To_Save_When_Dialog_Successfully_Closes()
{
var vm = CreateViewModel();
var project = new Project();
+ var temp = Path.GetTempFileName();
- _dialogService.Expect(x => x.ShowSaveFileDialog(Arg<ISaveFileDialogViewModel>.Is.Anything)).Return(true);
+ _dialogService.Expect(x => x.ShowSaveFileDialog(Arg<ISaveFileDialogViewModel>.Is.Anything))
+ .WhenCalled(m =>
+ {
+ var model = (SaveFileDialogViewModel) m.Arguments[0];
+ model.FileName = temp;
+ })
+ .Return(true);
vm.CurrentProject = project;
vm.Save();
-
- _projectService.AssertWasCalled(x => x.Save(Arg.Is(project)));
+
+ _projectService.AssertWasCalled(x => x.Save(Arg.Is(project), Arg<FileInfo>.Is.Anything));
}
[Fact]
View
12 Rhino.Licensing.AdminTool/Model/Product.cs
@@ -1,16 +1,23 @@
using System;
+using System.Runtime.Serialization;
using Caliburn.PresentationFramework;
namespace Rhino.Licensing.AdminTool.Model
{
- [Serializable]
+ [DataContract(Name = "Product", Namespace = "http://schemas.hibernatingrhinos.com/")]
public class Product : PropertyChangedBase
{
private string _publicKey;
private string _name;
private string _privateKey;
private Guid _id;
+ public Product()
+ {
+ Id = Guid.NewGuid();
+ }
+
+ [DataMember]
public virtual Guid Id
{
get { return _id; }
@@ -21,6 +28,7 @@ public virtual Guid Id
}
}
+ [DataMember]
public virtual string Name
{
get { return _name; }
@@ -31,6 +39,7 @@ public virtual string Name
}
}
+ [DataMember]
public virtual string PrivateKey
{
get { return _privateKey; }
@@ -41,6 +50,7 @@ public virtual string PrivateKey
}
}
+ [DataMember]
public virtual string PublicKey
{
get { return _publicKey; }
View
9 Rhino.Licensing.AdminTool/Model/Project.cs
@@ -1,9 +1,10 @@
using System;
+using System.Runtime.Serialization;
using Caliburn.PresentationFramework;
namespace Rhino.Licensing.AdminTool.Model
{
- [Serializable]
+ [DataContract(Name = "Project", Namespace = "http://schemas.hibernatingrhinos.com/")]
public class Project : PropertyChangedBase
{
private string _name;
@@ -14,7 +15,8 @@ public Project()
Product = new Product();
}
- public string Name
+ [DataMember]
+ public virtual string Name
{
get { return _name; }
set
@@ -24,7 +26,8 @@ public string Name
}
}
- public Product Product
+ [DataMember]
+ public virtual Product Product
{
get { return _product; }
set
View
1 Rhino.Licensing.AdminTool/Rhino.Licensing.AdminTool.csproj
@@ -93,6 +93,7 @@
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Data" />
+ <Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
View
63 Rhino.Licensing.AdminTool/Services/ProjectService.cs
@@ -1,42 +1,71 @@
-using System;
using System.IO;
+using System.Runtime.Serialization;
using Rhino.Licensing.AdminTool.Model;
namespace Rhino.Licensing.AdminTool.Services
{
public interface IProjectService
{
- void Save(Project project);
+ /// <summary>
+ /// Saves the project to disk
+ /// </summary>
+ /// <param name="project">project to save</param>
+ /// <param name="path">path to project file</param>
+ void Save(Project project, FileInfo path);
+ /// <summary>
+ /// Creates a new project
+ /// </summary>
+ /// <returns></returns>
Project Create();
- Project Load(FileInfo file);
+ /// <summary>
+ /// Loads a project
+ /// </summary>
+ /// <param name="path">path to load the project from</param>
+ /// <returns></returns>
+ Project Load(FileInfo path);
}
public class ProjectService : IProjectService
{
- public void Save(Project project)
+ public void Save(Project project, FileInfo path)
{
-
- }
-
- public Project Create()
- {
- return new Project()
+ using (var stream = new MemoryStream())
{
- Name = string.Empty,
- Product = new Product
+ var serializer = new DataContractSerializer(typeof(Project));
+ serializer.WriteObject(stream, project);
+
+ FileStream writer = null;
+
+ try
{
- Id = Guid.NewGuid(),
- Name = string.Empty
+ writer = path.OpenWrite();
+ writer.Write(stream.GetBuffer(), 0, (int) stream.Length);
}
- };
+ finally
+ {
+ if (writer != null)
+ {
+ writer.Flush();
+ writer.Close();
+ writer.Dispose();
+ }
+ }
+ }
}
- public Project Load(FileInfo file)
+ public Project Create()
{
- return null;
+ return new Project();
}
+ public Project Load(FileInfo path)
+ {
+ var reader = path.OpenRead();
+ var serializer = new DataContractSerializer(typeof(Project));
+
+ return serializer.ReadObject(reader) as Project;
+ }
}
}
View
3 Rhino.Licensing.AdminTool/ViewModels/ProjectViewModel.cs
@@ -1,3 +1,4 @@
+using System.IO;
using System.Security.Cryptography;
using Caliburn.PresentationFramework.Filters;
using Caliburn.PresentationFramework.Screens;
@@ -54,7 +55,7 @@ public virtual void Save()
if (result.GetValueOrDefault(false))
{
- _projectService.Save(CurrentProject);
+ _projectService.Save(CurrentProject, new FileInfo(dialog.FileName));
}
}
View
20 Rhino.Licensing.AdminTool/Views/ProjectView.xaml
@@ -12,18 +12,18 @@
<ToolBarPanel DockPanel.Dock="Top"
HorizontalAlignment="Left"
Orientation="Horizontal">
-
- <Button Style="{StaticResource ToolButton}"
+
+ <Button Style="{DynamicResource ToolButton}"
Content="Save"
cal:Message.Attach="[Event Click]=[Action Save]"/>
- <Button Style="{StaticResource ToolButton}"
+ <Button Style="{DynamicResource ToolButton}"
Content="Close"
cal:Message.Attach="[Event Click]=[Action TryClose]"/>
+
+ <Rectangle Style="{DynamicResource ToolSeparator}" />
- <Rectangle Style="{StaticResource ToolSeparator}" />
-
- <Button Style="{StaticResource ToolButton}"
+ <Button Style="{DynamicResource ToolButton}"
Content="Generate Key Pair"
cal:Message.Attach="[Event Click]=[Action GenerateKey]"/>
@@ -40,10 +40,10 @@
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
-
- <TextBlock Style="{StaticResource Label}" Text="Name: " Grid.Column="0" Grid.Row="0" />
- <TextBlock Style="{StaticResource Label}" Text="Public Key: " Grid.Column="0" Grid.Row="1"/>
- <TextBlock Style="{StaticResource Label}" Text="Private Key: " Grid.Column="0" Grid.Row="2"/>
+
+ <TextBlock Style="{DynamicResource Label}" Text="Name: " Grid.Column="0" Grid.Row="0" />
+ <TextBlock Style="{DynamicResource Label}" Text="Public Key: " Grid.Column="0" Grid.Row="1"/>
+ <TextBlock Style="{DynamicResource Label}" Text="Private Key: " Grid.Column="0" Grid.Row="2"/>
<TextBox Text="{Binding Path=CurrentProject.Product.Name}"
Grid.Column="1" Grid.Row="0" />

0 comments on commit 6b78c3c

Please sign in to comment.
Something went wrong with that request. Please try again.