diff --git a/README.md b/README.md index 192cb7c..8dd4e10 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,17 @@ __via curl__ from the root directory of your project, run: -`sh -c "$(curl -fsSL https://raw.githubusercontent.com/jgitver/jgitver-maven-plugin/master/src/doc/scripts/install.sh)"` +``` shell +sh -c "$(curl -fsSL https://raw.githubusercontent.com/jgitver/jgitver-maven-plugin/master/src/doc/scripts/install.sh)" +``` __via wget__ from the root directory of your project, run: -`sh -c "$(wget https://raw.githubusercontent.com/jgitver/jgitver-maven-plugin/master/src/doc/scripts/install.sh -O -)"` +``` shell +sh -c "$(wget https://raw.githubusercontent.com/jgitver/jgitver-maven-plugin/master/src/doc/scripts/install.sh -O -)" +``` __manually__ @@ -41,7 +45,7 @@ __manually__ 1. Create file `.mvn/extensions.xml` 1. Put the following content to `.mvn/extensions.xml` (adapt to [latest version](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22fr.brouillard.oss%22%20a%3A%22jgitver-maven-plugin%22)). - ``` + ``` xml @@ -55,10 +59,15 @@ __manually__ ### Configuration In order to control [jgitver-maven-plugin](#jgitver-maven-plugin) behavior, you can provide a configuration -file under `$rootProjectDir/.mvn/jgitver.config.xml` having the following format: +file under `$rootProjectDir/.mvn/jgitver.config.xml`. +The configuration file must be compliant with the [xml-schemas](https://jgitver.github.io) supported. -``` - +Here is an example configuration file: + +``` xml + true/false true/false true/false @@ -75,7 +84,7 @@ file under `$rootProjectDir/.mvn/jgitver.config.xml` having the following format pattern - NAME + NAME ... @@ -93,7 +102,7 @@ Those are available under the following properties name: "jgitver.meta" where `m You can then use them as standard maven properties in your build: -``` +``` xml maven-antrun-plugin @@ -104,7 +113,6 @@ You can then use them as standard maven properties in your build: - version calculated: ${jgitver.calculated_version} dirty: ${jgitver.dirty} head_committer_name: ${jgitver.head_committer_name} @@ -250,7 +258,7 @@ In this context, when executing tests, maven will try to activate extensions sta To avoid such behavior, you need to tell `jgitver-maven-plugin` to ignore some directories. If you do not have already a jgitver configuration file, create one under `.mvn/jgitver.config.xml` and declare some exclusions (see [configuration](#configuration)): -``` +``` xml target/local-repo diff --git a/src/it/xml-schemas/invalid-schema-1.0.0/.mvn/extensions.xml b/src/it/xml-schemas/invalid-schema-1.0.0/.mvn/extensions.xml new file mode 100644 index 0000000..0183989 --- /dev/null +++ b/src/it/xml-schemas/invalid-schema-1.0.0/.mvn/extensions.xml @@ -0,0 +1,25 @@ + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + \ No newline at end of file diff --git a/src/it/xml-schemas/invalid-schema-1.0.0/.mvn/jgitver.config.xml b/src/it/xml-schemas/invalid-schema-1.0.0/.mvn/jgitver.config.xml new file mode 100644 index 0000000..8cd07d9 --- /dev/null +++ b/src/it/xml-schemas/invalid-schema-1.0.0/.mvn/jgitver.config.xml @@ -0,0 +1,22 @@ + + + + \ No newline at end of file diff --git a/src/it/xml-schemas/invalid-schema-1.0.0/invoker.properties b/src/it/xml-schemas/invalid-schema-1.0.0/invoker.properties new file mode 100644 index 0000000..506a600 --- /dev/null +++ b/src/it/xml-schemas/invalid-schema-1.0.0/invoker.properties @@ -0,0 +1,2 @@ +invoker.buildResult = failure +invoker.goals = -X install diff --git a/src/it/xml-schemas/invalid-schema-1.0.0/pom.xml b/src/it/xml-schemas/invalid-schema-1.0.0/pom.xml new file mode 100644 index 0000000..f593f4d --- /dev/null +++ b/src/it/xml-schemas/invalid-schema-1.0.0/pom.xml @@ -0,0 +1,32 @@ + + + + 4.0.0 + + fr.brouillard.oss.it.xml-schemas + invalid-1_0_0 + 0 + pom + + Verify dirty state is part of project name + + + UTF-8 + + diff --git a/src/it/xml-schemas/invalid-schema-1.0.0/prebuild.groovy b/src/it/xml-schemas/invalid-schema-1.0.0/prebuild.groovy new file mode 100644 index 0000000..e6da956 --- /dev/null +++ b/src/it/xml-schemas/invalid-schema-1.0.0/prebuild.groovy @@ -0,0 +1,50 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver-maven-plugin] (matthieu@brouillard.fr) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +def log = new PrintWriter( new File(basedir, "prebuild.log").newWriter("UTF-8"), true ) +log.println( "Prebuild started at: " + new Date() + " in: " + basedir ) + +[ + "git --version", + "rm -rf .git", + "git init", + "git config user.name nobody", + "git config user.email nobody@nowhere.com", + "echo A > content", + "git add .", + "git commit --message=initial_commit", + "git tag -a 1.0.0 --message=release_1.0.0", + "echo B > content", + "git status", + "git log --graph --oneline" +].each{ command -> + + def proc = command.execute(null, basedir) + def sout = new StringBuilder(), serr = new StringBuilder() + proc.waitForProcessOutput(sout, serr) + + log.println( "cmd: " + command ) + log.println( "out:" ) ; log.println( sout.toString().trim() ) + log.println( "err:" ) ; log.println( serr.toString().trim() ) + log.println( "ret: " + proc.exitValue() ) + + assert proc.exitValue() == 0 + +} + +log.println( "Prebuild completed at: " + new Date() ) +log.close() +return true diff --git a/src/it/xml-schemas/invalid-schema-1.0.0/verify.groovy b/src/it/xml-schemas/invalid-schema-1.0.0/verify.groovy new file mode 100644 index 0000000..7d22db8 --- /dev/null +++ b/src/it/xml-schemas/invalid-schema-1.0.0/verify.groovy @@ -0,0 +1,46 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver-maven-plugin] (matthieu@brouillard.fr) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +def log = new PrintWriter( new File(basedir, "verify.log").newWriter("UTF-8"), true ) +log.println( "Verify started at: " + new Date() + " in: " + basedir ) + +[ + "rm -rf .git" +].each{ command -> + + def proc = command.execute(null, basedir) + def sout = new StringBuilder(), serr = new StringBuilder() + proc.waitForProcessOutput(sout, serr) + + log.println( "cmd: " + command ) + log.println( "out:" ) ; log.println( sout.toString().trim() ) + log.println( "err:" ) ; log.println( serr.toString().trim() ) + log.println( "ret: " + proc.exitValue() ) + + assert proc.exitValue() == 0 + +} + +def buildLog = new File(basedir, "build.log").readLines() + +// Check the version was used by the plugin execution +def saxException = buildLog.findAll { it =~ /org.xml.sax.SAXParseException.*Invalid content was found starting with element 'doesNotExist'/ } +log.println( "saxException: " + saxException ) +assert 0 < saxException.size() + +log.println( "Verify completed at: " + new Date() ) +log.close() +return true diff --git a/src/main/java/fr/brouillard/oss/jgitver/JGitverModelProcessor.java b/src/main/java/fr/brouillard/oss/jgitver/JGitverModelProcessor.java index ed28016..40b30c2 100644 --- a/src/main/java/fr/brouillard/oss/jgitver/JGitverModelProcessor.java +++ b/src/main/java/fr/brouillard/oss/jgitver/JGitverModelProcessor.java @@ -111,7 +111,7 @@ private void calculateVersionIfNecessary() throws Exception { .setUseDefaultBranchingPolicy(cfg.useDefaultBranchingPolicy) .setNonQualifierBranches(cfg.nonQualifierBranches); - if (cfg.branchPolicies != null) { + if (cfg.branchPolicies != null && !cfg.branchPolicies.isEmpty()) { List policies = cfg.branchPolicies.stream() .map(bp -> new BranchingPolicy(bp.pattern, bp.transformations)) .collect(Collectors.toList()); @@ -135,6 +135,7 @@ private Model provisionModel(Model model, Map options) throws IOExcep try { calculateVersionIfNecessary(); } catch (Exception ex) { + logger.error("failure while calculating version", ex); throw new IOException("cannot build a Model object using jgitver", ex); } diff --git a/src/main/java/fr/brouillard/oss/jgitver/cfg/BranchPolicy.java b/src/main/java/fr/brouillard/oss/jgitver/cfg/BranchPolicy.java index a488021..3dfe580 100644 --- a/src/main/java/fr/brouillard/oss/jgitver/cfg/BranchPolicy.java +++ b/src/main/java/fr/brouillard/oss/jgitver/cfg/BranchPolicy.java @@ -36,6 +36,7 @@ public class BranchPolicy { @XmlElement(name = "transformation") public List transformations = new LinkedList<>( Arrays.asList( - BranchNameTransformations.REPLACE_UNEXPECTED_CHARS_UNDERSCORE.name(), - BranchNameTransformations.LOWERCASE_EN.name())); + BranchNameTransformations.REPLACE_UNEXPECTED_CHARS_UNDERSCORE.name() + , BranchNameTransformations.LOWERCASE_EN.name()) + ); } diff --git a/src/main/java/fr/brouillard/oss/jgitver/cfg/Configuration.java b/src/main/java/fr/brouillard/oss/jgitver/cfg/Configuration.java index a8badea..27c79c0 100644 --- a/src/main/java/fr/brouillard/oss/jgitver/cfg/Configuration.java +++ b/src/main/java/fr/brouillard/oss/jgitver/cfg/Configuration.java @@ -48,5 +48,5 @@ public class Configuration { public List exclusions = new LinkedList<>(); @XmlElementWrapper(name = "branchPolicies") @XmlElement(name = "branchPolicy") - public List branchPolicies; + public List branchPolicies = new LinkedList<>(); } diff --git a/src/main/java/fr/brouillard/oss/jgitver/cfg/ConfigurationLoader.java b/src/main/java/fr/brouillard/oss/jgitver/cfg/ConfigurationLoader.java index 1de96f7..0ecf0f3 100644 --- a/src/main/java/fr/brouillard/oss/jgitver/cfg/ConfigurationLoader.java +++ b/src/main/java/fr/brouillard/oss/jgitver/cfg/ConfigurationLoader.java @@ -16,29 +16,41 @@ package fr.brouillard.oss.jgitver.cfg; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; +import java.io.StringReader; +import java.nio.file.Files; +import java.util.stream.Collectors; +import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; import org.apache.maven.MavenExecutionException; import org.codehaus.plexus.logging.Logger; +import org.xml.sax.SAXException; + +import fr.brouillard.oss.jgitver.cfg.schema.ConfigurationSchema; public class ConfigurationLoader { + private final static String NAMESPACE = "http://jgitver.github.io/maven/configuration/1.0.0-beta"; + /** * Loads a Configuration object from the root directory. + * * @param rootDirectory the root directory of the maven project * @param logger the logger to report activity - * @return a non null Configuration object from the file $rootDirectory/.mvn/jgitver.config.xml - * or a default one with default values if the configuration file does not exist - * @throws MavenExecutionException if the file exists but cannot be read correctly + * @return a non null Configuration object from the file + * $rootDirectory/.mvn/jgitver.config.xml or a default one with + * default values if the configuration file does not exist + * @throws MavenExecutionException + * if the file exists but cannot be read correctly */ public static Configuration loadFromRoot(File rootDirectory, Logger logger) throws MavenExecutionException { - JAXBContext jaxbContext; - Unmarshaller unmarshaller; File extensionMavenCoreDirectory = new File(rootDirectory, ".mvn"); File configurationXml = new File(extensionMavenCoreDirectory, "jgitver.config.xml"); if (!configurationXml.canRead()) { @@ -49,32 +61,37 @@ public static Configuration loadFromRoot(File rootDirectory, Logger logger) thro return new Configuration(); } } + try { logger.info("using jgitver configuration file: " + configurationXml); + String configurationContent = Files.readAllLines(configurationXml.toPath()).stream().collect(Collectors.joining("\n")); + + Configuration c = loadConfiguration(configurationContent); + return c; + } catch (JAXBException | IOException | SAXException ex) { + throw new MavenExecutionException("cannot read configuration file " + configurationXml, ex); + } + } + + private static Configuration loadConfiguration(String configurationContent) throws JAXBException, SAXException, IOException { + JAXBContext jaxbContext; + Unmarshaller unmarshaller; + + if (configurationContent.contains(NAMESPACE)) { + jaxbContext = JAXBContext.newInstance(ConfigurationSchema.class); + + StreamSource contentStreamSource = new StreamSource(new StringReader(configurationContent)); + Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(ConfigurationLoader.class.getResource("/schemas/jgitver-configuration-v1_0_0-beta.xsd")); + Validator validator = schema.newValidator(); + validator.validate(contentStreamSource); + unmarshaller = jaxbContext.createUnmarshaller(); + unmarshaller.setSchema(schema); + ConfigurationSchema cs = (ConfigurationSchema) unmarshaller.unmarshal(new StringReader(configurationContent)); + return cs.asConfiguration(); + } else { jaxbContext = JAXBContext.newInstance(Configuration.class); unmarshaller = jaxbContext.createUnmarshaller(); - final FileInputStream is = new FileInputStream(configurationXml); - try { - Configuration c = (Configuration) unmarshaller.unmarshal(is); - return c; - } finally { - try { - is.close(); - } catch (IOException ignore) { - // ignore - } - } - } catch (JAXBException | FileNotFoundException ex) { - throw new MavenExecutionException("cannot read configuration file " + configurationXml, ex); + return (Configuration) unmarshaller.unmarshal(new StringReader(configurationContent)); } } - -// public static void main(String[] args) throws Exception { -// JAXBContext context = JAXBContext.newInstance(Configuration.class); -// Marshaller marshaller = context.createMarshaller(); -// StringWriter sw = new StringWriter(); -// marshaller.marshal(new Configuration(), sw); -// -// System.out.println(sw.toString()); -// } } diff --git a/src/main/java/fr/brouillard/oss/jgitver/cfg/schema/BranchPolicySchema.java b/src/main/java/fr/brouillard/oss/jgitver/cfg/schema/BranchPolicySchema.java new file mode 100644 index 0000000..aaf7de2 --- /dev/null +++ b/src/main/java/fr/brouillard/oss/jgitver/cfg/schema/BranchPolicySchema.java @@ -0,0 +1,50 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver-maven-plugin] (matthieu@brouillard.fr) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package fr.brouillard.oss.jgitver.cfg.schema; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +import fr.brouillard.oss.jgitver.BranchingPolicy.BranchNameTransformations; +import fr.brouillard.oss.jgitver.cfg.BranchPolicy;; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class BranchPolicySchema { + @XmlElement(name = "pattern") + public String pattern; + @XmlElementWrapper(name = "transformations") + @XmlElement(name = "transformation") + public List transformations = new LinkedList<>( + Arrays.asList( + BranchNameTransformations.REPLACE_UNEXPECTED_CHARS_UNDERSCORE.name() + , BranchNameTransformations.LOWERCASE_EN.name()) + ); + + public BranchPolicy asBranchPolicy() { + BranchPolicy bp = new BranchPolicy(); + bp.pattern = pattern; + bp.transformations.addAll(transformations); + return bp; + } +} diff --git a/src/main/java/fr/brouillard/oss/jgitver/cfg/schema/ConfigurationSchema.java b/src/main/java/fr/brouillard/oss/jgitver/cfg/schema/ConfigurationSchema.java new file mode 100644 index 0000000..2f9d2a5 --- /dev/null +++ b/src/main/java/fr/brouillard/oss/jgitver/cfg/schema/ConfigurationSchema.java @@ -0,0 +1,72 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver-maven-plugin] (matthieu@brouillard.fr) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package fr.brouillard.oss.jgitver.cfg.schema; + +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +import fr.brouillard.oss.jgitver.cfg.Configuration; + +@XmlRootElement(name = "configuration") +@XmlAccessorType(XmlAccessType.FIELD) +public class ConfigurationSchema { + @XmlElement(name = "mavenLike") + public boolean mavenLike = true; + @XmlElement + public boolean autoIncrementPatch = true; + @XmlElement + public boolean useCommitDistance = false; + @XmlElement + public boolean useDirty = false; + @XmlElement + public boolean useDefaultBranchingPolicy = true; + @XmlElement + public boolean useGitCommitId = false; + @XmlElement + public int gitCommitIdLength = 8; + @XmlElement + public String nonQualifierBranches = "master"; + @XmlElementWrapper(name = "exclusions") + @XmlElement(name = "exclusion") + public List exclusions = new LinkedList<>(); + @XmlElementWrapper(name = "branchPolicies") + @XmlElement(name = "branchPolicy") + public List branchPolicies = new LinkedList<>(); + + public Configuration asConfiguration() { + Configuration c = new Configuration(); + c.mavenLike = mavenLike; + c.autoIncrementPatch = autoIncrementPatch; + c.useCommitDistance = useCommitDistance; + c.useDirty = useDirty; + c.useDefaultBranchingPolicy = useDefaultBranchingPolicy; + c.useGitCommitId = useGitCommitId; + c.gitCommitIdLength = gitCommitIdLength; + c.nonQualifierBranches = nonQualifierBranches; + + c.exclusions.addAll(exclusions); + c.branchPolicies.addAll(branchPolicies.stream().map(BranchPolicySchema::asBranchPolicy).collect(Collectors.toList())); + + return c; + } +} diff --git a/src/main/java/fr/brouillard/oss/jgitver/cfg/schema/package-info.java b/src/main/java/fr/brouillard/oss/jgitver/cfg/schema/package-info.java new file mode 100644 index 0000000..f16fa78 --- /dev/null +++ b/src/main/java/fr/brouillard/oss/jgitver/cfg/schema/package-info.java @@ -0,0 +1,20 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver-maven-plugin] (matthieu@brouillard.fr) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +@XmlSchema(namespace = "http://jgitver.github.io/maven/configuration/1.0.0-beta", elementFormDefault = XmlNsForm.QUALIFIED) +package fr.brouillard.oss.jgitver.cfg.schema; + +import javax.xml.bind.annotation.XmlNsForm; +import javax.xml.bind.annotation.XmlSchema; diff --git a/src/main/resources/schemas/jgitver-configuration-v1_0_0-beta.xsd b/src/main/resources/schemas/jgitver-configuration-v1_0_0-beta.xsd new file mode 100644 index 0000000..27bf689 --- /dev/null +++ b/src/main/resources/schemas/jgitver-configuration-v1_0_0-beta.xsd @@ -0,0 +1,92 @@ + + + + + + + + + add '-SNAPSHOT' to all versions that do not correspond to a release + + + + + control how a base version is ued to calculate the new version, here the patch number of the version will be incremented. If version X.Y.Z is found as base version, then X.Y.(Z+1) will be used for the new version. + + + + add a distance number from the base version as a version qualifier. For example if HEAD is 3 commit ahead the base version 1.0.0 then calculated version will be 1.0.0-3 + + + + add a 'dirty' qualifier if the version is calculated from a stale git directory (modified files, new files, ...) + + + + appends a qualifier with the git sha1 of the HEAD commit, the lenght is controled by the property 'gitCommitIdLength' + + + + + comma separated list of branches for which no qualifier should be defined + + + + + + + + + + + + relative path to a directory to be excluded by jgitver. Paths are relative to the base multi module directory of the maven project. + + + + + + + + + + + + + + + + java regex pattern that will match branch name. The regex pattern to be valid MUST contain a unique group selection. + + + + + + + + + a transformation to apply, value taken from fr.brouillard.oss.jgitver.BranchingPolicy#BranchNameTransformations + + + + + \ No newline at end of file diff --git a/src/test/java/fr/brouillard/oss/jgitver/cfg/ConfigurationLoaderTest.java b/src/test/java/fr/brouillard/oss/jgitver/cfg/ConfigurationLoaderTest.java index 6253955..c4fcc8f 100644 --- a/src/test/java/fr/brouillard/oss/jgitver/cfg/ConfigurationLoaderTest.java +++ b/src/test/java/fr/brouillard/oss/jgitver/cfg/ConfigurationLoaderTest.java @@ -18,6 +18,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; import java.io.IOException; @@ -50,6 +51,25 @@ public void can_load_a_simple_configuration() throws MavenExecutionException, IO } } + @Test + public void can_load_a_simple_configuration_with_xml_schema() throws MavenExecutionException, IOException { + try (ResourceConfigurationProvider fromResource = ResourceConfigurationProvider.fromResource("/config/simple.cfg.with.schema.xml")) { + Configuration cfg = ConfigurationLoader.loadFromRoot(fromResource.getConfigurationDirectory(), inMemoryLogger); + assertThat(cfg, notNullValue()); + + assertThat(cfg.useCommitDistance, is(true)); + assertThat(cfg.mavenLike, is(false)); + } + } + + @Test (expected=MavenExecutionException.class) + public void must_fail_loading_an_invalid_configuration_with_xml_schema() throws MavenExecutionException, IOException { + try (ResourceConfigurationProvider fromResource = ResourceConfigurationProvider.fromResource("/config/invalid-with-schema.xml")) { + Configuration cfg = ConfigurationLoader.loadFromRoot(fromResource.getConfigurationDirectory(), inMemoryLogger); + fail("should have failed loading an erroneous file"); + } + } + @Test public void can_load_a_complex_configuration_with_branching_policy() throws MavenExecutionException, IOException { try (ResourceConfigurationProvider fromResource = ResourceConfigurationProvider.fromResource("/config/complex-branch.cfg.xml")) { diff --git a/src/test/resources/config/invalid-with-schema.xml b/src/test/resources/config/invalid-with-schema.xml new file mode 100644 index 0000000..8cd07d9 --- /dev/null +++ b/src/test/resources/config/invalid-with-schema.xml @@ -0,0 +1,22 @@ + + + + \ No newline at end of file diff --git a/src/test/resources/config/simple.cfg.with.schema.xml b/src/test/resources/config/simple.cfg.with.schema.xml new file mode 100644 index 0000000..f8d4c0c --- /dev/null +++ b/src/test/resources/config/simple.cfg.with.schema.xml @@ -0,0 +1,6 @@ + + false + true + \ No newline at end of file