Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit aedd96c

Browse files
committed
Merge pull request #192 from obones/Issue196
Issue196
2 parents ee3ccb0 + 3fba9fa commit aedd96c

File tree

3 files changed

+301
-2
lines changed

3 files changed

+301
-2
lines changed

project/Remote/XmlConversionUtil.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,69 @@ public static object ConvertXmlToObject(Type messageType, string message)
121121

122122
return messageObj;
123123
}
124+
125+
/// <summary>
126+
/// Indicates whether or not a message string can be converted into an object.
127+
/// </summary>
128+
/// <param name="messageType">The type of message.</param>
129+
/// <param name="message">The XML of the message.</param>
130+
/// <returns>true if the message can be deserialized to the given message type.</returns>
131+
public static bool CanConvertXmlToObject(Type messageType, string message)
132+
{
133+
// Make sure the serialiser has been loaded
134+
if (!messageSerialisers.ContainsKey(messageType))
135+
{
136+
messageSerialisers[messageType] = new XmlSerializer(messageType);
137+
}
138+
139+
// Perform the test
140+
try
141+
{
142+
using (StringReader reader = new StringReader(message))
143+
using (XmlReader xmlReader = XmlReader.Create(reader))
144+
return messageSerialisers[messageType].CanDeserialize(xmlReader);
145+
}
146+
catch (XmlException)
147+
{
148+
return false;
149+
}
150+
}
151+
#endregion
152+
153+
#region ConvertObjectToXml()
154+
/// <summary>
155+
/// Converts an object into a message string
156+
/// </summary>
157+
/// <param name="anObject">The object of the message.</param>
158+
/// <returns>The XML of the message.</returns>
159+
public static string ConvertObjectToXml(object anObject)
160+
{
161+
string result = null;
162+
163+
Type messageType = anObject.GetType();
164+
165+
// Make sure the serialiser has been loaded
166+
if (!messageSerialisers.ContainsKey(messageType))
167+
{
168+
messageSerialisers[messageType] = new XmlSerializer(messageType);
169+
}
170+
171+
// Perform the actual conversion
172+
XmlWriterSettings settings = new XmlWriterSettings();
173+
settings.OmitXmlDeclaration = true;
174+
175+
using (StringWriter stringWriter = new StringWriter())
176+
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
177+
{
178+
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
179+
namespaces.Add("", "");
180+
181+
messageSerialisers[messageType].Serialize(xmlWriter, anObject, namespaces);
182+
result = stringWriter.ToString();
183+
}
184+
185+
return result;
186+
}
124187
#endregion
125188

126189
#region ConvertXmlToRequest()

project/UnitTests/Core/SourceControl/MultiSourceControlTest.cs

Lines changed: 182 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
34
using Exortech.NetReflector;
45
using NMock;
56
using NUnit.Framework;
67
using ThoughtWorks.CruiseControl.Core;
78
using ThoughtWorks.CruiseControl.Core.Sourcecontrol;
9+
using ThoughtWorks.CruiseControl.Remote;
810

911
namespace ThoughtWorks.CruiseControl.UnitTests.Core.Sourcecontrol
1012
{
@@ -136,8 +138,186 @@ private DynamicMock CreateModificationsSourceControlMock(Modification[] mods, In
136138
return mock;
137139
}
138140

139-
140-
[Test]
141+
private class MockSourceControl : ISourceControl
142+
{
143+
public Modification[] GetModifications(IIntegrationResult from, IIntegrationResult to)
144+
{
145+
Assert.AreEqual("first", from.SourceControlData[0].Value, "SourceControlData[0].Value");
146+
147+
to.SourceControlData.Clear();
148+
to.SourceControlData.Add(from.SourceControlData[0]);
149+
150+
return new Modification[] { };
151+
}
152+
153+
public void LabelSourceControl(IIntegrationResult result) { }
154+
public void GetSource(IIntegrationResult result) { }
155+
public void Initialize(IProject project) { }
156+
public void Purge(IProject project) { }
157+
}
158+
159+
160+
[Test]
161+
public void PassesIndividualSourceDataAndCombines()
162+
{
163+
IntegrationResult from = IntegrationResultMother.CreateSuccessful(DateTime.Now);
164+
IntegrationResult to = IntegrationResultMother.CreateSuccessful(DateTime.Now.AddDays(10));
165+
166+
string scValue = null;
167+
List<NameValuePair> list = new List<NameValuePair>();
168+
169+
list.Add(new NameValuePair("name0", "first"));
170+
scValue = XmlConversionUtil.ConvertObjectToXml(list);
171+
from.SourceControlData.Add(new NameValuePair("sc0", scValue));
172+
list.Clear();
173+
174+
list.Add(new NameValuePair("name1", "first"));
175+
list.Add(new NameValuePair("name2", "first"));
176+
scValue = XmlConversionUtil.ConvertObjectToXml(list);
177+
from.SourceControlData.Add(new NameValuePair("sc1", scValue));
178+
list.Clear();
179+
180+
List<ISourceControl> sourceControls = new List<ISourceControl>();
181+
sourceControls.Add(new MockSourceControl());
182+
sourceControls.Add(new MockSourceControl());
183+
184+
MultiSourceControl multiSourceControl = new MultiSourceControl();
185+
multiSourceControl.SourceControls = sourceControls.ToArray();
186+
187+
//// EXECUTE
188+
ArrayList returnedMods = new ArrayList(multiSourceControl.GetModifications(from, to));
189+
190+
//// VERIFY
191+
Assert.AreEqual(from.SourceControlData.Count, to.SourceControlData.Count, "SourceControlData.Count");
192+
193+
list.Add(new NameValuePair("name0", "first"));
194+
Assert.AreEqual(XmlConversionUtil.ConvertObjectToXml(list), to.SourceControlData[0].Value, "SourceControlData[0].Value");
195+
list.Clear();
196+
Assert.AreEqual("sc0", to.SourceControlData[0].Name, "SourceControlData[0].Name");
197+
198+
list.Add(new NameValuePair("name1", "first"));
199+
Assert.AreEqual(XmlConversionUtil.ConvertObjectToXml(list), to.SourceControlData[1].Value, "SourceControlData[1].Value");
200+
list.Clear();
201+
Assert.AreEqual("sc1", to.SourceControlData[1].Name, "SourceControlData[1].Name");
202+
}
203+
204+
[Test]
205+
public void PassesIndividualSourceDataAndCombinesSingleSourceControl()
206+
{
207+
IntegrationResult from = IntegrationResultMother.CreateSuccessful(DateTime.Now);
208+
IntegrationResult to = IntegrationResultMother.CreateSuccessful(DateTime.Now.AddDays(10));
209+
210+
string scValue = null;
211+
List<NameValuePair> list = new List<NameValuePair>();
212+
213+
list.Add(new NameValuePair("name0", "first"));
214+
scValue = XmlConversionUtil.ConvertObjectToXml(list);
215+
from.SourceControlData.Add(new NameValuePair("sc0", scValue));
216+
list.Clear();
217+
218+
List<ISourceControl> sourceControls = new List<ISourceControl>();
219+
sourceControls.Add(new MockSourceControl());
220+
221+
MultiSourceControl multiSourceControl = new MultiSourceControl();
222+
multiSourceControl.SourceControls = sourceControls.ToArray();
223+
224+
//// EXECUTE
225+
ArrayList returnedMods = new ArrayList(multiSourceControl.GetModifications(from, to));
226+
227+
//// VERIFY
228+
Assert.AreEqual(from.SourceControlData.Count, to.SourceControlData.Count, "SourceControlData.Count");
229+
230+
list.Add(new NameValuePair("name0", "first"));
231+
Assert.AreEqual(XmlConversionUtil.ConvertObjectToXml(list), to.SourceControlData[0].Value, "SourceControlData[0].Value");
232+
list.Clear();
233+
Assert.AreEqual("sc0", to.SourceControlData[0].Name, "SourceControlData[0].Name");
234+
}
235+
236+
[Test]
237+
public void MigratesSourceControlDataToNewFormat()
238+
{
239+
IntegrationResult from = IntegrationResultMother.CreateSuccessful(DateTime.Now);
240+
IntegrationResult to = IntegrationResultMother.CreateSuccessful(DateTime.Now.AddDays(10));
241+
242+
ArrayList mocks = new ArrayList();
243+
mocks.Add(CreateModificationsSourceControlMock(new Modification[] { }, from, to));
244+
mocks.Add(CreateModificationsSourceControlMock(null, from, to));
245+
246+
ArrayList scList = new ArrayList();
247+
foreach (DynamicMock mock in mocks)
248+
{
249+
scList.Add(mock.MockInstance);
250+
}
251+
scList.Add(new MockSourceControl());
252+
scList.Add(new MockSourceControl());
253+
254+
from.SourceControlData.Add(new NameValuePair("test", "first"));
255+
from.SourceControlData.Add(new NameValuePair("commit", "first"));
256+
257+
MultiSourceControl multiSourceControl = new MultiSourceControl();
258+
multiSourceControl.SourceControls = (ISourceControl[])scList.ToArray(typeof(ISourceControl));
259+
260+
//// EXECUTE
261+
ArrayList returnedMods = new ArrayList(multiSourceControl.GetModifications(from, to));
262+
263+
//// VERIFY
264+
Assert.AreEqual(4, to.SourceControlData.Count, "SourceControlData.Count");
265+
266+
List<NameValuePair> list = new List<NameValuePair>();
267+
268+
Assert.AreEqual(XmlConversionUtil.ConvertObjectToXml(list), to.SourceControlData[0].Value, "SourceControlData[0].Value");
269+
Assert.AreEqual("sc0", to.SourceControlData[0].Name, "SourceControlData[0].Name");
270+
271+
Assert.AreEqual(XmlConversionUtil.ConvertObjectToXml(list), to.SourceControlData[1].Value, "SourceControlData[1].Value");
272+
Assert.AreEqual("sc1", to.SourceControlData[1].Name, "SourceControlData[1].Name");
273+
274+
list.Add(new NameValuePair("test", "first"));
275+
Assert.AreEqual(XmlConversionUtil.ConvertObjectToXml(list), to.SourceControlData[2].Value, "SourceControlData[2].Value");
276+
list.Clear();
277+
Assert.AreEqual("sc2", to.SourceControlData[2].Name, "SourceControlData[2].Name");
278+
279+
list.Add(new NameValuePair("commit", "first"));
280+
Assert.AreEqual(XmlConversionUtil.ConvertObjectToXml(list), to.SourceControlData[3].Value, "SourceControlData[3].Value");
281+
list.Clear();
282+
Assert.AreEqual("sc3", to.SourceControlData[3].Name, "SourceControlData[3].Name");
283+
}
284+
285+
[Test]
286+
public void MigratesSourceControlDataToNewFormatSameSourceControlCount()
287+
{
288+
IntegrationResult from = IntegrationResultMother.CreateSuccessful(DateTime.Now);
289+
IntegrationResult to = IntegrationResultMother.CreateSuccessful(DateTime.Now.AddDays(10));
290+
291+
ArrayList scList = new ArrayList();
292+
scList.Add(new MockSourceControl());
293+
scList.Add(new MockSourceControl());
294+
295+
from.SourceControlData.Add(new NameValuePair("test", "first"));
296+
from.SourceControlData.Add(new NameValuePair("commit", "first"));
297+
298+
MultiSourceControl multiSourceControl = new MultiSourceControl();
299+
multiSourceControl.SourceControls = (ISourceControl[])scList.ToArray(typeof(ISourceControl));
300+
301+
//// EXECUTE
302+
ArrayList returnedMods = new ArrayList(multiSourceControl.GetModifications(from, to));
303+
304+
//// VERIFY
305+
Assert.AreEqual(2, to.SourceControlData.Count, "SourceControlData.Count");
306+
307+
List<NameValuePair> list = new List<NameValuePair>();
308+
309+
list.Add(new NameValuePair("test", "first"));
310+
Assert.AreEqual(XmlConversionUtil.ConvertObjectToXml(list), to.SourceControlData[0].Value, "SourceControlData[0].Value");
311+
list.Clear();
312+
Assert.AreEqual("sc0", to.SourceControlData[0].Name, "SourceControlData[0].Name");
313+
314+
list.Add(new NameValuePair("commit", "first"));
315+
Assert.AreEqual(XmlConversionUtil.ConvertObjectToXml(list), to.SourceControlData[1].Value, "SourceControlData[1].Value");
316+
list.Clear();
317+
Assert.AreEqual("sc1", to.SourceControlData[1].Name, "SourceControlData[1].Name");
318+
}
319+
320+
[Test]
141321
public void IfRequireChangesFromAllTrueAndAllSourceControlHasModificationsThenReturnMods()
142322
{
143323
//// SETUP

project/core/sourcecontrol/MultiSourceControl.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using Exortech.NetReflector;
33
using ThoughtWorks.CruiseControl.Core.Tasks;
4+
using ThoughtWorks.CruiseControl.Remote;
45
using ThoughtWorks.CruiseControl.Remote.Parameters;
56
using System.Xml;
67

@@ -115,10 +116,60 @@ public ISourceControl[] SourceControls
115116
/// <remarks></remarks>
116117
public override Modification[] GetModifications(IIntegrationResult from, IIntegrationResult to)
117118
{
119+
List<NameValuePair> originalSourceControlData = new List<NameValuePair>();
120+
List<NameValuePair> finalSourceControlData = new List<NameValuePair>();
121+
122+
var sourceControlDataType = from.SourceControlData.GetType();
123+
124+
// check that the source control data given to us is in the list of list format
125+
// if not, then convert it now
126+
var fromSourceControlDataCount = from.SourceControlData.Count;
127+
if (fromSourceControlDataCount > 0 &&
128+
(fromSourceControlDataCount != SourceControls.Length ||
129+
!XmlConversionUtil.CanConvertXmlToObject(sourceControlDataType, from.SourceControlData[0].Value)
130+
)
131+
)
132+
{
133+
var conversionList = new List<NameValuePair>();
134+
135+
for (int i = 0; i < SourceControls.Length; i++)
136+
originalSourceControlData.Add(new NameValuePair(string.Format("sc{0:d}", i), ""));
137+
138+
int scdIndex = fromSourceControlDataCount - 1;
139+
for (int i = originalSourceControlData.Count - 1; i >= 0; i--)
140+
{
141+
conversionList.Clear();
142+
143+
if (scdIndex >= 0)
144+
if (!XmlConversionUtil.CanConvertXmlToObject(sourceControlDataType, from.SourceControlData[scdIndex].Value))
145+
conversionList.Add(from.SourceControlData[scdIndex]);
146+
147+
originalSourceControlData[i].Value = XmlConversionUtil.ConvertObjectToXml(conversionList);
148+
149+
scdIndex--;
150+
}
151+
152+
}
153+
else
154+
{
155+
originalSourceControlData.AddRange(from.SourceControlData);
156+
}
157+
158+
var originalSourceControlDataCount = originalSourceControlData.Count;
118159
var modificationSet = new Dictionary<Modification, bool>();
160+
int sourceControlIndex = 0;
119161
foreach (ISourceControl sourceControl in SourceControls)
120162
{
163+
from.SourceControlData.Clear();
164+
if (sourceControlIndex < originalSourceControlDataCount)
165+
from.SourceControlData.AddRange((List<NameValuePair>)(XmlConversionUtil.ConvertXmlToObject(sourceControlDataType, originalSourceControlData[sourceControlIndex].Value)));
166+
167+
to.SourceControlData.Clear();
168+
121169
Modification[] mods = sourceControl.GetModifications(from, to);
170+
171+
finalSourceControlData.Add(new NameValuePair(string.Format("sc{0:d}", sourceControlIndex), XmlConversionUtil.ConvertObjectToXml(to.SourceControlData)));
172+
122173
if (mods != null && mods.Length > 0)
123174
{
124175
foreach (var mod in mods)
@@ -131,8 +182,13 @@ public override Modification[] GetModifications(IIntegrationResult from, IIntegr
131182
modificationSet.Clear();
132183
break;
133184
}
185+
186+
sourceControlIndex++;
134187
}
135188

189+
to.SourceControlData.Clear();
190+
to.SourceControlData.AddRange(finalSourceControlData);
191+
136192
var modArray = new Modification[modificationSet.Count];
137193
modificationSet.Keys.CopyTo(modArray, 0);
138194
return modArray;

0 commit comments

Comments
 (0)