1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.webkit.internal.codegen; 18 19 import com.intellij.openapi.fileTypes.FileType; 20 import com.intellij.openapi.fileTypes.FileTypeRegistry; 21 import com.intellij.openapi.project.Project; 22 import com.intellij.psi.PsiClass; 23 import com.intellij.psi.PsiElement; 24 import com.intellij.psi.PsiFile; 25 import com.intellij.psi.PsiFileFactory; 26 import com.intellij.psi.PsiJavaFile; 27 import com.intellij.psi.PsiRecursiveElementVisitor; 28 29 import java.io.File; 30 import java.io.IOException; 31 import java.net.URISyntaxException; 32 import java.net.URL; 33 import java.nio.file.Files; 34 35 public class TestUtils { 36 static final String TEST_DATA_DIR = "codegen/"; 37 private static final String EXPECTED_TEST_DATA_DIR = "codegen-expected/"; 38 39 /** 40 * Returns the directory containing test resource file dependencies. 41 */ 42 static File getTestDepsDir() { 43 return new File(TestUtils.getTestFilePath(TestUtils.TEST_DATA_DIR, "deps/")); 44 } 45 46 static PsiJavaFile getTestFile(Project project, String className) { 47 String fileName = className + ".java"; 48 return createPsiFileFromFile(project, getTestFilePath(TEST_DATA_DIR, fileName)); 49 } 50 51 static PsiJavaFile getExpectedTestFile(Project project, String className) { 52 String fileName = className + ".java"; 53 return createPsiFileFromFile(project, getTestFilePath(EXPECTED_TEST_DATA_DIR, fileName)); 54 } 55 56 private static String readEntireFile(File file) { 57 try { 58 return new String(Files.readAllBytes(file.toPath())); 59 } catch (IOException e) { 60 throw new RuntimeException(e); 61 } 62 } 63 64 static String getTestFilePath(String testDataDir, String fileName) { 65 URL resourceUrl = TestUtils.class.getClassLoader().getResource(testDataDir + fileName); 66 try { 67 return new File(resourceUrl.toURI()).getAbsolutePath(); 68 } catch (URISyntaxException e) { 69 throw new RuntimeException(e); 70 } 71 } 72 73 /** 74 * Use the project {@param project} to parse the file with name {@param fileName} into a PsiFile 75 * object. 76 */ 77 private static PsiJavaFile createPsiFileFromFile(Project project, String fileName) { 78 FileType type = FileTypeRegistry.getInstance().getFileTypeByFileName(fileName); 79 String code = readEntireFile(new File(fileName)); 80 return (PsiJavaFile) 81 PsiFileFactory.getInstance(project).createFileFromText(fileName, type, code); 82 } 83 84 /** 85 * Get a single class from a file - this should only be used as a utility method for testing. 86 */ 87 static PsiClass getSingleClassFromFile(PsiFile classFile) { 88 final PsiClass[] psiClass = {null}; 89 classFile.accept(new PsiRecursiveElementVisitor() { 90 @Override 91 public void visitElement(PsiElement element) { 92 if (element instanceof PsiClass) { 93 psiClass[0] = (PsiClass) element; 94 return; 95 } 96 super.visitElement(element); 97 } 98 }); 99 return psiClass[0]; 100 } 101 } 102