コード例 #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void test_ofClasspathUrl() throws Exception
        public virtual void test_ofClasspathUrl()
        {
            URL             url  = Resources.getResource("com/opengamma/strata/collect/io/TestFile.txt");
            ResourceLocator test = ResourceLocator.ofClasspathUrl(url);

            assertTrue(test.Locator.StartsWith("classpath", StringComparison.Ordinal));
            assertTrue(test.Locator.EndsWith("com/opengamma/strata/collect/io/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"));
            assertTrue(test.ToString().StartsWith("classpath", StringComparison.Ordinal));
            assertTrue(test.ToString().EndsWith("com/opengamma/strata/collect/io/TestFile.txt", StringComparison.Ordinal));
        }
コード例 #2
0
        // find the list of resources
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private static java.util.List<ResourceLocator> orderedResources0(String classpathResourceName) throws java.io.IOException
        private static IList <ResourceLocator> orderedResources0(string classpathResourceName)
        {
            ClassLoader             classLoader = ResourceLocator.classLoader();
            IList <string>          names       = new List <string>();
            IList <ResourceLocator> result      = new List <ResourceLocator>();

            foreach (string dir in RESOURCE_DIRS)
            {
                string name = CONFIG_PACKAGE + dir + "/" + classpathResourceName;
                names.Add(name);
                IList <URL> urls = Collections.list(classLoader.getResources(name));
                switch (urls.Count)
                {
                case 0:
                    continue;

                case 1:
                    result.Add(ResourceLocator.ofClasspathUrl(urls[0]));
                    break;

                default:
                    // handle case where Strata is on the classpath more than once
                    // only accept this if the data being read is the same in all URLs
                    ResourceLocator baseResource = ResourceLocator.ofClasspathUrl(urls[0]);
                    for (int i = 1; i < urls.Count; i++)
                    {
                        ResourceLocator otherResource = ResourceLocator.ofClasspathUrl(urls[i]);
                        if (!baseResource.ByteSource.contentEquals(otherResource.ByteSource))
                        {
                            log.severe("More than one file found on the classpath: " + name + ": " + urls);
                            throw new System.InvalidOperationException("More than one file found on the classpath: " + name + ": " + urls);
                        }
                    }
                    result.Add(baseResource);
                    break;
                }
            }
            if (result.Count == 0)
            {
                log.severe("No resource files found on the classpath: " + names);
                throw new System.InvalidOperationException("No files found on the classpath: " + names);
            }
            log.config(() => "Resources found: " + result);
            return(result);
        }