summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Rudyy <orudyy@apache.org>2014-03-21 12:46:13 +0000
committerAlex Rudyy <orudyy@apache.org>2014-03-21 12:46:13 +0000
commitf96b8856ba95a9d6d29981b8f41fa9319d9a6813 (patch)
treef927c1350c6024a05dded05a07ecbc469acbc77b
parentf34e698b3beb0aa5810adff8bc101b37aa6eb2d2 (diff)
downloadqpid-python-f96b8856ba95a9d6d29981b8f41fa9319d9a6813.tar.gz
QPID-5048: new maven enforcer plugin rule for checking license files
Patch supplied by Andrew MacBean <andymacbean@gmail.com> git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1579936 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--qpid/java/maven/qpid-enforcer-plugin-rules/pom.xml93
-rw-r--r--qpid/java/maven/qpid-enforcer-plugin-rules/src/main/java/org/apache/qpid/maven/enforcer/rule/RequireFileContentsAreEquivalent.java104
-rw-r--r--qpid/java/maven/qpid-enforcer-plugin-rules/src/test/java/org/apache/qpid/maven/enforcer/rule/TestRequireFileContentsAreEquivalent.java149
3 files changed, 346 insertions, 0 deletions
diff --git a/qpid/java/maven/qpid-enforcer-plugin-rules/pom.xml b/qpid/java/maven/qpid-enforcer-plugin-rules/pom.xml
new file mode 100644
index 0000000000..47a393b888
--- /dev/null
+++ b/qpid/java/maven/qpid-enforcer-plugin-rules/pom.xml
@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.qpid.enforcer</groupId>
+ <artifactId>qpid-enforcer-plugin-rules</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <name>Qpid Maven Enforcer Plugin Rules</name>
+ <description>Custom maven enforcer plugin rules for the Qpid maven build.</description>
+
+ <properties>
+ <api.version>1.3.1</api.version>
+ <maven.version>2.2.1</maven.version>
+ <plexus.container.version>1.5.5</plexus.container.version>
+ <junit.version>4.11</junit.version>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven.enforcer</groupId>
+ <artifactId>enforcer-rules</artifactId>
+ <version>${api.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>1.3.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.enforcer</groupId>
+ <artifactId>enforcer-api</artifactId>
+ <version>${api.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>${maven.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-core</artifactId>
+ <version>${maven.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-artifact</artifactId>
+ <version>${maven.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>${maven.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-container-default</artifactId>
+ <version>${plexus.container.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.enforcer</groupId>
+ <artifactId>enforcer-rules</artifactId>
+ <version>${api.version}</version>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ </build>
+
+</project>
diff --git a/qpid/java/maven/qpid-enforcer-plugin-rules/src/main/java/org/apache/qpid/maven/enforcer/rule/RequireFileContentsAreEquivalent.java b/qpid/java/maven/qpid-enforcer-plugin-rules/src/main/java/org/apache/qpid/maven/enforcer/rule/RequireFileContentsAreEquivalent.java
new file mode 100644
index 0000000000..11186a5c7f
--- /dev/null
+++ b/qpid/java/maven/qpid-enforcer-plugin-rules/src/main/java/org/apache/qpid/maven/enforcer/rule/RequireFileContentsAreEquivalent.java
@@ -0,0 +1,104 @@
+/**
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You 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 org.apache.qpid.maven.enforcer.rule;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.maven.enforcer.rule.api.EnforcerRule;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.apache.maven.plugins.enforcer.AbstractStandardEnforcerRule;
+
+public class RequireFileContentsAreEquivalent extends AbstractStandardEnforcerRule
+{
+
+ final static String WHITESPACE_REGEX = "\\s+";
+ final static String EMPTY_STRING = "";
+
+ File[] files;
+
+ @Override
+ public void execute(final EnforcerRuleHelper enforcerRuleHelper) throws EnforcerRuleException
+ {
+ if (files.length < 2)
+ {
+ throw new EnforcerRuleException("The file list must contain at least two files for comparison.");
+ }
+
+ boolean success = true;
+ boolean firstTime = true;
+ String referenceContent = null;
+
+ for (final File file : files)
+ {
+ try
+ {
+ final String fileContent = FileUtils.readFileToString(file);
+ if (firstTime)
+ {
+ referenceContent = fileContent;
+ firstTime = false;
+ }
+ else if (referenceContent != null && fileContent != null)
+ {
+ final String strippedReferenceContent = referenceContent.replaceAll(WHITESPACE_REGEX, EMPTY_STRING);
+ final String strippedFileContent = fileContent.replaceAll(WHITESPACE_REGEX, EMPTY_STRING);
+ if (!strippedReferenceContent.equalsIgnoreCase(strippedFileContent))
+ {
+ success = false;
+ break;
+ }
+ }
+ else
+ {
+ throw new EnforcerRuleException("Unable to read file contents");
+ }
+ }
+ catch (final IOException ioe)
+ {
+ throw new EnforcerRuleException("Cannot process file : " + file.getName(), ioe);
+ }
+ }
+
+ if (!success)
+ {
+ throw new EnforcerRuleException("Files specified are not equal in content");
+ }
+ }
+
+ @Override
+ public String getCacheId()
+ {
+ return Integer.toString(Arrays.hashCode(files));
+ }
+
+ @Override
+ public boolean isCacheable()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean isResultValid(EnforcerRule arg0)
+ {
+ return true;
+ }
+
+}
diff --git a/qpid/java/maven/qpid-enforcer-plugin-rules/src/test/java/org/apache/qpid/maven/enforcer/rule/TestRequireFileContentsAreEquivalent.java b/qpid/java/maven/qpid-enforcer-plugin-rules/src/test/java/org/apache/qpid/maven/enforcer/rule/TestRequireFileContentsAreEquivalent.java
new file mode 100644
index 0000000000..7a5d214972
--- /dev/null
+++ b/qpid/java/maven/qpid-enforcer-plugin-rules/src/test/java/org/apache/qpid/maven/enforcer/rule/TestRequireFileContentsAreEquivalent.java
@@ -0,0 +1,149 @@
+/**
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You 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 org.apache.qpid.maven.enforcer.rule;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.plugins.enforcer.EnforcerTestUtils;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestRequireFileContentsAreEquivalent
+{
+ final RequireFileContentsAreEquivalent rule = new RequireFileContentsAreEquivalent();
+
+ final static String TEST_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
+ final static String TEST_TEXT_WHITESPACE = "Lorem ipsum dolor sit amet,\n consectetur adipiscing \t elit. ";
+ final static String ALTERNATE_TEST_TEXT_WHITESPACE = " Lorem \t ip sum dolor \n sit amet,\n consectetur adipiscing \t elit.";
+ final static String DIFFERENT_TEST_TEXT = "Donec velit felis, semper dapibus mattis vitae";
+
+ @Test
+ public void testDifferentContentFiles() throws Exception
+ {
+ final File f1 = createTestFile(1, TEST_TEXT);
+ final File f2 = createTestFile(2, DIFFERENT_TEST_TEXT);
+
+ rule.files = new File[]
+ { f1, f2 };
+
+ try
+ {
+ rule.execute(EnforcerTestUtils.getHelper());
+ Assert.fail("Files with different content should have failed enforcer rule");
+ }
+ catch (final EnforcerRuleException ere)
+ {
+ // do nothing
+ }
+ }
+
+ @Test
+ public void testIdenticalContentFiles() throws Exception
+ {
+ final File f1 = createTestFile(1, TEST_TEXT);
+ final File f2 = createTestFile(2, TEST_TEXT);
+
+ rule.files = new File[]
+ { f1, f2 };
+
+ rule.execute(EnforcerTestUtils.getHelper());
+ }
+
+ @Test
+ public void testUsingOneFileTwice() throws Exception
+ {
+ final File f1 = createTestFile(1, TEST_TEXT);
+
+ rule.files = new File[]
+ { f1, f1 };
+
+ rule.execute(EnforcerTestUtils.getHelper());
+ }
+
+ @Test
+ public void testSimilarFiles() throws Exception
+ {
+ final File f1 = createTestFile(1, TEST_TEXT);
+ final File f2 = createTestFile(2, TEST_TEXT_WHITESPACE);
+
+ rule.files = new File[]
+ { f1, f2 };
+
+ rule.execute(EnforcerTestUtils.getHelper());
+ }
+
+ @Test
+ public void testMultipleFilesOneDifferent() throws Exception
+ {
+ final File f1 = createTestFile(1, TEST_TEXT);
+ final File f2 = createTestFile(2, TEST_TEXT_WHITESPACE);
+ final File f3 = createTestFile(3, ALTERNATE_TEST_TEXT_WHITESPACE);
+ final File f4 = createTestFile(4, DIFFERENT_TEST_TEXT);
+
+ rule.files = new File[]
+ { f1, f2, f3, f4 };
+
+ try
+ {
+ rule.execute(EnforcerTestUtils.getHelper());
+ Assert.fail("Files with different content should have failed enforcer rule");
+ }
+ catch (final EnforcerRuleException ere)
+ {
+ // do nothing
+ }
+ }
+
+ @Test
+ public void testMultipleFilesAllSimilar() throws Exception
+ {
+ final File f1 = createTestFile(1, TEST_TEXT);
+ final File f2 = createTestFile(2, TEST_TEXT);
+ final File f3 = createTestFile(3, TEST_TEXT_WHITESPACE);
+ final File f4 = createTestFile(4, ALTERNATE_TEST_TEXT_WHITESPACE);
+
+ rule.files = new File[]
+ { f1, f2, f3, f4 };
+
+ rule.execute(EnforcerTestUtils.getHelper());
+ }
+
+ @After
+ public void deleteTestFiles() throws Exception
+ {
+ for (File file : rule.files)
+ {
+ if (file.exists())
+ {
+ file.delete();
+ }
+ }
+ }
+
+ private File createTestFile(final int id, final String content) throws IOException
+ {
+ final File file = File.createTempFile(TestRequireFileContentsAreEquivalent.class.getName() +
+ "-testfile" + id, "tmp");
+ file.deleteOnExit();
+ FileUtils.writeStringToFile(file, content);
+ return file;
+ }
+}