Skip to content

Commit 2674569

Browse files
committed
more cleanup around test configuration
1 parent d788919 commit 2674569

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

src/test/java/BadLoggersTest.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
package categories;
19+
20+
/**
21+
* Used to Mark tests as integration tests.
22+
*/
23+
public interface IntegrationTest {
24+
}

src/test/java/org/sourcelab/kafka/connect/apiclient/ApiClientTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
package org.sourcelab.kafka.connect.apiclient;
1919

20+
import categories.IntegrationTest;
2021
import org.junit.Before;
2122
import org.junit.Test;
23+
import org.junit.experimental.categories.Category;
2224
import org.slf4j.Logger;
2325
import org.slf4j.LoggerFactory;
2426
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorPluginConfigDefinition;
@@ -33,6 +35,7 @@
3335
* Assumes kafka-connect is running at localhost. More or less a sanity test
3436
* rather than checking assertions.
3537
*/
38+
@Category(IntegrationTest.class)
3639
public class ApiClientTest {
3740
private static final Logger logger = LoggerFactory.getLogger(ApiClientTest.class);
3841

0 commit comments

Comments
 (0)