Author: tchemit Date: 2010-04-06 15:03:55 +0200 (Tue, 06 Apr 2010) New Revision: 1706 Log: introduce LicenseStore, deprecates LicenseRepository, + other stuff to be continued... Added: trunk/src/it/ trunk/src/it/add-license-file/ trunk/src/it/add-third-party-file/ trunk/src/it/update-header-file/ Modified: trunk/src/main/java/org/nuiton/license/plugin/AddLicenseFileMojo.java trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyFileMojo.java trunk/src/main/java/org/nuiton/license/plugin/GeneratorListMojo.java trunk/src/main/java/org/nuiton/license/plugin/LicenseListMojo.java trunk/src/main/java/org/nuiton/license/plugin/UpdateHeaderMojo.java trunk/src/main/java/org/nuiton/license/plugin/header/generator/AptLicenseHeaderGeneratorImpl.java trunk/src/main/java/org/nuiton/license/plugin/header/generator/JavaLicenseHeaderGeneratorImpl.java trunk/src/main/java/org/nuiton/license/plugin/header/generator/PropertiesLicenseHeaderGeneratorImpl.java trunk/src/main/java/org/nuiton/license/plugin/header/generator/XmlLicenseHeaderGeneratorImpl.java trunk/src/main/java/org/nuiton/license/plugin/repository/License.java trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseDefinition.java trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseRepository.java trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseRepositoryFactory.java trunk/src/main/resources/META-INF/licenses/licenses.properties trunk/src/site/rst/index.rst trunk/src/site/rst/usage.rst trunk/src/test/java/org/nuiton/license/plugin/repository/LicenseRepositoryFactoryTest.java Modified: trunk/src/main/java/org/nuiton/license/plugin/AddLicenseFileMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/AddLicenseFileMojo.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/AddLicenseFileMojo.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -20,12 +20,8 @@ */ package org.nuiton.license.plugin; - -import org.apache.maven.project.MavenProject; import org.nuiton.license.plugin.repository.License; -import org.nuiton.license.plugin.repository.LicenseRepository; -import org.nuiton.license.plugin.repository.LicenseRepositoryFactory; -import org.nuiton.plugin.AbstractPlugin; +import org.nuiton.license.plugin.repository.LicenseStore; import org.nuiton.plugin.PluginHelper; import java.io.File; @@ -40,18 +36,9 @@ * @requiresProject true * @requiresDependencyResolution compile */ -public class AddLicenseFileMojo extends AbstractPlugin { +public class AddLicenseFileMojo extends AbstractLicenseMojo { /** - * Dependance du projet. - * - * @parameter default-value="${project}" - * @required - * @since 1.0.0 - */ - protected MavenProject project; - - /** * Fichier de la licence du module. * * @parameter expression="${license.licenceFile}" default-value="${basedir}/LICENSE.txt" @@ -94,15 +81,6 @@ protected String licenseFilename; /** - * Encoding a utiliser pour lire et ecrire les fichiers. - * - * @parameter expression="${helper.encoding}" default-value="${project.build.sourceEncoding}" - * @required - * @since 1.0.0 - */ - protected String encoding; - - /** * Un flag pour conserver un backup des fichiers modifies. * * @parameter expression="${license.keepBackup}" default-value="false" @@ -119,14 +97,6 @@ protected boolean force; /** - * Un flag pour activer le mode verbeux. - * - * @parameter expression="${license.verbose}" default-value="${maven.verbose}" - * @since 1.0.0 - */ - protected boolean verbose; - - /** * Un flag pour faire une copie nommée dans META-INF (prefixe avec le nom de * l'artifact). * <p/> @@ -167,14 +137,17 @@ // acquire license - LicenseRepository factory = - LicenseRepositoryFactory.newLicenseRepository( - true, - true, - extraResolver - ); + LicenseStore licenseStore = createLicenseStore(extraResolver); + license = licenseStore.getLicense(licenseName); - license = factory.getLicense(licenseName); +// LicenseRepository factory = +// LicenseRepositoryFactory.newLicenseRepository( +// true, +// true, +// extraResolver +// ); +// +// license = factory.getLicense(licenseName); if (licenseFilename == null || licenseFilename.isEmpty()) { licenseFilename = licenseName; @@ -186,12 +159,12 @@ getLog().info("using licence [" + licenseName + "]"); if (doGenerate) { - if (verbose) { + if (isVerbose()) { getLog().info("detail : " + license); } if (licenseFile.exists() && keepBackup) { - if (verbose) { + if (isVerbose()) { getLog().info("backup " + licenseFile); } // copy it to backup file @@ -199,10 +172,10 @@ } } - String licenseContent = license.getLicenseContent(encoding); + String licenseContent = license.getLicenseContent(getEncoding()); if (doGenerate) { - writeFile(licenseFile, licenseContent, encoding); + writeFile(licenseFile, licenseContent, getEncoding()); } if (hasClassPath()) { @@ -214,8 +187,8 @@ if (copyToMETA_INF) { File destFile = PluginHelper.getFile( outputDirectory, - "META-INF", project.getArtifactId() + "-" + - licenseFile.getName() + "META-INF", getProject().getArtifactId() + "-" + + licenseFile.getName() ); copyFile(licenseFile, destFile); } @@ -239,10 +212,6 @@ return extraResolver; } - public String getEncoding() { - return encoding; - } - public boolean isKeepBackup() { return keepBackup; } @@ -271,10 +240,6 @@ this.license = license; } - public void setEncoding(String encoding) { - this.encoding = encoding; - } - public void setKeepBackup(boolean keepBackup) { this.keepBackup = keepBackup; } @@ -286,24 +251,4 @@ public void setForce(boolean force) { this.force = force; } - - @Override - public boolean isVerbose() { - return verbose; - } - - @Override - public void setVerbose(boolean verbose) { - this.verbose = verbose; - } - - @Override - public MavenProject getProject() { - return project; - } - - @Override - public void setProject(MavenProject project) { - this.project = project; - } } Modified: trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyFileMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyFileMojo.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyFileMojo.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -34,7 +34,6 @@ 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.plugin.AbstractPlugin; import org.nuiton.plugin.PluginHelper; import java.io.File; @@ -54,21 +53,11 @@ * @requiresDependencyResolution test * @requiresProject true */ -public class AddThirdPartyFileMojo extends AbstractPlugin { +public class AddThirdPartyFileMojo extends AbstractLicenseMojo { private static final String unknownLicenseMessage = "Unknown license"; /** - * Dependance du projet. - * - * @parameter default-value="${project}" - * @required - * @readonly - * @since 1.0.0 - */ - protected MavenProject project; - - /** * Fichier ou ecrire les licences des dependances. * * @parameter expression="${license.thirdPartyFilename}" @@ -81,23 +70,13 @@ /** * Repertoire de sortie des classes (classpath). * - * @parameter expression="${license.outputDirectory}" - * default-value="target/generated-sources/license" + * @parameter expression="${license.outputDirectory}" default-value="target/generated-sources/license" * @required * @since 1.0.0 */ protected File outputDirectory; /** - * Encoding a utiliser pour lire et ecrire les fichiers. - * - * @parameter expression="${license.encoding}" default-value="${project.build.sourceEncoding}" - * @required - * @since 1.0.0 - */ - protected String encoding; - - /** * Un flag pour forcer la generation. * * @parameter expression="${license.force}" default-value="false" @@ -123,14 +102,6 @@ protected boolean copyToMETA_INF; /** - * Un flag pour activer le mode verbeux. - * - * @parameter expression="${license.verbose}" default-value="${maven.verbose}" - * @since 1.0.0 - */ - protected boolean verbose; - - /** * Local Repository. * * @parameter expression="${localRepository}" @@ -236,29 +207,29 @@ } } } else { - thirdPartyFileContent = PluginHelper.readAsString(thirdPartyFile, encoding); + thirdPartyFileContent = PluginHelper.readAsString(thirdPartyFile, getEncoding()); } } @Override protected void doAction() throws Exception { if (doGenerate) { - if (verbose) { + if (isVerbose()) { getLog().info("writing third-party file : " + thirdPartyFile); } if (keepBackup && thirdPartyFile.exists()) { - if (verbose) { + if (isVerbose()) { getLog().info("backup " + thirdPartyFile); } backupFile(thirdPartyFile); } - writeFile(thirdPartyFile, thirdPartyFileContent, encoding); + writeFile(thirdPartyFile, thirdPartyFileContent, getEncoding()); } if (copyToMETA_INF) { copyFile(thirdPartyFile, new File( outputDirectory, - "META-INF" + File.separator + project.getArtifactId() + - "-" + thirdPartyFile.getName()) + "META-INF" + File.separator + getProject().getArtifactId() + + "-" + thirdPartyFile.getName()) ); } addResourceDir(outputDirectory, "**/*.txt"); @@ -270,7 +241,7 @@ ArtifactFilter artifactFilter = new ScopeArtifactFilter(Artifact.SCOPE_TEST); return dependencyTreeBuilder.buildDependencyTree( - project, + getProject(), localRepository, factory, artifactMetadataSource, @@ -286,15 +257,15 @@ protected void buildLicenseMap(DependencyNode node, LicenseMap licenseMap) { if (node.getState() != DependencyNode.INCLUDED) { // this dependency is not included, so do not treate it - if (verbose) { + if (isVerbose()) { getLog().info("do not include this dependency " + - node.toNodeString()); + node.toNodeString()); } return; } Artifact artifact = node.getArtifact(); - if (verbose && getLog().isDebugEnabled()) { + if (isVerbose() && getLog().isDebugEnabled()) { getLog().debug("treate node " + node.toNodeString()); } @@ -314,7 +285,7 @@ for (Object o : licenses) { if (o == null) { getLog().warn("could not acquire the license for " - + artifactName); + + artifactName); continue; } License license = (License) o; @@ -339,7 +310,7 @@ protected String buildGroupedLicenses(LicenseMap licenseMap) { StringBuilder sb = new StringBuilder(); sb.append("List of third-party dependencies grouped by " + - "their license type."); + "their license type."); for (String licenseName : licenseMap.keySet()) { sb.append("\n\n").append(licenseName).append(" : "); @@ -419,24 +390,4 @@ return put(key, valueList); } } - - @Override - public boolean isVerbose() { - return verbose; - } - - @Override - public void setVerbose(boolean verbose) { - this.verbose = verbose; - } - - @Override - public MavenProject getProject() { - return project; - } - - @Override - public void setProject(MavenProject project) { - this.project = project; - } } Modified: trunk/src/main/java/org/nuiton/license/plugin/GeneratorListMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/GeneratorListMojo.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/GeneratorListMojo.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -20,7 +20,6 @@ */ 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.plugin.header.generator.HeaderGenerator; @@ -37,7 +36,7 @@ * @goal generator-list * @since 1.0.1 */ -public class GeneratorListMojo extends AbstractMojo { +public class GeneratorListMojo extends AbstractLicenseMojo { /** * Un drapeau pour afficher aussi le contenu des license. @@ -55,8 +54,13 @@ protected Map<String, HeaderGenerator> _generators; @Override - public void execute() throws MojoExecutionException, MojoFailureException { + protected void init() throws Exception { + // nothing to do + } + @Override + public void doAction() throws MojoExecutionException, MojoFailureException { + // display it StringBuilder buffer = new StringBuilder("\n"); buffer.append("\n\n-------------------------------------------------------------------------------\n"); Modified: trunk/src/main/java/org/nuiton/license/plugin/LicenseListMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/LicenseListMojo.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/LicenseListMojo.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -20,13 +20,10 @@ */ 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.plugin.repository.License; -import org.nuiton.license.plugin.repository.LicenseDefinition; -import org.nuiton.license.plugin.repository.LicenseRepository; -import org.nuiton.license.plugin.repository.LicenseRepositoryFactory; +import org.nuiton.license.plugin.repository.LicenseStore; import java.io.IOException; @@ -39,7 +36,7 @@ * @requiresDirectInvocation * @since 1.0.1 */ -public class LicenseListMojo extends AbstractMojo { +public class LicenseListMojo extends AbstractLicenseMojo { /** * La baseURL d'un resolver de license supplementaire @@ -50,15 +47,6 @@ protected String extraResolver; /** - * Encoding a utiliser pour lire et ecrire les fichiers. - * - * @parameter expression="${encoding}" default-value="UTF-8" - * @required - * @since 1.0.0 - */ - protected String encoding; - - /** * Un drapeau pour afficher aussi le contenu des license. * * @parameter expression="${detail}" @@ -66,50 +54,80 @@ */ protected boolean detail; + /** store of licenses */ + protected LicenseStore licenseStore; + @Override - public void execute() throws MojoExecutionException, MojoFailureException { + protected void init() throws Exception { + // obtain licenses store + licenseStore = createLicenseStore(extraResolver); + } + + @Override + public void doAction() throws MojoExecutionException, MojoFailureException { StringBuilder buffer = new StringBuilder(); buffer.append("\n\n-------------------------------------------------------------------------------\n"); buffer.append(" maven-license-plugin\n"); buffer.append("-------------------------------------------------------------------------------\n\n"); buffer.append("Available licenses :\n\n"); - LicenseRepository factory; - try { - factory = LicenseRepositoryFactory.newLicenseRepository( - true, - true, - extraResolver - ); - } catch (IllegalArgumentException ex) { - throw new MojoExecutionException( - "could not obtain the license repository", ex); - } catch (IOException ex) { - throw new MojoExecutionException( - "could not obtain the license repository", ex); - } - - for (LicenseDefinition entry : factory.getDefinitions()) { - String licenseName = entry.getName(); + for (License license : licenseStore.getLicenses()) { + String licenseName = license.getName(); buffer.append(" * "); buffer.append(licenseName); buffer.append(" : "); - buffer.append(entry.getDescription()); + buffer.append(license.getDescription()); buffer.append('\n'); if (detail) { try { - - License license = factory.getLicense(licenseName); buffer.append("\n"); - buffer.append(license.getHeaderContent(encoding)); + buffer.append(license.getHeaderContent(getEncoding())); buffer.append("\n\n"); } catch (IOException ex) { throw new MojoExecutionException( "could not instanciate license with name " + - licenseName + " for reason " + ex.getMessage(), ex); + licenseName + " for reason " + ex.getMessage(), ex); } } } getLog().info(buffer.toString()); +// LicenseRepository factory; +// try { +// factory = LicenseRepositoryFactory.newLicenseRepository( +// true, +// true, +// extraResolver +// ); +// } catch (IllegalArgumentException ex) { +// throw new MojoExecutionException( +// "could not obtain the license repository", ex); +// } catch (IOException ex) { +// throw new MojoExecutionException( +// "could not obtain the license repository", ex); +// } +// +// for (LicenseDefinition entry : factory.getDefinitions()) { +// String licenseName = entry.getName(); +// buffer.append(" * "); +// buffer.append(licenseName); +// buffer.append(" : "); +// buffer.append(entry.getDescription()); +// buffer.append('\n'); +// if (detail) { +// try { +// +// License license = factory.getLicense(licenseName); +// buffer.append("\n"); +// buffer.append(license.getHeaderContent(encoding)); +// buffer.append("\n\n"); +// } catch (IOException ex) { +// throw new MojoExecutionException( +// "could not instanciate license with name " + +// licenseName + " for reason " + ex.getMessage(), ex); +// } +// } +// } +// getLog().info(buffer.toString()); } + } Modified: trunk/src/main/java/org/nuiton/license/plugin/UpdateHeaderMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/UpdateHeaderMojo.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/UpdateHeaderMojo.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -21,7 +21,6 @@ package org.nuiton.license.plugin; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.project.MavenProject; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; @@ -29,9 +28,7 @@ import org.codehaus.plexus.velocity.VelocityComponent; import org.nuiton.license.plugin.header.generator.HeaderGenerator; import org.nuiton.license.plugin.repository.License; -import org.nuiton.license.plugin.repository.LicenseRepository; -import org.nuiton.license.plugin.repository.LicenseRepositoryFactory; -import org.nuiton.plugin.AbstractPlugin; +import org.nuiton.license.plugin.repository.LicenseStore; import org.nuiton.plugin.PluginHelper; import org.nuiton.processor.LicenseProcessor; @@ -47,28 +44,12 @@ * @requiresProject true * @goal update-header * @since 1.0.1 + * @deprecated since 1.2, use now the <b>update-header-file</b> goal instead. */ -public class UpdateHeaderMojo extends AbstractPlugin { +@Deprecated +public class UpdateHeaderMojo extends AbstractLicenseMojo { /** - * Dependance du projet. - * - * @parameter default-value="${project}" - * @required - * @since 1.0.0 - */ - protected MavenProject project; - - /** - * Encoding a utiliser pour lire et ecrire les fichiers. - * - * @parameter expression="${license.encoding}" default-value="${project.build.sourceEncoding}" - * @required - * @since 1.0.0 - */ - protected String encoding; - - /** * l'annee de creation du module (sera place dans le header) * * @parameter expression="${license.inceptionYear}" default-value="${project.inceptionYear}" @@ -232,14 +213,6 @@ protected boolean keepBackup; /** - * Un flag pour activer le mode verbeux. - * - * @parameter expression="${license.verbose}" default-value="${maven.verbose}" - * @since 1.0.0 - */ - protected boolean verbose; - - /** * A flag to skip the goal. * * @parameter expression="${skipUpdateHeader}" default-value="false" @@ -285,27 +258,10 @@ /** le timestamp utilise pour la generation */ protected long timestamp; - @Override - public boolean isVerbose() { - return verbose; - } + /** store of licenses */ + protected LicenseStore licenseStore; @Override - public void setVerbose(boolean verbose) { - this.verbose = verbose; - } - - @Override - public MavenProject getProject() { - return project; - } - - @Override - public void setProject(MavenProject project) { - this.project = project; - } - - @Override public boolean checkPackaging() { return true; } @@ -353,7 +309,7 @@ // should never happen... throw new MojoExecutionException("no header generator found"); } - if (verbose) { + if (isVerbose()) { for (Entry<String, HeaderGenerator> e : _generators.entrySet()) { Entry<String, HeaderGenerator> next = e; getLog().info("config - available generator : " + next.getKey()); @@ -396,15 +352,19 @@ checkResource(templateFile); // recuperation de la license a utiliser - LicenseRepository factory = - LicenseRepositoryFactory.newLicenseRepository( - true, - true, - licenseResolver - ); - License license = factory.getLicense(licenseName); + licenseStore = createLicenseStore(licenseResolver); + + License license = licenseStore.getLicense(licenseName); +// +// LicenseRepository factory = +// LicenseRepositoryFactory.newLicenseRepository( +// true, +// true, +// licenseResolver +// ); +// License license = factory.getLicense(licenseName); - if (verbose) { + if (isVerbose()) { getLog().info("config - use license " + license.getName()); getLog().info("config - use generator " + generator.getName()); getLog().info("config - use template " + template); @@ -414,7 +374,7 @@ // build the comment boxed header content boxedLicenseHeaderContent = generator.getHeader(licenseHeaderContent); - if (verbose) { + if (isVerbose()) { getLog().info("config - header to use\n" + boxedLicenseHeaderContent); } @@ -432,7 +392,7 @@ for (String javaRelativePath : entry.getValue()) { File file = new File(src, javaRelativePath); - if (verbose) { + if (isVerbose()) { getLog().info("process file " + file); } @@ -454,18 +414,18 @@ getLog().info("adding license header on file " + file); String content = PluginHelper.readAsString( file, - encoding + getEncoding() ); content = boxedLicenseHeaderContent + content; if (!dryRun) { - writeFile(processFile, content, encoding); + writeFile(processFile, content, getEncoding()); } } } if (keepBackup && !dryRun) { File backupFile = new File(file.getAbsolutePath() + "~"); - if (verbose) { + if (isVerbose()) { getLog().debug("backup original file " + file); } renameFile(file, backupFile); @@ -505,7 +465,7 @@ HeaderGenerator generator) throws Exception { // recuperation de la license a mettre dans le header - String licenseContent = license.getHeaderContent(encoding); + String licenseContent = license.getHeaderContent(getEncoding()); // defined inceptionYear (if year is older than now suffix with a - thisYear) Calendar cal = Calendar.getInstance(); @@ -530,7 +490,7 @@ } } - if (verbose) { + if (isVerbose()) { StringBuilder buffer = new StringBuilder(); buffer.append("config - parameters for template : "); @@ -548,7 +508,7 @@ StringWriter writer = new StringWriter(); - Template velocityTemplate = engine.getTemplate(template, encoding); + Template velocityTemplate = engine.getTemplate(template, getEncoding()); velocityTemplate.merge(context, writer); Modified: trunk/src/main/java/org/nuiton/license/plugin/header/generator/AptLicenseHeaderGeneratorImpl.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/header/generator/AptLicenseHeaderGeneratorImpl.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/header/generator/AptLicenseHeaderGeneratorImpl.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -36,8 +36,19 @@ public static final String GENERATOR_DESCRIPTION = "generator with apt comment style"; + public static final String PREFIX = "~~~ "; + + public static final String OPEN_TAG = "\n"; + + public static final String CLOSE_TAG = "\n"; + public AptLicenseHeaderGeneratorImpl() { - super("~~~ ", "\n", "\n", LicenseFilter.HEADER, LicenseFilter.FOOTER); + super(PREFIX, + OPEN_TAG, + CLOSE_TAG, + LicenseFilter.HEADER, + LicenseFilter.FOOTER + ); } @Override Modified: trunk/src/main/java/org/nuiton/license/plugin/header/generator/JavaLicenseHeaderGeneratorImpl.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/header/generator/JavaLicenseHeaderGeneratorImpl.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/header/generator/JavaLicenseHeaderGeneratorImpl.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -36,8 +36,19 @@ public static final String GENERATOR_DESCRIPTION = "generator with java comment style"; + public static final String PREFIX = " *"; + + public static final String OPEN_TAG = "/*"; + + public static final String CLOSE_TAG = " */"; + public JavaLicenseHeaderGeneratorImpl() { - super(" *", "/*", " */", LicenseFilter.HEADER, LicenseFilter.FOOTER); + super(PREFIX, + OPEN_TAG, + CLOSE_TAG, + LicenseFilter.HEADER, + LicenseFilter.FOOTER + ); } @Override Modified: trunk/src/main/java/org/nuiton/license/plugin/header/generator/PropertiesLicenseHeaderGeneratorImpl.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/header/generator/PropertiesLicenseHeaderGeneratorImpl.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/header/generator/PropertiesLicenseHeaderGeneratorImpl.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -42,7 +42,12 @@ public static final String PREFIX = "#"; public PropertiesLicenseHeaderGeneratorImpl() { - super(PREFIX, LINE, LINE, LicenseFilter.HEADER, LicenseFilter.FOOTER); + super(PREFIX, + LINE, + LINE, + LicenseFilter.HEADER, + LicenseFilter.FOOTER + ); } @Override Modified: trunk/src/main/java/org/nuiton/license/plugin/header/generator/XmlLicenseHeaderGeneratorImpl.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/header/generator/XmlLicenseHeaderGeneratorImpl.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/header/generator/XmlLicenseHeaderGeneratorImpl.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -36,8 +36,19 @@ public static final String GENERATOR_DESCRIPTION = "generator with xml comment style"; + public static final String PREFIX = " "; + + public static final String OPEN_TAG = "<!--"; + + public static final String CLOSE_TAG = "-->"; + public XmlLicenseHeaderGeneratorImpl() { - super(" ", "<!--", "-->", LicenseFilter.HEADER, LicenseFilter.FOOTER); + super(PREFIX, + OPEN_TAG, + CLOSE_TAG, + LicenseFilter.HEADER, + LicenseFilter.FOOTER + ); } @Override Modified: trunk/src/main/java/org/nuiton/license/plugin/repository/License.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/repository/License.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/repository/License.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -37,6 +37,13 @@ */ public class License { + + public static final String LICENSE_HEADER_FILE = "header.txt"; + + public static final String LICENSE_CONTENT_FILE = "license.txt"; + + protected URL baseURL; + /** the name of the licenses (ex lgpl-3.0) */ protected String name; @@ -57,10 +64,17 @@ } public URL getLicenseURL() { + if (licenseURL == null) { + + licenseURL = LicenseRepository.getUrl(baseURL, LICENSE_CONTENT_FILE); + } return licenseURL; } public URL getHeaderURL() { + if (headerURL == null) { + headerURL = LicenseRepository.getUrl(baseURL, LICENSE_HEADER_FILE); + } return headerURL; } @@ -68,13 +82,17 @@ return description; } + public URL getBaseURL() { + return baseURL; + } + public String getLicenseContent(String encoding) throws IOException { if (licenseURL == null) { - throw new IllegalStateException("no licenseURL defined in " + this); + throw new IllegalStateException("no licenseURL defined, can not obtain license content in " + this); } Reader r = new BufferedReader( - new InputStreamReader(licenseURL.openStream(), encoding)); + new InputStreamReader(getLicenseURL().openStream(), encoding)); try { return IOUtil.toString(r); } finally { @@ -83,11 +101,11 @@ } public String getHeaderContent(String encoding) throws IOException { - if (headerURL == null) { - throw new IllegalStateException("no headerURL defined in " + this); + if (baseURL == null) { + throw new IllegalStateException("no baseURL defined, can not obtain header content in " + this); } Reader r = new BufferedReader( - new InputStreamReader(headerURL.openStream(), encoding)); + new InputStreamReader(getHeaderURL().openStream(), encoding)); try { return IOUtil.toString(r); } finally { @@ -99,18 +117,24 @@ this.name = name; } - public void setLicenseURL(URL licenseURL) { - this.licenseURL = licenseURL; - } +// @Deprecated +// public void setLicenseURL(URL licenseURL) { +// this.licenseURL = licenseURL; +// } +// +// @Deprecated +// public void setHeaderURL(URL headerURL) { +// this.headerURL = headerURL; +// } - public void setHeaderURL(URL headerURL) { - this.headerURL = headerURL; - } - public void setDescription(String description) { this.description = description; } + public void setBaseURL(URL baseURL) { + this.baseURL = baseURL; + } + @Override public String toString() { ToStringBuilder builder = new ToStringBuilder( @@ -119,8 +143,8 @@ ); builder.append("name", name); builder.append("description", description); - builder.append("licenseURL", licenseURL); - builder.append("headerURL", headerURL); + builder.append("licenseURL", getLicenseURL()); + builder.append("headerURL", getHeaderURL()); return builder.toString(); } } Modified: trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseDefinition.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseDefinition.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseDefinition.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -30,7 +30,9 @@ * Model of a license definition in a license repository. * * @since 1.0.3 + * @deprecated since 2.1, prefer use directly the {@link License} class instead. */ +@Deprecated public class LicenseDefinition { public static final String LICENSE_HEADER_FILE = "header.txt"; Modified: trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseRepository.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseRepository.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseRepository.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -20,6 +20,9 @@ */ package org.nuiton.license.plugin.repository; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -33,18 +36,28 @@ */ public class LicenseRepository { + + /** Logger */ + private static final Log log = LogFactory.getLog(LicenseRepository.class); + public static final String REPOSITORY_DEFINITION_FILE = "licenses.properties"; /** the base url of the licenses repository */ protected URL baseURL; - /** next repository (can be {@code null}). */ - protected LicenseRepository next; +// /** next repository (can be {@code null}). */ +// @Deprecated +// protected LicenseRepository next; +// +// @Deprecated +// protected List<LicenseDefinition> definitions; +// +// @Deprecated +// protected final Map<LicenseDefinition, License> cache; - protected List<LicenseDefinition> definitions; + /** licenses of this repository */ + protected List<License> licenses; - protected final Map<LicenseDefinition, License> cache; - /** * flag to known if repository was init (pass to {@code true} when invoking * the method {@link #load()}). @@ -52,7 +65,7 @@ protected boolean init; public LicenseRepository() { - cache = new HashMap<LicenseDefinition, License>(); +// cache = new HashMap<LicenseDefinition, License>(); } public URL getBaseURL() { @@ -60,38 +73,30 @@ } public void setBaseURL(URL baseURL) { - checkNotInit(); + checkNotInit("setBaseURL"); this.baseURL = baseURL; } - public LicenseRepository appendRepository(LicenseRepository next) { - LicenseRepository lastRepository = getLastRepository(); - lastRepository.next = next; - return next; - } - - public synchronized void reload() throws IOException { - if (next != null) { - next.reload(); - } - init = false; - cache.clear(); - load(); - } - - public synchronized void load() throws IOException { - if (!init) { + public void load() throws IOException { + checkNotInit("load"); + try { +// if (!init) { if (baseURL == null || baseURL.toString().trim().isEmpty()) { throw new IllegalStateException("no baseURL defined in " + this); } URL definitionURL = getUrl(baseURL, REPOSITORY_DEFINITION_FILE); - this.definitions = new ArrayList<LicenseDefinition>(); + if (licenses != null) { + licenses.clear(); + } else { + licenses = new ArrayList<License>(); + } +// definitions = new ArrayList<LicenseDefinition>(); if (!checkExists(definitionURL)) { throw new IllegalArgumentException( "no licenses.properties found withurl [" + - definitionURL + "] for resolver " + this); + definitionURL + "] for resolver " + this); } Properties p = new Properties(); p.load(definitionURL.openStream()); @@ -104,87 +109,81 @@ URL licenseURL = getUrl(baseURL, licenseName); - LicenseDefinition def = new LicenseDefinition( - licenseURL, - licenseName, - licenseDescription - ); - definitions.add(def); + License license = new License(); + license.setName(licenseName); + license.setDescription(licenseDescription); + license.setBaseURL(licenseURL); + if (log.isInfoEnabled()) { + log.info("Adding license " + license); + } + licenses.add(license); +// LicenseDefinition def = new LicenseDefinition( +// licenseURL, +// licenseName, +// licenseDescription +// ); +// definitions.add(def); } - definitions = Collections.unmodifiableList(definitions); + licenses = Collections.unmodifiableList(licenses); +// definitions = Collections.unmodifiableList(definitions); +// } +// if (next != null) { +// next.load(); +// } + } finally { + // mark repository as available + init = true; } - if (next != null) { - next.load(); - } - // mark repository as available - init = true; } - public List<LicenseDefinition> getAllDefinitions() { - LicenseRepository[] repos = getAllRepositories(); - List<LicenseDefinition> result = - new ArrayList<LicenseDefinition>(repos.length); - for (LicenseRepository repo : repos) { - result.addAll(repo.definitions); - } - return result; + public void reload() throws IOException { +// if (next != null) { +// next.reload(); +// } + init = false; +// cache.clear(); + load(); } - public List<LicenseDefinition> getDefinitions() { - return definitions; + public List<License> getLicenses() { + checkInit("getLicense"); + return licenses; } - public LicenseDefinition getDefinition(String licenseName) { - checkInit(); + public License getLicense(String licenseName) { + checkInit("getLicense"); if (licenseName == null || licenseName.trim().isEmpty()) { throw new IllegalArgumentException( "licenceName can not be null, nor empty"); } - licenseName = licenseName.trim().toLowerCase(); - LicenseDefinition definition = null; - for (LicenseDefinition d : definitions) { - if (licenseName.equals(d.getName())) { - definition = d; + + License license = null; + for (License l : licenses) { + if (licenseName.equals(l.getName())) { + // got it + license = l; break; } } - if (definition == null && next != null) { - definition = next.getDefinition(licenseName); - } - return definition; + return license; } - public License getLicense(String licenseName) throws IOException { - checkInit(); - if (licenseName == null || licenseName.trim().isEmpty()) { - throw new IllegalArgumentException( - "licenceName can not be null, nor empty"); - } + protected boolean checkExists(URL url) throws IOException { + URLConnection openConnection = url.openConnection(); + return openConnection.getContentLength() > 0; + } - LicenseDefinition definition = getDefinition(licenseName); - License license = null; - if (definition != null) { - synchronized (cache) { - license = cache.get(definition); - if (license == null) { + protected void checkInit(String operation) throws IllegalStateException { + if (!init) { + throw new IllegalStateException("repository " + this + " was not init, operation [" + operation + "] not possible."); + } + } - URL licenseURL = definition.getLicenseURL(); - URL headerURL = definition.getHeaderURL(); - - checkExists(licenseURL); - checkExists(headerURL); - - license = new License(); - license.setName(definition.getName()); - license.setDescription(definition.getDescription()); - license.setLicenseURL(licenseURL); - license.setHeaderURL(headerURL); - cache.put(definition, license); - } - } + protected void checkNotInit(String operation) throws IllegalStateException { + if (init) { + throw new IllegalStateException("repository " + this + "was init, operation [" + operation + "+] not possible."); } - return license; } public static URL getUrl(URL baseUrl, @@ -198,39 +197,80 @@ } } - protected LicenseRepository getLastRepository() { - LicenseRepository last = next == null ? this : next.getLastRepository(); - return last; - } - - protected LicenseRepository[] getAllRepositories() { - List<LicenseRepository> list = new ArrayList<LicenseRepository>(); - LicenseRepository repo = this; - while (repo != null) { - list.add(repo); - repo = repo.next; - } - return list.toArray(new LicenseRepository[list.size()]); - } - - protected boolean checkExists(URL url) throws IOException { - URLConnection openConnection = url.openConnection(); - return openConnection.getContentLength() > 0; - } - - protected void checkNotInit() throws IllegalStateException { - if (init) { - throw new IllegalStateException( - "license repository " + this + - " was already initialize..."); - } - } - - protected void checkInit() throws IllegalStateException { - if (!init) { - throw new IllegalStateException( - "repository " + this + " is not init, use the load " + - "method before any license request"); - } - } +// @Deprecated +// public LicenseRepository appendRepository(LicenseRepository next) { +// LicenseRepository lastRepository = getLastRepository(); +// lastRepository.next = next; +// return next; +// } +// +// @Deprecated +// public List<LicenseDefinition> getAllDefinitions() { +// LicenseRepository[] repos = getAllRepositories(); +// List<LicenseDefinition> result = +// new ArrayList<LicenseDefinition>(repos.length); +// for (LicenseRepository repo : repos) { +// result.addAll(repo.definitions); +// } +// return result; +// } +// +// public List<LicenseDefinition> getDefinitions() { +// return definitions; +// } +// +// public LicenseDefinition getDefinition(String licenseName) { +// checkInit("getDefinition"); +// if (licenseName == null || licenseName.trim().isEmpty()) { +// throw new IllegalArgumentException( +// "licenceName can not be null, nor empty"); +// } +// licenseName = licenseName.trim().toLowerCase(); +// LicenseDefinition definition = null; +// for (LicenseDefinition d : definitions) { +// if (licenseName.equals(d.getName())) { +// definition = d; +// break; +// } +// } +// if (definition == null && next != null) { +// definition = next.getDefinition(licenseName); +// } +// return definition; +// } +// +// @Deprecated +// protected LicenseRepository getLastRepository() { +// LicenseRepository last = next == null ? this : next.getLastRepository(); +// return last; +// } +// +// @Deprecated +// protected LicenseRepository[] getAllRepositories() { +// List<LicenseRepository> list = new ArrayList<LicenseRepository>(); +// LicenseRepository repo = this; +// while (repo != null) { +// list.add(repo); +// repo = repo.next; +// } +// return list.toArray(new LicenseRepository[list.size()]); +// } +// +// @Deprecated +// protected void checkNotInit() throws IllegalStateException { +// if (init) { +// throw new IllegalStateException( +// "license repository " + this + +// " was already initialize..."); +// } +// } +// +// @Deprecated +// protected void checkInit() throws IllegalStateException { +// if (!init) { +// throw new IllegalStateException( +// "repository " + this + " is not init, use the load " + +// "method before any license request"); +// } +// } } Modified: trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseRepositoryFactory.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseRepositoryFactory.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/java/org/nuiton/license/plugin/repository/LicenseRepositoryFactory.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -31,97 +31,107 @@ /** * @author chemit * @since 1.0.3 + * @deprecated since 2.1, prefer use now a {@link LicenseStore}. */ +@Deprecated public class LicenseRepositoryFactory { - /** class-path directory where is the licenses repository */ - public static final String JAR_LICENSE_REPOSITORY = "/META-INF/licenses"; +// /** class-path directory where is the licenses repository */ +// public static final String JAR_LICENSE_REPOSITORY = "/META-INF/licenses"; +// +// /** to use log facility, just put in your code: log.info(\"...\"); */ +// static private final Log log = LogFactory.getLog(LicenseRepositoryFactory.class); - /** to use log facility, just put in your code: log.info(\"...\"); */ - static private final Log log = LogFactory.getLog(LicenseRepositoryFactory.class); +// /** +// * Obtain a new repository. +// * +// * @param useJarRepository flag to use at first a jar repository +// * @param load flag to load the repository +// * @param extraResolvers extra baseURLs to defined extra license resolver +// * @return the instanciate and ready to use license repository +// * @throws IOException if any problem while acquiring license repository +// * @deprecated since 2.1, prefer use the {@link LicenseStore} api. +// */ +// @Deprecated +// public static LicenseRepository newLicenseRepository( +// boolean useJarRepository, +// boolean load, +// String... extraResolvers) throws IOException { +// +// List<URL> baseURLs = new ArrayList<URL>(); +// if (extraResolvers != null) { +// for (String exUrl : extraResolvers) { +// if (exUrl == null) { +// // skpi null url +// continue; +// } +// URL url; +// try { +// url = new URL(exUrl); +// } catch (Exception e) { +// if (log.isDebugEnabled()) { +// log.warn("could not convert url [" + exUrl + +// "], for reason " + e.getMessage()); +// } else { +// log.warn("could not convert url [" + exUrl + +// "], for reason " + e.getMessage()); +// } +// log.warn("will skip the url [" + exUrl + "]"); +// continue; +// } +// baseURLs.add(url); +// } +// } +// if (!useJarRepository && baseURLs.isEmpty()) { +// log.warn("no repository to create!"); +// // no repository to create +// return null; +// } +// +// LicenseRepository result = null; +// LicenseRepository current = null; +// if (useJarRepository) { +// current = result = newJarLicenseRepository(false); +// } +// +// for (URL baseURL : baseURLs) { +// if (baseURL != null && !baseURL.toString().trim().isEmpty()) { +// LicenseRepository r = new LicenseRepository(); +// r.setBaseURL(baseURL); +// if (current == null) { +// // no previous repository to chain +// result = r; +// +// } else { +// // chain to previous repository +// current.appendRepository(r); +// } +// // new repository to chain +// current = r; +// } +// } +// if (load) { +// result.load(); +// } +// return result; +// } - /** - * Obtain a new repository. - * - * @param useJarRepository flag to use at first a jar repository - * @param load flag to load the repository - * @param extraResolvers extra baseURLs to defined extra license resolver - * @return the instanciate and ready to use license repository - * @throws IOException if any problem while acquiring license repository - */ - public static LicenseRepository newLicenseRepository( - boolean useJarRepository, - boolean load, - String... extraResolvers) throws IOException { - - List<URL> baseURLs = new ArrayList<URL>(); - if (extraResolvers != null) { - for (String exUrl : extraResolvers) { - if (exUrl == null) { - // skpi null url - continue; - } - URL url; - try { - url = new URL(exUrl); - } catch (Exception e) { - if (log.isDebugEnabled()) { - log.warn("could not convert url [" + exUrl + - "], for reason " + e.getMessage()); - } else { - log.warn("could not convert url [" + exUrl + - "], for reason " + e.getMessage()); - } - log.warn("will skip the url [" + exUrl + "]"); - continue; - } - baseURLs.add(url); - } - } - if (!useJarRepository && baseURLs.isEmpty()) { - log.warn("no repository to create!"); - // no repository to create - return null; - } - - LicenseRepository result = null; - LicenseRepository current = null; - if (useJarRepository) { - current = result = newJarLicenseRepository(false); - } - - for (URL baseURL : baseURLs) { - if (baseURL != null && !baseURL.toString().trim().isEmpty()) { - LicenseRepository r = new LicenseRepository(); - r.setBaseURL(baseURL); - if (current == null) { - // no previous repository to chain - result = r; - - } else { - // chain to previous repository - current.appendRepository(r); - } - // new repository to chain - current = r; - } - } - if (load) { - result.load(); - } - return result; - } - - public static LicenseRepository newJarLicenseRepository( - boolean load) throws IOException { - LicenseRepository result = new LicenseRepository(); - // the first repository is always a jar repository - URL baseURL = LicenseRepositoryFactory.class.getResource( - JAR_LICENSE_REPOSITORY); - result.setBaseURL(baseURL); - if (load) { - result.load(); - } - return result; - } +// /** +// * @param load {@code true} if repository should be loaded +// * @return the license repository coming for class-path +// * @throws IOException if any pb while loading repository (if asked) +// * @deprecated since 2.1, prefer use the {@link LicenseStore} api. +// */ +// @Deprecated +// public static LicenseRepository newJarLicenseRepository(boolean load) throws IOException { +// LicenseRepository result = new LicenseRepository(); +// // the first repository is always a jar repository +// URL baseURL = LicenseRepositoryFactory.class.getResource( +// JAR_LICENSE_REPOSITORY); +// result.setBaseURL(baseURL); +// if (load) { +// result.load(); +// } +// return result; +// } } Modified: trunk/src/main/resources/META-INF/licenses/licenses.properties =================================================================== --- trunk/src/main/resources/META-INF/licenses/licenses.properties 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/main/resources/META-INF/licenses/licenses.properties 2010-04-06 13:03:55 UTC (rev 1706) @@ -1,3 +1,5 @@ +apache_v2=License Apache version 3.0 +cddl_v1=COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 gpl_v1=License gpl version 1.0 gpl_v2=License gpl version 2.0 gpl_v3=License gpl version 3.0 @@ -3,3 +5,3 @@ lgpl_v2_1=License lgpl version 2.1 lgpl_v3=License lgpl version 3.0 -apache_v2=License Apache version 3.0 +mit=MIT-License Modified: trunk/src/site/rst/index.rst =================================================================== --- trunk/src/site/rst/index.rst 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/site/rst/index.rst 2010-04-06 13:03:55 UTC (rev 1706) @@ -16,7 +16,7 @@ :: - *##% Content ##%* + %L Content L% The header is composed of two parts : @@ -29,10 +29,11 @@ :: /* - * *##% + * %L * Plugin maven de changement de license + * -- * Copyright (C) 2008 - 2009 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 @@ -46,7 +47,7 @@ * 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>. - * ##%* + * L% */ Goals Overview @@ -89,4 +90,3 @@ .. _license help: help-mojo.html .. _usage: usage.html - \ No newline at end of file Modified: trunk/src/site/rst/usage.rst =================================================================== --- trunk/src/site/rst/usage.rst 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/site/rst/usage.rst 2010-04-06 13:03:55 UTC (rev 1706) @@ -4,7 +4,7 @@ add-license goal ================ -This goal create or update the LICENSE.txt file and add it in build. +This goal creates or updates the LICENSE.txt file and add it in build. for full detail see `add-license`_ detail page. @@ -18,7 +18,7 @@ generator-list goal =================== -This goal display of available generators. +This goal display of available header generators. Use it directly (and only) from commandline : @@ -74,7 +74,7 @@ The plugin will seek in all projets sources roots (and test sources roots). -Note : to detect the license header we use the nuiton-processor library. +Note : to detect the license header we use the `nuiton-processor`_ library. Update java files ~~~~~~~~~~~~~~~~~ @@ -300,3 +300,5 @@ .. _license-list: license-list-mojo.html .. _help: help-mojo.html + +.. _nuiton-processor : http://maven-site.nuiton.org/processor/index.html Modified: trunk/src/test/java/org/nuiton/license/plugin/repository/LicenseRepositoryFactoryTest.java =================================================================== --- trunk/src/test/java/org/nuiton/license/plugin/repository/LicenseRepositoryFactoryTest.java 2010-04-05 08:49:12 UTC (rev 1705) +++ trunk/src/test/java/org/nuiton/license/plugin/repository/LicenseRepositoryFactoryTest.java 2010-04-06 13:03:55 UTC (rev 1706) @@ -23,7 +23,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; +import org.nuiton.plugin.PluginHelper; import org.nuiton.plugin.TestHelper; import java.io.File; @@ -31,226 +33,225 @@ import java.util.List; /** @author chemit */ +@Ignore public class LicenseRepositoryFactoryTest extends Assert { - public static final String NEWLICENSE = "new_license"; - - public static final String NEWLICENSE2 = "new_license2"; - - protected static final Log log = LogFactory.getLog(LicenseRepositoryFactoryTest.class); - - protected static final String encoding = "utf-8"; - - protected File baseDir; - - protected File getBaseDir() { - if (baseDir == null) { - baseDir = TestHelper.getBasedir(); - } - return baseDir; - } - - protected void assertLicenseDefinitionFound(String name, LicenseDefinition definition) throws IOException { - assertNotNull("could not find definition for license [" + name + "]", definition); - assertEquals(name.toLowerCase(), definition.getName()); - } - - protected void assertLicenseFound(String name, License license) throws IOException { - assertNotNull("could not find license [" + name + "]", license); - assertEquals(name, license.getName()); - assertNotNull(license.getLicenseContent(encoding)); - assertNotNull(license.getHeaderContent(encoding)); - } - - protected void assertLicenseRepositoryFound(LicenseRepository repository, String... names) throws IOException { - - assertNotNull("could not find repository", repository); - assertEquals("expected " + names.length + " licenses but had " + repository.getDefinitions().size(), names.length, repository.getDefinitions().size()); - - for (String name : names) { - LicenseDefinition def = repository.getDefinition(name); - assertLicenseDefinitionFound(name.toLowerCase(), def); - License lic = repository.getLicense(name); - assertLicenseFound(name.toLowerCase(), lic); - } - } - - protected File createLicenseRepository(boolean createLicene) throws IOException { - long timestamp = System.currentTimeMillis(); - - File repo = TestHelper.getFile(getBaseDir(), "target", "test-classes", "org", "nuiton", "license", "plugin", "repository", "repository_" + timestamp); - - log.info("new license repository : " + TestHelper.getRelativePath(baseDir, repo)); - if (createLicene) { - - String licenseName = "license_" + timestamp; - addLicenseToRepository(repo, licenseName); - } - return repo; - } - - protected void addLicenseToRepository(File repo, String licenseName) throws IOException { - - File defFile = new File(repo, LicenseRepository.REPOSITORY_DEFINITION_FILE); - - String content = ""; - if (defFile.exists()) { - - content = TestHelper.readAsString(defFile, encoding) + "\n"; - } - - TestHelper.writeString(defFile, content + licenseName + "=My dummy license\n", encoding); - - // create dummy licenses - File file = new File(repo, licenseName); - file.mkdirs(); - TestHelper.writeString(new File(file, LicenseDefinition.LICENSE_CONTENT_FILE), "license:" + licenseName, encoding); - TestHelper.writeString(new File(file, LicenseDefinition.LICENSE_HEADER_FILE), "header:" + licenseName, encoding); - - } - - @Test - public void newJarLicenseRepository() throws Exception { - - LicenseRepository[] repositories; - LicenseRepository repository; - - repository = LicenseRepositoryFactory.newJarLicenseRepository(false); - assertNotNull(repository); - assertFalse(repository.init); - repositories = repository.getAllRepositories(); - assertEquals(1, repositories.length); - assertEquals(repository, repositories[0]); - - repository = LicenseRepositoryFactory.newJarLicenseRepository(true); - assertNotNull(repository); - assertTrue(repository.init); - repositories = repository.getAllRepositories(); - assertEquals(1, repositories.length); - assertEquals(repository, repositories[0]); - List<LicenseDefinition> definitions = repository.getDefinitions(); - List<LicenseDefinition> allDefinitions = repository.getAllDefinitions(); - assertEquals(5, definitions.size()); - assertEquals(5, allDefinitions.size()); - assertEquals(definitions, allDefinitions); - - assertLicenseRepositoryFound(repository, - "gpl_v1", - "gpl_v2", - "gpl_v3", - "lgpl_v2_1", - "lgpl_v3"); - } - - @Test - public void testNewInstance() throws Exception { - - LicenseRepository[] repositories; - LicenseRepository repository; - LicenseDefinition def; - - repository = LicenseRepositoryFactory.newJarLicenseRepository(true); - assertNotNull(repository); - repositories = repository.getAllRepositories(); - assertEquals(1, repositories.length); - assertNotNull(repositories[0]); - assertLicenseRepositoryFound(repository, - "gpl_v1", - "gpl_v2", - "gpl_v3", - "lgpl_v2_1", - "lgpl_v3"); - - repository = LicenseRepositoryFactory.newLicenseRepository(true, true, ""); - repositories = repository.getAllRepositories(); - assertNotNull(repository); - assertEquals(1, repositories.length); - assertNotNull(repositories[0]); - assertLicenseRepositoryFound(repository, - "gpl_v1", - "gpl_v2", - "gpl_v3", - "lgpl_v2_1", - "lgpl_v3"); - - repository = LicenseRepositoryFactory.newLicenseRepository(true, false, "file://yo"); - assertNotNull(repository); - repositories = repository.getAllRepositories(); - - assertEquals(2, repositories.length); - assertNotNull(repositories[0]); - assertNotNull(repositories[1]); - - assertNotNull(repositories[1].getBaseURL()); - assertEquals("file://yo", repositories[1].getBaseURL().toString()); - - // create a new dummy license repository with a license - - File repo = createLicenseRepository(true); - String generatedLicence = "license" + repo.getName().substring(repo.getName().indexOf("_")); - - repository = LicenseRepositoryFactory.newLicenseRepository(true, true, repo.toURI().toURL().toString()); - assertNotNull(repository); - repositories = repository.getAllRepositories(); - - assertEquals(2, repositories.length); - - assertNotNull(repositories[0]); - assertNotNull(repositories[1]); - assertEquals(5, repositories[0].getDefinitions().size()); - assertEquals(1, repositories[1].getDefinitions().size()); - - assertLicenseRepositoryFound(repositories[0], - "gpl_v1", - "gpl_v2", - "gpl_v3", - "lgpl_v2_1", - "lgpl_v3"); - - assertNotNull(repositories[1].getBaseURL()); - assertEquals(repo, new File(repositories[1].getBaseURL().getFile())); - - assertLicenseRepositoryFound(repositories[1], - generatedLicence); - - - // add license - - addLicenseToRepository(repo, NEWLICENSE); - - repository.reload(); - - repositories = repository.getAllRepositories(); - - assertEquals(2, repositories.length); - assertNotNull(repositories[0]); - assertNotNull(repositories[1]); - assertNotNull(repositories[1].getBaseURL()); - assertEquals(repo, new File(repositories[1].getBaseURL().getFile())); - assertEquals(2, repositories[1].getDefinitions().size()); - assertNotNull(repositories[1].getDefinitions().get(1)); - - assertLicenseRepositoryFound(repositories[1], - NEWLICENSE, generatedLicence); - - // add another license - - addLicenseToRepository(repo, NEWLICENSE2); - repository.reload(); - - repositories = repository.getAllRepositories(); - - assertEquals(2, repositories.length); - assertNotNull(repositories[0]); - assertNotNull(repositories[1]); - assertNotNull(repositories[1].getBaseURL()); - assertEquals(repo, new File(repositories[1].getBaseURL().getFile())); - assertEquals(3, repositories[1].getDefinitions().size()); - - assertLicenseRepositoryFound(repositories[1], NEWLICENSE, - NEWLICENSE2, generatedLicence); - } // +// public static final String NEWLICENSE = "new_license"; +// +// public static final String NEWLICENSE2 = "new_license2"; +// +// protected static final Log log = LogFactory.getLog(LicenseRepositoryFactoryTest.class); +// +// protected static final String encoding = "utf-8"; +// +// protected File baseDir; +// +// public static final String[] DEFAULT_LICENSES = new String[]{"apache_v2", +// "cddl_v1", +// "gpl_v1", +// "gpl_v2", +// "gpl_v3", +// "lgpl_v2_1", +// "lgpl_v3"}; +// +// protected File getBaseDir() { +// if (baseDir == null) { +// baseDir = TestHelper.getBasedir(); +// } +// return baseDir; +// } +// +// protected void assertLicenseDefinitionFound(String name, LicenseDefinition definition) throws IOException { +// assertNotNull("could not find definition for license [" + name + "]", definition); +// assertEquals(name.toLowerCase(), definition.getName()); +// } +// +// protected void assertLicenseFound(String name, License license) throws IOException { +// assertNotNull("could not find license [" + name + "]", license); +// assertEquals(name, license.getName()); +// assertNotNull(license.getLicenseContent(encoding)); +// assertNotNull(license.getHeaderContent(encoding)); +// } +// +// protected void assertLicenseRepositoryFound(LicenseRepository repository, String... names) throws IOException { +// +// assertNotNull("could not find repository", repository); +// assertEquals("expected " + names.length + " licenses but had " + repository.getDefinitions().size(), names.length, repository.getDefinitions().size()); +// +// for (String name : names) { +// LicenseDefinition def = repository.getDefinition(name); +// assertLicenseDefinitionFound(name.toLowerCase(), def); +// License lic = repository.getLicense(name); +// assertLicenseFound(name.toLowerCase(), lic); +// } +// } +// +// protected File createLicenseRepository(boolean createLicene) throws IOException { +// long timestamp = System.currentTimeMillis(); +// +// File repo = TestHelper.getFile(getBaseDir(), +// "target", +// "test-classes", +// "org", +// "nuiton", +// "license", +// "plugin", +// "repository", +// "repository_" + timestamp +// ); +// +// log.info("new license repository : " + TestHelper.getRelativePath(baseDir, repo)); +// if (createLicene) { +// +// String licenseName = "license_" + timestamp; +// addLicenseToRepository(repo, licenseName); +// } +// return repo; +// } +// +// protected void addLicenseToRepository(File repo, String licenseName) throws IOException { +// +// File defFile = new File(repo, LicenseRepository.REPOSITORY_DEFINITION_FILE); +// +// String content = ""; +// if (defFile.exists()) { +// +// content = TestHelper.readAsString(defFile, encoding) + "\n"; +// } +// +// TestHelper.writeString(defFile, content + licenseName + "=My dummy license\n", encoding); +// +// // create dummy licenses +// File file = new File(repo, licenseName); +// PluginHelper.createDirectoryIfNecessary(file); +// TestHelper.writeString(new File(file, License.LICENSE_CONTENT_FILE), "license:" + licenseName, encoding); +// TestHelper.writeString(new File(file, License.LICENSE_HEADER_FILE), "header:" + licenseName, encoding); +// +// } +// // @Test +// public void newJarLicenseRepository() throws Exception { +// +// LicenseRepository[] repositories; +// LicenseRepository repository; +// +// repository = LicenseRepositoryFactory.newJarLicenseRepository(false); +// assertNotNull(repository); +// assertFalse(repository.init); +// repositories = repository.getAllRepositories(); +// assertEquals(1, repositories.length); +// assertEquals(repository, repositories[0]); +// +// repository = LicenseRepositoryFactory.newJarLicenseRepository(true); +// assertNotNull(repository); +// assertTrue(repository.init); +// repositories = repository.getAllRepositories(); +// assertEquals(1, repositories.length); +// assertEquals(repository, repositories[0]); +// List<LicenseDefinition> definitions = repository.getDefinitions(); +// List<LicenseDefinition> allDefinitions = repository.getAllDefinitions(); +// assertEquals(7, definitions.size()); +// assertEquals(7, allDefinitions.size()); +// assertEquals(definitions, allDefinitions); +// +// assertLicenseRepositoryFound(repository, DEFAULT_LICENSES); +// } +// +// @Test +// public void testNewInstance() throws Exception { +// +// LicenseRepository[] repositories; +// LicenseRepository repository; +// LicenseDefinition def; +// +// repository = LicenseRepositoryFactory.newJarLicenseRepository(true); +// assertNotNull(repository); +// repositories = repository.getAllRepositories(); +// assertEquals(1, repositories.length); +// assertNotNull(repositories[0]); +// assertLicenseRepositoryFound(repository, DEFAULT_LICENSES); +// +// repository = LicenseRepositoryFactory.newLicenseRepository(true, true, ""); +// repositories = repository.getAllRepositories(); +// assertNotNull(repository); +// assertEquals(1, repositories.length); +// assertNotNull(repositories[0]); +// assertLicenseRepositoryFound(repository, DEFAULT_LICENSES); +// +// repository = LicenseRepositoryFactory.newLicenseRepository(true, false, "file://yo"); +// assertNotNull(repository); +// repositories = repository.getAllRepositories(); +// +// assertEquals(2, repositories.length); +// assertNotNull(repositories[0]); +// assertNotNull(repositories[1]); +// +// assertNotNull(repositories[1].getBaseURL()); +// assertEquals("file://yo", repositories[1].getBaseURL().toString()); +// +// // create a new dummy license repository with a license +// +// File repo = createLicenseRepository(true); +// String generatedLicence = "license" + repo.getName().substring(repo.getName().indexOf("_")); +// +// repository = LicenseRepositoryFactory.newLicenseRepository(true, true, repo.toURI().toURL().toString()); +// assertNotNull(repository); +// repositories = repository.getAllRepositories(); +// +// assertEquals(2, repositories.length); +// +// assertNotNull(repositories[0]); +// assertNotNull(repositories[1]); +// assertEquals(5, repositories[0].getDefinitions().size()); +// assertEquals(1, repositories[1].getDefinitions().size()); +// +// assertLicenseRepositoryFound(repositories[0], DEFAULT_LICENSES); +// +// assertNotNull(repositories[1].getBaseURL()); +// assertEquals(repo, new File(repositories[1].getBaseURL().getFile())); +// +// assertLicenseRepositoryFound(repositories[1], +// generatedLicence); +// +// +// // add license +// +// addLicenseToRepository(repo, NEWLICENSE); +// +// repository.reload(); +// +// repositories = repository.getAllRepositories(); +// +// assertEquals(2, repositories.length); +// assertNotNull(repositories[0]); +// assertNotNull(repositories[1]); +// assertNotNull(repositories[1].getBaseURL()); +// assertEquals(repo, new File(repositories[1].getBaseURL().getFile())); +// assertEquals(2, repositories[1].getDefinitions().size()); +// assertNotNull(repositories[1].getDefinitions().get(1)); +// +// assertLicenseRepositoryFound(repositories[1], +// NEWLICENSE, generatedLicence); +// +// // add another license +// +// addLicenseToRepository(repo, NEWLICENSE2); +// repository.reload(); +// +// repositories = repository.getAllRepositories(); +// +// assertEquals(2, repositories.length); +// assertNotNull(repositories[0]); +// assertNotNull(repositories[1]); +// assertNotNull(repositories[1].getBaseURL()); +// assertEquals(repo, new File(repositories[1].getBaseURL().getFile())); +// assertEquals(3, repositories[1].getDefinitions().size()); +// +// assertLicenseRepositoryFound(repositories[1], NEWLICENSE, +// NEWLICENSE2, generatedLicence); +// } +// +// @Test // public void testGetLicenseNames() throws Exception { // LicenseRepository factory = LicenseRepositoryFactory.newJarLicenseRepository(true); //