1 /* 2 * Copyright (C) 2007-2010 Jlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 The JavaParser Team. 4 * 5 * This file is part of JavaParser. 6 * 7 * JavaParser can be used either under the terms of 8 * a) the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * b) the terms of the Apache License 12 * 13 * You should have received a copy of both licenses in LICENCE.LGPL and 14 * LICENCE.APACHE. Please refer to those files for details. 15 * 16 * JavaParser is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License for more details. 20 */ 21 22 package com.github.javaparser.wiki_samples; 23 24 import org.apache.commons.io.IOUtils; 25 26 import java.io.File; 27 import java.io.FileOutputStream; 28 import java.io.InputStream; 29 import java.io.OutputStream; 30 31 import static org.junit.Assert.*; 32 33 /** 34 * Creates a temporary test file that a sample can use. This way we don't have to rewrite the samples to fit them into 35 * these tests. 36 */ 37 public class TestFileToken implements AutoCloseable { 38 private final String filename; 39 40 public TestFileToken(String filename) { 41 this.filename = filename; 42 try { 43 try (InputStream i = getClass().getResourceAsStream("TestFile.java"); OutputStream o = new FileOutputStream(filename)) { 44 assertNotNull(i); 45 IOUtils.copy(i, o); 46 } 47 } catch (Exception e) { 48 e.printStackTrace(); 49 fail(e.getMessage()); 50 } 51 } 52 53 @Override 54 public void close() { 55 boolean deleted = new File(filename).delete(); 56 assertTrue(deleted); 57 } 58 } 59