Beispiel #1
0
		public void Storage2_Milestone4_UseReferences()
		{
			var fileName = "Storage2_Milestone2_TestFile.txt";
			var filePath = RepositoryPath.Combine(TestRoot.Path, fileName);
			var text = "Lorem ipsum dolor sit amet...";

			if(ContentType.GetByName("File2") == null)
				ContentTypeInstaller.InstallContentType(@"<?xml version=""1.0"" encoding=""utf-8""?>
					<ContentType name=""File2"" parentType=""File"" handler=""SenseNet.ContentRepository.File"" xmlns=""http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition"">
						<DisplayName>Folder</DisplayName>
						<Description>Use folders to group information to one place</Description>
						<Icon>Folder</Icon>
						<Fields>
							<Field name=""References"" type=""Reference"" />
						</Fields>
					</ContentType>");

			int rootId = TestRoot.Id;
			int file2ctId = ContentType.GetByName("File2").Id;
			int systemId = Node.LoadNode("/Root/System").Id;

			if (Node.Exists(filePath))
                Node.ForceDelete(filePath);

			//----------------------------- Create

			var file = new File(TestRoot, "File2");
			file.Name = fileName;
			file.GetBinary("Binary").FileName = fileName;
			file.GetBinary("Binary").SetStream(Tools.GetStreamFromString(text));
			file.AddReferences("References", new Node[] { TestRoot, ContentType.GetByName("File2") });

			file.Save();

			//----------------------------- Load

			var loadedNode = Node.Load<File>(filePath);
			var loadedStream = loadedNode.GetBinary("Binary").GetStream();
			var loadedText = Tools.GetStreamString(loadedStream);
			var loadedReferences = loadedNode.GetReferences("References").ToList<Node>();

			Assert.IsTrue(loadedText == text, "#1");
			Assert.IsTrue(loadedReferences.Count == 2, "#2");
			var id0 = loadedReferences[0].Id;
			var id1 = loadedReferences[1].Id;
			Assert.IsTrue(id0 == rootId || id0 == file2ctId, "#3");
			Assert.IsTrue(id1 == rootId || id1 == file2ctId, "#4");

			//----------------------------- Change references

			var refs = ((IEnumerable<Node>)loadedNode["References"]).ToList();
			refs.Remove(TestRoot);

			Assert.IsTrue(refs.Count == 2, "#5");

            loadedNode.RemoveReference("References", TestRoot);
            refs = ((IEnumerable<Node>)loadedNode["References"]).ToList();

			Assert.IsTrue(refs.Count == 1, "#6");
			id0 = refs[0].Id;
			Assert.IsTrue(id0 == file2ctId, "#7");

			refs.Add(Node.LoadNode("/Root/System"));

            loadedNode.SetReferences("References", refs);
            refs = ((IEnumerable<Node>)loadedNode["References"]).ToList();

			Assert.IsTrue(refs.Count == 2, "#8");
			id0 = refs[0].Id;
			id1 = refs[1].Id;
			Assert.IsTrue(id0 == file2ctId, "#9");
			Assert.IsTrue(id1 == systemId, "#10");

			loadedNode.Save();

			//----------------------------- Reload

			var reloadedNode = Node.Load<File>(filePath);
			var reloadedStream = reloadedNode.GetBinary("Binary").GetStream();
			var reloadedText = Tools.GetStreamString(reloadedStream);
			var reloadedReferences = reloadedNode.GetReferences("References").ToList<Node>();

			Assert.IsTrue(reloadedText == text, "#11");
			Assert.IsTrue(reloadedReferences.Count == 2, "#12");
			id0 = reloadedReferences[0].Id;
			id1 = reloadedReferences[1].Id;
            Assert.IsTrue(id0 != id1, "#13");
            Assert.IsTrue(id0 == file2ctId || id0 == systemId, "#14");
            Assert.IsTrue(id1 == file2ctId || id1 == systemId, "#15");

			//----------------------------- Reload

            var identityCheck = reloadedNode.GetReferences("References");
            var countBefore = identityCheck.ToArray().Length;
            reloadedNode.AddReference("References", Repository.Root);
            var countAfter = identityCheck.ToArray().Length;
            Assert.IsTrue(countAfter == countBefore + 1, "#15");
		}