1 /******************************************************************************* 2 * Copyright (c) 2007, 2015 BEA Systems, Inc. and others 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * wharley@bea.com - initial API and implementation 13 * IBM Corporation - fixed a resource leak warning 14 *******************************************************************************/ 15 16 package org.eclipse.jdt.compiler.apt.tests; 17 18 import java.io.BufferedWriter; 19 import java.io.File; 20 import java.io.FileWriter; 21 import java.io.IOException; 22 import java.net.URLClassLoader; 23 import java.nio.charset.Charset; 24 import java.util.ArrayList; 25 import java.util.HashSet; 26 import java.util.Iterator; 27 import java.util.List; 28 import java.util.Locale; 29 import java.util.Set; 30 31 import javax.lang.model.SourceVersion; 32 import javax.tools.JavaFileManager; 33 import javax.tools.JavaFileObject; 34 import javax.tools.StandardJavaFileManager; 35 36 import org.eclipse.jdt.internal.compiler.apt.util.EclipseFileManager; 37 38 import junit.framework.TestCase; 39 40 /** 41 * Test the implementation of the Filer interface, 42 * in more detail than BatchDispatchTests does. 43 * @since 3.4 44 */ 45 public class FileManagerTests extends TestCase { 46 47 /* (non-Javadoc) 48 * @see junit.framework.TestCase#setUp() 49 */ setUp()50 protected void setUp() throws Exception { 51 super.setUp(); 52 BatchTestUtils.init(); 53 } 54 testFileManager()55 public void testFileManager() { 56 String tmpFolder = System.getProperty("java.io.tmpdir"); 57 File dir = new File(tmpFolder, "src" + System.currentTimeMillis()); 58 dir.mkdirs(); 59 File inputFile = new File(dir, "X.java"); 60 BufferedWriter writer = null; 61 try { 62 writer = new BufferedWriter(new FileWriter(inputFile)); 63 writer.write("public class X {}"); 64 writer.flush(); 65 writer.close(); 66 } catch (IOException e) { 67 // ignore 68 } finally { 69 if (writer != null) { 70 try { 71 writer.close(); 72 } catch (IOException e) { 73 // ignore 74 } 75 } 76 } 77 StandardJavaFileManager fileManager = null; 78 try { 79 fileManager = new EclipseFileManager(Locale.getDefault(), Charset.defaultCharset()); 80 81 List<File> fins = new ArrayList<File>(); 82 fins.add(dir); 83 84 JavaFileManager.Location sourceLoc = javax.tools.StandardLocation.SOURCE_PATH; 85 fileManager.setLocation(sourceLoc, fins); 86 87 Set<JavaFileObject.Kind> fileTypes = new HashSet<JavaFileObject.Kind>(); 88 fileTypes.add(JavaFileObject.Kind.SOURCE); 89 90 Iterable<? extends JavaFileObject> compilationUnits = fileManager.list(sourceLoc, "", fileTypes, true); 91 92 Iterator<? extends JavaFileObject> it = compilationUnits.iterator(); 93 StringBuilder builder = new StringBuilder(); 94 while (it.hasNext()) { 95 JavaFileObject next = it.next(); 96 String name = next.getName(); 97 name = name.replace('\\', '/'); 98 int lastIndexOf = name.lastIndexOf('/'); 99 builder.append(name.substring(lastIndexOf + 1)); 100 } 101 assertEquals("Wrong contents", "X.java", String.valueOf(builder)); 102 103 List<File> files = new ArrayList<File>(); 104 files.add(dir); 105 try { 106 fileManager.getJavaFileObjectsFromFiles(files); 107 fail("IllegalArgumentException should be thrown but not"); 108 } catch(IllegalArgumentException iae) { 109 // Do nothing 110 } 111 112 } catch (IOException e) { 113 e.printStackTrace(); 114 } finally { 115 try { 116 fileManager.close(); 117 } catch (IOException e) { 118 //ignore the exception 119 } 120 } 121 // check that the .class file exist for X 122 assertTrue("delete failed", inputFile.delete()); 123 assertTrue("delete failed", dir.delete()); 124 } 125 // Test that JavaFileManager#inferBinaryName returns null for invalid file testInferBinaryName()126 public void testInferBinaryName() { 127 String tmpFolder = System.getProperty("java.io.tmpdir"); 128 File dir = new File(tmpFolder, "src" + System.currentTimeMillis()); 129 dir.mkdirs(); 130 File inputFile = new File(dir, "test.txt"); 131 BufferedWriter writer = null; 132 try { 133 writer = new BufferedWriter(new FileWriter(inputFile)); 134 writer.write("This is not a valid Java file"); 135 writer.flush(); 136 writer.close(); 137 } catch (IOException e) { 138 } finally { 139 if (writer != null) { 140 try { 141 writer.close(); 142 } catch (IOException e) { 143 } 144 } 145 } 146 try { 147 StandardJavaFileManager fileManager = new EclipseFileManager(Locale.getDefault(), Charset.defaultCharset()); 148 149 List<File> fins = new ArrayList<File>(); 150 fins.add(dir); 151 JavaFileManager.Location sourceLoc = javax.tools.StandardLocation.SOURCE_PATH; 152 fileManager.setLocation(sourceLoc, fins); 153 154 Set<JavaFileObject.Kind> fileTypes = new HashSet<JavaFileObject.Kind>(); 155 fileTypes.add(JavaFileObject.Kind.OTHER); 156 157 Iterable<? extends JavaFileObject> compilationUnits = fileManager.list(sourceLoc, "", fileTypes, true); 158 JavaFileObject invalid = null; 159 for (JavaFileObject javaFileObject : compilationUnits) { 160 invalid = javaFileObject; 161 break; 162 } 163 String inferredName = fileManager.inferBinaryName(sourceLoc, invalid); 164 fileManager.close(); 165 assertNull("Should return null for invalid file", inferredName); 166 } catch (IOException e) { 167 e.printStackTrace(); 168 } 169 assertTrue("delete failed", inputFile.delete()); 170 assertTrue("delete failed", dir.delete()); 171 } 172 testBug460085()173 public void testBug460085() { 174 if (isOnJRE9()) return; 175 try { 176 boolean found = false; 177 EclipseFileManager fileManager = null; 178 fileManager = new EclipseFileManager(Locale.getDefault(), Charset.defaultCharset()); 179 Iterable <? extends File> files = fileManager.getLocation(javax.tools.StandardLocation.PLATFORM_CLASS_PATH); 180 Iterator<? extends File> iter = files.iterator(); 181 while (iter.hasNext()) { 182 File f = iter.next(); 183 if ("rt.jar".equals(f.getName())) { 184 found = true; 185 break; 186 } 187 } 188 fileManager.close(); 189 assertTrue("rt.jar not found", found); 190 } catch (IOException e) { 191 fail(e.getMessage()); 192 } 193 } isOnJRE9()194 private boolean isOnJRE9() { 195 try { 196 SourceVersion.valueOf("RELEASE_9"); 197 } catch(IllegalArgumentException iae) { 198 return false; 199 } 200 return true; 201 } 202 testBug466878_getResource_defaultPackage()203 public void testBug466878_getResource_defaultPackage() throws Exception { 204 EclipseFileManager fileManager = new EclipseFileManager(Locale.getDefault(), Charset.defaultCharset()); 205 List<File> classpath = new ArrayList<>(); 206 classpath.add(new File(BatchTestUtils.getPluginDirectoryPath(), "resources/targets/filemanager/classes")); 207 classpath.add(new File(BatchTestUtils.getPluginDirectoryPath(), "resources/targets/filemanager/dependency.zip")); 208 fileManager.setLocation(javax.tools.StandardLocation.CLASS_PATH, classpath); 209 assertNotNull(fileManager.getFileForInput(javax.tools.StandardLocation.CLASS_PATH, "", "dirresource.txt")); 210 assertNotNull(fileManager.getFileForInput(javax.tools.StandardLocation.CLASS_PATH, "", "jarresource.txt")); 211 fileManager.close(); 212 } 213 testBug514121_getClassloader_close()214 public void testBug514121_getClassloader_close() throws Exception { 215 EclipseFileManager fileManager = new EclipseFileManager(Locale.getDefault(), Charset.defaultCharset()); 216 List<File> classpath = new ArrayList<>(); 217 classpath.add(new File(BatchTestUtils.getPluginDirectoryPath(), "resources/targets/filemanager/dependency.zip")); 218 fileManager.setLocation(javax.tools.StandardLocation.ANNOTATION_PROCESSOR_PATH, classpath); 219 URLClassLoader loader = (URLClassLoader) fileManager 220 .getClassLoader(javax.tools.StandardLocation.ANNOTATION_PROCESSOR_PATH); 221 assertNotNull(loader.findResource("jarresource.txt")); // sanity check 222 fileManager.close(); 223 assertNull(loader.findResource("jarresource.txt")); // assert the classloader is closed 224 } 225 } 226