View Javadoc
1   package io.jawk;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * Jawk
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2006 - 2026 MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import java.net.URISyntaxException;
26  import java.nio.file.Path;
27  import java.nio.file.Paths;
28  
29  /**
30   * Resolves vendored integration-test resources directly from the repository
31   * checkout instead of relying on the Maven test classpath layout.
32   */
33  public final class CompatibilityTestResources {
34  
35  	private CompatibilityTestResources() {}
36  
37  	/**
38  	 * Resolves the Maven project directory from the compiled location of an
39  	 * integration-test class.
40  	 *
41  	 * @param anchor a class loaded from the current test output
42  	 * @return the project base directory
43  	 */
44  	public static Path projectDirectory(Class<?> anchor) {
45  		try {
46  			Path testClassesDirectory = Paths
47  					.get(anchor.getProtectionDomain().getCodeSource().getLocation().toURI())
48  					.toAbsolutePath()
49  					.normalize();
50  			Path targetDirectory = testClassesDirectory.getParent();
51  			if (targetDirectory == null) {
52  				throw new IllegalStateException("Cannot determine Maven target directory from " + testClassesDirectory);
53  			}
54  			Path projectDirectory = targetDirectory.getParent();
55  			if (projectDirectory == null) {
56  				throw new IllegalStateException("Cannot determine project directory from " + targetDirectory);
57  			}
58  			return projectDirectory;
59  		} catch (URISyntaxException ex) {
60  			throw new IllegalStateException("Cannot resolve project directory", ex);
61  		}
62  	}
63  
64  	/**
65  	 * Resolves a vendored integration-test resource directory relative to
66  	 * {@code src/it/resources}.
67  	 *
68  	 * @param anchor a class loaded from the current test output
69  	 * @param firstSegment the first resource path segment under
70  	 *        {@code src/it/resources}
71  	 * @param additionalSegments any remaining resource path segments
72  	 * @return the resolved resource directory path
73  	 */
74  	public static Path resourceDirectory(Class<?> anchor, String firstSegment, String... additionalSegments) {
75  		Path resourceDirectory = projectDirectory(anchor).resolve(Paths.get("src", "it", "resources", firstSegment));
76  		for (String segment : additionalSegments) {
77  			resourceDirectory = resourceDirectory.resolve(segment);
78  		}
79  		return resourceDirectory;
80  	}
81  }