GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
VersionSemanticsUtil.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.junit.util;
29
30import java.io.File;
31import java.io.IOException;
32import java.net.URISyntaxException;
33import java.util.ArrayList;
34import java.util.List;
35import java.util.Optional;
36
37import org.junit.Assert;
38
39import com.jogamp.common.net.Uri;
40import com.jogamp.common.util.JarUtil;
41import com.jogamp.common.util.VersionNumberString;
42
43import japicmp.cmp.JarArchiveComparator;
44import japicmp.cmp.JarArchiveComparatorOptions;
45import japicmp.config.Options;
46import japicmp.cmp.JApiCmpArchive;
47import japicmp.model.JApiChangeStatus;
48import japicmp.model.JApiClass;
49import japicmp.output.stdout.StdoutOutputGenerator;
50import japicmp.versioning.SemanticVersion;
51
53
54 /**
55 * Library compatibility type. From most compatible to less compatible.
56 */
57 public enum CompatibilityType {
58
59 /**
60 * No (public) changes.
61 */
63
64 /**
65 * Only <i>added</i> and <i>deprecated</i> changes,
66 * i.e. source compatible changes.
67 */
69
70 /**
71 * Contains binary compatible changes,
72 * but may not be fully source compatible
73 * and may contain changed values of fields.
74 */
76
77 /**
78 * Contains non binary compatible changes.
79 */
80 NON_BACKWARD_COMPATIBLE
81 }
82
83 public static void testVersion2(final CompatibilityType expectedCompatibilityType,
84 final File previousJar, final VersionNumberString preVersionNumber,
85 final Class<?> currentJarClazz, final ClassLoader currentJarCL, final VersionNumberString curVersionNumber,
86 final String excludesArgOption, final boolean summaryOnly) throws IllegalArgumentException, URISyntaxException, IOException
87 {
88 // Get containing JAR file "TestJarsInJar.jar" and add it to the TempJarCache
89 final Uri currentJarUri = JarUtil.getJarUri(currentJarClazz.getName(), currentJarCL).getContainedUri();
90 testVersion2(expectedCompatibilityType,
91 previousJar, preVersionNumber,
92 currentJarUri.toFile(), curVersionNumber,
93 excludesArgOption, summaryOnly);
94 }
95
96 public static void testVersion2(final CompatibilityType expectedCompatibilityType,
97 final File previousJar, final VersionNumberString preVersionNumber,
98 final File currentJar, final VersionNumberString curVersionNumber,
99 final String excludesArgOption, final boolean summaryOnly) throws IllegalArgumentException, IOException, URISyntaxException
100 {
101 final Options options = Options.newDefault();
102 options.setSemanticVersioning(true);
103 options.setIgnoreMissingClasses(true);
104 options.setReportOnlySummary(summaryOnly);
105 options.setOutputOnlyBinaryIncompatibleModifications(true);
106 if( null != excludesArgOption && excludesArgOption.length() > 0 ) {
107 options.addExcludeFromArgument(Optional.ofNullable(excludesArgOption), false);
108 }
109
110 final JApiCmpArchive previous = new JApiCmpArchive(previousJar, preVersionNumber.getVersionString());
111 final JApiCmpArchive current = new JApiCmpArchive(currentJar, curVersionNumber.getVersionString());
112 final Optional<SemanticVersion> previousVersion = previous.getVersion().getSemanticVersion();
113 final Optional<SemanticVersion> currentVersion = current.getVersion().getSemanticVersion();
114 final JarArchiveComparatorOptions comparatorOptions = JarArchiveComparatorOptions.of(options);
115 final JarArchiveComparator jarArchiveComparator = new JarArchiveComparator(comparatorOptions);
116
118 final List<JApiClass> jApiClasses1 = jarArchiveComparator.compare(previous, current);
119 final List<JApiClass> jApiClasses2 = new ArrayList<JApiClass>(jApiClasses1.size());
120 for(final JApiClass jApiClass : jApiClasses1) {
121 final boolean unchanged = jApiClass.getChangeStatus() == JApiChangeStatus.UNCHANGED && jApiClass.getChangeStatus() != JApiChangeStatus.MODIFIED;
122 if( !unchanged ) {
123 jApiClasses2.add(jApiClass);
124 switch( detectedCompatibilityType ) {
125 case BACKWARD_COMPATIBLE_IMPLEMENTER:
126 if( !jApiClass.isBinaryCompatible() ) {
127 detectedCompatibilityType = CompatibilityType.NON_BACKWARD_COMPATIBLE;
128 } else if( !jApiClass.isSourceCompatible() ) {
129 detectedCompatibilityType = CompatibilityType.BACKWARD_COMPATIBLE_BINARY;
130 } else {
131 detectedCompatibilityType = CompatibilityType.BACKWARD_COMPATIBLE_SOURCE;
132 }
133 break;
134 case BACKWARD_COMPATIBLE_SOURCE:
135 if( !jApiClass.isBinaryCompatible() ) {
136 detectedCompatibilityType = CompatibilityType.NON_BACKWARD_COMPATIBLE;
137 } else if( !jApiClass.isSourceCompatible() ) {
138 detectedCompatibilityType = CompatibilityType.BACKWARD_COMPATIBLE_BINARY;
139 }
140 break;
141 case BACKWARD_COMPATIBLE_BINARY:
142 if( !jApiClass.isBinaryCompatible() ) {
143 detectedCompatibilityType = CompatibilityType.NON_BACKWARD_COMPATIBLE;
144 }
145 break;
146 case NON_BACKWARD_COMPATIBLE:
147 // NOP
148 break;
149 }
150 }
151 }
152
153 final int comp = detectedCompatibilityType.compareTo(expectedCompatibilityType);
154 final boolean compOK = 0 >= comp;
155 final String compS;
156 if( 0 > comp ) {
157 compS = "< ";
158 } else if ( 0 == comp ) {
159 compS = "==";
160 } else {
161 compS = "> ";
162 }
163
164 System.err.println("Semantic Version Test (japicmp)");
165 System.err.println(" Previous version: "+previousVersion+" - "+previousJar.toString());
166 System.err.println(" Current version: "+currentVersion+" - "+currentJar.toString());
167 System.err.println(" Compat. expected: "+expectedCompatibilityType);
168 System.err.println(" Compat. detected: "+detectedCompatibilityType);
169 System.err.println(" Compat. result: detected "+compS+" expected -> "+(compOK ? "OK" : "ERROR"));
170 final String resS;
171 if( compOK ) {
172 resS = " Current version "+curVersionNumber+" is "+expectedCompatibilityType+" to previous version "+preVersionNumber+", actually "+detectedCompatibilityType;
173 } else {
174 resS = " Current version "+curVersionNumber+" is not "+expectedCompatibilityType+" to previous version "+preVersionNumber+", but "+detectedCompatibilityType;
175 }
176 System.err.println(resS);
177 System.err.printf("%n%n");
178
179 final StdoutOutputGenerator out = new StdoutOutputGenerator(options, jApiClasses2);
180 System.err.println(out.generate());
181 // final MarkdownOptions mdOpts = MarkdownOptions.newDefault(options);
182 // final MarkdownOutputGenerator mdGen = new MarkdownOutputGenerator(mdOpts, jApiClasses2);
183 // System.err.println(mdGen.generate());
184
185 Assert.assertTrue(resS, compOK);
186
187 /***
188 //Provide version number for previous and current Jar files.
189 final Version previous = Version.parse(...);
190 final Version current = Version.parse(...);
191
192 //Validates that current version number is valid based on semantic versioning principles.
193 final boolean compatible = delta.validate(previous, current);
194 */
195 }
196}
This class implements an immutable Uri as defined by RFC 2396.
Definition: Uri.java:160
final File toFile()
If this instance is a file scheme, implementation decodes [ "//"+authority ] + path,...
Definition: Uri.java:1390
final Uri getContainedUri()
If this instance's schemeSpecificPart contains a Uri itself, a sub-Uri, return schemeSpecificPart + #...
Definition: Uri.java:1437
static Uri getJarUri(final String clazzBinName, final ClassLoader cl)
The Class's "com.jogamp.common.GlueGenVersion" Uri jar:sub_protocol:/some/path/gluegen-rt....
Definition: JarUtil.java:139
VersionNumber specialization, holding the versionString this instance is derived from.
static void testVersion2(final CompatibilityType expectedCompatibilityType, final File previousJar, final VersionNumberString preVersionNumber, final Class<?> currentJarClazz, final ClassLoader currentJarCL, final VersionNumberString curVersionNumber, final String excludesArgOption, final boolean summaryOnly)
static void testVersion2(final CompatibilityType expectedCompatibilityType, final File previousJar, final VersionNumberString preVersionNumber, final File currentJar, final VersionNumberString curVersionNumber, final String excludesArgOption, final boolean summaryOnly)
BACKWARD_COMPATIBLE_SOURCE
Only added and deprecated changes, i.e.
BACKWARD_COMPATIBLE_BINARY
Contains binary compatible changes, but may not be fully source compatible and may contain changed va...