Người nghiện Internet chuyên nghiệp • Người đam mê trò chơi • Người sáng tạo công nghệ
Người nghiện Internet chuyên nghiệp • Người đam mê trò chơi • Người sáng tạo công nghệ

Chạy Checkstyle như một bài kiểm tra JUnit tự động

Cách chạy Checkstyle như một bài kiểm tra JUnit tự động!
Trang này đã được các thực tập sinh AI đầy nhiệt huyết của tôi dịch từ tiếng Anh để thuận tiện cho bạn. Các bạn vẫn đang trong quá trình học hỏi, nên có thể còn một vài lỗi nhỏ. Để có thông tin chính xác nhất, vui lòng tham khảo phiên bản tiếng Anh.
Trang chủ Blog Chạy Checkstyle như một bài kiểm tra JUnit tự động

Xin lưu ý rằng bài đăng trên blog này được xuất bản vào tháng 6 năm 2010, vì vậy tùy thuộc vào thời điểm bạn đọc, một số phần có thể đã lỗi thời. Rất tiếc, tôi không thể luôn cập nhật đầy đủ các bài đăng này để đảm bảo thông tin luôn chính xác.

    If you are looking for a great code analysis tool to help you review your Java code, Checkstyle is a good choice. It basically checks your code towards a set of given checks of your choice, everything from checking that the code is following naming conventions, to spotting duplicated code.
    There are several ways to use Checkstyle, either by using it with a build automation tool such as Apache Maven or as an Ant task, but you can also simply execute it through a command line.
    However if you are interested you can also wrap it into a JUnit test. Even though it is really not a unit test, it could be really convenient as it allows you run it along with the rest of your tests.
    Here is a basic example code:
    public class CheckStyleTest extends TestCase { @Test public void testCheckStyle() throws Exception { /* * Files */ File ROOT = new File("src/"); System.out.println("Root is set to "" + ROOT.getAbsolutePath() + ""."); List files = new ArrayList(); listFiles(files, ROOT, "java"); System.out.println("Found " + files.size() + " Java source files."); /* * Listener */ ByteArrayOutputStream sos = new ByteArrayOutputStream(); AuditListener listener = new DefaultLogger(sos, false); /* * Configuration */ InputSource inputSource = new InputSource(CheckStyleTest.class.getResourceAsStream("configuration.xml")); Configuration configuration = ConfigurationLoader.loadConfiguration(inputSource, new PropertiesExpander(System.getProperties()), false); /* * Create checker */ Checker checker = new Checker(); checker.setModuleClassLoader(Checker.class.getClassLoader()); checker.configure(configuration); checker.addListener(listener); /* * Process */ int errors = checker.process(files); System.out.println("Found " + errors + " check style errors."); System.out.println(sos.toString()); assertTrue(errors + " check style errors found. " + sos.toString(), errors == 0); /* * Clean up */ checker.destroy(); } private static void listFiles(List files, File folder, String extension) { if (folder.canRead()) { if (folder.isDirectory()) { for (File f : folder.listFiles()) { listFiles(files, f, extension); } } else if (folder.toString().endsWith("." + extension)) { files.add(folder); } } } }
    As you can notice the test will try and read from a file called "configuration.xml". File this has to be in the same folder as where you put this test. "configuration.xml" is simply the checks that Checkstyle should perform. How to write this document is described on the their configuration page.
    A sample of the "configuration.xml" file would be:
    <module name="Checker"> <module name="TreeWalker"> <module name="CovariantEquals"/> <module name="EmptyStatement"/> <module name="FallThrough"/> <module name="SuperClone"/> <module name="AvoidStarImport"/> <module name="UnusedImports"/> <module name="AvoidStaticImport"/> <module name="EqualsHashCode"/> </module> </module>

    Được viết bởi Special Agent Squeaky. Xuất bản lần đầu ngày 29/06/2010. Cập nhật lần cuối ngày 29/06/2010.

    📺 Xem video mới nhất của Squeaky!

    Cách thêm phụ đề thời gian thực cho phát trực tiếp của bạn một cách đơn giản