|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.gradle.p2; |
| 17 | + |
| 18 | + |
| 19 | +import com.diffplug.gradle.GradleIntegrationTest; |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.regex.Matcher; |
| 23 | +import java.util.regex.Pattern; |
| 24 | +import org.junit.Assert; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +public class CategoryPublisherTest extends GradleIntegrationTest { |
| 28 | + |
| 29 | + // Eclipse version used for application execution |
| 30 | + private static String ECLIPSE_PDE_VERSION = "4.8.0"; |
| 31 | + |
| 32 | + private static String FEATURE_ID = "goomph.test.feature"; |
| 33 | + private static String FEATURE_VERSION = "1.1.1"; |
| 34 | + private static String FEATURE_JAR_NAME = FEATURE_ID + "_" + FEATURE_VERSION + ".jar"; |
| 35 | + |
| 36 | + private static String PLUGIN_NAME = "goomph.test.plugin"; |
| 37 | + private static String PLUGIN_VERSION = "0.2.0"; |
| 38 | + private static String PLUGIN_JAR_NAME = PLUGIN_NAME + "_" + PLUGIN_VERSION + ".jar"; |
| 39 | + |
| 40 | + // Directory used as target of the applications |
| 41 | + private static String PROJECT_DIR_PATH = "project"; |
| 42 | + private static String PLUGINS_DIR_PATH = PROJECT_DIR_PATH + "/plugins"; |
| 43 | + private static String FEATURES_DIR_PATH = PROJECT_DIR_PATH + "/features"; |
| 44 | + |
| 45 | + private static String CATEGORY_FILE_PATH = "category/category.xml"; |
| 46 | + private static String CATEGORY_NAME = "TestCategory"; |
| 47 | + |
| 48 | + private static String PUBLISH_CATEGORY_TASK_NAME = "publishCategory"; |
| 49 | + private static String PUBLISH_FEATURES_AND_BUNDLES_TASK_NAME = "publishFeaturesAndBundles"; |
| 50 | + |
| 51 | + /** |
| 52 | + * Tests the update site creation using the {@see FeaturesAndBundlesPublisher} |
| 53 | + * and {@see CategoryPublisher} |
| 54 | + **/ |
| 55 | + @Test |
| 56 | + public void testCreateUpdateSite() throws IOException { |
| 57 | + |
| 58 | + write( |
| 59 | + "build.gradle", |
| 60 | + "plugins {", |
| 61 | + " id 'com.diffplug.p2.asmaven'", |
| 62 | + "}", |
| 63 | + "import com.diffplug.gradle.pde.EclipseRelease", |
| 64 | + "import com.diffplug.gradle.p2.CategoryPublisher", |
| 65 | + "import com.diffplug.gradle.p2.FeaturesAndBundlesPublisher", |
| 66 | + "tasks.register('testProjectJar', Jar) {", |
| 67 | + " archiveFileName = 'test.jar'", |
| 68 | + " destinationDirectory = file('" + PLUGINS_DIR_PATH + "')", |
| 69 | + " manifest{attributes('Bundle-SymbolicName': '" + PLUGIN_NAME + "', 'Bundle-Version': '" + PLUGIN_VERSION + "')}", |
| 70 | + "}", |
| 71 | + "tasks.register('" + PUBLISH_FEATURES_AND_BUNDLES_TASK_NAME + "') {", |
| 72 | + " dependsOn('testProjectJar')", |
| 73 | + " doLast {", |
| 74 | + " new FeaturesAndBundlesPublisher().with {", |
| 75 | + " source(file('" + PROJECT_DIR_PATH + "'))", |
| 76 | + " inplace()", |
| 77 | + " append()", |
| 78 | + " publishArtifacts()", |
| 79 | + " runUsingBootstrapper()", |
| 80 | + " }", |
| 81 | + " }", |
| 82 | + "}", |
| 83 | + "tasks.register('" + PUBLISH_CATEGORY_TASK_NAME + "') {", |
| 84 | + " doLast {", |
| 85 | + " new CategoryPublisher(EclipseRelease.official('" + ECLIPSE_PDE_VERSION + "')).with {", |
| 86 | + " metadataRepository(file('" + PROJECT_DIR_PATH + "'))", |
| 87 | + " categoryDefinition(file('" + CATEGORY_FILE_PATH + "'))", |
| 88 | + " runUsingPdeInstallation()", |
| 89 | + " }", |
| 90 | + " }", |
| 91 | + "}"); |
| 92 | + |
| 93 | + folder.newFolder(PLUGINS_DIR_PATH); |
| 94 | + folder.newFolder(FEATURES_DIR_PATH); |
| 95 | + |
| 96 | + writeFeatureXml(); |
| 97 | + writeCategoryDefinition(); |
| 98 | + |
| 99 | + /* Execute FeaturesAndBundlesPublisher using the file structure: |
| 100 | + * project |
| 101 | + * - features |
| 102 | + * - feature.xml |
| 103 | + * - plugins |
| 104 | + * - test.jar // created by task 'testProjectJar' |
| 105 | + */ |
| 106 | + gradleRunner().forwardOutput().withArguments(PUBLISH_FEATURES_AND_BUNDLES_TASK_NAME).build(); |
| 107 | + |
| 108 | + // Verify result of FeaturesAndBundlesPublisher application execution |
| 109 | + String artifactsXml = read(PROJECT_DIR_PATH + "/artifacts.xml"); |
| 110 | + Assert.assertTrue("FeaturesAndBundles application does not found plugin specified in features.xml", |
| 111 | + artifactsXml.contains("id='goomph.test.plugin'")); |
| 112 | + Assert.assertTrue("FeaturesAndBundles application does not create a feature jar", |
| 113 | + new File(folder.getRoot(), FEATURES_DIR_PATH + '/' + FEATURE_JAR_NAME).exists()); |
| 114 | + Assert.assertTrue("FeaturesAndBundles application does not create a plugin jar", |
| 115 | + new File(folder.getRoot(), PLUGINS_DIR_PATH + '/' + PLUGIN_JAR_NAME).exists()); |
| 116 | + |
| 117 | + Pattern categoryContextPattern = Pattern.compile("<property\\s+name='org.eclipse.equinox.p2.name'\\s+value='" + CATEGORY_NAME + "'\\s*/>\\s*" + |
| 118 | + "<property\\s+name='org.eclipse.equinox.p2.type.category'\\s+value='true'\\s*/>"); |
| 119 | + |
| 120 | + String contentXML = read(PROJECT_DIR_PATH + "/content.xml"); |
| 121 | + Matcher m = categoryContextPattern.matcher(contentXML); |
| 122 | + Assert.assertFalse("content.xml already contains category metadata", m.find()); |
| 123 | + |
| 124 | + // Execute CategoryPublisher |
| 125 | + gradleRunner().forwardOutput().withArguments(PUBLISH_CATEGORY_TASK_NAME).build(); |
| 126 | + |
| 127 | + // Verify result of CategoryPublisher application execution |
| 128 | + contentXML = read(PROJECT_DIR_PATH + "/content.xml"); |
| 129 | + m = categoryContextPattern.matcher(contentXML); |
| 130 | + Assert.assertTrue("CategoryPublisher application does not add the category metadata to content.xml", m.find()); |
| 131 | + } |
| 132 | + |
| 133 | + private void writeFeatureXml() throws IOException { |
| 134 | + write(FEATURES_DIR_PATH + "/feature.xml", |
| 135 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", |
| 136 | + "<feature id=\"" + FEATURE_ID + "\" label=\"test\" version=\"" + FEATURE_VERSION + "\">", |
| 137 | + " <description></description>", |
| 138 | + " <plugin id=\"" + PLUGIN_NAME + "\"", |
| 139 | + " download-size=\"0\"", |
| 140 | + " install-size=\"0\"", |
| 141 | + " version=\"" + PLUGIN_VERSION + "\"", |
| 142 | + " unpack=\"false\"/>", |
| 143 | + "</feature>"); |
| 144 | + } |
| 145 | + |
| 146 | + private void writeCategoryDefinition() throws IOException { |
| 147 | + write(CATEGORY_FILE_PATH, |
| 148 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", |
| 149 | + "<site>", |
| 150 | + " <description url=\"empty url\"></description>", |
| 151 | + " <feature url=\"\"", |
| 152 | + " id=\"" + FEATURE_ID + "\"", |
| 153 | + " version=\"" + FEATURE_VERSION + "\">", |
| 154 | + " <category name=\"test\"/>", |
| 155 | + " </feature>", |
| 156 | + " <category-def name=\"test\"", |
| 157 | + " label=\"" + CATEGORY_NAME + "\"/>", |
| 158 | + "</site>"); |
| 159 | + } |
| 160 | +} |
0 commit comments