|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | +package org.glassfish.jersey.test.artifacts; |
| 17 | + |
| 18 | +import org.apache.maven.model.Model; |
| 19 | +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Properties; |
| 27 | + |
| 28 | +public class ArchetypesTest { |
| 29 | + public static final String[] archetypePoms = { |
| 30 | + "../../archetypes/jersey-example-java8-webapp/src/main/resources/archetype-resources/pom.xml", |
| 31 | + "../../archetypes/jersey-heroku-webapp/src/main/resources/archetype-resources/pom.xml", |
| 32 | + "../../archetypes/jersey-quickstart-grizzly2/src/main/resources/archetype-resources/pom.xml", |
| 33 | + "../../archetypes/jersey-quickstart-webapp/src/main/resources/archetype-resources/pom.xml", |
| 34 | + }; |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testPropertiesVersion() throws XmlPullParserException, IOException { |
| 38 | + Properties properties = MavenUtil.getModelFromFile("../../pom.xml").getProperties(); |
| 39 | +// System.out.println(properties); |
| 40 | + TestResult testResult = new TestResult(); |
| 41 | + for (String pom : archetypePoms) { |
| 42 | + File pomFile = new File(pom); |
| 43 | + Assert.assertTrue("The pom file " + pom + " does not exist", pomFile.exists()); |
| 44 | + Assert.assertTrue("The pom file " + pom + " cannot be read", pomFile.canRead()); |
| 45 | + |
| 46 | + boolean failed = false; |
| 47 | + Model pomModel = MavenUtil.getModelFromFile(pom); |
| 48 | + Properties pomProperties = pomModel.getProperties(); |
| 49 | + for (Map.Entry<Object, Object> pomEntry : pomProperties.entrySet()) { |
| 50 | + if (pomEntry.getKey().equals("jersey.config.test.container.port")) { |
| 51 | + // Skip the following |
| 52 | + continue; |
| 53 | + } |
| 54 | + // Update the names with the ones in Jersey |
| 55 | + Map.Entry<Object, Object> updatedEntry = updateEntry(pomEntry); |
| 56 | + // Check the properties are there |
| 57 | + if (properties.getProperty(updatedEntry.getKey().toString()) == null) { |
| 58 | + testResult.ok().append("Property ") |
| 59 | + .append(pomEntry.getKey().toString()) |
| 60 | + .append(" from ").append(pom).println(" not in Jersey"); |
| 61 | + failed = true; |
| 62 | + } |
| 63 | + // check the values |
| 64 | + else if (!properties.getProperty(updatedEntry.getKey().toString()).equals(updatedEntry.getValue())) { |
| 65 | + testResult.exception().append("The property ") |
| 66 | + .append(pomEntry.getKey().toString()) |
| 67 | + .append(" in archetype pom ") |
| 68 | + .append(pom) |
| 69 | + .append(" not equals Jersey ") |
| 70 | + .println(properties.getProperty(pomEntry.getKey().toString())); |
| 71 | + failed = true; |
| 72 | + } |
| 73 | + } |
| 74 | + if (!failed) { |
| 75 | + testResult.ok().append("The properties in archetype pom ").append(pom).println(" equals Jersey"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (!testResult.result()) { |
| 80 | + Assert.fail(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private Map.Entry<Object, Object> updateEntry(Map.Entry<Object, Object> pomEntry) { |
| 85 | + if (pomEntry.getKey().equals("junit-jupiter.version")) { |
| 86 | + return new Map.Entry<Object, Object>() { |
| 87 | + @Override |
| 88 | + public Object getKey() { |
| 89 | + return "junit5.version"; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Object getValue() { |
| 94 | + return pomEntry.getValue(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Object setValue(Object value) { |
| 99 | + return value; |
| 100 | + } |
| 101 | + }; |
| 102 | + } |
| 103 | + return pomEntry; |
| 104 | + } |
| 105 | +} |
0 commit comments