1 /* 2 * Copyright (C) 2025 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 package com.android.dependencymapper; 17 18 import static org.junit.Assert.assertEquals; 19 20 import org.junit.BeforeClass; 21 import org.junit.Test; 22 23 import java.net.URISyntaxException; 24 import java.nio.file.Path; 25 import java.nio.file.Paths; 26 import java.util.HashMap; 27 import java.util.List; 28 import java.util.Map; 29 30 public class JavaSourceAnalyzerTest { 31 private static List<JavaSourceData> mJavaSourceDataList; 32 33 private static final String SOURCES_RSP_PATH = 34 "tests/res/testfiles/sources.rsp"; 35 36 @BeforeClass beforeClass()37 public static void beforeClass() throws URISyntaxException { 38 Path path = Paths.get(SOURCES_RSP_PATH); 39 // Perform source analysis. 40 mJavaSourceDataList = JavaSourceAnalyzer.analyze(path); 41 } 42 43 @Test validateSourceData()44 public void validateSourceData() { 45 Map<String, String> expectedSourceData = expectedSourceData(); 46 int expectedFileCount = expectedSourceData.size(); 47 int actualFileCount = 0; 48 for (JavaSourceData javaSourceData : mJavaSourceDataList) { 49 String file = javaSourceData.getFilePath(); 50 if (expectedSourceData.containsKey(file)) { 51 actualFileCount++; 52 assertEquals("Source Data not generated correctly for " + file, 53 expectedSourceData.get(file), javaSourceData.getPackagePrependedFileName()); 54 } 55 } 56 assertEquals("Not all source files processed", expectedFileCount, actualFileCount); 57 } 58 expectedSourceData()59 private Map<String, String> expectedSourceData() { 60 Map<String, String> expectedSourceData = new HashMap<>(); 61 expectedSourceData.put("tests/res/testdata/annotation/AnnotationUsage.java", 62 "res.testdata.annotation.AnnotationUsage.java"); 63 expectedSourceData.put("tests/res/testdata/constants/ConstantUsage.java", 64 "res.testdata.constants.ConstantUsage.java"); 65 expectedSourceData.put("tests/res/testdata/inheritance/BaseClass.java", 66 "res.testdata.inheritance.BaseClass.java"); 67 expectedSourceData.put("tests/res/testdata/methods/FieldUsage.java", 68 "res.testdata.methods.FieldUsage.java"); 69 return expectedSourceData; 70 } 71 } 72