From f48144faa8c1d2bd3896f1082b18b00cb718af6a Mon Sep 17 00:00:00 2001 From: Yass Almardoud Date: Mon, 15 Jul 2024 14:42:41 +0200 Subject: [PATCH 1/8] #478 Bug fix Added in XmlRuleParserPlugin only to accept xml rule files Moved XmlHelper from core rule to shared Added test in XmlRuleParserPluginTest Adjusted import in order --- .../core/rule/impl/reader/XmlHelper.java | 31 -------- .../rule/impl/reader/XmlRuleParserPlugin.java | 6 +- .../api/model/XmlRuleParserPluginTest.java | 55 +++++++++++++- .../plugin/PluginConfigurationReaderImpl.java | 2 +- .../core/shared/xml/XmlHelper.java | 75 +++++++++++++++++++ 5 files changed, 135 insertions(+), 34 deletions(-) delete mode 100644 core/rule/src/main/java/com/buschmais/jqassistant/core/rule/impl/reader/XmlHelper.java create mode 100644 core/shared/src/main/java/com/buschmais/jqassistant/core/shared/xml/XmlHelper.java diff --git a/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/impl/reader/XmlHelper.java b/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/impl/reader/XmlHelper.java deleted file mode 100644 index cf73aada3b..0000000000 --- a/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/impl/reader/XmlHelper.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.buschmais.jqassistant.core.rule.impl.reader; - -import javax.xml.XMLConstants; -import javax.xml.validation.Schema; -import javax.xml.validation.SchemaFactory; - -import org.xml.sax.SAXException; - -/** - * Provides utility functions for working with XML files. - */ -public class XmlHelper { - - /** - * Return a {@link Schema} instance for the given resource. - * - * @param resource - * The resource. - * @return The {@link Schema} instance. - */ - public static Schema getSchema(String resource) { - Schema schema; - try { - SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - schema = schemaFactory.newSchema(XmlHelper.class.getResource(resource)); - } catch (SAXException e) { - throw new IllegalStateException("Cannot read rules schema.", e); - } - return schema; - } -} diff --git a/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/impl/reader/XmlRuleParserPlugin.java b/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/impl/reader/XmlRuleParserPlugin.java index 06a736d291..503037c36e 100644 --- a/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/impl/reader/XmlRuleParserPlugin.java +++ b/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/impl/reader/XmlRuleParserPlugin.java @@ -14,10 +14,12 @@ import com.buschmais.jqassistant.core.rule.api.source.RuleSource; import com.buschmais.jqassistant.core.rule.impl.SourceExecutable; import com.buschmais.jqassistant.core.shared.xml.JAXBUnmarshaller; +import com.buschmais.jqassistant.core.shared.xml.XmlHelper; import org.jqassistant.schema.rule.v2.*; import static com.buschmais.jqassistant.core.rule.impl.reader.IndentHelper.removeIndent; +import static com.buschmais.jqassistant.core.shared.xml.XmlHelper.rootElementMatches; import static java.util.stream.Collectors.toSet; /** * A {@link RuleParserPlugin} implementation. @@ -38,7 +40,9 @@ public void initialize() { @Override public boolean accepts(RuleSource ruleSource) { - return ruleSource.getId().toLowerCase().endsWith(".xml"); + return ruleSource.getId() + .toLowerCase() + .endsWith(".xml") && rootElementMatches(ruleSource::getInputStream, qname -> "jqassistant-rules".equals(qname.getLocalPart())); } @Override diff --git a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java index d73b3212a0..15f0abb4b2 100644 --- a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java +++ b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java @@ -1,12 +1,17 @@ package com.buschmais.jqassistant.core.rule.api.model; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.net.URL; import java.util.Collection; import java.util.Map; import java.util.Set; +import javax.xml.stream.XMLStreamException; + import com.buschmais.jqassistant.core.rule.api.configuration.Rule; import com.buschmais.jqassistant.core.rule.api.reader.RuleParserPlugin; +import com.buschmais.jqassistant.core.rule.api.source.RuleSource; import com.buschmais.jqassistant.core.rule.api.source.UrlRuleSource; import com.buschmais.jqassistant.core.rule.impl.reader.XmlRuleParserPlugin; @@ -18,6 +23,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class XmlRuleParserPluginTest { @@ -42,7 +50,8 @@ void scriptRule() throws Exception { private void verifyExecutableRule(Collection rules) { for (AbstractRule rule : rules) { assertThat(rule).isInstanceOf(ExecutableRule.class); - assertThat(((ExecutableRule) rule).getExecutable().getLanguage()).isEqualTo("javascript"); + assertThat(((ExecutableRule) rule).getExecutable() + .getLanguage()).isEqualTo("javascript"); } } @@ -100,4 +109,48 @@ void ruleSchema_1_8() throws RuleException { assertThat(conceptIds.size()).isEqualTo(1); assertThat(conceptIds, IsCollectionContaining.hasItems("test")); } + + + private RuleSource ruleSource; + private XmlRuleParserPlugin yourClassUnderTest; + + + @Test + void testAccepts_ValidXmlFile_ReturnsTrue() throws XMLStreamException, IOException { + XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); + RuleSource mockRuleSource = mock(RuleSource.class); + + when(mockRuleSource.getId()).thenReturn("jqassistant-rules.xml"); + when(mockRuleSource.getInputStream()).thenReturn( + new ByteArrayInputStream("".getBytes()) + ); + boolean result = xmlRuleParserPlugin.accepts(mockRuleSource); + assertTrue(result); + } + @Test + void testAccepts_InvalidXmlFile_ReturnsFalse() throws XMLStreamException, IOException { + XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); + RuleSource mockRuleSource = mock(RuleSource.class); + + when(mockRuleSource.getId()).thenReturn("report.xml"); + when(mockRuleSource.getInputStream()).thenReturn( + new ByteArrayInputStream("".getBytes()) + ); + boolean result = xmlRuleParserPlugin.accepts(mockRuleSource); + assertFalse(result); + } +// @Test +// void testAccepts_InvalidXFile_ReturnsFalse() throws XMLStreamException, IOException { +// XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); +// RuleSource mockRuleSource = mock(RuleSource.class); +// +// when(mockRuleSource.getId()).thenReturn("report.txt"); +// when(mockRuleSource +// .getInputStream()).thenReturn( new ByteArrayInputStream("".getBytes()) +// ); +// boolean result = xmlRuleParserPlugin.accepts(mockRuleSource); +// assertFalse(result); +// } + + // new ByteArrayInputStream("".getBytes()) } diff --git a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/impl/plugin/PluginConfigurationReaderImpl.java b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/impl/plugin/PluginConfigurationReaderImpl.java index 5576dd3bba..af3e8eb20b 100644 --- a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/impl/plugin/PluginConfigurationReaderImpl.java +++ b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/impl/plugin/PluginConfigurationReaderImpl.java @@ -11,10 +11,10 @@ import javax.xml.validation.Schema; -import com.buschmais.jqassistant.core.rule.impl.reader.XmlHelper; import com.buschmais.jqassistant.core.runtime.api.plugin.PluginClassLoader; import com.buschmais.jqassistant.core.runtime.api.plugin.PluginConfigurationReader; import com.buschmais.jqassistant.core.shared.xml.JAXBUnmarshaller; +import com.buschmais.jqassistant.core.shared.xml.XmlHelper; import org.jqassistant.schema.plugin.v2.JqassistantPlugin; import org.slf4j.Logger; diff --git a/core/shared/src/main/java/com/buschmais/jqassistant/core/shared/xml/XmlHelper.java b/core/shared/src/main/java/com/buschmais/jqassistant/core/shared/xml/XmlHelper.java new file mode 100644 index 0000000000..066aed98e0 --- /dev/null +++ b/core/shared/src/main/java/com/buschmais/jqassistant/core/shared/xml/XmlHelper.java @@ -0,0 +1,75 @@ +package com.buschmais.jqassistant.core.shared.xml; + +import java.io.IOException; +import java.io.InputStream; +import java.util.function.Predicate; + +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; + +import lombok.extern.slf4j.Slf4j; +import org.xml.sax.SAXException; + +/** + * Provides utility functions for working with XML files. + */ +@Slf4j +public class XmlHelper { + + private final static XMLInputFactory xmlInputFactory; + + static { + xmlInputFactory = XMLInputFactory.newInstance(); + xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false); + } + + public static XMLInputFactory getXMLInputFactory() { + return xmlInputFactory; + } + + public static boolean rootElementMatches(InputStreamSupplier inputStreamSupplier, Predicate rootElementPredicate) { + try (InputStream stream = inputStreamSupplier.get()) { + XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(stream); + while (reader.hasNext()) { + if (reader.next() == XMLStreamConstants.START_ELEMENT) { + return rootElementPredicate.test(reader.getName()); + } + } + } catch (XMLStreamException e) { + log.warn("Cannot parse XML file."); + } catch (IOException e) { + throw new IllegalStateException("Cannot close XML document.", e); + } + return false; + } + + public interface InputStreamSupplier { + + InputStream get() throws IOException; + + } + + /** + * Return a {@link Schema} instance for the given resource. + * + * @param resource + * The resource. + * @return The {@link Schema} instance. + */ + public static Schema getSchema(String resource) { + Schema schema; + try { + SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + schema = schemaFactory.newSchema(XmlHelper.class.getResource(resource)); + } catch (SAXException e) { + throw new IllegalStateException("Cannot read rules schema.", e); + } + return schema; + } +} From 466c4df6ec188632f7dd0145e01acef3ed2d50bb Mon Sep 17 00:00:00 2001 From: Yass Almardoud Date: Mon, 15 Jul 2024 14:59:18 +0200 Subject: [PATCH 2/8] #478 Bug fix Added 1 test in XmlRuleParserPluginTest Cleaned code --- .../api/model/XmlRuleParserPluginTest.java | 43 ++++++++----------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java index 15f0abb4b2..fae492e9d5 100644 --- a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java +++ b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java @@ -24,8 +24,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class XmlRuleParserPluginTest { @@ -116,41 +115,37 @@ void ruleSchema_1_8() throws RuleException { @Test - void testAccepts_ValidXmlFile_ReturnsTrue() throws XMLStreamException, IOException { + void testAccepts_ValidXmlFile_ReturnsTrue() throws IOException { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); RuleSource mockRuleSource = mock(RuleSource.class); - when(mockRuleSource.getId()).thenReturn("jqassistant-rules.xml"); + doReturn("jqassistant-rules.xml").when(mockRuleSource).getId(); when(mockRuleSource.getInputStream()).thenReturn( - new ByteArrayInputStream("".getBytes()) - ); - boolean result = xmlRuleParserPlugin.accepts(mockRuleSource); - assertTrue(result); + new ByteArrayInputStream("".getBytes())); + assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isTrue(); + } @Test - void testAccepts_InvalidXmlFile_ReturnsFalse() throws XMLStreamException, IOException { + void testAccepts_InvalidXmlFile_ReturnsFalse() throws IOException { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); RuleSource mockRuleSource = mock(RuleSource.class); - when(mockRuleSource.getId()).thenReturn("report.xml"); + doReturn("rules.xml").when(mockRuleSource).getId(); when(mockRuleSource.getInputStream()).thenReturn( new ByteArrayInputStream("".getBytes()) ); - boolean result = xmlRuleParserPlugin.accepts(mockRuleSource); - assertFalse(result); + + assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isFalse(); + } + @Test + void testAccepts_InvalidXFile_ReturnsFalse() { + XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); + RuleSource mockRuleSource = mock(RuleSource.class); + + doReturn("report.txt").when(mockRuleSource).getId(); + + assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isFalse(); } -// @Test -// void testAccepts_InvalidXFile_ReturnsFalse() throws XMLStreamException, IOException { -// XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); -// RuleSource mockRuleSource = mock(RuleSource.class); -// -// when(mockRuleSource.getId()).thenReturn("report.txt"); -// when(mockRuleSource -// .getInputStream()).thenReturn( new ByteArrayInputStream("".getBytes()) -// ); -// boolean result = xmlRuleParserPlugin.accepts(mockRuleSource); -// assertFalse(result); -// } // new ByteArrayInputStream("".getBytes()) } From 31dd1539f14e13c97935c51dd7669b25c462e217 Mon Sep 17 00:00:00 2001 From: Yass Almardoud Date: Mon, 15 Jul 2024 15:09:41 +0200 Subject: [PATCH 3/8] #478 Bug fix Adjusted import list in XmlRuleParserPluginTest --- .../core/rule/api/model/XmlRuleParserPluginTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java index fae492e9d5..d725f82726 100644 --- a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java +++ b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java @@ -7,7 +7,6 @@ import java.util.Map; import java.util.Set; -import javax.xml.stream.XMLStreamException; import com.buschmais.jqassistant.core.rule.api.configuration.Rule; import com.buschmais.jqassistant.core.rule.api.reader.RuleParserPlugin; @@ -15,6 +14,7 @@ import com.buschmais.jqassistant.core.rule.api.source.UrlRuleSource; import com.buschmais.jqassistant.core.rule.impl.reader.XmlRuleParserPlugin; +import org.hamcrest.MatcherAssert; import org.hamcrest.core.IsCollectionContaining; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -22,8 +22,6 @@ import org.mockito.junit.jupiter.MockitoExtension; import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) @@ -106,7 +104,7 @@ void ruleSchema_1_8() throws RuleException { RuleSet ruleSet = RuleSetTestHelper.readRuleSet("/rules-1.8.xml", configuration); Set conceptIds = ruleSet.getConceptBucket().getIds(); assertThat(conceptIds.size()).isEqualTo(1); - assertThat(conceptIds, IsCollectionContaining.hasItems("test")); + MatcherAssert.assertThat(conceptIds, IsCollectionContaining.hasItems("test")); } From 846818f4a63d152f4244239a3cdc00125948bc69 Mon Sep 17 00:00:00 2001 From: Yass Almardoud Date: Mon, 15 Jul 2024 15:19:09 +0200 Subject: [PATCH 4/8] #478 Bug fix --- .../core/rule/api/model/XmlRuleParserPluginTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java index 936fabb643..92421dd028 100644 --- a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java +++ b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java @@ -106,11 +106,9 @@ void ruleSchema_1_8() throws RuleException { MatcherAssert.assertThat(conceptIds, IsCollectionContaining.hasItems("test")); } - private RuleSource ruleSource; private XmlRuleParserPlugin yourClassUnderTest; - @Test void testAccepts_ValidXmlFile_ReturnsTrue() throws IOException { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); @@ -120,7 +118,6 @@ void testAccepts_ValidXmlFile_ReturnsTrue() throws IOException { when(mockRuleSource.getInputStream()).thenReturn( new ByteArrayInputStream("".getBytes())); assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isTrue(); - } @Test void testAccepts_InvalidXmlFile_ReturnsFalse() throws IOException { @@ -144,5 +141,4 @@ void testAccepts_InvalidXFile_ReturnsFalse() { assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isFalse(); } - // new ByteArrayInputStream("".getBytes()) } From 427fab420e0b99184a5bd5501a0553499d1530ed Mon Sep 17 00:00:00 2001 From: Yass Almardoud Date: Tue, 16 Jul 2024 10:37:12 +0200 Subject: [PATCH 5/8] #479 Cleaned XmlRuleParserPluginTest --- .../core/rule/api/model/XmlRuleParserPluginTest.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java index 92421dd028..0490147449 100644 --- a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java +++ b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java @@ -106,8 +106,6 @@ void ruleSchema_1_8() throws RuleException { MatcherAssert.assertThat(conceptIds, IsCollectionContaining.hasItems("test")); } - private RuleSource ruleSource; - private XmlRuleParserPlugin yourClassUnderTest; @Test void testAccepts_ValidXmlFile_ReturnsTrue() throws IOException { @@ -124,11 +122,7 @@ void testAccepts_InvalidXmlFile_ReturnsFalse() throws IOException { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); RuleSource mockRuleSource = mock(RuleSource.class); - doReturn("rules.xml").when(mockRuleSource).getId(); - when(mockRuleSource.getInputStream()).thenReturn( - new ByteArrayInputStream("".getBytes()) - ); - + doReturn("report.xml").when(mockRuleSource).getId(); assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isFalse(); } @Test From 978042c62849701a6d593fbe871b888800a1c2a4 Mon Sep 17 00:00:00 2001 From: Yass Almardoud Date: Tue, 16 Jul 2024 11:14:05 +0200 Subject: [PATCH 6/8] #482 Cleaned XmlRuleParserPluginTest --- .../core/rule/api/model/XmlRuleParserPluginTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java index 0490147449..9215df81fe 100644 --- a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java +++ b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java @@ -106,7 +106,6 @@ void ruleSchema_1_8() throws RuleException { MatcherAssert.assertThat(conceptIds, IsCollectionContaining.hasItems("test")); } - @Test void testAccepts_ValidXmlFile_ReturnsTrue() throws IOException { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); @@ -117,6 +116,7 @@ void testAccepts_ValidXmlFile_ReturnsTrue() throws IOException { new ByteArrayInputStream("".getBytes())); assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isTrue(); } + @Test void testAccepts_InvalidXmlFile_ReturnsFalse() throws IOException { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); @@ -125,13 +125,13 @@ void testAccepts_InvalidXmlFile_ReturnsFalse() throws IOException { doReturn("report.xml").when(mockRuleSource).getId(); assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isFalse(); } + @Test void testAccepts_InvalidXFile_ReturnsFalse() { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); RuleSource mockRuleSource = mock(RuleSource.class); doReturn("report.txt").when(mockRuleSource).getId(); - assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isFalse(); } From 433f354497ef03855959b90da6c091696d8b5ba9 Mon Sep 17 00:00:00 2001 From: Yass Almardoud Date: Tue, 16 Jul 2024 13:59:01 +0200 Subject: [PATCH 7/8] #482 Cleaned XmlRuleParserPluginTest --- .../api/model/XmlRuleParserPluginTest.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java index 9215df81fe..14dd77f330 100644 --- a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java +++ b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java @@ -7,7 +7,6 @@ import java.util.Map; import java.util.Set; - import com.buschmais.jqassistant.core.rule.api.configuration.Rule; import com.buschmais.jqassistant.core.rule.api.reader.RuleParserPlugin; import com.buschmais.jqassistant.core.rule.api.source.RuleSource; @@ -47,20 +46,25 @@ void scriptRule() throws Exception { private void verifyExecutableRule(Collection rules) { for (AbstractRule rule : rules) { assertThat(rule).isInstanceOf(ExecutableRule.class); - assertThat(((ExecutableRule) rule).getExecutable().getLanguage()).isEqualTo("javascript"); + assertThat(((ExecutableRule) rule).getExecutable() + .getLanguage()).isEqualTo("javascript"); } } @Test void ruleParameters() throws Exception { RuleSet ruleSet = RuleSetTestHelper.readRuleSet("/parameters.xml", configuration); - Concept concept = ruleSet.getConceptBucket().getById("test:ConceptWithParameters"); + Concept concept = ruleSet.getConceptBucket() + .getById("test:ConceptWithParameters"); verifyParameters(concept, false); - Concept conceptWithDefaultValues = ruleSet.getConceptBucket().getById("test:ConceptWithParametersAndDefaultValues"); + Concept conceptWithDefaultValues = ruleSet.getConceptBucket() + .getById("test:ConceptWithParametersAndDefaultValues"); verifyParameters(conceptWithDefaultValues, true); - Constraint constraint = ruleSet.getConstraintBucket().getById("test:ConstraintWithParameters"); + Constraint constraint = ruleSet.getConstraintBucket() + .getById("test:ConstraintWithParameters"); verifyParameters(constraint, false); - Constraint constraintWithDefaultValues = ruleSet.getConstraintBucket().getById("test:ConstraintWithParametersAndDefaultValues"); + Constraint constraintWithDefaultValues = ruleSet.getConstraintBucket() + .getById("test:ConstraintWithParametersAndDefaultValues"); verifyParameters(constraintWithDefaultValues, true); } @@ -88,20 +92,27 @@ void urlSource() throws Exception { assertThat(reader.accepts(ruleSource)).isEqualTo(true); reader.parse(ruleSource, ruleSetBuilder); RuleSet ruleSet = ruleSetBuilder.getRuleSet(); - assertThat(ruleSet.getConceptBucket().size()).isEqualTo(1); - assertThat(ruleSet.getConstraintBucket().size()).isEqualTo(1); - assertThat(ruleSet.getConceptBucket().getIds()).containsExactly("java:Throwable"); - assertThat(ruleSet.getConstraintBucket().getIds()).containsExactly("example:ConstructorOfDateMustNotBeUsed"); - assertThat(ruleSet.getGroupsBucket().size()).isEqualTo(1); - - Group group = ruleSet.getGroupsBucket().getById("default"); + assertThat(ruleSet.getConceptBucket() + .size()).isEqualTo(1); + assertThat(ruleSet.getConstraintBucket() + .size()).isEqualTo(1); + assertThat(ruleSet.getConceptBucket() + .getIds()).containsExactly("java:Throwable"); + assertThat(ruleSet.getConstraintBucket() + .getIds()).containsExactly("example:ConstructorOfDateMustNotBeUsed"); + assertThat(ruleSet.getGroupsBucket() + .size()).isEqualTo(1); + + Group group = ruleSet.getGroupsBucket() + .getById("default"); assertThat(group.getId()).isEqualTo("default"); } @Test void ruleSchema_1_8() throws RuleException { RuleSet ruleSet = RuleSetTestHelper.readRuleSet("/rules-1.8.xml", configuration); - Set conceptIds = ruleSet.getConceptBucket().getIds(); + Set conceptIds = ruleSet.getConceptBucket() + .getIds(); assertThat(conceptIds.size()).isEqualTo(1); MatcherAssert.assertThat(conceptIds, IsCollectionContaining.hasItems("test")); } @@ -111,9 +122,9 @@ void testAccepts_ValidXmlFile_ReturnsTrue() throws IOException { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); RuleSource mockRuleSource = mock(RuleSource.class); - doReturn("jqassistant-rules.xml").when(mockRuleSource).getId(); - when(mockRuleSource.getInputStream()).thenReturn( - new ByteArrayInputStream("".getBytes())); + doReturn("jqassistant-rules.xml").when(mockRuleSource) + .getId(); + when(mockRuleSource.getInputStream()).thenReturn(new ByteArrayInputStream("".getBytes())); assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isTrue(); } @@ -122,7 +133,8 @@ void testAccepts_InvalidXmlFile_ReturnsFalse() throws IOException { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); RuleSource mockRuleSource = mock(RuleSource.class); - doReturn("report.xml").when(mockRuleSource).getId(); + doReturn("report.xml").when(mockRuleSource) + .getId(); assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isFalse(); } @@ -131,7 +143,8 @@ void testAccepts_InvalidXFile_ReturnsFalse() { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); RuleSource mockRuleSource = mock(RuleSource.class); - doReturn("report.txt").when(mockRuleSource).getId(); + doReturn("report.txt").when(mockRuleSource) + .getId(); assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isFalse(); } From 8bf26ec3d4207c50b732997a664f5323c222d89e Mon Sep 17 00:00:00 2001 From: Yass Almardoud Date: Wed, 17 Jul 2024 09:54:53 +0200 Subject: [PATCH 8/8] #479 Updated XmlRuleParserPluginTest.java --- .../core/rule/api/model/XmlRuleParserPluginTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java index 14dd77f330..817003da52 100644 --- a/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java +++ b/core/rule/src/test/java/com/buschmais/jqassistant/core/rule/api/model/XmlRuleParserPluginTest.java @@ -133,8 +133,9 @@ void testAccepts_InvalidXmlFile_ReturnsFalse() throws IOException { XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin(); RuleSource mockRuleSource = mock(RuleSource.class); - doReturn("report.xml").when(mockRuleSource) + doReturn("jqassistant-report.xml").when(mockRuleSource) .getId(); + when(mockRuleSource.getInputStream()).thenReturn(new ByteArrayInputStream("".getBytes())); assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isFalse(); }