From e1e139919dce9381955ec9094518fb11d02001ff Mon Sep 17 00:00:00 2001 From: antelder Date: Sat, 8 Aug 2009 08:40:54 +0000 Subject: Start porting the 2.x distribution license legal check testcase from 2.x to 1.x git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@802321 13f79535-47bb-0310-9956-ffa450edef68 --- .../itest/distro-license-check/pom.xml | 51 ++++++++ .../src/test/java/itest/LicenseTestCase.java | 144 +++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 branches/sca-java-1.x/itest/distro-license-check/pom.xml create mode 100644 branches/sca-java-1.x/itest/distro-license-check/src/test/java/itest/LicenseTestCase.java (limited to 'branches') diff --git a/branches/sca-java-1.x/itest/distro-license-check/pom.xml b/branches/sca-java-1.x/itest/distro-license-check/pom.xml new file mode 100644 index 0000000000..212c4bb0e6 --- /dev/null +++ b/branches/sca-java-1.x/itest/distro-license-check/pom.xml @@ -0,0 +1,51 @@ + + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-itest + 1.6-SNAPSHOT + ../pom.xml + + itest-distro-license-check + Apache Tuscany SCA iTest Distribution License Check + + + + org.apache.tuscany.sca + tuscany-distribution + 1.6-SNAPSHOT + pom + + + + junit + junit + 4.5 + test + + + + + + ${artifactId} + + diff --git a/branches/sca-java-1.x/itest/distro-license-check/src/test/java/itest/LicenseTestCase.java b/branches/sca-java-1.x/itest/distro-license-check/src/test/java/itest/LicenseTestCase.java new file mode 100644 index 0000000000..9bcd3c3a93 --- /dev/null +++ b/branches/sca-java-1.x/itest/distro-license-check/src/test/java/itest/LicenseTestCase.java @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package itest; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.StringTokenizer; +import java.util.zip.ZipEntry; +import java.util.zip.ZipException; +import java.util.zip.ZipFile; + +import junit.framework.TestCase; + +public class LicenseTestCase extends TestCase { + + public void testCreateComponent() throws ZipException, IOException { + + File archive = new File("..\\..\\distribution\\target\\apache-tuscany-sca-1.6-SNAPSHOT.zip"); + ZipFile zf = new ZipFile(archive); + try { + + String licenstText = getLicenseText(zf); + + List jarsInArchive = getJarsInDistro(zf); + + List jarsNotInLicense = getJarsNotInLicense(licenstText, jarsInArchive); + if (jarsNotInLicense.size() > 0) { + System.out.println("jarsNotInLicense: " + jarsNotInLicense); + } + + List jarsNotInArchive = getJarsNotInArchive(licenstText, jarsInArchive); + if (jarsNotInArchive.size() > 0) { + System.out.println("jarsNotInArchive: " + jarsNotInArchive); + } + + assertTrue("License errors, check log for details", jarsNotInArchive.size()==0 && jarsNotInLicense.size()==0); + + } finally { + zf.close(); + } + } + + private List getJarsNotInLicense(String licenstText, List jarsInArchive) { + List jarsNotInLicense = new ArrayList(); + for (String jarName : jarsInArchive) { + if (!licenseHasJar(licenstText, jarName)) { + jarsNotInLicense.add(jarName); + } + } + return jarsNotInLicense; + } + + private List getJarsNotInArchive(String licenstText, List jarsInArchive) throws IOException { + List jarsNotInArchive = new ArrayList(); + BufferedReader reader = new BufferedReader(new StringReader(licenstText)); + String line = null; + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.contains(".jar")) { + StringTokenizer st = new StringTokenizer(line); + while (st.hasMoreTokens()) { + String s = st.nextToken(); + if (s.contains(".jar")) { + if (s.startsWith("(")) { + s = s.substring(1); + } + if (s.endsWith(",") || s.endsWith(":")) { + s = s.substring(0, s.length()-1); + } + if (s.endsWith(")")) { + s = s.substring(0, s.length()-1); + } + if (!jarsInArchive.contains(s) && !s.startsWith("tuscany-")) { + jarsNotInArchive.add(s); + } + } + } + } + } + return jarsNotInArchive; + } + + private boolean licenseHasJar(String licenstText, String jarName) { + return !licenstText.contains(jarName); + } + + private String getLicenseText(ZipFile zf) throws IOException { + ZipEntry ze = zf.getEntry("tuscany-sca-1.6-SNAPSHOT/LICENSE"); + InputStream in = zf.getInputStream(ze); + String l = readLICENSE(in); + return l; + } + + private List getJarsInDistro(ZipFile zf) { + ZipEntry ze; + List jarsInArchive = new ArrayList(); + for (Enumeration e = zf.entries(); e.hasMoreElements();) { + ze = e.nextElement(); + String name = ze.getName(); + if (name.endsWith(".jar")) { + jarsInArchive.add(name); + } + } + System.out.println("jarsInArchive: " + jarsInArchive.size()); + return jarsInArchive; + } + + private static String readLICENSE(InputStream in) throws java.io.IOException { + StringBuffer fileData = new StringBuffer(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + char[] buf = new char[1024]; + int numRead = 0; + while ((numRead = reader.read(buf)) != -1) { + String readData = String.valueOf(buf, 0, numRead); + fileData.append(readData); + buf = new char[1024]; + } + reader.close(); + return fileData.toString(); + } +} -- cgit v1.2.3