CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
DataPortalExceptionTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DataPortalExceptionTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Text;
11using Csla.Test.Security;
12using System.Data;
13using System.Data.SqlClient;
14using Csla.TestHelpers;
16
17#if !NUNIT
18using Microsoft.VisualStudio.TestTools.UnitTesting;
19
20#else
21using NUnit.Framework;
22using TestClass = NUnit.Framework.TestFixtureAttribute;
23using TestInitialize = NUnit.Framework.SetUpAttribute;
24using TestCleanup = NUnit.Framework.TearDownAttribute;
25using TestMethod = NUnit.Framework.TestAttribute;
26#endif
27
29{
30 [TestClass()]
32 {
33 private static TestDIContext _testDIContext;
34
36 public static void ClassInitialize(TestContext context)
37 {
38 _testDIContext = TestDIContextFactory.CreateDefaultContext();
39 }
40
41#if DEBUG
42 [TestMethod()]
43
44 public void CheckInnerExceptionsOnSave()
45 {
46 IDataPortal<DataPortal.TransactionalRoot> dataPortal = _testDIContext.CreateDataPortal<DataPortal.TransactionalRoot>();
48
49 DataPortal.TransactionalRoot root = DataPortal.TransactionalRoot.NewTransactionalRoot(dataPortal);
50 root.FirstName = "Billy";
51 root.LastName = "lastname";
52 root.SmallColumn = "too long for the database"; //normally would be prevented through validation
53
54 string baseException = string.Empty;
55 string baseInnerException = string.Empty;
56 string baseInnerInnerException = string.Empty;
57 string exceptionSource = string.Empty;
58
59 try
60 {
61 root = root.Save();
62 }
63 catch (Csla.DataPortalException ex)
64 {
65 baseException = ex.Message;
66 baseInnerException = ex.InnerException.Message;
67 baseInnerInnerException = ex.InnerException.InnerException?.Message;
68 exceptionSource = ex.InnerException.InnerException?.Source;
69 Assert.IsNull(ex.BusinessObject, "Business object shouldn't be returned");
70 }
71
72 //check base exception
73 Assert.IsTrue(baseException.StartsWith("DataPortal.Update failed"), "Exception should start with 'DataPortal.Update failed'");
74 Assert.IsTrue(baseException.Contains("String or binary data would be truncated."),
75 "Exception should contain 'String or binary data would be truncated.'");
76 //check inner exception
77 Assert.AreEqual("TransactionalRoot.DataPortal_Insert method call failed", baseInnerException);
78 //check inner exception of inner exception
79 Assert.AreEqual("String or binary data would be truncated.\r\nThe statement has been terminated.", baseInnerInnerException);
80
81 //check what caused inner exception's inner exception (i.e. the root exception)
82#if (NETFRAMEWORK)
83 Assert.AreEqual(".Net SqlClient Data Provider", exceptionSource);
84#else
85 Assert.AreEqual("Core .Net SqlClient Data Provider", exceptionSource);
86#endif
87
88 //verify that the implemented method, DataPortal_OnDataPortal
89 //was called for the business object that threw the exception
90 Assert.AreEqual("Called", TestResults.GetResult("OnDataPortalException"));
91 }
92#endif
93
94 [TestMethod()]
96 {
97 IDataPortal<DataPortal.TransactionalRoot> dataPortal = _testDIContext.CreateDataPortal<DataPortal.TransactionalRoot>();
99
100 string baseException = string.Empty;
101 string baseInnerException = string.Empty;
102 string baseInnerInnerException = string.Empty;
103
104 try
105 {
106 //this will throw an exception
108 }
109 catch (Csla.DataPortalException ex)
110 {
111 baseException = ex.Message;
112 baseInnerException = ex.InnerException.Message;
113 baseInnerInnerException = ex.InnerException.InnerException.Message;
114 }
115
116 Assert.IsTrue(baseException.StartsWith("DataPortal.Delete failed"), "Should start with 'DataPortal.Delete failed'");
117 Assert.IsTrue(baseException.Contains("DataPortal_Delete: you chose an unlucky number"));
118 Assert.AreEqual("TransactionalRoot.DataPortal_Delete method call failed", baseInnerException);
119 Assert.AreEqual("DataPortal_Delete: you chose an unlucky number", baseInnerInnerException);
120
121 //verify that the implemented method, DataPortal_OnDataPortal
122 //was called for the business object that threw the exception
123 Assert.AreEqual("Called", TestResults.GetResult("OnDataPortalException"));
124 }
125
126 [TestMethod()]
128 {
129 IDataPortal<DataPortal.TransactionalRoot> dataPortal = _testDIContext.CreateDataPortal<DataPortal.TransactionalRoot>();
131
132 string baseException = string.Empty;
133 string baseInnerException = string.Empty;
134 string baseInnerInnerException = string.Empty;
135
136 try
137 {
138 //this will throw an exception
141 }
142 catch (Csla.DataPortalException ex)
143 {
144 baseException = ex.Message;
145 baseInnerException = ex.InnerException.Message;
146 baseInnerInnerException = ex.InnerException.InnerException.Message;
147 }
148
149 Assert.IsTrue(baseException.StartsWith("DataPortal.Fetch failed"), "Should start with 'DataPortal.Fetch failed'");
150 Assert.IsTrue(baseException.Contains("DataPortal_Fetch: you chose an unlucky number"),
151 "Should contain with 'DataPortal_Fetch: you chose an unlucky number'");
152 Assert.AreEqual("TransactionalRoot.DataPortal_Fetch method call failed", baseInnerException);
153 Assert.AreEqual("DataPortal_Fetch: you chose an unlucky number", baseInnerInnerException);
154
155 //verify that the implemented method, DataPortal_OnDataPortal
156 //was called for the business object that threw the exception
157 Assert.AreEqual("Called", TestResults.GetResult("OnDataPortalException"));
158 }
159 }
160}
This exception is returned for any errors occurring during the server-side DataPortal invocation.
Client side data portal used for making asynchronous data portal calls in .NET.
Definition: DataPortalT.cs:24
static void DeleteTransactionalRoot(int ID, IDataPortal< TransactionalRoot > dataPortal)
static TransactionalRoot GetTransactionalRoot(int ID, IDataPortal< TransactionalRoot > dataPortal)
Static dictionary-like class that offers similar functionality to GlobalContext This is used in tests...
Definition: TestResults.cs:21
static void Reinitialise()
Reinitialise the dictionary, clearing any existing results, ready for the next test
Definition: TestResults.cs:69
static string GetResult(string key)
Get a result of an operation from the underlying results dictionary
Definition: TestResults.cs:49
Type to carry context information for DI in unit tests
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17