示例#1
0
        /// <summary>
        /// Create zip archive for requested <code>sourceToCompress</code>.
        /// If <code>sourceToCompress</code> is a directory then content of that directory and all its sub-directories will be added to the archive.
        /// If <code>sourceToCompress</code> does not exist or is an empty directory then archive will not be created. </summary>
        /// <param name="fileSystem"> source file system </param>
        /// <param name="sourceToCompress"> source file to compress </param>
        /// <param name="destinationZip"> zip file compress source to </param>
        /// <exception cref="IOException"> when underlying file system access produce IOException </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static void zip(org.neo4j.io.fs.FileSystemAbstraction fileSystem, java.io.File sourceToCompress, java.io.File destinationZip) throws java.io.IOException
        public static void Zip(FileSystemAbstraction fileSystem, File sourceToCompress, File destinationZip)
        {
            if (!fileSystem.FileExists(sourceToCompress))
            {
                return;
            }
            if (IsEmptyDirectory(fileSystem, sourceToCompress))
            {
                return;
            }
            IDictionary <string, string> env = MapUtil.stringMap("create", "true");
            Path rootPath           = sourceToCompress.toPath();
            URI  archiveAbsoluteURI = URI.create("jar:file:" + destinationZip.toURI().RawPath);

            using (FileSystem zipFs = FileSystems.newFileSystem(archiveAbsoluteURI, env))
            {
                IList <FileHandle> fileHandles = fileSystem.StreamFilesRecursive(sourceToCompress).collect(toList());
                foreach (FileHandle fileHandle in fileHandles)
                {
                    Path sourcePath = fileHandle.File.toPath();
                    Path zipFsPath  = fileSystem.IsDirectory(sourceToCompress) ? zipFs.getPath(rootPath.relativize(sourcePath).ToString()) : zipFs.getPath(sourcePath.FileName.ToString());
                    if (zipFsPath.Parent != null)
                    {
                        Files.createDirectories(zipFsPath.Parent);
                    }
                    Files.copy(sourcePath, zipFsPath);
                }
            }
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldContinueAfterError() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void ShouldContinueAfterError()
        {
            DiagnosticsReporter reporter   = new DiagnosticsReporter();
            MyProvider          myProvider = new MyProvider(_fileSystem);

            reporter.RegisterOfflineProvider(myProvider);

            myProvider.AddFile("logs/a.txt", CreateNewFileWithContent("a.txt", "file a"));

            Path          destination = _testDirectory.file("logs.zip").toPath();
            ISet <string> classifiers = new HashSet <string>();

            classifiers.Add("logs");
            classifiers.Add("fail");
            using (MemoryStream baos = new MemoryStream())
            {
                PrintStream            @out     = new PrintStream(baos);
                NonInteractiveProgress progress = new NonInteractiveProgress(@out, false);

                reporter.Dump(classifiers, destination, progress, true);

                assertThat(baos.ToString(), @is(string.Format("1/2 fail.txt%n" + "....................  20%%%n" + "..........%n" + "Error: Step failed%n" + "2/2 logs/a.txt%n" + "....................  20%%%n" + "....................  40%%%n" + "....................  60%%%n" + "....................  80%%%n" + ".................... 100%%%n%n")));
            }

            // Verify content
            URI uri = URI.create("jar:file:" + destination.toAbsolutePath().toUri().RawPath);

            using (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap()))
            {
                IList <string> fileA = Files.readAllLines(fs.getPath("logs/a.txt"));
                assertEquals(1, fileA.Count);
                assertEquals("file a", fileA[0]);
            }
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldBeAbleToAttachToPidAndRunThreadDump() throws java.io.IOException, org.neo4j.commandline.admin.CommandFailed, org.neo4j.commandline.admin.IncorrectUsage
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldBeAbleToAttachToPidAndRunThreadDump()
        {
            long pid = PID;

            assertThat(pid, @is(not(0)));

            // Write config file
            Files.createFile(TestDirectory.file("neo4j.conf").toPath());

            // write neo4j.pid file
            File run = TestDirectory.directory("run");

            Files.write(Paths.get(run.AbsolutePath, "neo4j.pid"), pid.ToString().GetBytes());

            // Run command, should detect running instance
            try
            {
                using (RealOutsideWorld outsideWorld = new RealOutsideWorld())
                {
                    string[] args    = new string[] { "threads", "--to=" + TestDirectory.absolutePath().AbsolutePath + "/reports" };
                    Path     homeDir = TestDirectory.directory().toPath();
                    DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand(homeDir, homeDir, outsideWorld);
                    diagnosticsReportCommand.Execute(args);
                }
            }
            catch (IncorrectUsage e)
            {
                if (e.Message.Equals("Unknown classifier: threads"))
                {
                    return;                              // If we get attach API is not available for example in some IBM jdk installs, ignore this test
                }
                throw e;
            }

            // Verify that we took a thread dump
            File reports = TestDirectory.directory("reports");

            File[] files = reports.listFiles();
            assertThat(files, notNullValue());
            assertThat(Files.Length, @is(1));

            Path report = files[0].toPath();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.net.URI uri = java.net.URI.create("jar:file:" + report.toUri().getRawPath());
            URI uri = URI.create("jar:file:" + report.toUri().RawPath);

            using (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap()))
            {
                string threadDump = new string( Files.readAllBytes(fs.getPath("threaddump.txt")));
//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getCanonicalName method:
                assertThat(threadDump, containsString(typeof(DiagnosticsReportCommandIT).FullName));
            }
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static void verifyContent(java.nio.file.Path destination) throws java.io.IOException
        private static void VerifyContent(Path destination)
        {
            URI uri = URI.create("jar:file:" + destination.toAbsolutePath().toUri().RawPath);

            using (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap()))
            {
                IList <string> fileA = Files.readAllLines(fs.getPath("logs/a.txt"));
                assertEquals(1, fileA.Count);
                assertEquals("file a", fileA[0]);

                IList <string> fileB = Files.readAllLines(fs.getPath("logs/b.txt"));
                assertEquals(1, fileB.Count);
                assertEquals("file b", fileB[0]);
            }
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void test_ofPath_fileInZipFile() throws Exception
        public virtual void test_ofPath_fileInZipFile()
        {
            Path zip = Paths.get("src/test/resources/com/opengamma/strata/collect/io/TestFile.zip");

            using (FileSystem fs = FileSystems.newFileSystem(zip, null))
            {
                Path            path    = fs.getPath("TestFile.txt").toAbsolutePath();
                ResourceLocator test    = ResourceLocator.ofPath(path);
                string          locator = test.Locator;
                assertTrue(locator.StartsWith("url:jar:file:", StringComparison.Ordinal));
                assertTrue(locator.EndsWith("src/test/resources/com/opengamma/strata/collect/io/TestFile.zip!/TestFile.txt", StringComparison.Ordinal));
                assertEquals(test.ByteSource.read()[0], 'H');
                assertEquals(test.CharSource.readLines(), ImmutableList.of("HelloWorld"));
                assertEquals(test.getCharSource(StandardCharsets.UTF_8).readLines(), ImmutableList.of("HelloWorld"));
                assertEquals(test.ToString(), locator);
            }
        }
示例#6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public void dump(java.util.Set<String> classifiers, java.nio.file.Path destination, DiagnosticsReporterProgress progress, boolean force) throws java.io.IOException
        public virtual void Dump(ISet <string> classifiers, Path destination, DiagnosticsReporterProgress progress, bool force)
        {
            // Collect sources
            IList <DiagnosticsReportSource> sources = new List <DiagnosticsReportSource>();

            foreach (DiagnosticsOfflineReportProvider provider in _providers)
            {
                ((IList <DiagnosticsReportSource>)sources).AddRange(provider.GetDiagnosticsSources(classifiers));
            }

            // Add additional sources
            foreach (KeyValuePair <string, IList <DiagnosticsReportSource> > classifier in _additionalSources.SetOfKeyValuePairs())
            {
                if (classifiers.Contains("all") || classifiers.Contains(classifier.Key))
                {
                    ((IList <DiagnosticsReportSource>)sources).AddRange(classifier.Value);
                }
            }

            // Make sure target directory exists
            Path destinationFolder = destination.Parent;

            Files.createDirectories(destinationFolder);

            // Estimate an upper bound of the final size and make sure it will fit, if not, end reporting
            EstimateSizeAndCheckAvailableDiskSpace(destination, progress, sources, destinationFolder, force);

            // Compress all files to destination
            IDictionary <string, object> env = new Dictionary <string, object>();

            env["create"]      = "true";
            env["useTempFile"] = true;

            // NOTE: we need the toUri() in order to handle windows file paths
            URI uri = URI.create("jar:file:" + destination.toAbsolutePath().toUri().RawPath);

            using (FileSystem fs = FileSystems.newFileSystem(uri, env))
            {
                progress.TotalSteps = sources.Count;
                for (int i = 0; i < sources.Count; i++)
                {
                    DiagnosticsReportSource source = sources[i];
                    Path path = fs.getPath(source.DestinationPath());
                    if (path.Parent != null)
                    {
                        Files.createDirectories(path.Parent);
                    }

                    progress.Started(i + 1, path.ToString());
                    try
                    {
                        source.AddToArchive(path, progress);
                    }
                    catch (Exception e)
                    {
                        progress.Error("Step failed", e);
                        continue;
                    }
                    progress.Finished();
                }
            }
        }