Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

478 jqa tries to load rules from jqa report file #482

Merged
merged 9 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.buschmais.jqassistant.core.rule.api.model;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reformat the class with the jQA settings


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 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;

Expand All @@ -18,6 +21,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class XmlRuleParserPluginTest {
Expand Down Expand Up @@ -112,4 +116,37 @@ void ruleSchema_1_8() throws RuleException {
assertThat(conceptIds.size()).isEqualTo(1);
MatcherAssert.assertThat(conceptIds, IsCollectionContaining.hasItems("test"));
}

@Test
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("<jqassistant-rules>".getBytes()));
assertThat(xmlRuleParserPlugin.accepts(mockRuleSource)).isTrue();
}

@Test
void testAccepts_InvalidXmlFile_ReturnsFalse() throws IOException {
XmlRuleParserPlugin xmlRuleParserPlugin = new XmlRuleParserPlugin();
RuleSource mockRuleSource = mock(RuleSource.class);

doReturn("jqassistant-report.xml").when(mockRuleSource)
.getId();
when(mockRuleSource.getInputStream()).thenReturn(new ByteArrayInputStream("<jqassistant-report>".getBytes()));
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<QName> 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;
}
}
Loading