Пример #1
0
        public TestResourceResult Compile(string cmdline, out string generatedCode, out string modifiedResX)
        {
            using (TempFile resx = TempFile.FromExtension(".resx"))
                using (TempFile resources = TempFile.Attach(Path.ChangeExtension(resx.TempPath, ".resources")))
                    using (TempFile rescs = TempFile.Attach(Path.ChangeExtension(resx.TempPath, ".Designer.cs")))
                    {
                        BuildResX(resx.TempPath);
                        BuildResource(resources.TempPath);

                        TextWriter stdout = Console.Out;
                        try
                        {
                            using (TextWriter tw = new StreamWriter(rescs.Open()))
                            {
                                Console.SetOut(tw);
                                Generators.Commands.ResX(resx.TempPath, Namespace, ClassName, ResxNamespace, Public, Partial, Test, false, typeof(ApplicationException).FullName);
                            }

                            generatedCode = rescs.ReadAllText();
                            modifiedResX  = resx.ReadAllText();
                        }
                        finally
                        { Console.SetOut(stdout); }

                        Assembly asm = Compile(
                            String.Format("/reference:{0} /resource:{1},{2}.{3}.resources {4}", GetType().Assembly.Location, resources.TempPath, ResxNamespace, ClassName, cmdline),
                            rescs.TempPath);

                        return(new TestResourceResult(asm, Namespace, ClassName));
                    }
        }
Пример #2
0
        public void TestDerivedFileName()
        {
            string path;

            using (DisposingList l = new DisposingList())
            {
                TempFile ftarget = new TempFile();
                ftarget.Delete();

                l.Add(ftarget);
                TempFile fbigone = TempFile.Attach(String.Format("{0}.~{1:x4}", ftarget.TempPath, 0x10008 - 25));
                fbigone.Create().Dispose();
                Assert.IsTrue(fbigone.Exists);
                l.Add(fbigone);
                path = ftarget.TempPath;

                string tmpName;
                Dictionary <string, object> names = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

                for (int i = 0; i < 25; i++)
                {
                    Stream s = ReplaceFile.CreateDerivedFile(ftarget.TempPath, out tmpName);
                    l.Add(TempFile.Attach(tmpName));
                    l.Add(s);
                    names.Add(tmpName, null);
                }

                fbigone.Delete();
                Assert.AreEqual(25, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length);
            }
            Assert.AreEqual(0, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length);
        }
Пример #3
0
        public void TestCreateMessageAssembly()
        {
            /*
             * This is the most simple form of message generation in which we generate a complete dll with
             * the message resource (and optional versioning).  The dll includes the TestResXClass1.Constants.cs
             * and the TestResXClass1.InstallUtil.cs as generated in the example TestGenerateWin32Resource().
             */
            using (TempDirectory tmp = new TempDirectory())
                using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
                    using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
                        using (TempFile dllout = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.dll")))
                        {
                            asminfo.WriteAllText(AsminfoFormat);

                            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                            builder1.Add(".AutoLog", true);
                            builder1.Add(".EventSource", "HelloWorld");
                            builder1.Add("Value1", "value for 1", "#MessageId = 1");
                            builder1.BuildResX(resx1.TempPath);

                            Generators.Commands.ResXtoMessageDll(dllout.TempPath, new string[] { resx1.TempPath },
                                                                 tmp.TempPath, asminfo.TempPath, "TestNamespace", Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")), "/debug-");

                            Assert.IsTrue(dllout.Exists);
                        }
        }
Пример #4
0
        public void TestProjectResXWithBadAssemblyInfo()
        {
            /* Bad assembly info... */
            using (TempDirectory tmp = new TempDirectory())
                using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
                    using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
                        using (TempFile csproj = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.csproj")))
                            using (TempFile manifest = TempFile.Attach(Path.Combine(tmp.TempPath, "App.manifest")))
                                using (TempFile appico = TempFile.Attach(Path.Combine(tmp.TempPath, "App.ico")))
                                {
                                    asminfo.WriteAllText(AsminfoFormat + "[InvalidAttribute]");
                                    csproj.WriteAllText(String.Format(ProjFormat, Path.GetFileName(resx1.TempPath)));
                                    manifest.WriteAllText(AppManifest);

                                    TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                                    builder1.Add(".AutoLog", true);
                                    builder1.Add(".EventSource", "HelloWorld");
                                    builder1.Add("Value1", "value for 1", "#MessageId = 1");
                                    builder1.BuildResX(resx1.TempPath);

                                    using (Stream s = appico.Open())
                                        Properties.Resources.App.Save(s);

                                    Generators.Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1", null, String.Empty, Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")));
                                }
        }
        void TestRecoveryOnNew(BPlusTree<Guid, TestInfo>.OptionsV2 options, int count, int added)
        {
            BPlusTree<Guid, TestInfo> tree = null;
            var temp = TempFile.Attach(options.FileName);
            Dictionary<Guid, TestInfo> data = new Dictionary<Guid, TestInfo>();
            try
            {
                Assert.IsNotNull(options.TransactionLog);
                temp.Delete();
                tree = new BPlusTree<Guid, TestInfo>(options);
                using (var log = options.TransactionLog)
                {
                    using ((IDisposable)new PropertyValue(tree, "_storage").Value)
                        Insert(tree, data, Environment.ProcessorCount, count, TimeSpan.MaxValue);
                    //Add extra data...
                    AppendToLog(log, TestInfo.Create(added, data));
                }
                tree = null;
                //No file... yet...
                Assert.IsFalse(File.Exists(options.FileName));
                //Now recover...
                using (var recovered = new BPlusTree<Guid, TestInfo>(options))
                {
                    TestInfo.AssertEquals(data, recovered);
                }

                Assert.IsTrue(File.Exists(options.FileName));
            }
            finally
            {
                temp.Dispose();
                if (tree != null)
                    tree.Dispose();
            }
        }
Пример #6
0
        public void TestBenchmarkWriteSpeed()
        {
            //Write 2,147,483,776 bytes in: 00:02:09.7934237 (in chunks of 128 bytes)
            //Write 4,295,032,832 bytes in: 00:00:18.4990581 (in chunks of 65536 bytes)
            //Logged 2,398,000,000 bytes in: 00:00:36.7621027


            string newpath = Path.Combine(@"C:\Temp\LogTest\", Guid.NewGuid() + ".tmp");

            using (TempFile tmp = TempFile.Attach(newpath))
            {
                byte[]   bytes;
                DateTime start;
                //bytes = new byte[128];
                //new Random().NextBytes(bytes);

                //start = DateTime.UtcNow;
                //using (var io = new FileStream(tmp.TempPath, FileMode.Append, FileAccess.Write, FileShare.Read, 8))
                //{
                //    for (int i = 0; i <= 16777216; i++)
                //        io.Write(bytes, 0, 128);
                //}
                //Console.WriteLine("Write {0:n0} bytes in: {1}", tmp.Length, DateTime.UtcNow - start);
                //tmp.Delete();

                var options = new TransactionLogOptions <Guid, byte[]>(
                    tmp.TempPath, PrimitiveSerializer.Guid, PrimitiveSerializer.Bytes)
                {
                    FileBuffer  = ushort.MaxValue,
                    FileOptions = FileOptions.None | FileOptions.SequentialScan,
                };

                Guid[] ids = new Guid[1000000];
                for (int i = 0; i < ids.Length; i++)
                {
                    ids[i] = Guid.NewGuid();
                }

                bytes = new byte[100];
                new Random().NextBytes(bytes);

                start = DateTime.UtcNow;

                using (var log = new TransactionLog <Guid, byte[]>(options))
                {
                    foreach (Guid id in ids)
                    {
                        var token = log.BeginTransaction();
                        for (int i = 0; i < 20; i++)
                        {
                            log.AddValue(ref token, id, bytes);
                        }
                        log.CommitTransaction(ref token);
                    }
                }

                Console.WriteLine("Logged {0:n0} bytes in: {1}", tmp.Length, DateTime.UtcNow - start);
            }
        }
Пример #7
0
        void TestRecoveryOnExisting(BPlusTree <Guid, TestInfo> .OptionsV2 options, int count, int added)
        {
            BPlusTree <Guid, TestInfo> tree = null;
            var temp = TempFile.Attach(options.FileName);
            Dictionary <Guid, TestInfo> dataFirst, data = new Dictionary <Guid, TestInfo>();

            try
            {
                temp.Delete();
                Assert.IsNotNull(options.TransactionLog);

                using (tree = new BPlusTree <Guid, TestInfo>(options))
                {
                    Insert(tree, data, 1, 100, TimeSpan.MaxValue);
                    TestInfo.AssertEquals(data, tree);
                    Assert.IsFalse(temp.Exists);
                }
                tree = null;
                Assert.IsTrue(File.Exists(options.TransactionLogFileName));

                // All data commits to output file
                Assert.IsTrue(temp.Exists);
                TestInfo.AssertEquals(data, BPlusTree <Guid, TestInfo> .EnumerateFile(options));

                dataFirst = new Dictionary <Guid, TestInfo>(data);
                DateTime modified = temp.Info.LastWriteTimeUtc;

                tree = new BPlusTree <Guid, TestInfo>(options);
                using (var log = options.TransactionLog)
                {
                    using ((IDisposable) new PropertyValue(tree, "_storage").Value)
                        Insert(tree, data, Environment.ProcessorCount, count, TimeSpan.MaxValue);
                    //Add extra data...
                    AppendToLog(log, TestInfo.Create(added, data));
                }
                tree = null;

                //Still only contains original data
                Assert.AreEqual(modified, temp.Info.LastWriteTimeUtc);
                TestInfo.AssertEquals(dataFirst, BPlusTree <Guid, TestInfo> .EnumerateFile(options));

                //Now recover...
                using (var recovered = new BPlusTree <Guid, TestInfo>(options))
                {
                    TestInfo.AssertEquals(data, recovered);
                }
            }
            finally
            {
                temp.Dispose();
                if (tree != null)
                {
                    tree.Dispose();
                }
            }
        }
Пример #8
0
        public void TestGenerateWin32Resource()
        {
            /*
             * Required: This generation type is used to embed the message resource into the current assembly
             * by using a custom Win32Resource setting in the project, for this example you would use:
             *     <Win32Resource>Resources\TestResXClass1.res</Win32Resource>
             *
             * FYI: You must manually add this to the project or else VStudio will erase <ApplicationIcon>
             * The generated res file contains the <ApplicationIcon> as well as assembly version information
             * from the "AssemblyInfo.cs" file included in the project.  If you want to manually supply the
             * assembly info you can point it to a specific assemblyInfo.cs file, or to a dll to extract it
             * from.
             *
             * Optionally you may include "Resources\TestResXClass1.Constants.cs" to define constants for all
             * hresults, facilities, and categories that are defined.
             *
             * Optionally you may include "Resources\TestResXClass1.InstallUtil.cs" to define an installer for
             * the event log registration.  If you need modifications to this installer, simply copy and paste
             * and use it for a starting point.
             */
            using (TempDirectory tmp = new TempDirectory())
                using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
                    using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
                        using (TempFile csproj = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.csproj")))
                            using (TempFile manifest = TempFile.Attach(Path.Combine(tmp.TempPath, "App.manifest")))
                                using (TempFile appico = TempFile.Attach(Path.Combine(tmp.TempPath, "App.ico")))
                                {
                                    asminfo.WriteAllText(AsminfoFormat);
                                    csproj.WriteAllText(String.Format(ProjFormat, Path.GetFileName(resx1.TempPath)));
                                    manifest.WriteAllText(AppManifest);

                                    TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                                    builder1.Add(".AutoLog", true);
                                    builder1.Add(".EventSource", "HelloWorld");
                                    builder1.Add("Value1", "value for 1", "#MessageId = 1");
                                    builder1.BuildResX(resx1.TempPath);

                                    using (Stream s = appico.Open())
                                        Properties.Resources.App.Save(s);

                                    Generators.Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1", null, String.Empty, Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")));

                                    Assert.IsTrue(Directory.Exists(Path.Combine(tmp.TempPath, "Resources")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\MSG00409.bin")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.mc")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.h")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc")));
                                    Assert.IsTrue(File.ReadAllText(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc")).Contains("1.2.3.4"));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.res")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.Constants.cs")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.InstallUtil.cs")));
                                }
        }
Пример #9
0
        void TestRecoveryOnNew(BPlusTree <Guid, TestInfo> .OptionsV2 options, int count, int added)
        {
            BPlusTree <Guid, TestInfo> tree = null;
            var temp = TempFile.Attach(options.FileName);
            Dictionary <Guid, TestInfo> data = new Dictionary <Guid, TestInfo>();

            try
            {
                Assert.IsNotNull(options.TransactionLog);
                temp.Delete();
                tree = new BPlusTree <Guid, TestInfo>(options);
                using (var log = options.TransactionLog)
                {
                    using ((IDisposable) new PropertyValue(tree, "_storage").Value)
                        Insert(tree, data, Environment.ProcessorCount, count, TimeSpan.MaxValue);
                    //Add extra data...
                    AppendToLog(log, TestInfo.Create(added, data));
                }
                tree = null;
                //No data... yet...
                using (TempFile testempty = TempFile.FromCopy(options.FileName))
                {
                    var testoptions = options.Clone();
                    testoptions.TransactionLogFileName = null;
                    testoptions.TransactionLog         = null;
                    testoptions.FileName = testempty.TempPath;

                    using (var empty = new BPlusTree <Guid, TestInfo>(testoptions))
                    {
                        empty.EnableCount();
                        Assert.AreEqual(0, empty.Count);
                    }
                }

                //Now recover...
                using (var recovered = new BPlusTree <Guid, TestInfo>(options))
                {
                    TestInfo.AssertEquals(data, recovered);
                }
            }
            finally
            {
                temp.Dispose();
                if (tree != null)
                {
                    tree.Dispose();
                }
            }
        }
Пример #10
0
            /// <summary>
            /// Called to send a specific length of bytes to a server identified by serverKey.  The transfer
            /// is a blocking call and returns on success or raises an exception.  If Abort() is called durring
            /// the transfer, or if a ProgressChanged event handler raises the OperationCanceledException, the
            /// transfer is silently terminated and the method will return false.
            /// </summary>
            /// <param name="location">A string of up to 1024 bytes in length</param>
            /// <param name="filename">The name of the file to write to</param>
            /// <returns>True if the file was successfully received by the server</returns>
            public bool Download(string location, string filename)
            {
                FileStreamFactory file = new FileStreamFactory(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

                using (TempFile temp = TempFile.Attach(filename))
                    using (StreamCache cache = new StreamCache(file, LimitThreads))
                    {
                        temp.Length = 0;
                        if (Download(location, cache))
                        {
                            temp.Detatch();
                            return(true);
                        }
                    }
                return(false);
            }
Пример #11
0
        public void TestAttach()
        {
            string path = Path.GetTempFileName();

            Assert.IsTrue(File.Exists(path));
            File.WriteAllText(path, "Test");

            TempFile filea = TempFile.Attach(path);

            Assert.AreEqual(path, filea.TempPath);
            Assert.AreEqual("Test", File.ReadAllText(filea.TempPath));

            filea.Dispose();
            Assert.IsFalse(File.Exists(path));

            using (TempFile filec = new TempFile(path))
                Assert.IsFalse(File.Exists(path));
        }
Пример #12
0
        public void TestReplaceFiles()
        {
            string testdata = Guid.NewGuid().ToString();
            string tmpPath;

            TempFile replace = new TempFile();

            using (ReplaceFile temp = new ReplaceFile(replace.TempPath))
            {
                Assert.AreEqual(temp.TargetFile, replace.TempPath);
                tmpPath = temp.TempPath;
                Assert.IsTrue(temp.Exists);
                temp.WriteAllText(testdata);
                //missing commit:
                //temp.Commit();
            }
            Assert.AreEqual(0, replace.Length);
            Assert.IsFalse(File.Exists(tmpPath));

            string backupfile = Path.ChangeExtension(replace.TempPath, ".bak");

            File.Delete(Path.ChangeExtension(replace.TempPath, ".bak"));
            Assert.IsFalse(File.Exists(backupfile));
            replace.WriteAllText("backup");

            //now for real
            using (ReplaceFile temp = new ReplaceFile(replace.TempPath, ".bak"))
            {
                tmpPath = temp.TempPath;
                Assert.IsTrue(temp.Exists);
                temp.WriteAllText(testdata);
                temp.Commit();
            }
            Assert.IsFalse(File.Exists(tmpPath));
            Assert.AreEqual(testdata, replace.ReadAllText());

            Assert.IsTrue(File.Exists(backupfile));
            using (TempFile fbackup = TempFile.Attach(backupfile))
                Assert.AreEqual("backup", fbackup.ReadAllText());
        }
Пример #13
0
        public void TestProjectResXVersionByAssembly()
        {
            /* Demonstrates versioning from an existing assembly... */
            using (TempDirectory tmp = new TempDirectory())
                using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
                    using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
                        using (TempFile csproj = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.csproj")))
                            using (TempFile manifest = TempFile.Attach(Path.Combine(tmp.TempPath, "App.manifest")))
                                using (TempFile appico = TempFile.Attach(Path.Combine(tmp.TempPath, "App.ico")))
                                {
                                    asminfo.WriteAllText(AsminfoFormat);
                                    csproj.WriteAllText(String.Format(ProjFormat, Path.GetFileName(resx1.TempPath)));
                                    manifest.WriteAllText(AppManifest);

                                    TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                                    builder1.Add(".AutoLog", true);
                                    builder1.Add(".EventSource", "HelloWorld");
                                    builder1.Add("Value1", "value for 1", "#MessageId = 1");
                                    builder1.BuildResX(resx1.TempPath);

                                    using (Stream s = appico.Open())
                                        Properties.Resources.App.Save(s);

                                    Generators.Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1",
                                                                    typeof(Generators.Commands).Assembly.Location, String.Empty,
                                                                    Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")));

                                    Assert.IsTrue(Directory.Exists(Path.Combine(tmp.TempPath, "Resources")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\MSG00409.bin")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.mc")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.h")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc")));
                                    Assert.IsTrue(File.ReadAllText(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc"))
                                                  .Contains(typeof(Generators.Commands).Assembly.GetName().Version.ToString()));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.res")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.Constants.cs")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.InstallUtil.cs")));
                                }
        }
Пример #14
0
        public void TestBadPathOnAttach()
        {
            TempFile f = TempFile.Attach("@~+_(%!&($_~!(&*+%_~&^%^|||&&&\\\\ THIS IS AN INVALID FILE NAME.*");

            f.Dispose();
        }