예제 #1
0
        public void TestDirectoryHierarchy()
        {
            var fileSystem = new Repository();

            var nodeOne   = new LocalFileTree();
            var nodeTwo   = new LocalFileTree();
            var nodeThree = new ZipNodeTree();

            string appPath = Path.Combine(TestingUtils.GetTestingBaseFolder(), "testdata");

            var tempDir = Path.Combine(Path.Combine(appPath, @"Data2\temp"));

            if (Directory.Exists(tempDir))
            {
                Directory.Delete(tempDir, true);
            }


            Assert.IsTrue(nodeOne.Build(Path.Combine(appPath, @"Data\")));
            Assert.IsTrue(nodeTwo.Build(Path.Combine(appPath, @"Data2\")));
            Assert.IsTrue(nodeThree.Build(Path.Combine(appPath, @"Data.zip")));

            fileSystem.AddSystem("First", nodeOne);
            fileSystem.AddSystem("Second", nodeTwo);
            fileSystem.AddSystem("Third", nodeThree);

            Assert.IsTrue(fileSystem.DirExists(@"subfolder01"));
            Assert.IsTrue(fileSystem.DirExists(@"subfolder02"));
            Assert.IsTrue(fileSystem.DirExists(@"alphabetagamma"));
            Assert.IsTrue(fileSystem.DirExists(@"subfolder02\subfolder_a"));
            Assert.IsTrue(fileSystem.DirExists(@"subfolder02\subfolder_a\subfolder_a_b"));
            Assert.IsTrue(fileSystem.DirExists(@"subfolder02\subfolder_a\subfolder_a_c"));

            Assert.IsTrue(fileSystem.FileExists(@"colours.txt"));
            Assert.IsTrue(fileSystem.FileExists(@"File01.txt"));
            Assert.IsTrue(fileSystem.FileExists(@"subfolder02\booga.txt"));
            Assert.IsTrue(fileSystem.FileExists(@"subfolder02\subfolder_a\geoip.txt"));
            Assert.IsTrue(fileSystem.FileExists(@"subfolder02\subfolder_a\subfolder_a_b\another_test.folder.txt"));
            Assert.IsTrue(fileSystem.FileExists(@"subfolder01\map01.txt"));

            // Get Data from various filesystems
            // This should be coming from nodeOne
            using (Stream fs = fileSystem.Open(@"alphabetagamma\db.sample.txt"))
            {
                byte[]       buffer = new byte[1024];
                UTF8Encoding temp   = new UTF8Encoding(true);

                int amountRead   = fs.Read(buffer, 0, buffer.Length);
                int accumulation = amountRead;
                while (amountRead > 0)
                {
                    string contents = temp.GetString(buffer, 0, amountRead);
                    Console.Write(contents);
                    amountRead    = fs.Read(buffer, 0, buffer.Length);
                    accumulation += amountRead;
                }
                Console.WriteLine("");
                Console.WriteLine("Read {0} bytes", accumulation);
            }

            // This should be coming from nodeTwo
            using (Stream fs = fileSystem.Open(@"File01.txt"))
            {
                byte[]       buffer = new byte[1024];
                UTF8Encoding temp   = new UTF8Encoding(true);

                int amountRead   = fs.Read(buffer, 0, buffer.Length);
                int accumulation = amountRead;
                while (amountRead > 0)
                {
                    string contents = temp.GetString(buffer, 0, amountRead);
                    Console.Write(contents);
                    amountRead    = fs.Read(buffer, 0, buffer.Length);
                    accumulation += amountRead;
                }
                Console.WriteLine("");
                Console.WriteLine("Read {0} bytes", accumulation);
            }

            // This should be coming from nodeThree
            using (Stream fs = fileSystem.Open(@"subfolder02\subfolder_a\subfolder_a_c\data.data.data.txt"))
            {
                byte[]       buffer = new byte[1024];
                UTF8Encoding temp   = new UTF8Encoding(true);

                int amountRead   = fs.Read(buffer, 0, buffer.Length);
                int accumulation = amountRead;
                while (amountRead > 0)
                {
                    string contents = temp.GetString(buffer, 0, amountRead);
                    Console.Write(contents);
                    amountRead    = fs.Read(buffer, 0, buffer.Length);
                    accumulation += amountRead;
                }
                Console.WriteLine("");
                Console.WriteLine("Read {0} bytes", accumulation);
            }

            // Write a file - this should go into nodeTwo
            Byte[] info = new UTF8Encoding(true).GetBytes("This is a test of data being written into the FileSystem.");
            Assert.IsTrue(fileSystem.Write(@"temp\sample_file.txt", info, info.Length));
            Assert.IsTrue(fileSystem.FileExists(@"temp\sample_file.txt"));
            Assert.IsTrue(nodeTwo.FileExists(@"temp\sample_file.txt"));
            Assert.IsTrue(fileSystem.Delete(@"temp\sample_file.txt"));
        }
        public void TestZipFile()
        {
            ZipNodeTree tree = new ZipNodeTree();

            string appPath = Path.Combine(TestingUtils.GetTestingBaseFolder(), "testdata");

            Assert.IsTrue(tree.Build(Path.Combine(appPath, @"Data.zip")));

            foreach (var item in tree.FlatTree)
            {
                Console.WriteLine("{0}: {1}", item.Value.NodeType.ToString(), item.Value.Name);
            }

            Console.WriteLine("End list of contents");

            Assert.IsTrue(tree.FileExists(@"colours.txt"));
            Assert.IsTrue(tree.DirExists(@"subfolder01"));
            Assert.IsTrue(tree.DirExists(@"subfolder02"));
            Assert.IsTrue(tree.DirExists(@"alphabetagamma"));
            Assert.IsTrue(tree.DirExists(@"subfolder02\subfolder_a"));
            Assert.IsTrue(tree.DirExists(@"subfolder02\subfolder_a\subfolder_a_b"));
            Assert.IsTrue(tree.DirExists(@"subfolder02\subfolder_a\subfolder_a_c"));

            Assert.IsFalse(tree.DirExists(@"AttributesCrap"));

            using (Stream fs = tree.Open(@"alphabetagamma\db.sample.txt"))
            {
                byte[]       buffer = new byte[1024];
                UTF8Encoding temp   = new UTF8Encoding(true);

                int amountRead   = fs.Read(buffer, 0, buffer.Length);
                int accumulation = amountRead;
                while (amountRead > 0)
                {
                    string contents = temp.GetString(buffer, 0, amountRead);
                    Console.Write(contents);
                    amountRead    = fs.Read(buffer, 0, buffer.Length);
                    accumulation += amountRead;
                }
                Console.WriteLine("");
                Console.WriteLine("Read {0} bytes", accumulation);
            }

            Console.WriteLine("Next Test run");

            using (Stream fs = tree.Open(@"colours01.txt"))
            {
                byte[]       buffer = new byte[1024];
                UTF8Encoding temp   = new UTF8Encoding(true);

                int amountRead   = fs.Read(buffer, 0, buffer.Length);
                int accumulation = amountRead;
                while (amountRead > 0)
                {
                    Console.WriteLine(temp.GetString(buffer, 0, amountRead));
                    amountRead    = fs.Read(buffer, 0, buffer.Length);
                    accumulation += amountRead;
                }
                Console.WriteLine("");
                Console.WriteLine("Read {0} bytes", accumulation);
            }
        }
        public void TestLocalFileSystem()
        {
            string appPath = Path.Combine(TestingUtils.GetTestingBaseFolder(), "testdata");

            var rootPath = Path.Combine(appPath, @"Data\");

            // Cleanup from old run (just in case - we want a clean run)
            var tempDir = Path.Combine(rootPath, "temp");

            if (Directory.Exists(tempDir))
            {
                Directory.Delete(tempDir, true);
            }

            LocalFileTree tree = new LocalFileTree();

            Assert.IsTrue(tree.Build(rootPath));

            foreach (var item in tree.FlatTree)
            {
                Console.WriteLine("{0}: {1}", item.Value.NodeType.ToString(), item.Value.Name);
            }

            Console.WriteLine("End list of contents");

            Assert.IsTrue(tree.FileExists(@"colours.txt"));
            Assert.IsTrue(tree.DirExists(@"subfolder01"));
            Assert.IsTrue(tree.DirExists(@"subfolder02"));
            Assert.IsTrue(tree.DirExists(@"alphabetagamma"));
            Assert.IsTrue(tree.DirExists(@"subfolder02\subfolder_a"));
            Assert.IsTrue(tree.DirExists(@"subfolder02\subfolder_a\subfolder_a_b"));
            Assert.IsFalse(tree.DirExists(@"subfolder02\subfolder_a\subfolder_a_c"));

            using (Stream fs = tree.Open(@"alphabetagamma\db.sample.txt"))
            {
                byte[]       buffer = new byte[1024];
                UTF8Encoding temp   = new UTF8Encoding(true);

                int amountRead   = fs.Read(buffer, 0, buffer.Length);
                int accumulation = amountRead;
                while (amountRead > 0)
                {
                    string contents = temp.GetString(buffer, 0, amountRead);
                    Console.Write(contents);
                    amountRead    = fs.Read(buffer, 0, buffer.Length);
                    accumulation += amountRead;
                }
                Console.WriteLine("");
                Console.WriteLine("Read {0} bytes", accumulation);
            }

            Console.WriteLine("Next Test run");
            using (Stream fs = tree.Open(@"colours01.txt"))
            {
                byte[]       buffer = new byte[1024];
                UTF8Encoding temp   = new UTF8Encoding(true);

                int amountRead   = fs.Read(buffer, 0, buffer.Length);
                int accumulation = amountRead;
                while (amountRead > 0)
                {
                    Console.WriteLine(temp.GetString(buffer, 0, amountRead));
                    amountRead    = fs.Read(buffer, 0, buffer.Length);
                    accumulation += amountRead;
                }
                Console.WriteLine("");
                Console.WriteLine("Read {0} bytes", accumulation);
            }

            Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
            Assert.IsTrue(tree.Write(@"temp\sample_file.txt", info, info.Length));
            Assert.IsTrue(tree.FileExists(@"temp\sample_file.txt"));
            Assert.IsTrue(tree.Delete(@"temp\sample_file.txt"));
            Assert.IsFalse(tree.FileExists(@"temp\sample_file.txt"));
            Assert.IsFalse(File.Exists(Path.Combine(rootPath, @"temp\sample_file.txt")));
        }