Author: tchemit Date: 2009-05-11 21:25:22 +0000 (Mon, 11 May 2009) New Revision: 1528 Added: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/JarLicenseResolver.java maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/License.java maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseFactory.java maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseGenerator.java maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseGeneratorFactory.java maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseResolver.java maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/AbstractLicensePlugin.java maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/AvailableLicensesPlugin.java maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/LicensePlugin.java maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/ThirdPartyPlugin.java maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/BaseLicenseTestCase.java maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/JarLicenseResolverTest.java maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseFactoryTest.java maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseGeneratorFactoryTest.java maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseResolverTest.java maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/plugin/ maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/ maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/ maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/plugin/ maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/plugin/LicensePluginTest/ maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/plugin/LicensePluginTest/licenseOne.txt maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/plugin/LicensePluginTest/testOne.xml Modified: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/codelutin/license/plugin/impl/LicensePluginTest.java Log: migrate to nuiton Copied: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/JarLicenseResolver.java (from rev 1524, maven-nuiton-project-helper-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/JarLicenseResolver.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/JarLicenseResolver.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,33 @@ +package org.nuiton.license; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.File; +import java.net.URL; + +/** + * Un resolver de license dans le classpath. + * <p/> + * Le chemin de base est le suivant : + * <pre> + * /META-INF/licenses/ + * </pre> + * + * @author chemit + * @see LicenseResolver + */ +public class JarLicenseResolver extends LicenseResolver { + + protected static final Log log = LogFactory.getLog(JarLicenseResolver.class); + + public JarLicenseResolver() { + super(acquireBaseURL()); + } + + protected static String acquireBaseURL() { + String prefix = File.separator + "META-INF" + File.separator + "licenses"; + URL licenseURL = JarLicenseResolver.class.getResource(prefix); + return licenseURL.toString(); + } +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/JarLicenseResolver.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/License.java (from rev 1525, maven-nuiton-project-helper-plugin/trunk/src/main/java/org/codelutin/license/License.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/License.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/License.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,89 @@ +package org.nuiton.license; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.InputStream; +import java.net.URL; +import org.codehaus.plexus.util.IOUtil; + +/** + * The model of a license. + * + * @author chemit + */ +public class License { + + /** the name of the licenses (ex lgpl-3.0) */ + protected String name; + /** url of the license's content */ + protected URL licenseURL; + /** url of the header's template */ + protected URL headerURL; + + public License() { + } + + public String getName() { + return name; + } + + public URL getLicenseURL() { + return licenseURL; + } + + public URL getHeaderURL() { + return headerURL; + } + + public String getLicenseContent(String encoding) throws IOException { + if (licenseURL == null) { + throw new IllegalStateException("no licenseURL defined in " + this); + } + + InputStream inputStream = licenseURL.openStream(); + Reader r = new BufferedReader(new InputStreamReader(inputStream, encoding)); + try { + return IOUtil.toString(r); +// return FileUtil.readAsString(r); + } finally { + r.close(); + } + } + + public String getHeaderContent(String encoding) throws IOException { + if (headerURL == null) { + throw new IllegalStateException("no headerURL defined in " + this); + } + Reader r = new BufferedReader(new InputStreamReader(headerURL.openStream(), encoding)); + try { + return IOUtil.toString(r); +// return FileUtil.readAsString(r); + } finally { + r.close(); + } + } + + public void setName(String name) { + this.name = name; + } + + public void setLicenseURL(URL licenseURL) { + this.licenseURL = licenseURL; + } + + public void setHeaderURL(URL headerURL) { + this.headerURL = headerURL; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(super.toString()); + sb.append("<name:").append(name); + sb.append(", licenseURL:").append(licenseURL.toString()); + sb.append(", headerURL:").append(headerURL.toString()); + sb.append(">"); + return sb.toString(); + } +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/License.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseFactory.java (from rev 1524, maven-nuiton-project-helper-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseFactory.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseFactory.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,73 @@ +package org.nuiton.license; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +/** + * Class responsible of instanciate {@link License}. + * <p/> + * The factory use a table of {@link #resolvers} to lookup for licenses. + * <p/> + * To obtain an instance of a {@link org.codelutin.license.LicenseFactory} use the method {@link #newInstance(String[])}. + * + * @author chemit + */ +public class LicenseFactory { + + protected List<LicenseResolver> resolvers; + + public static LicenseFactory newInstance(String... extraResolvers) throws IllegalArgumentException { + LicenseFactory result = new LicenseFactory(); + // always put jar resolver first + result.addResolver(new JarLicenseResolver()); + + if (extraResolvers != null) { + for (String extraResolver : extraResolvers) { + if (extraResolver != null && !extraResolver.trim().isEmpty()) { + // add a extra resolver + result.addResolver(new LicenseResolver(extraResolver)); + } + } + } + return result; + } + + public void addResolver(LicenseResolver licenseResolver) { + resolvers.add(licenseResolver); + } + + public Map<String, String> getLicenseNames() throws IllegalArgumentException { + Map<String, String> result = new TreeMap<String, String>(); + for (LicenseResolver resolver : resolvers) { + Map<String, String> tmp = resolver.getLicenseNames(); + for (Entry<String, String> s : tmp.entrySet()) { + if (!result.containsKey(s.getKey())) { + result.put(s.getKey(), s.getValue()); + } + } + } + return result; + } + + + public License revolv(String licenseName) throws IllegalArgumentException { + if (licenseName == null) { + throw new IllegalArgumentException("licenseName can not be null"); + } + for (LicenseResolver resolver : resolvers) { + License result = resolver.resolv(licenseName); + if (result != null) { + return result; + } + } + throw new IllegalArgumentException("could not find the license " + licenseName + " with resolvers" + resolvers); + } + + protected LicenseFactory() throws IllegalArgumentException { + this.resolvers = new ArrayList<LicenseResolver>(); + } + +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseFactory.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseGenerator.java (from rev 1524, maven-nuiton-project-helper-plugin/trunk/src/main/java/org/codelutin/license/LicenseGenerator.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseGenerator.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseGenerator.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,12 @@ +package org.nuiton.license; + +/** + * A simple Contract to generate a new header for a given type of file + * + * @author chemit + */ +public interface LicenseGenerator { + + String getHeader(String licenseHeaderContent); + +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseGenerator.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseGeneratorFactory.java (from rev 1524, maven-nuiton-project-helper-plugin/trunk/src/main/java/org/codelutin/license/LicenseGeneratorFactory.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseGeneratorFactory.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseGeneratorFactory.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,73 @@ +package org.nuiton.license; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * A factory of {@link LicenseFactory} + * + * @author chemit + */ +public class LicenseGeneratorFactory { + + public static class LicenceGeneratorContext { + + File src; + String[] files; + + public LicenceGeneratorContext(File src, String[] files) { + this.src = src; + this.files = files; + } + + public File getSrc() { + return src; + } + + public String[] getFiles() { + return files; + } + + public void setSrc(File src) { + this.src = src; + } + + public void setFiles(String[] files) { + this.files = files; + } + + } + + protected static Map<LicenseGenerator, List<String>> availableGenerator; + + public static Map<LicenseGenerator, List<String>> getAvailableGenerator() { + if (availableGenerator == null) { + availableGenerator = new HashMap<LicenseGenerator, List<String>>(); + } + return availableGenerator; + } + + public static List<String> getGeneratorMatchers(LicenseGenerator generator) { + List<String> result = null; + if (availableGenerator != null) { + result = availableGenerator.get(generator); + if (result == null) { + // add a new empty list in cache + result = new ArrayList<String>(); + availableGenerator.put(generator, result); + } + } + return result; + } + + public static void addGenerator(LicenseGenerator generator, String matcher) { + List<String> matchers = getGeneratorMatchers(generator); + if (matcher.isEmpty() || !matcher.contains(matcher)) { + matchers.add(matcher); + } + } + +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseGeneratorFactory.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseResolver.java (from rev 1524, maven-nuiton-project-helper-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseResolver.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseResolver.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,136 @@ +package org.nuiton.license; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collections; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.TreeMap; + +/** + * Un resolver de license a partir d'un {@link #baseURL} . + * <p/> + * On doit avoir un fichier + * <pre>baseURL/licenses.txt + * </pre> qui contient la liste des licenses gerees par ce resolver. + * <p/> + * Chaque license est definit par deux fichiers : + * <pre> + * baseURL/licenseName/license.txt + * baseURL/licenseName/header.txt + * </pre> + * + * @author chemit + */ +public class LicenseResolver { + + protected static final Log log = LogFactory.getLog(LicenseResolver.class); + + /** + * BaseURL + * + * @required + */ + protected String baseURL; + + /** list of known licenses with this resolver */ + protected Map<String, String> licenseNames; + + public LicenseResolver() { + } + + public LicenseResolver(String baseURL) { + this.baseURL = baseURL; + } + + public String getBaseURL() { + return baseURL; + } + + public void setBaseURL(String baseURL) { + this.baseURL = baseURL; + // reset licenseNames + licenseNames = null; + } + + public Map<String, String> getLicenseNames() { + if (licenseNames == null) { + if (baseURL == null || baseURL.trim().isEmpty()) { + throw new IllegalArgumentException("no baseURL defined in " + this); + } + + Map<String, String> list = new TreeMap<String, String>(); + try { + URL licenseURL = new URL(baseURL + "/licenses.properties"); + + if (!checkExists(licenseURL)) { + throw new IllegalArgumentException("no licenses.properties found withurl [" + licenseURL + "] for resolver " + this); + } + Properties p = new Properties(); + p.load(licenseURL.openStream()); + for (Entry<Object, Object> entry : p.entrySet()) { + list.put((String) entry.getKey(), entry.getValue() + " [" + baseURL + "]"); + } + } catch (MalformedURLException e) { + throw new IllegalArgumentException(e); + } catch (IOException e) { + return null; + } + + licenseNames = Collections.unmodifiableMap(list); + } + return licenseNames; + } + + public License resolv(String licenseName) throws IllegalArgumentException { + if (baseURL == null || baseURL.trim().isEmpty()) { + throw new IllegalArgumentException("no baseURL defined in " + this); + } + if (licenseName == null || licenseName.trim().isEmpty()) { + throw new IllegalArgumentException("licenceName can not be null"); + } + licenseName = licenseName.trim().toLowerCase(); + + if (!getLicenseNames().containsKey(licenseName)) { + // license not found + return null; + } + + try { + URL licenseURL = new URL(baseURL + "/" + licenseName + "/license.txt"); + URL headerURL = new URL(baseURL + "/" + licenseName + "/header.txt"); + + if (!checkExists(licenseURL) || !checkExists(headerURL)) { + // no content find + return null; + } + + License license = new License(); + license.setName(licenseName); + license.setLicenseURL(licenseURL); + license.setHeaderURL(headerURL); + return license; + + + } catch (MalformedURLException e) { + throw new IllegalArgumentException(e); + } catch (IOException e) { + return null; + } + } + + @Override + public String toString() { + return super.toString() + "<baseURL:" + baseURL + ", licenseNames:" + licenseNames + ">"; + } + + protected boolean checkExists(URL url) throws IOException { + return url.openConnection().getContentLength() > 0; + } + +} \ No newline at end of file Property changes on: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/LicenseResolver.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/AbstractLicensePlugin.java (from rev 1525, maven-nuiton-project-helper-plugin/trunk/src/main/java/org/codelutin/license/plugin/AbstractLicensePlugin.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/AbstractLicensePlugin.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/AbstractLicensePlugin.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,242 @@ +/** + * *##% Plugin maven pour switcher les licenses + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.nuiton.license.plugin; + +import java.io.ByteArrayInputStream; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; + +import java.io.File; +import java.io.InputStream; +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.io.RawInputStreamFacade; + +/** + * Un MOJO de base pour les autres MOJO concrets avec les options communes. + * + * @author chemit + */ +public abstract class AbstractLicensePlugin extends AbstractMojo { + + protected abstract boolean shouldSkip(); + + /** + * la methode qui est lancee au debut de la methode {@link #execute()} pour preparer l'init du goal. + * + * @return <code>true</code> if there is something to generate, <code>false</code> otherwise. + * @throws Exception if any + */ + protected abstract boolean init() throws Exception; + + /** + * Do plugin action. + * <p/> + * The method {@link #execute()} invoke this method only and only if : + * <ul> + * <li>{@link #shouldSkip()} returns <code>false</code> (filtrer project type, for example).</li> + * <li>{@link #force} is activate or method {@link #init()} returns <code>true</code>.</li> + * </ul> + * + * @throws Exception if any + */ + protected abstract void doAction() throws Exception; + /** + * Dependance du projet. + * + * @parameter default-value="${project}" + * @required + */ + protected MavenProject project; + /** + * Repertoire de sortie des classes (classpath). + * + * @parameter expression="${licence-switcher.outputDirectory}" default-value="${project.build.outputDirectory}" + * @required + */ + protected File outputDirectory; + /** + * Encoding a utiliser pour lire et ecrire les fichiers. + * + * @parameter expression="${licence-switcher.encoding}" default-value="${project.build.sourceEncoding}" + * @required + */ + protected String encoding; + /** + * Un flag pour conserver un backup des fichiers modifies. + * + * @parameter expression="${license-switcher.keepBackup}" default-value="false" + */ + protected boolean keepBackup; + /** + * Un flag pour activer le mode verbeux. + * + * @parameter expression="${license-switcher.verbose}" default-value="${maven.verbose}" + */ + protected boolean verbose; + /** + * Un flag pour forcer la generation. + * + * @parameter expression="${license-switcher.force}" default-value="false" + */ + protected boolean force; + /** + * Fichier ou ecrire les licences des dependances. + * + * @parameter expression="${license-switcher.pomFile}" default-value="pom.xml" + * @required + */ + protected File pomFile; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + try { + + if (shouldSkip()) { + getLog().info("skip switch goal for packaging " + project.getPackaging()); + return; + } + + boolean shouldGenerate = init(); + + if (!(force || shouldGenerate)) { + getLog().info("all files are up-to-date."); + return; + } + + doAction(); + + } catch (Exception e) { + throw new MojoExecutionException("could not init goal " + getClass().getSimpleName() + " for reason : " + e.getMessage(), e); + } + } + + /** + * Test if a file exists and is newer than the {@link #pomFile} file. + * + * @param f the file to test + * @return <code>true</code> if file exists and is newer than the pom file, + * <code>false</code> otherwise. + */ + protected boolean isFileNewerThanPomFile(File f) { + return f.exists() && f.lastModified() > pomFile.lastModified(); + } + + public MavenProject getProject() { + return project; + } + + public File getOutputDirectory() { + return outputDirectory; + } + + public String getEncoding() { + return encoding; + } + + public boolean isKeepBackup() { + return keepBackup; + } + + public boolean isVerbose() { + return verbose; + } + + public boolean isForce() { + return force; + } + + public File getPomFile() { + return pomFile; + } + + public void setProject(MavenProject project) { + this.project = project; + } + + public void setOutputDirectory(File outputDirectory) { + this.outputDirectory = outputDirectory; + } + + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + public void setKeepBackup(boolean keepBackup) { + this.keepBackup = keepBackup; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + public void setForce(boolean force) { + this.force = force; + } + + public void setPomFile(File pomFile) { + this.pomFile = pomFile; + } + + /** + * Does the actual copy of the file and logging. + * + * @param srcFile represents the file to copy. + * @param destFile file name of destination file. + * + * @throws MojoExecutionException with a message if an + * error occurs. + */ + protected void copyFile(File srcFile, File destFile) + throws MojoExecutionException { + try { + getLog().info("Copying " + srcFile.getName() + " to " + destFile); + + FileUtils.copyFile(srcFile, destFile); + + } catch (Exception e) { + throw new MojoExecutionException("Error copying from " + srcFile + " to " + destFile, e); + } + } + + /** + * Does the actual copy of the content to a fileand logging. + * + * @param content represents the content to write into file. + * @param destFile file name of destination file. + * @param encoding encoding to use to write file + * @throws MojoExecutionException with a message if an + * error occurs. + */ + protected void copyFile(String content, File destFile, String encoding) + throws MojoExecutionException { + try { + getLog().info("Copying content to " + destFile); + + InputStream in = new ByteArrayInputStream(content.getBytes(encoding)); + RawInputStreamFacade facade = new RawInputStreamFacade(in); + + FileUtils.copyStreamToFile(facade, destFile); + + } catch (Exception e) { + throw new MojoExecutionException("Error copying content to " + destFile, e); + } + } +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/AbstractLicensePlugin.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/AvailableLicensesPlugin.java (from rev 1524, maven-nuiton-project-helper-plugin/trunk/src/main/java/org/codelutin/license/plugin/AvailableLicensesPlugin.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/AvailableLicensesPlugin.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/AvailableLicensesPlugin.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,57 @@ +/** + * *##% Plugin maven pour switcher les licenses + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.nuiton.license.plugin; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.nuiton.license.LicenseFactory; + +import java.util.Map.Entry; + +/** + * Le goal pour afficher dans la console les differentes licenses connues. + * + * @author chemit + * @goal available-licenses + * @requiresProject false + */ +public class AvailableLicensesPlugin extends AbstractMojo { + + /** + * La baseURL d'un resolver de license supplementaire + * + * @parameter expression="${license-switcher.extraResolver}" + */ + protected String extraResolver; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + StringBuilder sb = new StringBuilder(); + sb.append("Available licenses :\n"); + + LicenseFactory factory = LicenseFactory.newInstance(extraResolver); + + for (Entry<String, String> license : factory.getLicenseNames().entrySet()) { + sb.append(" * ").append(license.getKey()).append(" : ").append(license.getValue()).append('\n'); + } + getLog().info(sb.toString()); + } + +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/AvailableLicensesPlugin.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/LicensePlugin.java (from rev 1525, maven-nuiton-project-helper-plugin/trunk/src/main/java/org/codelutin/license/plugin/impl/LicensePlugin.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/LicensePlugin.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/LicensePlugin.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,160 @@ +/** + * *##% Plugin maven pour switcher les licenses + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.nuiton.license.plugin; + +import org.nuiton.license.License; +import org.nuiton.license.LicenseFactory; +import org.nuiton.license.plugin.AbstractLicensePlugin; + +import java.io.File; + +/** + * Le goal pour ajouter le fichier LICENSE.txt dans le classpath (et le generer s'il n'existe pas). + * + * @author chemit + * @goal add-license + * @phase generate-resources + * @requiresProject true + * @requiresDependencyResolution compile + */ +public class LicensePlugin extends AbstractLicensePlugin { + + /** + * Fichier de la licence du module. + * + * @parameter expression="${license-switcher.licenceFile}" default-value="${basedir}/LICENSE.txt" + * @required + * @readonly + */ + protected File licenseFile; + /** + * Le type de license a appliquer. + * + * @parameter expression="${license-switcher.licenseName}" + * @required + */ + protected String licenseName; + /** + * La baseURL d'un resolver de license supplementaire + * + * @parameter expression="${license-switcher.extraResolver}" + */ + protected String[] extraResolver; + protected License license; + + @Override + protected boolean shouldSkip() { + return project != null && ("pom".equals(project.getPackaging()) || "site".equals(project.getPackaging())); + } + + @Override + protected boolean init() throws Exception { + + // must generate if file does not exist + boolean doGenerate = false; + + if (!force) { + // regenerate only if file exists and is newer than pom file + doGenerate = !isFileNewerThanPomFile(licenseFile); + } + + if (doGenerate) { + + // acquire license + + LicenseFactory factory = LicenseFactory.newInstance(extraResolver); + + license = factory.revolv(licenseName); + + } + return doGenerate; + } + + @Override + protected void doAction() throws Exception { + getLog().info("using licence [" + licenseName + "]"); + + if (verbose) { + getLog().info("detail : " + license); + } + + if (licenseFile.exists() && keepBackup) { + if (verbose) { + getLog().info("backup " + licenseFile); + } + // copy it to backup file + File backup = new File(licenseFile.getAbsolutePath() + "~"); + licenseFile.renameTo(backup); + } +// try { + String licenseContent = license.getLicenseContent(encoding); + + //FileUtil.writeString(licenseFile, licenseContent, encoding); + + // copy LICENSE.txt to classpath + File target = new File(outputDirectory, licenseFile.getName()); + +// try { + + copyFile(licenseContent, licenseFile, encoding); + + copyFile(licenseFile, target); + + +// } catch (IOException e) { +// throw new MojoExecutionException("could not write file " + target + " for reason : " + e.getMessage(), e); +// } +// +// } catch (IOException e) { +// throw new MojoExecutionException("could not write license file " + licenseFile + " for reason : " + e.getMessage(), e); +// } + } + + public File getLicenseFile() { + return licenseFile; + } + + public String getLicenseName() { + return licenseName; + } + + public License getLicense() { + return license; + } + + public String[] getExtraResolver() { + return extraResolver; + } + + public void setLicenseFile(File licenseFile) { + this.licenseFile = licenseFile; + } + + public void setLicenseName(String licenseName) { + this.licenseName = licenseName; + } + + public void setExtraResolver(String[] extraResolver) { + this.extraResolver = extraResolver; + } + + public void setLicense(License license) { + this.license = license; + } +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/LicensePlugin.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/ThirdPartyPlugin.java (from rev 1525, maven-nuiton-project-helper-plugin/trunk/src/main/java/org/codelutin/license/plugin/impl/ThirdPartyPlugin.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/ThirdPartyPlugin.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/ThirdPartyPlugin.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,311 @@ +/** + * *##% Plugin maven pour switcher les licenses + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.nuiton.license.plugin; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactCollector; +import org.apache.maven.artifact.resolver.filter.ArtifactFilter; +import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; +import org.apache.maven.model.License; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuildingException; +import org.apache.maven.shared.dependency.tree.DependencyNode; +import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder; +import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException; +import org.nuiton.license.plugin.AbstractLicensePlugin; + +import java.io.File; +import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * Le goal pour copier le fichier THIRD-PARTY.txt (contenant les licenses de toutes les dependances du projet) + * dans le classpath (et le generer s'il n'existe pas). + * + * @author chemit + * @goal add-third-party + * @phase process-classes + * @requiresDependencyResolution test + * @requiresProject true + */ +public class ThirdPartyPlugin extends AbstractLicensePlugin { + + private static final String unknownLicenseMessage = "Unknown license"; + /** + * Local Repository. + * + * @parameter expression="${localRepository}" + * @required + * @readonly + */ + protected ArtifactRepository localRepository; + /** + * Remote repositories used for the project. + * + * @parameter expression="${project.remoteArtifactRepositories}" + * @required + * @readonly + */ + protected List<?> remoteRepositories; + /** + * Fichier ou ecrire les licences des dependances. + * + * @parameter expression="${license-switcher.thirdPartyFile}" default-value="${project.build.outputDirectory}/THIRD-PARTY.txt" + * @required + */ + protected File thirdPartyFile; + /** + * Dependency tree builder component. + * + * @component + */ + protected DependencyTreeBuilder dependencyTreeBuilder; + /** + * Artifact Factory component. + * + * @component + */ + protected ArtifactFactory factory; + /** + * Artifact metadata source component. + * + * @component + */ + protected ArtifactMetadataSource artifactMetadataSource; + /** + * Artifact collector component. + * + * @component + */ + protected ArtifactCollector collector; + /** + * Maven Project Builder component. + * + * @component + */ + protected MavenProjectBuilder mavenProjectBuilder; + /** + * content of third party file (only computed if {@link #force} is active or the + * {@link #thirdPartyFile} does not exist, or is not up-to-date. + */ + protected String thirdPartyFileContent; + + @Override + protected boolean shouldSkip() { + return "pom".equals(project.getPackaging()) || "site".equals(project.getPackaging()); + } + + @Override + protected boolean init() throws Exception { + + boolean doGenerate = false; + + if (!force) { + // regenerate only if file exists and is newer than pom file + doGenerate = !isFileNewerThanPomFile(thirdPartyFile); + } + + if (doGenerate) { + + // prepare thirdPartyFileContent + + DependencyNode dependencyTreeNode = resolveProject(); + + LicenseMap licenseMap = new LicenseMap(); + + for (Object o : dependencyTreeNode.getChildren()) { + + buildLicenseMap((DependencyNode) o, licenseMap); + } + + thirdPartyFileContent = buildGroupedLicenses(licenseMap); + + // log dependencies with no license + SortedSet<String> dependenciesWithNoLicense = licenseMap.get(unknownLicenseMessage); + if (dependenciesWithNoLicense != null) { + for (String dep : dependenciesWithNoLicense) { + // no license found for the dependency + getLog().warn("no license found for dependency " + dep); + } + } + } + + return doGenerate; + } + + @Override + protected void doAction() throws Exception { + if (verbose) { + getLog().info("writing third-party file : " + thirdPartyFile); + } + if (keepBackup && thirdPartyFile.exists()) { + if (verbose) { + getLog().info("backup " + thirdPartyFile); + } + thirdPartyFile.renameTo(new File(thirdPartyFile.getAbsolutePath() + '~')); + } + copyFile(thirdPartyFileContent, thirdPartyFile, encoding); +// try { +// FileUtil.writeString(thirdPartyFile, thirdPartyFileContent, encoding); +// } catch (IOException e) { +// throw new MojoExecutionException("could not write file " + thirdPartyFile + " for reason : " + e.getMessage(), e); +// } + } + + /** @return resolve the dependency tree */ + protected DependencyNode resolveProject() { + try { + ArtifactFilter artifactFilter = new ScopeArtifactFilter(Artifact.SCOPE_TEST); + return dependencyTreeBuilder.buildDependencyTree(project, localRepository, factory, + artifactMetadataSource, artifactFilter, collector); + } catch (DependencyTreeBuilderException e) { + getLog().error("Unable to build dependency tree.", e); + return null; + } + } + + protected void buildLicenseMap(DependencyNode node, LicenseMap licenseMap) { + if (node.getState() != DependencyNode.INCLUDED) { + // this dependency is not included, so do not treate it + if (verbose) { + getLog().info("do not include this dependency " + node.toNodeString()); + } + return; + } + Artifact artifact = node.getArtifact(); + + if (verbose && getLog().isDebugEnabled()) { + getLog().debug("treate node " + node.toNodeString()); + } + + if (!Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) { + try { + MavenProject artifactProject = getMavenProjectFromRepository(artifact); + String artifactName = getArtifactName(artifactProject); + + List<?> licenses = artifactProject.getLicenses(); + + if (licenses.isEmpty()) { + // no license found for the dependency + licenseMap.put(unknownLicenseMessage, artifactName); + + } else { + for (Object o : licenses) { + if (o == null) { + getLog().warn("could not acquire the license for " + artifactName); + continue; + } + License license = (License) o; + String licenseKey = license.getName(); + if (license.getName() == null) { + licenseKey = license.getUrl(); + } + licenseMap.put(licenseKey, artifactName); + } + } + } catch (ProjectBuildingException e) { + getLog().error("ProjectBuildingException error : ", e); + } + } + if (!node.getChildren().isEmpty()) { + for (Object o : node.getChildren()) { + buildLicenseMap((DependencyNode) o, licenseMap); + } + } + } + + protected String buildGroupedLicenses(LicenseMap licenseMap) { + StringBuilder sb = new StringBuilder(); + sb.append("List of third-party dependencies grouped by their license type."); + for (String licenseName : licenseMap.keySet()) { + sb.append("\n\n").append(licenseName).append(" : "); + + SortedSet<String> projects = licenseMap.get(licenseName); + + for (String projectName : projects) { + sb.append("\n * ").append(projectName); + } + } + return sb.toString(); + } + + protected String getArtifactName(MavenProject artifactProject) { + StringBuilder sb = new StringBuilder(); + + sb.append(artifactProject.getName()); + sb.append(" ("); + sb.append(artifactProject.getGroupId()); + sb.append(":"); + sb.append(artifactProject.getArtifactId()); + sb.append(":"); + sb.append(artifactProject.getVersion()); + sb.append(" - "); + String url = artifactProject.getUrl(); + sb.append(url == null ? "no url defined" : url); + sb.append(")"); + + return sb.toString(); + } + + /** + * Get the <code>Maven project</code> from the repository depending the <code>Artifact</code> given. + * + * @param artifact an artifact + * @return the Maven project for the given artifact + * @throws ProjectBuildingException if any + */ + protected MavenProject getMavenProjectFromRepository(Artifact artifact) + throws ProjectBuildingException { + + boolean allowStubModel = false; + + if (!"pom".equals(artifact.getType())) { + artifact = factory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), + artifact.getVersion(), artifact.getScope()); + allowStubModel = true; + } + + // TODO: we should use the MavenMetadataSource instead + return mavenProjectBuilder.buildFromRepository(artifact, remoteRepositories, localRepository, + allowStubModel); + } + + protected class LicenseMap extends java.util.TreeMap<String, SortedSet<String>> { + + private static final long serialVersionUID = 864199843545688069L; + + public SortedSet<String> put(String key, String value) { + // handle multiple values as a set to avoid duplicates + SortedSet<String> valueList = get(key); + if (valueList == null) { + valueList = new TreeSet<String>(); + } + if (getLog().isDebugEnabled()) { + getLog().debug("key:" + key + ",value: " + value); + } + valueList.add(value); + return put(key, valueList); + } + } +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/main/java/org/nuiton/license/plugin/ThirdPartyPlugin.java ___________________________________________________________________ Name: svn:mergeinfo + Modified: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/codelutin/license/plugin/impl/LicensePluginTest.java =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/test/java/org/codelutin/license/plugin/impl/LicensePluginTest.java 2009-05-11 21:25:01 UTC (rev 1527) +++ maven-nuiton-project-helper-plugin/trunk/src/test/java/org/codelutin/license/plugin/impl/LicensePluginTest.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -1,5 +1,6 @@ package org.codelutin.license.plugin.impl; +import org.nuiton.license.plugin.LicensePlugin; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.codelutin.license.plugin.BasePluginTestCase; Copied: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/BaseLicenseTestCase.java (from rev 1525, maven-nuiton-project-helper-plugin/trunk/src/test/java/org/codelutin/license/BaseLicenseTestCase.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/BaseLicenseTestCase.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/BaseLicenseTestCase.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,80 @@ +package org.nuiton.license; + +import org.nuiton.license.License; +import java.io.ByteArrayInputStream; +import org.junit.Assert; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.io.RawInputStreamFacade; + +/** @author chemit */ +public abstract class BaseLicenseTestCase extends Assert { + + protected static final String encoding = "utf-8"; + + protected File getBaseDir() { + String path = System.getenv("basedir"); + if (path == null) { + path = new File("").getAbsolutePath(); + } + return new File(path); + } + + protected void assertLicenseFound(String name, License license) throws IOException { + assertNotNull(license); + assertEquals(name, license.getName()); + assertNotNull(license.getLicenseContent(encoding)); + assertNotNull(license.getHeaderContent(encoding)); + } + + protected File createLicenseRepository(boolean createLicene) throws IOException { + long timestamp = System.currentTimeMillis(); + + File repo = new File(getBaseDir(), "target" + File.separator + "licenses_" + timestamp); + + if (createLicene) { + + String licenseName = "dummy_" + timestamp; + addLicenseToRepository(repo, licenseName); + } + return repo; + } + + protected void addLicenseToRepository(File repo, String licenseName) throws IOException { + + File defFile = new File(repo, "licenses.properties"); + + String content = ""; + if (defFile.exists()) { + + content = IOUtil.toString(new FileInputStream(defFile), encoding) + "\n"; +// content = FileUtil.readAsString(defFile, encoding) + "\n"; + } + + + write(defFile, content + licenseName + "=My dummy license\n", encoding); +// FileUtil.writeString(defFile, content + licenseName + "=My dummy license\n", encoding); + // create dummy licenses + File file = new File(repo, licenseName); + file.mkdirs(); + write(new File(file, "license.txt"), "license:" + licenseName, encoding); + write(new File(file, "header.txt"), "header:" + licenseName, encoding); +// FileUtil.writeString(new File(file, "license.txt"), "license:" + licenseName, encoding); +// FileUtil.writeString(new File(file, "header.txt"), "header:" + licenseName, encoding); + + } + + protected void write(File destFile, String content, String encoding) throws IOException { + + InputStream in = new ByteArrayInputStream(content.getBytes(encoding)); + RawInputStreamFacade facade = new RawInputStreamFacade(in); + + FileUtils.copyStreamToFile(facade, destFile); + } +} + Property changes on: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/BaseLicenseTestCase.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/JarLicenseResolverTest.java (from rev 1524, maven-nuiton-project-helper-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/JarLicenseResolverTest.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/JarLicenseResolverTest.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,64 @@ +package org.nuiton.license; + +import org.nuiton.license.License; +import org.nuiton.license.JarLicenseResolver; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Map; + +/** @author chemit */ +public class JarLicenseResolverTest extends BaseLicenseTestCase { + + @Test(expected = IllegalArgumentException.class) + public void testResolvFailed1() throws Exception { + new JarLicenseResolver().resolv(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolvFailed2() throws Exception { + new JarLicenseResolver().resolv(""); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolvFailed3() throws Exception { + new JarLicenseResolver().resolv(" "); + } + + @Test + public void testResolv() throws Exception { + JarLicenseResolver resolver = new JarLicenseResolver(); + + { + String name = "dummy_" + System.currentTimeMillis(); + License license = resolver.resolv(name); + assertNull(license); + } + + for (String name : new String[]{ + "gpl_v1", + "gpl_v2", + "gpl_v3", + "lgpl_v2_1", + "lgpl_v3"}) { + License license = resolver.resolv(name); + assertLicenseFound(name, license); + } + + } + + @Test + public void testGetLicenseNames() throws Exception { + JarLicenseResolver resolver = new JarLicenseResolver(); + Map<String, String> licenseNames = resolver.getLicenseNames(); + + assertEquals(Arrays.asList("gpl_v1", + "gpl_v2", + "gpl_v3", + "lgpl_v2_1", + "lgpl_v3"), new ArrayList<String>(licenseNames.keySet())); + } + + +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/JarLicenseResolverTest.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseFactoryTest.java (from rev 1524, maven-nuiton-project-helper-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseFactoryTest.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseFactoryTest.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,83 @@ +package org.nuiton.license; + +import org.nuiton.license.LicenseFactory; +import org.nuiton.license.LicenseResolver; +import org.nuiton.license.JarLicenseResolver; +import org.junit.Test; + +import java.io.File; +import java.util.Map; + +/** @author chemit */ +public class LicenseFactoryTest extends BaseLicenseTestCase { + + @Test + public void testNewInstance() throws Exception { + + LicenseFactory factory = LicenseFactory.newInstance(); + assertNotNull(factory); + assertEquals(1, factory.resolvers.size()); + assertNotNull(factory.resolvers.get(0)); + assertEquals(JarLicenseResolver.class, factory.resolvers.get(0).getClass()); + + factory = LicenseFactory.newInstance(""); + assertNotNull(factory); + assertEquals(1, factory.resolvers.size()); + assertNotNull(factory.resolvers.get(0)); + assertEquals(JarLicenseResolver.class, factory.resolvers.get(0).getClass()); + + factory = LicenseFactory.newInstance("yo"); + assertNotNull(factory); + + assertEquals(2, factory.resolvers.size()); + assertNotNull(factory.resolvers.get(0)); + assertEquals(JarLicenseResolver.class, factory.resolvers.get(0).getClass()); + assertNotNull(factory.resolvers.get(1)); + assertEquals(LicenseResolver.class, factory.resolvers.get(1).getClass()); + assertEquals("yo", factory.resolvers.get(1).getBaseURL()); + + factory.addResolver(new LicenseResolver("yo2")); + assertEquals(3, factory.resolvers.size()); + assertNotNull(factory.resolvers.get(0)); + assertEquals(JarLicenseResolver.class, factory.resolvers.get(0).getClass()); + assertNotNull(factory.resolvers.get(1)); + assertEquals(LicenseResolver.class, factory.resolvers.get(1).getClass()); + assertEquals("yo", factory.resolvers.get(1).getBaseURL()); + assertEquals(LicenseResolver.class, factory.resolvers.get(2).getClass()); + assertEquals("yo2", factory.resolvers.get(2).getBaseURL()); + + } + + @Test + public void testGetLicenseNames() throws Exception { + LicenseFactory factory = LicenseFactory.newInstance(); + + int expectedJarResolverLicenses = 5; + Map<String, String> licenseNames = factory.getLicenseNames(); + //todo make dynamic that test + assertEquals(expectedJarResolverLicenses, licenseNames.size()); + + // create a new dummy license repository with a license + File repo = createLicenseRepository(true); + + LicenseResolver resolver = new LicenseResolver(); + resolver.setBaseURL(repo.toURI().toURL().toString()); + + factory.addResolver(resolver); + + licenseNames = factory.getLicenseNames(); + assertEquals(expectedJarResolverLicenses + 1, licenseNames.size()); + + // add a new license to repo + long timestamp = System.currentTimeMillis(); + String licenseName = "dummy2_" + timestamp; + addLicenseToRepository(repo, licenseName); + + factory = LicenseFactory.newInstance(repo.toURI().toURL().toString()); + + licenseNames = factory.getLicenseNames(); + assertEquals(expectedJarResolverLicenses + 2, licenseNames.size()); + + } + +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseFactoryTest.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseGeneratorFactoryTest.java (from rev 1524, maven-nuiton-project-helper-plugin/trunk/src/test/java/org/codelutin/license/LicenseGeneratorFactoryTest.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseGeneratorFactoryTest.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseGeneratorFactoryTest.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,34 @@ +package org.nuiton.license; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** @author chemit */ +public class LicenseGeneratorFactoryTest { + + @Before + public void setUp() { + // Add your code here + } + + @After + public void tearDown() { + // Add your code here + } + + @Test + public void testGetAvailableGenerator() { + // Add your code here + } + + @Test + public void testGetGeneratorMatchers() { + // Add your code here + } + + @Test + public void testAddGenerator() { + // Add your code here + } +} Property changes on: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseGeneratorFactoryTest.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseResolverTest.java (from rev 1524, maven-nuiton-project-helper-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java) =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseResolverTest.java (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/test/java/org/nuiton/license/LicenseResolverTest.java 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,115 @@ +package org.nuiton.license; + +import org.nuiton.license.License; +import org.nuiton.license.LicenseResolver; +import org.junit.Test; + +import java.io.File; +import java.util.Map; + +/** @author chemit */ +public class LicenseResolverTest extends BaseLicenseTestCase { + + @Test(expected = IllegalArgumentException.class) + public void testResolvIllegalArgumentException1() throws Exception { + + LicenseResolver resolver = new LicenseResolver(); + // no baseURL + // no licenseName + // no licenses.txt + resolver.resolv(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolvIllegalArgumentException2() throws Exception { + + LicenseResolver resolver = new LicenseResolver(); + + // no baseURL + // no licenseName + // no licenses.txt + resolver.resolv(""); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolvIllegalArgumentException3() throws Exception { + + LicenseResolver resolver = new LicenseResolver(); + + // no baseURL + // no licenseName + // no licenses.txt + resolver.resolv(" "); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolvIllegalArgumentException4() throws Exception { + + LicenseResolver resolver = new LicenseResolver(); + + // no baseURL + // no licenses.txt + resolver.resolv("dummy"); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolvIllegalArgumentException5() throws Exception { + + LicenseResolver resolver = new LicenseResolver(); + + + resolver.setBaseURL("dummy"); + // no licenses.txt + resolver.resolv("dummy"); + } + + @Test + public void testResolv() throws Exception { + + LicenseResolver resolver = new LicenseResolver(); + + long timestamp = System.currentTimeMillis(); + String licenseName = "dummy_" + timestamp; + + File repo = createLicenseRepository(false); + + resolver.setBaseURL(repo.toURI().toURL().toString()); + + License license; + + try { + // no licenses.properties + resolver.resolv(licenseName); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(true); + } + + addLicenseToRepository(repo, licenseName); + + //FileUtil.writeString(new File(repo, "licenses.properties"), licenseName + "=My dummy license\n", encoding); + //license = resolver.resolv(licenseName); + //assertNull(license); + + Map<String, String> licenseNames = resolver.getLicenseNames(); + assertNotNull(licenseNames); + assertEquals(1, licenseNames.size()); + assertTrue(licenseNames.containsKey(licenseName)); + + // create dummy licenses + //File file = new File(repo, licenseName); + //file.mkdirs(); + //FileUtil.writeString(new File(file, "license.txt"), "license", encoding); + //FileUtil.writeString(new File(file, "header.txt"), "header", encoding); + + license = resolver.resolv(licenseName); + assertNotNull(license); + assertEquals(licenseName, license.getName()); + assertNotNull(license.getLicenseContent(encoding)); + assertNotNull(license.getHeaderContent(encoding)); + assertEquals("license:" + licenseName, license.getLicenseContent(encoding)); + assertEquals("header:" + licenseName, license.getHeaderContent(encoding)); + + } + +} \ No newline at end of file Added: maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/plugin/LicensePluginTest/licenseOne.txt =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/plugin/LicensePluginTest/licenseOne.txt (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/plugin/LicensePluginTest/licenseOne.txt 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1 @@ +dummy license \ No newline at end of file Added: maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/plugin/LicensePluginTest/testOne.xml =================================================================== --- maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/plugin/LicensePluginTest/testOne.xml (rev 0) +++ maven-nuiton-project-helper-plugin/trunk/src/test/resources/org/nuiton/license/plugin/LicensePluginTest/testOne.xml 2009-05-11 21:25:22 UTC (rev 1528) @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<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"> + + <groupId>org.nuiton</groupId> + <artifactId>maven-nuiton-project-helper-plugin-testOne</artifactId> + <version>0</version> + + <build> + + <plugins> + <plugin> + <groupId>org.nuiton</groupId> + <artifactId>maven-nuiton-project-helper-plugin</artifactId> + <configuration> + <encoding>UTF-8</encoding> + <licenseName>lgpl_v3</licenseName> + <licenseFile>licenseOne.txt</licenseFile> + <pomFile>testOne.xml</pomFile> + <outputDirectory>target/tests/LicenPluginTest/test-java</outputDirectory> + <verbose>true</verbose> + </configuration> + <executions> + <execution> + <goals> + <goal>add-license</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file