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.
MethodCallerTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="MethodCallerTests.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.IO;
10using System.Reflection;
11using Csla.Reflection;
12using UnitDriven;
13using System.Collections.Generic;
14
15#if !NUNIT
16using Microsoft.VisualStudio.TestTools.UnitTesting;
17#else
18using NUnit.Framework;
19using TestClass = NUnit.Framework.TestFixtureAttribute;
20using TestInitialize = NUnit.Framework.SetUpAttribute;
21using TestCleanup = NUnit.Framework.TearDownAttribute;
22using TestMethod = NUnit.Framework.TestAttribute;
23#endif
24
26{
27 [TestClass]
28 public class MethodCallerTests
29 {
30 [TestMethod]
31 public void CallSuccess()
32 {
33 Csla.Reflection.MethodCaller.CallMethod(this, "DoSuccess", null);
34 }
35
36 [TestMethod]
37 public void CallException()
38 {
39 try
40 {
41 Csla.Reflection.MethodCaller.CallMethod(this, "DoException", null);
42 }
43 catch (Exception ex)
44 {
45 Assert.IsInstanceOfType(ex, typeof(Csla.Reflection.CallMethodException), "Should be a CallMethodException");
46 }
47 }
48
49 [TestMethod]
50 public void CallInnerException()
51 {
52 try
53 {
54 Csla.Reflection.MethodCaller.CallMethod(this, "DoInnerException", null);
55 }
56 catch (Exception ex)
57 {
58#if MSTEST
59 Assert.IsInstanceOfType(ex, typeof(Csla.Reflection.CallMethodException), "Should be a CallMethodException");
60 Assert.IsInstanceOfType(ex.InnerException, typeof(MemberAccessException), "Inner should be a MemberAccessException");
61 Assert.IsInstanceOfType(ex.InnerException.InnerException, typeof(NotSupportedException), "Inner inner should be a NotSupportedException");
62#else
63 Assert.IsInstanceOfType(typeof(Csla.Reflection.CallMethodException), ex, "Should be a CallMethodException");
64 Assert.IsInstanceOfType(typeof(MemberAccessException), ex.InnerException, "Inner should be a MemberAccessException");
65 Assert.IsInstanceOfType(typeof(NotSupportedException), ex.InnerException.InnerException, "Inner inner should be a NotSupportedException");
66#endif
67 }
68 }
69
70 [TestMethod]
72 {
73 Dictionary<string, List<int>> table = new Dictionary<string, List<int>>();
74
75 table.Add("Column1", new List<int>());
76 table["Column1"].Add(1);
77 table["Column1"].Add(2);
78 var returnValue = Csla.Reflection.MethodCaller.CallMethod(this, "GetData", table["Column1"]);
79 Assert.AreEqual(1, returnValue, "table.Rows");
80 returnValue = Csla.Reflection.MethodCaller.CallMethod(this, "GetData", table["Column1"].ToArray());
81 Assert.AreEqual(2, returnValue, "rows");
82 returnValue = Csla.Reflection.MethodCaller.CallMethod(this, "GetData", new string[] { "a", "b" });
83 Assert.AreEqual(3, returnValue, "string[]");
84 returnValue = Csla.Reflection.MethodCaller.CallMethod(this, "GetData", 1, table);
85 Assert.AreEqual(4, returnValue, "1, rows");
86 returnValue = Csla.Reflection.MethodCaller.CallMethod(this, "GetData", 1, new string[] { "a", "b" });
87 Assert.AreEqual(5, returnValue, "1, string[]");
88 }
89
90 [TestMethod]
91 public void CallSuccessParams()
92 {
93 var returnValue = Csla.Reflection.MethodCaller.CallMethod(this, "MethodWithParams", new object[] { 1, 2 });
94 Assert.AreEqual(returnValue, 1);
95 returnValue = Csla.Reflection.MethodCaller.CallMethod(this, "MethodWithParams", new object[] { 123 });
96 Assert.AreEqual(returnValue, 1);
97 returnValue = Csla.Reflection.MethodCaller.CallMethod(this, "MethodWithParams");
98 Assert.AreEqual(returnValue, 1);
99 }
100
101 [TestMethod]
103 {
104
105 var returnValue1 = Csla.Reflection.MethodCaller.CallMethod(new Caller1(), "Method1");
106 Assert.AreEqual(returnValue1, 1);
107 var returnValue2 = Csla.Reflection.MethodCaller.CallMethod(new Caller2(), "Method1");
108 Assert.AreEqual(returnValue2, 2);
109 var returnValue3 = Csla.Reflection.MethodCaller.CallMethod(new Caller3(), "Method1");
110 Assert.AreEqual(returnValue3, 2);
111
112 returnValue1 = Csla.Reflection.MethodCaller.CallMethod(new Caller1(), "Method2", 0);
113 Assert.AreEqual(returnValue1, 1);
114 returnValue2 = Csla.Reflection.MethodCaller.CallMethod(new Caller2(), "Method2", 0);
115 Assert.AreEqual(returnValue2, 2);
116 returnValue3 = Csla.Reflection.MethodCaller.CallMethod(new Caller3(), "Method2", 0);
117 Assert.AreEqual(returnValue3, 2);
118 }
119
120#if DEBUG
121#if !WINDOWS_PHONE
122 [TestMethod]
123 [Ignore]
124 public void CallDynamicIsFasterThanReflectionSuccess()
125 {
126 int times = 100000;
127
128 TimeSpan dynamicTime, reflectionTime;
129 var start = DateTime.Now;
130 for (int x = 0; x < times; x++)
131 {
132 Csla.Reflection.MethodCaller.CallMethod(this, "DoSuccess");
133 }
134 var end = DateTime.Now;
135 dynamicTime = end - start;
136
137 start = DateTime.Now;
138 for (int x = 0; x < times; x++)
139 {
140 var doSuccess = this.GetType().GetMethod("DoSuccess", BindingFlags.Instance | BindingFlags.Public);
141 doSuccess.Invoke(this, null);
142 }
143 end = DateTime.Now;
144 reflectionTime = end - start;
145
146 Assert.IsTrue(dynamicTime < reflectionTime, string.Format("Dynamic {0} should be faster than reflection {1}", dynamicTime, reflectionTime));
147 }
148#endif
149#endif
150
151 [TestMethod]
152 [ExpectedException(typeof(CallMethodException))]
154 {
155 MemoryStream ms = new MemoryStream();
156 var t = this.GetType();
157 var foo = t.GetMethod("foo");
158 Csla.Reflection.MethodCaller.CallMethod(this, foo, "This should be a MemoryStream object not a string...");
159 }
160
161 [TestMethod]
163 {
164 MemoryStream ms = new MemoryStream();
165 var t = this.GetType();
166 var foo = t.GetMethod("foo");
167 Csla.Reflection.MethodCaller.CallMethod(this, foo, ms);
168 }
169
170 public void foo(MemoryStream ms)
171 {
172 // As ridiculous as this Assert seems, it's actually possible for ms
173 // to be the wrong type. See bug #550.
174#if MSTEST
175 Assert.IsInstanceOfType(ms, typeof(MemoryStream));
176#else
177 Assert.IsInstanceOfType(typeof(MemoryStream), ms);
178#endif
179 }
180
181 public void DoSuccess()
182 {
183 }
184
185 public void DoException()
186 {
187 throw new NotSupportedException("DoException");
188 }
189
190 public void DoInnerException()
191 {
192 try
193 {
194 DoException();
195 }
196 catch (Exception ex)
197 {
198 throw new MemberAccessException("DoInnerException", ex);
199 }
200 }
201
202 public int GetData(object rows)
203 {
204 return 1;
205 }
206
207 public int GetData(params int[] rows)
208 {
209 return 2;
210 }
211
212 public int GetData(params string[] rows)
213 {
214 return 3;
215 }
216
217 public int GetData(int x, Dictionary<string, List<int>> rows)
218 {
219 return 4;
220 }
221
222 public int GetData(int x, params string[] rows)
223 {
224 return 5;
225 }
226
227 public int MethodWithParams(params object[] e)
228 {
229 return 1;
230 }
231 }
232}
This exception is returned from the CallMethod method in the server-side DataPortal and contains the ...
int GetData(int x, params string[] rows)
int GetData(int x, Dictionary< string, List< int > > rows)