001/* 002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.tribuo.tests; 018 019import com.oracle.labs.mlrg.olcut.util.IOUtil; 020 021import java.io.BufferedReader; 022import java.io.BufferedWriter; 023import java.io.IOException; 024import java.io.InputStreamReader; 025import java.nio.charset.Charset; 026import java.nio.charset.StandardCharsets; 027import java.nio.file.Files; 028import java.nio.file.Path; 029 030/** 031 * Utils for working with classpath resources at test time. 032 */ 033public final class Resources { 034 private Resources() { 035 } 036 037 public static Path copyResourceToTmp(String resource) throws IOException { 038 Path path = Files.createTempFile("test", ".csv"); 039 path.toFile().deleteOnExit(); 040 try (BufferedReader reader = new BufferedReader(new InputStreamReader(IOUtil.getInputStream(resource), StandardCharsets.UTF_8)); 041 BufferedWriter writer = Files.newBufferedWriter(path, Charset.defaultCharset())) { 042 043 String ln; 044 while ((ln = reader.readLine()) != null) { 045 writer.write(ln); 046 writer.newLine(); 047 } 048 049 return path; 050 } 051 } 052}