3rdparty/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Bugs/ParserBugTests.cs
/// <summary>
/// Bug 12383 - [AST] Non existing namespaces generated
/// </summary>
Bug 12383 - [AST] Non existing namespaces generated
Bug 12383 - [ AST ] Non existing namespaces generated
[Test]
		public void TestBug12383()
		{
			string code = @"using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using Kajabity.Tools.Java;
using Xamarin.Installer.Core;
namespace Xamarin.Installer.Core.Components.Android
{
	public class AndroidComponentSystemImage : AndroidComponentBase
	{
		public string Abi { get; private set; }
		public uint ApiLevel { get; private set; }
		public override string Name {
			get { return String.Format (SYSTEM_IMAGE_NAME_FORMAT, ApiLevel); }
		}
		public override string UserFriendlyName {
			get { return String.Format (""{0} System Image for API {1} ({2})"", LicenseVendor, ApiLevel, Abi); }
		}
		public override string LicenseTitle {
			get { return String.Format (""{0} System Image"", LicenseVendor); }
		}
		string LicenseVendor {
			get {
				string ul = Info.UsesLicense;
				if (String.IsNullOrWhiteSpace (ul))
					return ""Android"";
				if (ul.IndexOf (""intel"", StringComparison.OrdinalIgnoreCase) >= 0)
					return ""Intel"";
				else if (ul.IndexOf (""mips"", StringComparison.OrdinalIgnoreCase) >= 0)
					return ""MIPS"";
				else
					return ""Android"";
		}
		public override bool Init (AndroidComponentInfoBase componentInfo, AndroidSDKComponentInfo sdkInfo)
		{
			if (!base.Init (componentInfo, sdkInfo))
				return false;
			var info = componentInfo as AndroidComponentSystemImageInfo;
			if (info == null) {
				LogUnexpectedComponentInfoType (componentInfo);
				return false;
			}
			ApiLevel = info.ApiLevel;
			Abi = info.Abi;
			return true;
		}
		public override bool Install (DownloadedItem item, AndroidSdkInstance sdk)
		{
			if (item == null)
				throw new ArgumentNullException (""item"");
			if (sdk == null)
				throw new ArgumentNullException (""sdk"");
			bool success = true;
			string message = ""Android SDK Platform API level {0}, ABI {1} system image failed to install."";
			Log.Debug (""Installing system image for Android SDK Platform API {0}, ABI {1}."", ApiLevel, Abi);
			try {
				if (!DoInstall (item.FilePath, sdk.Path, ""system-images"", ""android-"" + ApiLevel, Abi)) {
					Log.Warning (message, ApiLevel, Abi);
					success = false;
				} else
					UpdateSourceProperties (sdk.Path);
			} catch (Exception ex) {
				Log.Warning (message, ex, ApiLevel, Abi);
				success = false;
			}
			return success;
		}
		public override bool IsInstallationNeeded (AndroidSdkInstance instance, AndroidPlatformSpecificToolNames toolNames)
		{
			Installed = false;
			if (instance == null)
				throw new ArgumentNullException (""instance"");
			string systemImagePath = Path.Combine (instance.Path, ""system-images"", ""android-"" + ApiLevel, Abi);
			AndroidDownloadsCharter charter = instance.DownloadCharter;
			JavaProperties props;
			if (!Directory.Exists (systemImagePath)) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""System image {0}/{1} directory does not exist"", ApiLevel, Abi);
				return true;
			}
			if (!ReadJavaProperties (Path.Combine (systemImagePath, ""source.properties""), out props) || props == null) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""Unable to read System Image {0}/{1} properties, assuming it's outdated"", ApiLevel, Abi);
				return true;
			}
			Installed = true;
			string rev;
			Version instv;
			if (!props.GetPkgRevision (out rev, out instv)) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""Unable to read System Image {0}/{1} revision, assuming it's outdated"", ApiLevel, Abi);
				return true;
			}
			string value;
			if (!props.GetProperty<string> (""SystemImage.Abi"", out value) || String.IsNullOrEmpty (value)) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""Unable to read System Image {0}/{1} ABI, assuming it's outdated"", ApiLevel, Abi);
				return true;
			}
			if (String.Compare (value, Abi, StringComparison.OrdinalIgnoreCase) != 0) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""Installed System Image {0} ABI ({1}) is different ({2}), assuming it's outdated"", ApiLevel, Abi, value);
				return true;
			}
			if (Revision.AndroidTryParse (true) > instv) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""Installed System Image {0}/{1} revision {2} is older than the required one ({3})"", ApiLevel, Abi, instv, Revision);
				return true;
			}
			bool needsDownload = false;
			if (!File.Exists (Path.Combine (systemImagePath, ""system.img""))) {
				if (!File.Exists (Path.Combine (systemImagePath, ""images"", Abi, ""system.img""))) {
					needsDownload = true;
					charter.DownloadSystemImages = true;
					instance.AddUpdateReason (""system.img not found for API {0}, ABI {1}"", ApiLevel, Abi);
				}
			}
			return needsDownload;
		}
		protected override void UpdateSourceProperties (string targetBasePath)
		{
			var props = new JavaProperties ();
			props.SetProperty (""Pkg.Desc"", Description);
			props.SetProperty (""Archive.Os"", OS.ToUpperInvariant ());
			props.SetProperty (""Archive.Arch"", Arch.ToUpperInvariant ());
			props.SetProperty (""AndroidVersion.ApiLevel"", ApiLevel.ToString ());
			props.SetProperty (""Pkg.Revision"", Revision.ToString ());
			props.SetProperty (""SystemImage.Abi"", Abi);
			props.SetProperty (""Pkg.SourceUrl"", SDKInfo.RepositoryUrl);
			SaveProperties (props, targetBasePath, ""system-images"", ""android-"" + ApiLevel, Abi);
		}
	}
}
";
			var unit = SyntaxTree.Parse(code);
			Assert.AreEqual(1, unit.DescendantNodesAndSelf().OfType<NamespaceDeclaration>().Count());
		}
[ Test ] public void TestBug12383 ( ) { string code = @"using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using Kajabity.Tools.Java;
using Xamarin.Installer.Core;

namespace Xamarin.Installer.Core.Components.Android
{
	public class AndroidComponentSystemImage : AndroidComponentBase
	{
		public string Abi { get; private set; }

		public uint ApiLevel { get; private set; }

		public override string Name {
			get { return String.Format (SYSTEM_IMAGE_NAME_FORMAT, ApiLevel); }
		}

		public override string UserFriendlyName {
			get { return String.Format (""{0} System Image for API {1} ({2})"", LicenseVendor, ApiLevel, Abi); }
		}

		public override string LicenseTitle {
			get { return String.Format (""{0} System Image"", LicenseVendor); }
		}

		string LicenseVendor {
			get {
				string ul = Info.UsesLicense;
				if (String.IsNullOrWhiteSpace (ul))
					return ""Android"";
				if (ul.IndexOf (""intel"", StringComparison.OrdinalIgnoreCase) >= 0)
					return ""Intel"";
				else if (ul.IndexOf (""mips"", StringComparison.OrdinalIgnoreCase) >= 0)
					return ""MIPS"";
				else
					return ""Android"";
			
		}

		public override bool Init (AndroidComponentInfoBase componentInfo, AndroidSDKComponentInfo sdkInfo)
		{
			if (!base.Init (componentInfo, sdkInfo))
				return false;

			var info = componentInfo as AndroidComponentSystemImageInfo;
			if (info == null) {
				LogUnexpectedComponentInfoType (componentInfo);
				return false;
			}
			ApiLevel = info.ApiLevel;
			Abi = info.Abi;

			return true;
		}

		public override bool Install (DownloadedItem item, AndroidSdkInstance sdk)
		{
			if (item == null)
				throw new ArgumentNullException (""item"");
			if (sdk == null)
				throw new ArgumentNullException (""sdk"");

			bool success = true;
			string message = ""Android SDK Platform API level {0}, ABI {1} system image failed to install."";

			Log.Debug (""Installing system image for Android SDK Platform API {0}, ABI {1}."", ApiLevel, Abi);
			try {
				if (!DoInstall (item.FilePath, sdk.Path, ""system-images"", ""android-"" + ApiLevel, Abi)) {
					Log.Warning (message, ApiLevel, Abi);
					success = false;
				} else
					UpdateSourceProperties (sdk.Path);
			} catch (Exception ex) {
				Log.Warning (message, ex, ApiLevel, Abi);
				success = false;
			}

			return success;
		}

		public override bool IsInstallationNeeded (AndroidSdkInstance instance, AndroidPlatformSpecificToolNames toolNames)
		{
			Installed = false;
			if (instance == null)
				throw new ArgumentNullException (""instance"");

			string systemImagePath = Path.Combine (instance.Path, ""system-images"", ""android-"" + ApiLevel, Abi);
			AndroidDownloadsCharter charter = instance.DownloadCharter;
			JavaProperties props;

			if (!Directory.Exists (systemImagePath)) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""System image {0}/{1} directory does not exist"", ApiLevel, Abi);
				return true;
			}

			if (!ReadJavaProperties (Path.Combine (systemImagePath, ""source.properties""), out props) || props == null) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""Unable to read System Image {0}/{1} properties, assuming it's outdated"", ApiLevel, Abi);
				return true;
			}

			Installed = true;
			string rev;
			Version instv;
			if (!props.GetPkgRevision (out rev, out instv)) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""Unable to read System Image {0}/{1} revision, assuming it's outdated"", ApiLevel, Abi);
				return true;
			}

			string value;
			if (!props.GetProperty<string> (""SystemImage.Abi"", out value) || String.IsNullOrEmpty (value)) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""Unable to read System Image {0}/{1} ABI, assuming it's outdated"", ApiLevel, Abi);
				return true;
			}

			if (String.Compare (value, Abi, StringComparison.OrdinalIgnoreCase) != 0) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""Installed System Image {0} ABI ({1}) is different ({2}), assuming it's outdated"", ApiLevel, Abi, value);
				return true;
			}

			if (Revision.AndroidTryParse (true) > instv) {
				charter.DownloadSystemImages = true;
				instance.AddUpdateReason (""Installed System Image {0}/{1} revision {2} is older than the required one ({3})"", ApiLevel, Abi, instv, Revision);
				return true;
			}

			bool needsDownload = false;
			if (!File.Exists (Path.Combine (systemImagePath, ""system.img""))) {
				if (!File.Exists (Path.Combine (systemImagePath, ""images"", Abi, ""system.img""))) {
					needsDownload = true;
					charter.DownloadSystemImages = true;
					instance.AddUpdateReason (""system.img not found for API {0}, ABI {1}"", ApiLevel, Abi);
				}
			}

			return needsDownload;
		}

		protected override void UpdateSourceProperties (string targetBasePath)
		{
			var props = new JavaProperties ();

			props.SetProperty (""Pkg.Desc"", Description);
			props.SetProperty (""Archive.Os"", OS.ToUpperInvariant ());
			props.SetProperty (""Archive.Arch"", Arch.ToUpperInvariant ());
			props.SetProperty (""AndroidVersion.ApiLevel"", ApiLevel.ToString ());
			props.SetProperty (""Pkg.Revision"", Revision.ToString ());
			props.SetProperty (""SystemImage.Abi"", Abi);
			props.SetProperty (""Pkg.SourceUrl"", SDKInfo.RepositoryUrl);

			SaveProperties (props, targetBasePath, ""system-images"", ""android-"" + ApiLevel, Abi);
		}
	}
}

" ; var unit = SyntaxTree . Parse ( code ) ; Assert . AreEqual ( 1 , unit . DescendantNodesAndSelf ( ) . OfType < NamespaceDeclaration > ( ) . Count ( ) ) ; }
Bug 12383 - [AST] Non existing namespaces generated
Bug 12383 - [ AST ] Non existing namespaces generated