]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/contrib/thrift-maven-plugin/src/test/java/org/apache/thrift/maven/TestAbstractThriftMojo.java
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / contrib / thrift-maven-plugin / src / test / java / org / apache / thrift / maven / TestAbstractThriftMojo.java
CommitLineData
f67539c2
TL
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.thrift.maven;
20
21import com.google.common.collect.Lists;
22import com.google.common.collect.Sets;
23import org.apache.maven.artifact.repository.ArtifactRepository;
24import org.junit.Before;
25import org.junit.Test;
26import org.mockito.Mockito;
27
28import java.io.File;
29import java.util.Set;
30
31import static junit.framework.TestCase.assertEquals;
32
33
34public class TestAbstractThriftMojo {
35
36 private ThriftTestCompileMojo mojo;
37 private File testRootDir;
38 private ArtifactRepository mavenRepository;
39
40
41 @Before
42 public void setUp() throws Exception {
43 final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
44 testRootDir = new File(tmpDir, "thrift-test");
45
46 // the truncatePath method assumes a maven repository, but it only cares about the base dir
47 mavenRepository = Mockito.mock(ArtifactRepository.class);
48 Mockito.when(mavenRepository.getBasedir()).thenReturn("/test/maven/repo/basedir");
49
50 mojo = new ThriftTestCompileMojo();
51 mojo.setLocalMavenRepository(mavenRepository);
52 }
53
54 @Test
55 public void testMakeThriftPathFromJars() throws Throwable {
56 final File temporaryThriftFileDirectory = testRootDir;
57
58 // The SharedIdl.jar file contains the same idl/shared.thrift and idl/tutorial.thrift hierarchy
59 // used by other tests. It's used here to represent a dependency of the project maven is building,
60 // one that is contributing .thrift IDL files as well as any other artifacts.
61 final Iterable<File> classpathElementFiles = Lists.newArrayList(
62 new File("target/SharedIdl.jar")
63 );
64
65 final Set<File> thriftDirectories = mojo.makeThriftPathFromJars(temporaryThriftFileDirectory, classpathElementFiles);
66
67 // The results should be a path to a directory named after the JAR itself (assuming no path hashing,
68 // but see below for a separate test of that) representing the root of a hierarchy containing thrift
69 // files, suitable for providing to the thrift compiler as an include directory. In this case, that
70 // means it points to the directory containing the "idl" hierarchy rather than to the idl directory
71 // itself.
72 final Set<File> expected = Sets.newHashSet(
73 new File(testRootDir, "target/SharedIdl.jar")
74 );
75
76 assertEquals("makeThriftPathFromJars should return thrift IDL base path from within JAR", expected, thriftDirectories);
77 }
78
79 @Test
80 public void testTruncatePath() throws Throwable {
81 // JAR path is unrelated to maven repo, and should be unchanged
82 assertEquals("/path/to/somejar.jar", mojo.truncatePath("/path/to/somejar.jar"));
83
84 // JAR path is within maven repo, and should be made relative to the repo
85 assertEquals("path/to/somejar.jar", mojo.truncatePath("/test/maven/repo/basedir/path/to/somejar.jar"));
86
87 // JAR path contains forward slashes that should be normalized
88 assertEquals("/path/to/somejar.jar", mojo.truncatePath("\\path\\to\\somejar.jar"));
89 }
90
91 @Test
92 public void testTruncatePathWithDependentPathHashing() throws Throwable {
93 mojo.setHashDependentPaths(true);
94
95 // hashDependentPaths set to true, the JAR path is immediately hashed (MD5) and converted to a hex string
96
97 assertEquals("1c85950987b23493462cf3c261d9510a", mojo.truncatePath("/path/to/somejar.jar"));
98 assertEquals("39fc2b4c34cb6cb0da38bed5d8b5fc67", mojo.truncatePath("/test/maven/repo/basedir/path/to/somejar.jar"));
99 assertEquals("25b6924f5b0e19486d0ff88448e999d5", mojo.truncatePath("\\path\\to\\somejar.jar"));
100 }
101
102}