| /******************************************************************************* |
| * Copyright (c) 2010 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.wtp.layout.tests; |
| |
| import java.io.File; |
| import java.io.FileWriter; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.net.URI; |
| import java.net.URISyntaxException; |
| import java.util.ArrayList; |
| import java.util.Collection; |
| import java.util.Collections; |
| import java.util.Iterator; |
| import java.util.List; |
| import java.util.Properties; |
| |
| import junit.framework.Test; |
| import junit.framework.TestCase; |
| import junit.framework.TestSuite; |
| |
| import org.eclipse.core.runtime.OperationCanceledException; |
| import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
| import org.eclipse.equinox.internal.p2.metadata.License; |
| import org.eclipse.equinox.p2.core.IProvisioningAgent; |
| import org.eclipse.equinox.p2.core.ProvisionException; |
| import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| import org.eclipse.equinox.p2.metadata.ILicense; |
| import org.eclipse.equinox.p2.query.IQueryResult; |
| import org.eclipse.equinox.p2.query.QueryUtil; |
| import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; |
| import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; |
| import org.eclipse.wtp.releng.tests.TestActivator; |
| |
| /** |
| * Tests that licenses in the helios repository are consistent with the |
| * platform feature license. |
| * |
| * This was based on test attached to bug 306627 |
| * https://bugs.eclipse.org/bugs/show_bug.cgi?id=306627 |
| */ |
| public class TestRepo extends TestCase { |
| static final String defaultURLToTest = "http://download.eclipse.org/releases/staging"; |
| |
| public static Test suite() { |
| System.out.println("suite for layout"); |
| return new TestSuite(TestRepo.class); |
| } |
| |
| private static final String EOL = System.getProperty("line.separator", "\n"); |
| |
| |
| public void testLicenses() throws URISyntaxException, ProvisionException, OperationCanceledException, IOException { |
| String repoURL = System.getProperty("repoURLToTest"); |
| System.out.println("repoURLToTest: " + repoURL); |
| if (repoURL == null) { |
| repoURL = defaultURLToTest; |
| } |
| System.out.println("repoURLToTest: " + repoURL); |
| // String referenceFeature = System.getProperty("referenceFeature"); |
| // System.out.println("referenceFeature: " + referenceFeature); |
| // if (referenceFeature == null) { |
| // referenceFeature = defaultReferenceFeature; |
| // } |
| // System.out.println("referenceFeature: " + referenceFeature); |
| URI repoLocation = new URI(repoURL); |
| IMetadataRepository repo = getMetadataRepositoryManager().loadRepository(repoLocation, null); |
| if (repo == null) { |
| System.out.println("no repository found at " + repoLocation.toString()); |
| } |
| else { |
| |
| |
| IQueryResult<IInstallableUnit> allFeatures = repo.query(QueryUtil.createIUGroupQuery(), null); |
| // IQueryResult<IInstallableUnit> platform = |
| // allFeatures.query(QueryUtil.createIUQuery(referenceFeature), |
| // null); |
| assertFalse(allFeatures.isEmpty()); |
| // if (platform == null) { |
| // System.out.println("did not find reference feature: " + referenceFeature); |
| // System.out.println("list of existing features: "); |
| // Iterator<IInstallableUnit> itFeatures = allFeatures.iterator(); |
| // while (itFeatures.hasNext()) { |
| // System.out.println(itFeatures.next()); |
| // } |
| // } |
| // assertNotNull(platform); |
| // assertFalse(platform.isEmpty()); |
| // IInstallableUnit platformFeature = platform.iterator().next(); |
| // ILicense platformLicense = |
| // platformFeature.getLicenses(null).iterator().next(); |
| |
| Properties properties = new Properties(); |
| InputStream inStream = this.getClass().getResourceAsStream("standard.properties"); |
| properties.load(inStream); |
| String body = properties.getProperty("license"); |
| ILicense standardLicense = new License(null, body, null); |
| |
| List<IInstallableUnit> noLicense = new ArrayList<IInstallableUnit>(); |
| List<IInstallableUnit> extraLicense = new ArrayList<IInstallableUnit>(); |
| List<IInstallableUnit> goodLicense = new ArrayList<IInstallableUnit>(); |
| List<IInstallableUnit> badLicense = new ArrayList<IInstallableUnit>(); |
| checkLicenses(standardLicense, allFeatures, goodLicense, badLicense, noLicense, extraLicense); |
| |
| printReport(goodLicense, badLicense, noLicense, extraLicense); |
| } |
| } |
| |
| private void printReport(List<IInstallableUnit> goodLicense, List<IInstallableUnit> badLicense, List<IInstallableUnit> noLicense, List<IInstallableUnit> extraLicense) { |
| String SPACER = "<br />======================="; |
| |
| |
| |
| FileWriter outfileWriter = null; |
| File outfile = null; |
| String testDirName = System.getProperty("junit-report-output"); |
| try { |
| outfile = new File(testDirName, "licenseConsisency.html"); |
| outfileWriter = new FileWriter(outfile); |
| |
| |
| |
| println(outfileWriter, "<br /><br />Summary:" + SPACER); |
| println(outfileWriter, "Features with conforming license: " + goodLicense.size()); |
| println(outfileWriter, "Features with different license: " + badLicense.size()); |
| println(outfileWriter, "Features with no license: " + noLicense.size()); |
| println(outfileWriter, "Features with extra licenses: " + extraLicense.size()); |
| println(outfileWriter, "======================="); |
| |
| println(outfileWriter, "<br /><br />Details:" + SPACER); |
| |
| println(outfileWriter, "Features with no license:" + SPACER); |
| for (IInstallableUnit unit : sort(noLicense)) { |
| println(outfileWriter, unit.getId()); |
| } |
| |
| println(outfileWriter, "<br /><br />Features with different license:" + SPACER); |
| for (IInstallableUnit unit : sort(badLicense)) { |
| println(outfileWriter, unit.getId()); |
| } |
| |
| println(outfileWriter, "<br /><br />Features with matching license:" + SPACER); |
| for (IInstallableUnit unit : sort(goodLicense)) { |
| println(outfileWriter, unit.getId()); |
| } |
| } |
| catch (IOException e) { |
| e.printStackTrace(); |
| } |
| finally { |
| if (outfileWriter != null) { |
| try { |
| outfileWriter.close(); |
| } |
| catch (IOException e) { |
| // weirdness |
| e.printStackTrace(); |
| } |
| } |
| } |
| if (badLicense.size() > 0 || extraLicense.size() > 0 || noLicense.size() > 0) { |
| fail("Errors in license consistency. For list, see " + outfile.getAbsolutePath()); |
| } |
| } |
| |
| private void println(FileWriter out, String string) throws IOException { |
| out.write("<p>" + string + "</p>" + EOL); |
| |
| } |
| |
| private List<IInstallableUnit> sort(List<IInstallableUnit> noLicense) { |
| Collections.sort(noLicense); |
| return noLicense; |
| } |
| |
| private void checkLicenses(ILicense platformLicense, IQueryResult<IInstallableUnit> allFeatures, List<IInstallableUnit> goodLicense, List<IInstallableUnit> badLicense, List<IInstallableUnit> noLicense, List<IInstallableUnit> extraLicense) { |
| for (IInstallableUnit feature : allFeatures.toUnmodifiableSet()) { |
| // ignore groups that are not features |
| if (!feature.getId().endsWith(".feature.group")) |
| continue; |
| Collection<ILicense> licenses = feature.getLicenses(null); |
| if (licenses.isEmpty()) { |
| noLicense.add(feature); |
| continue; |
| } |
| if (licenses.size() != 1) { |
| extraLicense.add(feature); |
| continue; |
| } |
| ILicense featureLicense = licenses.iterator().next(); |
| if (!platformLicense.getUUID().equals(featureLicense.getUUID())) { |
| badLicense.add(feature); |
| continue; |
| } |
| goodLicense.add(feature); |
| } |
| } |
| |
| private void checkProviderNames(IQueryResult<IInstallableUnit> allIUs) throws IOException { |
| FileWriter outfileWriter = null; |
| File outfile = null; |
| List<IInstallableUnit> incorrectProviderName = new ArrayList<IInstallableUnit>(); |
| String testDirName = System.getProperty("junit-report-output"); |
| try { |
| outfile = new File(testDirName, "incorrectProviderNames.html"); |
| outfileWriter = new FileWriter(outfile); |
| |
| for (IInstallableUnit iu : allIUs.toUnmodifiableSet()) { |
| String providerName = iu.getProperty(IInstallableUnit.PROP_PROVIDER, null); |
| if (!"Eclipse Web Tools Platform".equals(providerName)) { |
| incorrectProviderName.add(iu); |
| String iuId = iu.getId(); |
| String iuVersion = iu.getVersion().toString(); |
| println(outfileWriter, iuId + " " + iuVersion + " " + providerName); |
| } |
| } |
| if (incorrectProviderName.size() > 0) { |
| fail("Errors in consistent, correct provider name. For list, see " + outfile.getAbsolutePath()); |
| } |
| } |
| finally { |
| if (outfileWriter != null) { |
| try { |
| outfileWriter.close(); |
| } |
| catch (IOException e) { |
| // would be weird |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
| |
| public void testProviderNames() throws URISyntaxException, ProvisionException, OperationCanceledException, IOException { |
| String repoURL = System.getProperty("repoURLToTest"); |
| |
| if (repoURL == null) { |
| repoURL = defaultURLToTest; |
| } |
| System.out.println("repoURLToTest: " + repoURL); |
| URI repoLocation = new URI(repoURL); |
| IMetadataRepository repo = getMetadataRepositoryManager().loadRepository(repoLocation, null); |
| if (repo == null) { |
| System.out.println("no repository found at " + repoLocation.toString()); |
| } |
| else { |
| IQueryResult<IInstallableUnit> allIUs = repo.query(QueryUtil.createIUAnyQuery(), null); |
| assertFalse(allIUs.isEmpty()); |
| checkProviderNames(allIUs); |
| } |
| } |
| |
| protected static IMetadataRepositoryManager getMetadataRepositoryManager() { |
| return (IMetadataRepositoryManager) getAgent().getService(IMetadataRepositoryManager.SERVICE_NAME); |
| } |
| |
| protected static IProvisioningAgent getAgent() { |
| // get the global agent for the currently running system |
| return (IProvisioningAgent) ServiceHelper.getService(TestActivator.getContext(), IProvisioningAgent.SERVICE_NAME); |
| } |
| } |