|
| 1 | +/** |
| 2 | + * Copyright 2018 SourceLab.org https://github.com/SourceLabOrg/kafka-connect-client |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 5 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 6 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 7 | + * persons to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | + * |
| 9 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 10 | + * Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 13 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 14 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 15 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 16 | + */ |
| 17 | + |
| 18 | +import org.junit.Test; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.io.FileNotFoundException; |
| 24 | +import java.util.Scanner; |
| 25 | +import java.util.regex.Matcher; |
| 26 | +import java.util.regex.Pattern; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertFalse; |
| 29 | + |
| 30 | +/** |
| 31 | + * Look for LoggerFactory.getLogger(classname) where classname doesn't match the class its part of. |
| 32 | + * This is kind of a half assed way to do this. |
| 33 | + */ |
| 34 | +public class BadLoggersTest { |
| 35 | + private static final Logger logger = LoggerFactory.getLogger(BadLoggersTest.class); |
| 36 | + |
| 37 | + // Weak attempt |
| 38 | + private static final Pattern regexPattern = Pattern.compile("LoggerFactory.getLogger\\((.*)\\.class\\)"); |
| 39 | + |
| 40 | + @Test |
| 41 | + public void doTest() throws FileNotFoundException { |
| 42 | + // Hacky way to determine root path |
| 43 | + final File currentPath = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath()); |
| 44 | + final File projectRootPath = currentPath.getParentFile().getParentFile(); |
| 45 | + logger.info("Root Path: {}", projectRootPath); |
| 46 | + |
| 47 | + // Walk all the files in the path |
| 48 | + walk(projectRootPath); |
| 49 | + } |
| 50 | + |
| 51 | + private void walk(File root) throws FileNotFoundException { |
| 52 | + File[] list = root.listFiles(); |
| 53 | + |
| 54 | + if (list == null) return; |
| 55 | + |
| 56 | + for (File f : list) { |
| 57 | + if (f.isDirectory()) { |
| 58 | + walk(f); |
| 59 | + } else { |
| 60 | + // Skip non java source files |
| 61 | + if (!f.getAbsoluteFile().getPath().endsWith(".java")) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + testFile(f); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private void testFile(File myFile) throws FileNotFoundException { |
| 70 | + String fileData = new Scanner(myFile).useDelimiter("\\Z").next(); |
| 71 | + |
| 72 | + // Look for our pattern |
| 73 | + Matcher matches = regexPattern.matcher(fileData); |
| 74 | + |
| 75 | + // If we didn't find a match |
| 76 | + if (!matches.find()) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Grab out the Class name |
| 81 | + String loggerClassName = matches.group(1); |
| 82 | + if (loggerClassName == null) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Get class name from the file name |
| 87 | + // I bet this will be completely broken for inner classes... |
| 88 | + // if you run into that, just exclude it? or figure out a better solution to this :p |
| 89 | + String className = myFile.getName().replace(".java", ""); |
| 90 | + if (!className.equals(loggerClassName)) { |
| 91 | + logger.info("Class {} ClassNameUsedByLogger {} ", className, loggerClassName); |
| 92 | + assertFalse("Found instance of logger using wrong class? " + myFile.getPath() + " Using " + loggerClassName, true); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments