001/* 
002 * Copyright (c) 2007-2011, Regents of the University of Colorado 
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
011 * 
012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE. 
023 */
024
025package org.cleartk.test.util;
026
027import java.io.File;
028import java.io.IOException;
029import java.util.ArrayList;
030import java.util.Collections;
031import java.util.Iterator;
032import java.util.List;
033
034import org.apache.commons.io.filefilter.IOFileFilter;
035import org.apache.commons.io.filefilter.SuffixFileFilter;
036import org.apache.commons.io.filefilter.TrueFileFilter;
037import org.apache.uima.util.FileUtils;
038import org.junit.Assert;
039
040/**
041 * <br>
042 * Copyright (c) 2007-2011, Regents of the University of Colorado <br>
043 * All rights reserved.
044 */
045
046public class LicenseTestUtil {
047
048  public static void testDescriptorFiles(String directoryName) throws IOException {
049    List<String> filesMissingLicense = new ArrayList<String>();
050
051    File directory = new File(directoryName);
052    Iterator<?> files = org.apache.commons.io.FileUtils.iterateFiles(
053        directory,
054        new SuffixFileFilter(".xml"),
055        TrueFileFilter.INSTANCE);
056
057    while (files.hasNext()) {
058      File file = (File) files.next();
059      String fileText = FileUtils.file2String(file);
060
061      if (fileText.indexOf("Copyright (c) ") == -1
062          || fileText.indexOf("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"") == -1) {
063
064        if (file.getName().equals("GENIAcorpus3.02.articleA.pos.xml"))
065          continue;
066        if (file.getParent().equals("src/org/cleartk/descriptor".replace('/', File.separatorChar)))
067          continue;
068        filesMissingLicense.add(file.getPath());
069      }
070    }
071
072    if (filesMissingLicense.size() > 0) {
073      String message = String.format(
074          "%d descriptor files with no license. ",
075          filesMissingLicense.size());
076      System.err.println(message);
077      Collections.sort(filesMissingLicense);
078      for (String path : filesMissingLicense) {
079        System.err.println(path);
080      }
081      Assert.fail(message);
082    }
083  }
084
085  public static void testJavaFiles(String directoryName) throws IOException {
086    testFiles(directoryName, new SuffixFileFilter(".java"));
087  }
088
089  public static void testJavaFiles(
090      String directoryName,
091      List<String> excludePackageNames,
092      List<String> excludeJavaFiles) throws IOException {
093    testFiles(directoryName, new SuffixFileFilter(".java"), excludePackageNames, excludeJavaFiles);
094  }
095
096  public static void testFiles(String directoryName, IOFileFilter fileFilter) throws IOException {
097    List<String> excludePackageNames = Collections.emptyList();
098    List<String> excludeFiles = Collections.emptyList();
099    testFiles(directoryName, fileFilter, excludePackageNames, excludeFiles);
100  }
101
102  public static void testFiles(
103      String directoryName,
104      IOFileFilter fileFilter,
105      List<String> excludePackageNames,
106      List<String> excludeFiles) throws IOException {
107
108    List<String> filesMissingLicense = new ArrayList<String>();
109    File directory = new File(directoryName);
110    Iterator<?> files = org.apache.commons.io.FileUtils.iterateFiles(
111        directory,
112        fileFilter,
113        TrueFileFilter.INSTANCE);
114
115    while (files.hasNext()) {
116      File file = (File) files.next();
117      String fileText = FileUtils.file2String(file);
118
119      if (excludePackage(file, excludePackageNames)) {
120        continue;
121      }
122      if (excludeFile(file, excludeFiles)) {
123        continue;
124      }
125
126      if (fileText.indexOf("Copyright (c) ") == -1
127          || fileText.indexOf("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"") == -1) {
128        filesMissingLicense.add(file.getPath());
129      } else {
130        if (fileText.indexOf("Copyright (c) ", 300) == -1)
131          filesMissingLicense.add(file.getPath());
132      }
133
134    }
135
136    if (filesMissingLicense.size() > 0) {
137      String message = String.format(
138          "%d source files with no license. ",
139          filesMissingLicense.size());
140      System.err.println(message);
141      Collections.sort(filesMissingLicense);
142      for (String path : filesMissingLicense) {
143        System.err.println(path);
144      }
145      Assert.fail(message);
146    }
147
148  }
149
150  private static boolean excludeFile(File file, List<String> excludeFiles) {
151    String fileName = file.getName();
152    return excludeFiles.contains(fileName);
153  }
154
155  private static boolean excludePackage(File file, List<String> excludePackageNames) {
156    File parent = file.getParentFile();
157    while (parent != null) {
158      String parentName = parent.getName();
159      if (excludePackageNames.contains(parentName)) {
160        return true;
161      }
162      parent = parent.getParentFile();
163    }
164    return false;
165  }
166
167  public static void testJavaFilesGPL(String directoryName) throws IOException {
168    testFilesGPL(directoryName, new SuffixFileFilter(".java"));
169  }
170
171  public static void testJavaFilesGPL(
172      String directoryName,
173      List<String> excludePackageNames,
174      List<String> excludeFiles) throws IOException {
175    testFilesGPL(directoryName, new SuffixFileFilter(".java"), excludePackageNames, excludeFiles);
176  }
177
178  public static void testFilesGPL(String directoryName, IOFileFilter fileFilter) throws IOException {
179    List<String> excludePackageNames = Collections.emptyList();
180    List<String> excludeFiles = Collections.emptyList();
181    testFilesGPL(directoryName, fileFilter, excludePackageNames, excludeFiles);
182  }
183
184  public static void testFilesGPL(
185      String directoryName,
186      IOFileFilter fileFilter,
187      List<String> excludePackageNames,
188      List<String> excludeFiles) throws IOException {
189
190    List<String> filesMissingLicense = new ArrayList<String>();
191    File directory = new File(directoryName);
192    Iterator<?> files = org.apache.commons.io.FileUtils.iterateFiles(
193        directory,
194        fileFilter,
195        TrueFileFilter.INSTANCE);
196
197    while (files.hasNext()) {
198      File file = (File) files.next();
199      String fileText = FileUtils.file2String(file);
200
201      if (excludePackage(file, excludePackageNames)) {
202        continue;
203      }
204      if (excludeFile(file, excludeFiles)) {
205        continue;
206      }
207
208      if (fileText.indexOf("Copyright (c) ") == -1
209          || fileText.indexOf("GNU General Public License") == -1) {
210        filesMissingLicense.add(file.getPath());
211      } else {
212        if (fileText.indexOf("Copyright (c) ", 300) == -1)
213          filesMissingLicense.add(file.getPath());
214      }
215
216    }
217
218    if (filesMissingLicense.size() > 0) {
219      String message = String.format(
220          "%d source files with no license. ",
221          filesMissingLicense.size());
222      System.err.println(message);
223      Collections.sort(filesMissingLicense);
224      for (String path : filesMissingLicense) {
225        System.err.println(path);
226      }
227      Assert.fail(message);
228    }
229
230  }
231
232}