public void test_ofClasspath_absolute() throws Exception { ResourceLocator test = ResourceLocator.ofClasspath("/com/opengamma/strata/collect/io/TestFile.txt"); assertTrue(test.getLocator().startsWith("classpath")); assertTrue(test.getLocator().endsWith("com/opengamma/strata/collect/io/TestFile.txt")); assertEquals(test.getByteSource().read()[0], 'H'); assertEquals(test.getCharSource().readLines(), ImmutableList.of("HelloWorld")); assertEquals(test.getCharSource(StandardCharsets.UTF_8).readLines(), ImmutableList.of("HelloWorld")); assertTrue(test.toString().startsWith("classpath")); assertTrue(test.toString().endsWith("com/opengamma/strata/collect/io/TestFile.txt")); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Rates curve loader found multiple curves with the same name: .*") public void test_multiple_curves() { LegalEntityRatesCurvesCsvLoader.load( ALL_DATES.get(6), ResourceLocator.of(GROUPS), ResourceLocator.of(SETTINGS), ImmutableList.of(ResourceLocator.of(CURVES_1), ResourceLocator.of(CURVES_1), ResourceLocator.of(CURVES_2))); }
/** * Creates a resource from a string locator. * <p> * This accepts locators starting with 'classpath:', 'url:' or 'file:'. * It also accepts unprefixed locators, treated as files. * * @param locator the string form of the resource locator * @return the resource locator */ @FromString public static ResourceLocator of(String locator) { ArgChecker.notNull(locator, "locator"); try { if (locator.startsWith(CLASSPATH_URL_PREFIX)) { String urlStr = locator.substring(CLASSPATH_URL_PREFIX.length()); return ofClasspath(urlStr); } else if (locator.startsWith(FILE_URL_PREFIX)) { String fileStr = locator.substring(FILE_URL_PREFIX.length()); return ofFile(new File(fileStr)); } else if (locator.startsWith(URL_PREFIX)) { String pathStr = locator.substring(URL_PREFIX.length()); return ofUrl(new URL(pathStr)); } else { return ofFile(new File(locator)); } } catch (Exception ex) { throw new IllegalArgumentException("Invalid resource locator: " + locator, ex); } }
/** * Creates a resource from a {@code Path}. * <p> * This will return either a file locator or a URL locator. * * @param path path to the file to wrap * @return the resource locator * @throws IllegalArgumentException if the path is neither a file nor a URL */ public static ResourceLocator ofPath(Path path) { ArgChecker.notNull(path, "path"); try { return ofFile(path.toFile()); } catch (UnsupportedOperationException ex) { try { return ofUrl(path.toUri().toURL()); } catch (MalformedURLException ex2) { throw new IllegalArgumentException("Path could not be converted to a File or URL: " + path); } } }
private static List<ResourceLocator> orderedResources0(String classpathResourceName) throws IOException { ClassLoader classLoader = ResourceLocator.classLoader(); List<String> names = new ArrayList<>(); List<ResourceLocator> result = new ArrayList<>(); continue; case 1: result.add(ResourceLocator.ofClasspathUrl(urls.get(0))); break; default: ResourceLocator baseResource = ResourceLocator.ofClasspathUrl(urls.get(0)); for (int i = 1; i < urls.size(); i++) { ResourceLocator otherResource = ResourceLocator.ofClasspathUrl(urls.get(i)); if (!baseResource.getByteSource().contentEquals(otherResource.getByteSource())) { log.severe("More than one file found on the classpath: " + name + ": " + urls); throw new IllegalStateException("More than one file found on the classpath: " + name + ": " + urls);
/** * Loads one or more CSV format curve files for all available dates. * <p> * If the files contain a duplicate entry an exception will be thrown. * * @param groupsResource the curve groups CSV resource * @param settingsResource the curve settings CSV resource * @param curveValueResources the CSV resources for curves * @return the loaded curves, mapped by date and identifier * @throws IllegalArgumentException if the files contain a duplicate entry */ public static ImmutableListMultimap<LocalDate, LegalEntityCurveGroup> loadAllDates( ResourceLocator groupsResource, ResourceLocator settingsResource, Collection<ResourceLocator> curveValueResources) { Collection<CharSource> curveCharSources = curveValueResources.stream().map(r -> r.getCharSource()).collect(toList()); return parse(d -> true, groupsResource.getCharSource(), settingsResource.getCharSource(), curveCharSources); }
public void test_readOldJodaFormat() throws IOException { ResourceLocator file = ResourceLocator.ofClasspath("com/opengamma/strata/basics/date/ImmutableHolidayCalendar-Old.json"); String str = file.getCharSource().read(); ImmutableHolidayCalendar cal = JodaBeanSer.PRETTY.jsonReader().read(str, ImmutableHolidayCalendar.class); assertEquals(cal.getId(), HolidayCalendarId.of("NZAU")); }
@Test(dataProvider = "parse") public void parse(String location) { ByteSource resource = ResourceLocator.of(location).getByteSource(); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.matching("Party2")).parseTrades(resource); assertEquals(trades.size(), 1); }
private TradeReportTemplate parseIni(String resourceName) { ResourceLocator locator = ResourceLocator.of("classpath:" + resourceName); IniFile ini = IniFile.of(locator.getCharSource()); TradeReportTemplateIniLoader loader = new TradeReportTemplateIniLoader(); return loader.load(ini); }
public void test_test_writeCurveGroupDefinition_roundtrip() throws Exception { List<RatesCurveGroupDefinition> defn = RatesCurveGroupDefinitionCsvLoader.loadCurveGroupDefinitions(ResourceLocator.of(GROUPS_1)); File tempFile = File.createTempFile("TestCurveGroupLoading", "csv"); tempFile.deleteOnExit(); RatesCurveGroupDefinitionCsvLoader.writeCurveGroupDefinition(tempFile, defn.get(0)); assertEquals(RatesCurveGroupDefinitionCsvLoader.loadCurveGroupDefinitions(ResourceLocator.ofFile(tempFile)), defn); }
/** * Loads one or more CSV format trade files. * <p> * CSV files sometimes contain a Unicode Byte Order Mark. * This method uses {@link UnicodeBom} to interpret it. * * @param resources the CSV resources * @return the loaded trades, all errors are captured in the result */ public ValueWithFailures<List<Trade>> load(Collection<ResourceLocator> resources) { Collection<CharSource> charSources = resources.stream() .map(r -> UnicodeBom.toCharSource(r.getByteSource())) .collect(toList()); return parse(charSources); }
public void test_equalsHashCode() throws Exception { File file1 = new File("src/test/resources/com/opengamma/strata/collect/io/TestFile.txt"); File file2 = new File("src/test/resources/com/opengamma/strata/collect/io/Other.txt"); ResourceLocator a1 = ResourceLocator.ofFile(file1); ResourceLocator a2 = ResourceLocator.ofFile(file1); ResourceLocator b = ResourceLocator.ofFile(file2); assertEquals(a1.equals(a1), true); assertEquals(a1.equals(a2), true); assertEquals(a1.equals(b), false); assertEquals(a1.equals(null), false); assertEquals(a1.equals(ANOTHER_TYPE), false); assertEquals(a1.hashCode(), a2.hashCode()); }
/** * Creates a resource from a fully qualified resource name. * <p> * If the resource name does not start with a slash '/', one will be prepended. * Use {@link #ofClasspath(Class, String)} to get a relative resource. * <p> * In Java 9 and later, resources can be encapsulated due to the module system. * As such, this method is caller sensitive. * It finds the class of the method that called this one, and uses that to search for * resources using {@link Class#getResource(String)}. * * @param resourceName the resource name, which will have a slash '/' prepended if missing * @return the resource locator */ public static ResourceLocator ofClasspath(String resourceName) { ArgChecker.notNull(resourceName, "classpathLocator"); String searchName = resourceName.startsWith("/") ? resourceName : "/" + resourceName; Class<?> caller = Guavate.callerClass(3); return ofClasspath(caller, searchName); }
/** * Creates a resource locator for a classpath resource which is associated with a class. * <p> * The classpath is searched using the same method as {@code Class.getResource}. * <ul> * <li>If the resource name starts with '/' it is treated as an absolute path relative to the classpath root</li> * <li>Otherwise the resource name is treated as a path relative to the package containing the class</li> * </ul> * * @param cls the class used to find the resource * @param resourceName the resource name * @return the resource locator */ public static ResourceLocator ofClasspath(Class<?> cls, String resourceName) { ArgChecker.notNull(resourceName, "classpathLocator"); URL url = cls.getResource(resourceName); if (url == null) { throw new IllegalArgumentException("Resource not found: " + resourceName); } return ofClasspathUrl(url); }
/** * Loads one or more CSV format curve calibration files. * <p> * If the files contain a duplicate entry an exception will be thrown. * * @param groupsResource the curve groups CSV resource * @param settingsResource the curve settings CSV resource * @param curveNodeResources the CSV resources for curve nodes * @return the group definitions, mapped by name * @throws IllegalArgumentException if the files contain a duplicate entry */ public static ImmutableMap<CurveGroupName, RatesCurveGroupDefinition> load( ResourceLocator groupsResource, ResourceLocator settingsResource, Collection<ResourceLocator> curveNodeResources) { Collection<CharSource> curveNodeCharSources = curveNodeResources.stream().map(r -> r.getCharSource()).collect(toList()); return parse(groupsResource.getCharSource(), settingsResource.getCharSource(), curveNodeCharSources); }
public void test_parse_grid() { CharSource source = ResourceLocator.ofClasspath("com/opengamma/strata/loader/csv/sensitivity-grid.csv").getCharSource(); assertEquals(LOADER.isKnownFormat(source), true); ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER.parse(ImmutableList.of(source)); assertEquals(test.getFailures().size(), 0, test.getFailures().toString()); assertEquals(test.getValue().size(), 1); List<CurveSensitivities> list = test.getValue().get(""); assertEquals(list.size(), 1); CurveSensitivities csens0 = list.get(0); assertEquals(csens0.getTypedSensitivities().size(), 1); String tenors = "1D, 1W, 2W, 1M, 3M, 6M, 12M, 2Y, 5Y, 10Y"; assertSens(csens0, ZERO_RATE_DELTA, "GBP", GBP, tenors, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); assertSens(csens0, ZERO_RATE_DELTA, "USD-LIBOR-3M", USD, tenors, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); }
public void badSelector() { String location = "classpath:com/opengamma/strata/loader/fpml/ird-ex08-fra.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); FpmlDocumentParser parser = FpmlDocumentParser.of(allParties -> ImmutableList.of("rubbish")); assertThrows( () -> parser.parseTrades(resource), FpmlParseException.class, "Selector returned an ID .*"); }
/** * Loads one or more CSV format position files. * <p> * CSV files sometimes contain a Unicode Byte Order Mark. * This method uses {@link UnicodeBom} to interpret it. * * @param resources the CSV resources * @return the loaded positions, all errors are captured in the result */ public ValueWithFailures<List<Position>> load(Collection<ResourceLocator> resources) { Collection<CharSource> charSources = resources.stream() .map(r -> UnicodeBom.toCharSource(r.getByteSource())) .collect(toList()); return parse(charSources); }
public void test_ofClasspath_withClass_absolute() throws Exception { ResourceLocator test = ResourceLocator.ofClasspath(ResourceLocator.class, "/com/opengamma/strata/collect/io/TestFile.txt"); assertTrue(test.getLocator().startsWith("classpath")); assertTrue(test.getLocator().endsWith("com/opengamma/strata/collect/io/TestFile.txt")); assertEquals(test.getByteSource().read()[0], 'H'); assertEquals(test.getCharSource().readLines(), ImmutableList.of("HelloWorld")); assertEquals(test.getCharSource(StandardCharsets.UTF_8).readLines(), ImmutableList.of("HelloWorld")); assertTrue(test.toString().startsWith("classpath")); assertTrue(test.toString().endsWith("com/opengamma/strata/collect/io/TestFile.txt")); }
/** * Loads one or more CSV format curve files for all available dates. * <p> * If the files contain a duplicate entry an exception will be thrown. * * @param groupsResource the curve groups CSV resource * @param settingsResource the curve settings CSV resource * @param curveValueResources the CSV resources for curves * @return the loaded curves, mapped by date and identifier * @throws IllegalArgumentException if the files contain a duplicate entry */ public static ImmutableListMultimap<LocalDate, RatesCurveGroup> loadAllDates( ResourceLocator groupsResource, ResourceLocator settingsResource, Collection<ResourceLocator> curveValueResources) { Collection<CharSource> curveCharSources = curveValueResources.stream().map(r -> r.getCharSource()).collect(toList()); return parse(d -> true, groupsResource.getCharSource(), settingsResource.getCharSource(), curveCharSources); }