Update of /cvsroot/jcframework/dotnet/Nunit/Interfaces In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv815/Nunit/Interfaces Added Files: EmployeeInterfaceTests.vb IEmployee.vb IOptimistic.vb ManyToManyClasses.vb ManyToManyTests.vb TimestampTests.vb ValidatedEmployee.vb Log Message: Name changes to bring class library into line with Microsoft naming standards. FxCop directory added Nunit tests moved into main repository --- NEW FILE: ValidatedEmployee.vb --- Namespace Interfaces Public Class ValidatedEmployee Inherits EmployeeClass Implements AtomsFramework.IValidation Private _allowValidation As Boolean Public Property AllowValidation() As Boolean Get Return _allowvalidation End Get Set(ByVal Value As Boolean) _allowvalidation = Value End Set End Property Public Function IsValid() As Boolean Implements AtomsFramework.IValidation.IsValid Return _allowValidation End Function Public Function IsValidToDelete() As Boolean Implements AtomsFramework.IValidation.IsValidToDelete Return _allowValidation End Function End Class End Namespace --- NEW FILE: ManyToManyClasses.vb --- Imports AtomsFramework Namespace Interfaces #Region "Interface Definitions" Public Interface iM2MA Property GUIDValue() As String Property Description() As String Property M2MBCollection() As M2MBCollection End Interface Public Interface iM2MB Property GUIDValue() As String Property Description() As String Property M2MACollection() As M2MACollection End Interface #End Region #Region "Realizations" Public Class M2MA Implements iM2MA Private _guid As Guid = Guid.NewGuid Private _description As String Private _bCol As M2MBCollection Public Property Description() As String Implements iM2MA.Description Get Return _description End Get Set(ByVal Value As String) _description = Value End Set End Property Public Property M2MBCollection() As M2MBCollection Implements iM2MA.M2MBCollection Get Return _bCol End Get Set(ByVal Value As M2MBCollection) _bCol = Value End Set End Property Public Sub New() _bCol = New M2MBCollection End Sub Public Property GUIDValue() As String Implements iM2MA.GUIDValue Get If _guid.Equals(Guid.Empty) Then _guid = Guid.NewGuid End If Return _guid.ToString("N") End Get Set(ByVal value As String) _guid = New Guid(value) End Set End Property End Class Public Class M2MB Implements iM2MB Private _guid As Guid = Guid.NewGuid Private _description As String Private _aCol As M2MACollection Public Property Description() As String Implements iM2MB.Description Get Return _description End Get Set(ByVal Value As String) _description = Value End Set End Property Public Property M2MACollection() As M2MACollection Implements iM2MB.M2MaCollection Get Return _aCol End Get Set(ByVal Value As M2MACollection) _aCol = Value End Set End Property Public Property GUIDValue() As String Implements iM2MB.GUIDValue Get If _guid.Equals(Guid.Empty) Then _guid = Guid.NewGuid End If Return _guid.ToString("N") End Get Set(ByVal value As String) _guid = New Guid(value) End Set End Property Public Sub New() _aCol = New M2MACollection End Sub End Class #End Region #Region "M2MACollection" Public Class M2MACollection Inherits CollectionBase Public Overloads Sub Add(ByVal a As iM2MA) list.Add(a) End Sub Public Shadows Function Item(ByVal index As Integer) As iM2MA Return CType(list(index), iM2MA) End Function End Class #End Region #Region "M2MBCollection" Public Class M2MBCollection Inherits CollectionBase Public Overloads Sub Add(ByVal b As iM2MB) list.Add(b) End Sub Public Shadows Function Item(ByVal index As Integer) As iM2MB Return CType(list(index), M2MB) End Function End Class #End Region #Region "Interface Factories" Public Class M2MAFactory Implements AtomsFramework.IClassFactory Public Function CreateObject() As Object Implements AtomsFramework.IClassFactory.CreateObject Return New M2MA End Function End Class Public Class M2MBFactory Implements AtomsFramework.IClassFactory Public Function CreateObject() As Object Implements AtomsFramework.IClassFactory.CreateObject Return New M2MB End Function End Class #End Region End Namespace --- NEW FILE: IOptimistic.vb --- Namespace Interfaces Public Interface IOptimistic Property GuidValue() As String Property Description() As String Property CreatedDate() As Date Property ModifiedDate() As Date End Interface Public Class COptimistic Implements IOptimistic Private m_guid As Guid = Guid.Empty Private m_description As String Private m_created As Date Private m_modified As Date Public Property Description() As String Implements IOptimistic.Description Get Return m_description End Get Set(ByVal Value As String) If Not String.Equals(m_description, Value) Then m_description = Value m_modified = Now End If End Set End Property Public Property GuidValue() As String Implements IOptimistic.GuidValue Get If m_guid.Equals(Guid.Empty) Then m_guid = Guid.NewGuid End If Return m_guid.ToString("N") End Get Set(ByVal value As String) m_guid = New Guid(value) End Set End Property Public Property CreatedDate() As Date Implements IOptimistic.CreatedDate Get Return m_created End Get Set(ByVal Value As Date) m_created = Value End Set End Property Public Property ModifiedDate() As Date Implements IOptimistic.ModifiedDate Get Return m_modified End Get Set(ByVal Value As Date) m_modified = Value End Set End Property Public Sub New() m_created = Now End Sub End Class Public Class IOptimisticFactory Implements AtomsFramework.IClassFactory Public Function CreateObject() As Object Implements AtomsFramework.IClassFactory.CreateObject Return New COptimistic End Function End Class End Namespace --- NEW FILE: TimestampTests.vb --- Imports AtomsFramework Imports NUnit.Framework Namespace Interfaces <TestFixture()> Public Class TimestampTests Private pbroker As PersistenceBroker Private opt As IOptimistic <TestFixtureSetUp()> Public Sub Init() Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory Dim retry As Boolean = True While retry = True Try 'Remove any existing test database System.IO.File.Delete(".\db1.mdb") retry = False Catch iox As IO.IOException 'file is in use - so we will loop around until it is released Catch ex As Exception retry = False End Try End While System.IO.File.Copy(".\original db1.mdb", ".\db1.mdb") pbroker = New PersistenceBroker pbroker.init() End Sub <TestFixtureTearDown()> Public Sub Dispose() pbroker.Dispose() pbroker = Nothing End Sub <SetUp()> Public Sub TestInit() opt = New COptimistic End Sub <Test()> Public Sub SaveAndModify() Dim gVal As String gVal = opt.GuidValue pbroker.GetObject(opt) opt.Description = "a" pbroker.PersistChanges(opt) pbroker.ClearCache() opt = New COptimistic opt.GuidValue = gVal pbroker.GetObject(opt) Assert.AreEqual("a", opt.Description) opt.Description = "b" pbroker.PersistChanges(opt) End Sub End Class End Namespace --- NEW FILE: ManyToManyTests.vb --- Imports AtomsFramework Imports NUnit.Framework Namespace Interfaces <TestFixture()> Public Class ManyToManyTests Private pbroker As PersistenceBroker Private A As iM2MA Private B As iM2MB <TestFixtureSetUp()> Public Sub Init() Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory Dim retry As Boolean = True While retry = True Try 'Remove any existing test database System.IO.File.Delete(".\db1.mdb") retry = False Catch iox As IO.IOException 'file is in use - so we will loop around until it is released GC.Collect() Catch ex As Exception retry = False End Try End While System.IO.File.Copy(".\original db1.mdb", ".\db1.mdb") pbroker = New PersistenceBroker pbroker.init() End Sub <TestFixtureTearDown()> Public Sub Dispose() pbroker.Dispose() pbroker = Nothing End Sub <Test()> Public Sub SaveThenRetrieve() Dim gval As String Dim x As Integer, y As Integer A = New M2MA pbroker.GetObject(A) A.Description = "A-one" gval = A.GUIDValue B = New M2MB B.Description = "B-one" A.M2MBCollection.Add(B) B.M2MACollection.Add(A) Dim B2 As iM2MB = New M2MB B2.Description = "B-two" A.M2MBCollection.Add(B2) Dim A2 As iM2MA = New M2MA A2.Description = "A-two" B2.M2MACollection.Add(A2) pbroker.PersistChanges() pbroker.ClearCache() A = New M2MA A.GUIDValue = gval pbroker.GetObject(A) Assert.AreEqual(2, A.M2MBCollection.Count) 'Order of guids can affect order of the collection If A.M2MBCollection.Item(0).M2MACollection.Count = 1 Then x = 0 y = 1 Else x = 1 y = 0 End If B = A.M2MBCollection.Item(x) Assert.IsNotNull(B) Assert.AreEqual("B-one", B.Description) Assert.AreEqual(1, B.M2MACollection.Count) Assert.AreEqual(A.GUIDValue, B.M2MACollection.Item(0).GUIDValue) B = A.M2MBCollection.Item(y) Assert.IsNotNull(B) Assert.AreEqual("B-two", B.Description) Assert.AreEqual(2, B.M2MACollection.Count) End Sub <Test()> Public Sub SaveThenDelete() Dim gval As String Dim x As Integer, y As Integer A = New M2MA pbroker.GetObject(A) A.Description = "A1" gval = A.GUIDValue B = New M2MB B.Description = "B1" A.M2MBCollection.Add(B) B.M2MACollection.Add(A) Dim B2 As M2MB = New M2MB B2.Description = "B2" A.M2MBCollection.Add(B2) B2.M2MACollection.Add(A) Dim A2 As M2MA = New M2MA A2.Description = "A2" B2.M2MACollection.Add(A2) pbroker.PersistChanges() pbroker.ClearCache() A = New M2MA A.GUIDValue = gval pbroker.GetObject(A) Assert.AreEqual(2, A.M2MBCollection.Count) 'Order of guids can affect order of the collection If A.M2MBCollection.Item(0).M2MACollection.Count = 1 Then x = 0 y = 1 Else x = 1 y = 0 End If B = A.M2MBCollection.Item(x) Assert.IsNotNull(B) Assert.AreEqual("B1", B.Description) Assert.AreEqual(1, B.M2MACollection.Count) Assert.AreEqual(A.GUIDValue, B.M2MACollection.Item(0).GUIDValue) B = A.M2MBCollection.Item(y) Assert.IsNotNull(B) Assert.AreEqual("B2", B.Description) Assert.AreEqual(2, B.M2MACollection.Count) 'Now delete B2 - because of autodelete, allobjects should be deleted pbroker.MarkForDeletion(B) pbroker.PersistChanges() A = New M2MA A.GUIDValue = gval pbroker.GetObject(A) Assert.IsFalse(pbroker.getInjectedObject(A).Persistent, "Object A is still persistent") End Sub <Test()> Public Sub SaveThenChangeKeyValues() Dim gvalA, gvalB As String Dim s As String A = New M2MA pbroker.GetObject(A) A.Description = "A1" gvalA = A.GUIDValue B = New M2MB B.Description = "B1" gvalB = B.GUIDValue A.M2MBCollection.Add(B) B.M2MACollection.Add(A) pbroker.PersistChanges() A.GUIDValue = Guid.NewGuid.ToString("N") pbroker.PersistChanges(A) s = "select * from ManyToManyAB where AGuidValue = """ & gvalA & """ and BGuidValue = """ & gvalB & """" Dim c As ResultCursor = pbroker.ProcessPureSql(s, pbroker.GetClassMap(GetType(NunitTests.Interfaces.iM2MA)).RelationalDatabase.GetConnection) Assert.AreEqual(0, c.ResultSet.ResultSet.Tables(0).Rows.Count) End Sub <Test()> Public Sub RemoveFromCollection() Dim gvalA, gvalB As String Dim s As String A = New M2MA pbroker.GetObject(A) A.Description = "A1" gvalA = A.GUIDValue B = New M2MB B.Description = "B1" gvalB = B.GUIDValue A.M2MBCollection.Add(B) B.M2MACollection.Add(A) pbroker.PersistChanges() pbroker.ClearCache() A = New M2MA A.GUIDValue = gvalA pbroker.GetObject(A) Assert.IsTrue(pbroker.getInjectedObject(A).Persistent) 'Being bi-directional this should result in both A/B objects having empty collections A.M2MBCollection.RemoveAt(0) pbroker.PersistChanges(A) pbroker.ClearCache() A = New M2MA A.GUIDValue = gvalA pbroker.GetObject(A) B = New M2MB B.GUIDValue = gvalB pbroker.GetObject(B) Assert.IsTrue(pbroker.getInjectedObject(A).Persistent) Assert.IsTrue(pbroker.getInjectedObject(B).Persistent) Assert.AreEqual(0, A.M2MBCollection.Count) Assert.AreEqual(0, B.M2MACollection.Count) End Sub End Class End Namespace --- NEW FILE: EmployeeInterfaceTests.vb --- Imports AtomsFramework Imports NUnit.Framework Namespace Interfaces <TestFixture()> Public Class EmployeeInterfaceTests Private pbroker As PersistenceBroker Private emp As IEmployee Private eventcount As Integer = 0 <TestFixtureSetUp()> Public Sub Init() Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory Dim retry As Boolean = True While retry = True Try 'Remove any existing test database System.IO.File.Delete(".\db1.mdb") retry = False Catch iox As IO.IOException 'file is in use - so we will loop around until it is released Catch ex As Exception retry = False End Try End While System.IO.File.Copy(".\original db1.mdb", ".\db1.mdb") pbroker = New PersistenceBroker pbroker.init() End Sub <TestFixtureTearDown()> Public Sub Dispose() pbroker.Dispose() pbroker = Nothing End Sub <SetUp()> Public Sub TestInit() emp = New EmployeeClass End Sub <Test()> Public Sub LoadEmployee_a() emp.Name = "a" pbroker.FindObject(emp) Assert.IsTrue(pbroker.getInjectedObject(emp).Persistent) Assert.AreEqual(emp.Workers.Count, 0) Assert.IsTrue(Not emp.ReportsTo Is Nothing) Assert.AreEqual(emp.ReportsTo.Name, "c") Dim emp2 As IEmployee emp2 = CType(emp.ReportsTo.Workers(0), IEmployee) Assert.AreEqual(emp2.Name, emp.Name) Assert.AreSame(emp2, emp) End Sub <Test()> Public Sub LoadEmployee_ac() emp.Name = "ac" pbroker.FindObject(emp) Assert.IsTrue(pbroker.getInjectedObject(emp).Persistent) Assert.AreEqual(emp.Workers.Count, 3) emp = CType(emp.Workers.Item(1), IEmployee) Assert.AreEqual(emp.Name, "aa") Assert.AreEqual(emp.ReportsTo.Name, "ac") End Sub <Test()> Public Sub CheckSchemaBasedProperties() emp.Name = "ac" pbroker.FindObject(emp) Assert.AreEqual(50, pbroker.getInjectedObject(emp).getFieldLengthByName("Name")) Assert.IsTrue(pbroker.getInjectedObject(emp).getFieldTypeByName("Name") Is GetType([String])) End Sub <Test()> Public Sub SaveInvalidEmployee() Dim emp2 As ValidatedEmployee emp2 = New ValidatedEmployee emp2.AllowValidation = False emp = emp2 emp.Name = "invalid" pbroker.FindObject(emp) Assert.IsFalse(pbroker.getInjectedObject(emp).Persistent) Try pbroker.PersistChanges(emp) Assert.Fail("Didn't throw the exception") Catch End Try Assert.IsFalse(pbroker.getInjectedObject(emp).Persistent) Assert.IsTrue(emp.ReportsTo Is Nothing) End Sub <Test()> Public Sub SaveValidEmployee() Dim emp2 As ValidatedEmployee emp2 = New ValidatedEmployee emp2.AllowValidation = True emp = emp2 emp.Name = "valid" pbroker.FindObject(emp) Assert.IsFalse(pbroker.getInjectedObject(emp).Persistent) pbroker.PersistChanges(emp) Assert.IsTrue(pbroker.getInjectedObject(emp).Persistent) Assert.IsTrue(emp.ReportsTo Is Nothing) End Sub <Test()> Public Sub SaveAndDeleteValidEmp1() Dim emp2 As ValidatedEmployee emp2 = New ValidatedEmployee emp2.AllowValidation = True emp2.Name = "validCheck" pbroker.FindObject(emp2) Assert.IsFalse(pbroker.getInjectedObject(emp2).Persistent) pbroker.PersistChanges(emp2) emp2.AllowValidation = False pbroker.MarkForDeletion(emp2) Try pbroker.PersistChanges() Assert.Fail("Didn't throw an exception") Catch End Try pbroker.ClearCache() emp2 = New ValidatedEmployee emp2.Name = "validCheck" pbroker.GetObject(emp2) Assert.IsTrue(pbroker.getInjectedObject(emp2).Persistent) End Sub <Test()> Public Sub SaveThenSaveWithoutRetrieve() Dim oidvalue As String emp.Name = "SaveThisEmp" pbroker.FindObject(emp) Assert.IsFalse(pbroker.getInjectedObject(emp).Persistent) pbroker.PersistChanges(emp) emp = New EmployeeClass emp.Name = "SaveThisEmp" 'Should update record, not try to insert pbroker.PersistChanges(emp) Assert.IsTrue(pbroker.getInjectedObject(emp).Persistent, "persistent check failed") End Sub '<Test(), Ignore("Not yet ready")> Public Sub CheckEvents() ' AddHandler emp.NameChanged, AddressOf NameChanged ' eventcount = 0 ' emp.Name = "Events" ' Assert.AreEqual(1, eventcount) ' pbroker.FindObject(emp) ' pbroker.PersistChanges(emp) ' emp = New EmployeeClass ' emp.Name = "Events" ' pbroker.FindObject(emp) ' 'Should not have any events from the previous handler still firing ' eventcount = 0 ' emp.Name = "New Name 1" ' Assert.AreEqual(0, eventcount) ' AddHandler emp.NameChanged, AddressOf NameChanged ' emp.Name = "New Name 2" ' Assert.AreEqual(1, eventcount) 'End Sub '<Test(), Ignore("Not yet ready")> Public Sub CheckEvents2() ' AddHandler emp.NameChanged, AddressOf NameChanged ' eventcount = 0 ' emp.Name = "Events2" ' Assert.AreEqual(1, eventcount) ' pbroker.FindObject(emp) ' pbroker.PersistChanges(emp) ' emp = New EmployeeClass ' eventcount = 0 ' AddHandler emp.NameChanged, AddressOf NameChanged ' emp.Name = "Events2" ' Assert.AreEqual(1, eventcount) ' 'Should retain the event handlers ' pbroker.FindObject(emp) ' emp.Name = "Events2a" ' Assert.AreEqual(2, eventcount) 'End Sub '<Test(), Ignore("Not yet ready")> Public Sub CheckEvents3() ' AddHandler emp.NameChanged, AddressOf NameChanged ' eventcount = 0 ' emp.Name = "Events3" ' Assert.AreEqual(1, eventcount) ' pbroker.FindObject(emp) ' pbroker.PersistChanges(emp) ' pbroker.ClearCache() ' emp = New EmployeeClass ' eventcount = 0 ' AddHandler emp.NameChanged, AddressOf NameChanged ' emp.Name = "Events3" ' Assert.AreEqual(1, eventcount) ' 'Event should fire when property is set during retrieve ' pbroker.FindObject(emp) ' Assert.AreEqual(2, eventcount) ' 'Check event is still wired up after find completes ' emp.Name = "Events3a" ' Assert.AreEqual(3, eventcount) 'End Sub '<Test(), Ignore("Not yet ready")> Public Sub CheckEvents4() ' AddHandler emp.NameChanged, AddressOf NameChanged ' eventcount = 0 ' emp.Name = "Events4" ' Assert.AreEqual(1, eventcount) ' pbroker.FindObject(emp) ' pbroker.PersistChanges(emp) ' emp = New EmployeeClass ' emp.Name = "Events4" ' pbroker.FindObject(emp) ' 'Should not have any events from the previous handler still firing ' eventcount = 0 ' emp.Name = "Events4a" ' Assert.AreEqual(0, eventcount) ' AddHandler emp.NameChanged, AddressOf NameChanged ' emp.Name = "Events4b" ' Assert.AreEqual(1, eventcount) 'End Sub 'Private Sub NameChanged(ByVal sender As Object, ByVal e As System.EventArgs) ' eventcount += 1 'End Sub <Test()> Public Sub CacheTest() 'Check that local object changes don't impact the cache emp.Name = "Initial2" pbroker.FindObject(emp) pbroker.PersistChanges(emp) pbroker.ClearCache() emp = New EmployeeClass emp.Name = "Initial2" pbroker.FindObject(emp) 'Change local copy emp.Name = "Final" emp = New EmployeeClass emp.Name = "Initial2" pbroker.FindObject(emp) Assert.AreEqual("Initial2", emp.Name) End Sub End Class End Namespace --- NEW FILE: IEmployee.vb --- Namespace Interfaces Public Interface IEmployee Property Name() As String Property ReportsTo() As IEmployee Property ReportsToName() As String Property Workers() As ArrayList Property Team() As InheritedClasses.CTeam Property TeamName() As String Event NameChanged As EventHandler End Interface Public Class EmployeeClass Implements IEmployee Private m_name As String Private m_parentName As String Private m_parent As IEmployee Private m_children As ArrayList Private m_team As InheritedClasses.CTeam Private m_teamName As String Public Event NameChanged As EventHandler Implements IEmployee.NameChanged Public Property Name() As String Implements IEmployee.Name Get Return m_name End Get Set(ByVal Value As String) m_name = Value RaiseEvent NameChanged(Me, New System.EventArgs) End Set End Property Public Property ReportsTo() As IEmployee Implements IEmployee.ReportsTo Get Return m_parent End Get Set(ByVal Value As IEmployee) If Not Value Is Nothing Then m_parent = Value m_parentName = Value.Name End If End Set End Property Public Property ReportsToName() As String Implements IEmployee.ReportsToName Get If m_parent Is Nothing Then Return m_parentName Else Return (m_parent.Name) End If End Get Set(ByVal Value As String) m_parentName = Value End Set End Property Public Property Workers() As ArrayList Implements IEmployee.Workers Get Return m_children End Get Set(ByVal Value As ArrayList) m_children = Value End Set End Property Public Property Team() As InheritedClasses.CTeam Implements IEmployee.Team Get Return m_team End Get Set(ByVal Value As InheritedClasses.CTeam) If Not Value Is Nothing Then m_team = Value m_teamName = Value.OIDValue End If End Set End Property Public Property TeamName() As String Implements IEmployee.TeamName Get If m_team Is Nothing Then Return m_teamName Else Return (m_team.OIDValue) End If End Get Set(ByVal Value As String) m_teamName = Value End Set End Property Public Sub New() MyBase.New() m_children = New ArrayList End Sub End Class Public Class IEmployeeFactory Implements AtomsFramework.IClassFactory Public Function CreateObject() As Object Implements AtomsFramework.IClassFactory.CreateObject Return New EmployeeClass End Function End Class End Namespace |