From: Richard B. <rb...@us...> - 2005-02-28 23:06:00
|
Update of /cvsroot/jcframework/Nunit/Interfaces In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16284/Interfaces Added Files: ManyToManyClasses.vb ManyToManyTests.vb Log Message: New unit tests for many-to-many associations for non-inherited objects. Changed a few unit tests to work under new restrictrions for injected (tracked) objects. --- NEW FILE: ManyToManyClasses.vb --- Imports Atoms.Framework 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 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 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 Atoms.Framework.IClassFactory Public Function CreateObject() As Object Implements Atoms.Framework.IClassFactory.CreateObject Return New M2MA End Function End Class Public Class M2MBFactory Implements Atoms.Framework.IClassFactory Public Function CreateObject() As Object Implements Atoms.Framework.IClassFactory.CreateObject Return New M2MB End Function End Class #End Region End Namespace --- NEW FILE: ManyToManyTests.vb --- Imports Atoms.Framework Imports NUnit.Framework Namespace Interfaces <TestFixture()> Public Class ManyToManyTests Private pbroker As CPersistenceBroker 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 CPersistenceBroker 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.StartTracking(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.StartTracking(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.StartTracking(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 CCursor = pbroker.ProcessPureSQL(s, pbroker.getClassMap(GetType(NunitTests.Interfaces.iM2MA)).RelationalDatabase.getConnection(Nothing)) 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.StartTracking(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 |