/** * CodeNarcValidator can throw ValidationExctpion when source code contains some Groovy scripts * that violate static analysis rules. * * @throws Exception If something wrong happens inside. */ @Test(expected = ValidationException.class) public void failsOnIncorrectGroovySources() throws Exception { final Environment env = new Environment.Mock().withFile("src/Main.groovy", "System.out.println('hello')"); final Validator validator = new CodeNarcValidator(); validator.validate(env); }
/** * Should fail validation in case of wrong XML. * * @throws Exception If something wrong happens inside. */ @Test(expected = ValidationException.class) public void failsValidationOnWrongFile() throws Exception { final Environment env = new Environment.Mock().withFile("src/main/resources/invalid.xml", "<a></a>"); final Validator validator = new XmlValidator(); validator.validate(env); }
/** * CodeNarcValidator can exclude all files. * * @throws Exception If something wrong happens inside. */ @Test public void passesExcludeAllFiles() throws Exception { final Validator validator = new CodeNarcValidator(); final Environment env = new Environment.Mock() .withFile("src/ex3/Main.groovy", "System.out.println('hi2')") .withFile("src/ex4/Main2.groovy", "System.out.println('buy2')") .withExcludes("**/*.groovy"); validator.validate(env); }
/** * Should pass validation in case of correct XML. * * @throws Exception If something wrong happens inside. */ @Test public void passesValidationOnCorrectFile() throws Exception { final Environment env = new Environment.Mock() .withFile( "src/main/resources/valid.xml", // @checkstyle LineLength (1 line) "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<document xmlns=\"http://maven.apache.org/changes/1.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/changes/1.0.0 http://maven.apache.org/xsd/changes-1.0.0.xsd\">\n <body/>\n</document>\n"); final Validator validator = new XmlValidator(true); validator.validate(env); }
/** * Should fail validation in case of noNamespaceSchemaLocation attribute. specified on xml * instance, while targetNamespace attribute exists in schema * * @throws Exception If something wrong happens inside. */ @Test(expected = ValidationException.class) public void failValidationWithNoSchemaLocationAttr() throws Exception { final Environment env = new Environment.Mock() .withFile( "src/main/resources/valid3.xml", // @checkstyle LineLength (1 line) "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:noNamespaceSchemaLocation=\"http://maven.apache.org/xsd/decoration-1.3.0.xsd\" \n name=\"test\">\n</project>"); final Validator validator = new XmlValidator(); validator.validate(env); }
/** * Should pass validation if XML schema file is not accessible. * * @throws Exception If something wrong happens inside. * @todo #246 XmlValidator should be able to log IO problems (for example, inability to connect to * a server) and ignore them (see ticket #243).\ However, {@link com.jcabi.xml.StrictXML} * class outright throws an IllegalArgumentException in such cases. Let's find a way to detect * whether a failure was caused by such IO errorsand fix this test. */ @Test @org.junit.Ignore public void passesValidationIfSchemaIsNotAccessible() throws Exception { final Environment env = new Environment.Mock() .withFile( "src/main/resources/valid2.xml", // @checkstyle LineLength (1 line) "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<document xmlns=\"http://maven.apache.org/changes/1.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/changes/1.0.0 http://www.google.com\"></document>"); final Validator validator = new XmlValidator(); validator.validate(env); }
/** * Should pass validation if noNamespaceSchemaLocation attribute specified. * * @throws Exception If something wrong happens inside. * @checkstyle IndentationCheck (15 lines) */ @Test public void passesValidationIfNoSchemaLocationSpecified() throws Exception { final Environment env = new Environment.Mock() .withFile( "src/main/resources/valid4.xml", // @checkstyle LineLength (1 line) "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://simple.com/test.xsd\">\n</project>\n") .withFile( "src/main/resources/test.xsd", // @checkstyle LineLength (1 line) "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<schema xmlns=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\">\n<element name=\"project\" type=\"xs:anyType\"/>\n</schema>"); final Validator validator = new XmlValidator(); validator.validate(env); }
/** * XmlValidator can inform about missing end of line at the and of file. * * @throws Exception In case of error. */ @Test public void informsAboutMissingEolAtEof() throws Exception { final Environment env = new Environment.Mock() .withFile( "src/main/resources/valid5.xml", // @checkstyle LineLength (1 line) "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xmlns=\"http://maven.apache.org/DECORATION/1.3.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/DECORATION/1.3.0 http://maven.apache.org/xsd/decoration-1.3.0.xsd\" name=\"xockets-hadoop-transport\">\n</project>"); final Validator validator = new XmlValidator(true); String message = ""; try { validator.validate(env); } catch (final ValidationException ex) { message = ex.getMessage(); } MatcherAssert.assertThat( message, Matchers.allOf( Matchers.containsString("--- before"), Matchers.containsString("+++ after"), Matchers.containsString("</project>\n+"))); }
/** * Should fail validation for invalid XML if multiple schemas are specified. * * @throws Exception If something wrong happens inside. */ @Test(expected = ValidationException.class) public void failsValidationWithMultipleSchemas() throws Exception { // @checkstyle LineLength (10 lines) final String xml = new StringBuilder() .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?> ") .append("<beans xmlns=\"http://www.springframework.org/schema/beans\" ") .append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ") .append("xmlns:util=\"http://www.springframework.org/schema/util\" ") .append("xsi:schemaLocation=\"") .append( "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ") .append( "http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd\">") .append("<bean noSuchAttribute=\"fail\"/>") .append("<util:shouldFail/>") .append("</beans>") .toString(); final Environment env = new Environment.Mock().withFile("src/main/resources/invalid-multi.xml", xml); final Validator validator = new XmlValidator(false); validator.validate(env); }
/** * CodeNarcValidator can report full names of files that contain violations. * * @throws Exception If error message does not include filename. */ @Test(expected = ValidationException.class) public void reportsFullFileNamesOfGroovyScripts() throws Exception { final Environment env = new Environment.Mock().withFile("src/main/Foo.groovy", "System.out.println('foo')"); final Validator validator = new CodeNarcValidator(); final CodeNarcAppender appender = new CodeNarcAppender(); org.apache.log4j.Logger.getRootLogger().addAppender(appender); try { validator.validate(env); } catch (final ValidationException ex) { final List<String> messages = appender.getMessages(); final Pattern pattern = Pattern.compile("[a-zA-Z0-9_/\\\\:~.]+\\.groovy\\[\\d+\\]: .*"); for (final String message : messages) { if (message.startsWith("CodeNarc validated ")) { continue; } MatcherAssert.assertThat( pattern.matcher(message).matches(), Matchers.describedAs(message, Matchers.is(true))); } throw ex; } finally { org.apache.log4j.Logger.getRootLogger().removeAppender(appender); } }
/** * Should pass validation for valid XML when multiple schemas are specified. * * @throws Exception If something wrong happens inside. */ @Test public void passesValidationWithMultipleSchemas() throws Exception { // @checkstyle MultipleStringLiterals (50 lines) // @checkstyle LineLength (10 lines) final String xml = new StringBuilder() .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?> ") .append("<beans xmlns=\"http://www.springframework.org/schema/beans\" ") .append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ") .append("xmlns:util=\"http://www.springframework.org/schema/util\" ") .append("xsi:schemaLocation=\"") .append( "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ") .append( "http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd\">") .append("<bean id=\"bean\" class=\"bean\"></bean>") .append("<util:constant static-field=\"blah\"/>") .append("</beans>") .toString(); final Environment env = new Environment.Mock().withFile("src/main/resources/valid-multi.xml", xml); final Validator validator = new XmlValidator(false); validator.validate(env); }
/** * CodeNarcValidator can pass valid Groovy sources without any exceptions. * * @throws Exception If something wrong happens inside. */ @Test public void passesCorrectFilesWithoutExceptions() throws Exception { final Validator validator = new CodeNarcValidator(); final Environment env = new Environment.Mock().withFile("src/foo/Foo.groovy", "// empty"); validator.validate(env); }