1+ using System ;
2+ using System . IO ;
3+ using System . Text ;
4+ using LibGit2Sharp . Tests . TestHelpers ;
5+ using Xunit ;
6+
7+ namespace LibGit2Sharp . Tests
8+ {
9+ public class SetErrorFixture : BaseFixture
10+ {
11+
12+ private const string simpleExceptionMessage = "This is a simple exception message." ;
13+ private const string aggregateExceptionMessage = "This is aggregate exception." ;
14+ private const string outerExceptionMessage = "This is an outer exception." ;
15+ private const string innerExceptionMessage = "This is an inner exception." ;
16+ private const string innerExceptionMessage2 = "This is inner exception #2." ;
17+
18+ private const string expectedInnerExceptionHeaderText = "Inner exception:" ;
19+ private const string expectedAggregateExceptionHeaderText = "Contained exception:" ;
20+ private const string expectedAggregateExceptionsHeaderText = "Contained exceptions:" ;
21+
22+ [ Fact ]
23+ public void FormatSimpleException ( )
24+ {
25+ Exception exceptionToThrow = new Exception ( simpleExceptionMessage ) ;
26+ string expectedMessage = simpleExceptionMessage ;
27+
28+ AssertExpectedExceptionMessage ( expectedMessage , exceptionToThrow ) ;
29+ }
30+
31+ [ Fact ]
32+ public void FormatExceptionWithInnerException ( )
33+ {
34+ Exception exceptionToThrow = new Exception ( outerExceptionMessage , new Exception ( innerExceptionMessage ) ) ;
35+
36+ StringBuilder sb = new StringBuilder ( ) ;
37+ sb . AppendLine ( outerExceptionMessage ) ;
38+ sb . AppendLine ( ) ;
39+ AppendIndentedLine ( sb , expectedInnerExceptionHeaderText , 0 ) ;
40+ AppendIndentedText ( sb , innerExceptionMessage , 1 ) ;
41+ string expectedMessage = sb . ToString ( ) ;
42+
43+ AssertExpectedExceptionMessage ( expectedMessage , exceptionToThrow ) ;
44+ }
45+
46+ [ Fact ]
47+ public void FormatAggregateException ( )
48+ {
49+ Exception exceptionToThrow = new AggregateException ( aggregateExceptionMessage , new Exception ( innerExceptionMessage ) , new Exception ( innerExceptionMessage2 ) ) ;
50+
51+ StringBuilder sb = new StringBuilder ( ) ;
52+ sb . AppendLine ( aggregateExceptionMessage ) ;
53+ sb . AppendLine ( ) ;
54+
55+ AppendIndentedLine ( sb , expectedAggregateExceptionsHeaderText , 0 ) ;
56+
57+ AppendIndentedLine ( sb , innerExceptionMessage , 1 ) ;
58+ sb . AppendLine ( ) ;
59+
60+ AppendIndentedText ( sb , innerExceptionMessage2 , 1 ) ;
61+
62+ string expectedMessage = sb . ToString ( ) ;
63+
64+ AssertExpectedExceptionMessage ( expectedMessage , exceptionToThrow ) ;
65+ }
66+
67+ private void AssertExpectedExceptionMessage ( string expectedMessage , Exception exceptionToThrow )
68+ {
69+ Exception thrownException = null ;
70+
71+ ObjectId id = new ObjectId ( "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" ) ;
72+
73+ string repoPath = InitNewRepository ( ) ;
74+ using ( var repo = new Repository ( repoPath ) )
75+ {
76+ repo . ObjectDatabase . AddBackend ( new ThrowingOdbBackend ( exceptionToThrow ) , priority : 1 ) ;
77+
78+ try
79+ {
80+ repo . Lookup < Blob > ( id ) ;
81+ }
82+ catch ( Exception ex )
83+ {
84+ thrownException = ex ;
85+ }
86+ }
87+
88+ Assert . NotNull ( thrownException ) ;
89+ Assert . Equal ( expectedMessage , thrownException . Message ) ;
90+ }
91+
92+ private void AppendIndentedText ( StringBuilder sb , string text , int indentLevel )
93+ {
94+ sb . AppendFormat ( "{0}{1}" , IndentString ( indentLevel ) , text ) ;
95+ }
96+
97+ private void AppendIndentedLine ( StringBuilder sb , string text , int indentLevel )
98+ {
99+ sb . AppendFormat ( "{0}{1}{2}" , IndentString ( indentLevel ) , text , Environment . NewLine ) ;
100+ }
101+
102+ private string IndentString ( int level )
103+ {
104+ return new string ( ' ' , level * 4 ) ;
105+ }
106+
107+ #region ThrowingOdbBackend
108+
109+ private class ThrowingOdbBackend : OdbBackend
110+ {
111+ private Exception exceptionToThrow ;
112+
113+ public ThrowingOdbBackend ( Exception exceptionToThrow )
114+ {
115+ this . exceptionToThrow = exceptionToThrow ;
116+ }
117+
118+ protected override OdbBackendOperations SupportedOperations
119+ {
120+ get
121+ {
122+ return OdbBackendOperations . Read |
123+ OdbBackendOperations . ReadPrefix |
124+ OdbBackendOperations . Write |
125+ OdbBackendOperations . WriteStream |
126+ OdbBackendOperations . Exists |
127+ OdbBackendOperations . ExistsPrefix |
128+ OdbBackendOperations . ForEach |
129+ OdbBackendOperations . ReadHeader ;
130+ }
131+ }
132+
133+ public override int Read ( ObjectId oid , out UnmanagedMemoryStream data , out ObjectType objectType )
134+ {
135+ throw this . exceptionToThrow ;
136+ }
137+
138+ public override int ReadPrefix ( string shortSha , out ObjectId id , out UnmanagedMemoryStream data , out ObjectType objectType )
139+ {
140+ throw this . exceptionToThrow ;
141+ }
142+
143+ public override int Write ( ObjectId oid , Stream dataStream , long length , ObjectType objectType )
144+ {
145+ throw this . exceptionToThrow ;
146+ }
147+
148+ public override int WriteStream ( long length , ObjectType objectType , out OdbBackendStream stream )
149+ {
150+ throw this . exceptionToThrow ;
151+ }
152+
153+ public override bool Exists ( ObjectId oid )
154+ {
155+ throw this . exceptionToThrow ;
156+ }
157+
158+ public override int ExistsPrefix ( string shortSha , out ObjectId found )
159+ {
160+ throw this . exceptionToThrow ;
161+ }
162+
163+ public override int ReadHeader ( ObjectId oid , out int length , out ObjectType objectType )
164+ {
165+ throw this . exceptionToThrow ;
166+ }
167+
168+ public override int ReadStream ( ObjectId oid , out OdbBackendStream stream )
169+ {
170+ throw this . exceptionToThrow ;
171+ }
172+
173+ public override int ForEach ( ForEachCallback callback )
174+ {
175+ throw this . exceptionToThrow ;
176+ }
177+ }
178+
179+ #endregion
180+
181+ }
182+ }
0 commit comments