1 package com.simpligility.maven.plugins.android.common;
2
3 import com.simpligility.maven.plugins.android.AndroidNdk;
4 import com.simpligility.maven.plugins.android.common.AndroidExtension;
5 import com.simpligility.maven.plugins.android.common.Const;
6 import com.simpligility.maven.plugins.android.common.NativeHelper;
7
8 import org.apache.maven.artifact.Artifact;
9 import org.apache.maven.artifact.DefaultArtifact;
10 import org.apache.maven.plugin.MojoExecutionException;
11 import org.apache.maven.plugin.testing.SilentLog;
12 import org.apache.maven.plugin.testing.stubs.ArtifactStub;
13 import org.apache.maven.project.MavenProject;
14 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
15 import org.apache.maven.shared.dependency.graph.internal.DefaultDependencyGraphBuilder;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Rule;
19 import org.junit.Test;
20 import org.junit.rules.TemporaryFolder;
21
22 import java.io.File;
23 import java.util.Collections;
24 import java.util.Set;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31
32
33
34 public class NativeHelperTest {
35
36 @Rule
37 public TemporaryFolder apklibDir = new TemporaryFolder();
38
39 private NativeHelper nativeHelper;
40
41 @Before
42 public void setupNativeHelper() {
43 MavenProject project = new MavenProject();
44 project.setDependencyArtifacts(Collections.<Artifact>emptySet());
45
46 ArtifactStub apklib = new ArtifactStub() {
47 @Override
48 public String getId() {
49 return getArtifactId();
50 }
51 };
52 apklib.setArtifactId("some-apklib");
53 apklib.setGroupId("group");
54 apklib.setType(AndroidExtension.APKLIB);
55 project.addAttachedArtifact(apklib);
56
57 final DependencyGraphBuilder dependencyGraphBuilder = new DefaultDependencyGraphBuilder();
58 nativeHelper = new NativeHelper(project, dependencyGraphBuilder, new SilentLog());
59 }
60
61 @Test
62 public void shouldNotIncludeLibsFolderAsNativeDependenciesSourceWhenNoNativeLibsInside() throws Exception {
63 new File(apklibDir.getRoot(), "some-apklib/libs").mkdirs();
64 new File(apklibDir.getRoot(), "some-apklib/libs/some.jar").createNewFile();
65
66 Set<Artifact> nativeDependencies = nativeHelper.getNativeDependenciesArtifacts( null, apklibDir.getRoot(), true);
67
68 assertTrue("Included JARs as native dependencies, but shouldn't", nativeDependencies.isEmpty());
69 }
70
71 @Test
72 public void shouldIncludeLibsFolderAsNativeDependenciesSourceWhenNativeLibsInside() throws Exception {
73 new File(apklibDir.getRoot(), "some-apklib/libs").mkdirs();
74 new File(apklibDir.getRoot(), "some-apklib/libs/some.jar").createNewFile();
75 new File(apklibDir.getRoot(), "some-apklib/libs/some.so").createNewFile();
76
77 Set<Artifact> nativeDependencies = nativeHelper.getNativeDependenciesArtifacts( null, apklibDir.getRoot(), true);
78
79 assertTrue("Included attached native artifacts, but shouldn't", nativeDependencies.isEmpty());
80 }
81
82 @Test
83 public void architectureResolutionForPlainArchitectureClassifier() throws Exception
84 {
85 for (String ndkArchitecture : AndroidNdk.NDK_ARCHITECTURES)
86 {
87 Artifact artifact = new DefaultArtifact("acme", "acme", "1.0", "runtime", Const.ArtifactType.NATIVE_SYMBOL_OBJECT, ndkArchitecture, null);
88 String architecture = NativeHelper.extractArchitectureFromArtifact(artifact, "armeabi");
89 assertNotNull("unexpected null architecture", architecture);
90 assertEquals("unexpected architecture", ndkArchitecture, architecture);
91 }
92
93 }
94
95 @Test
96 public void architectureResolutionForMixedArchitectureClassifier() throws Exception
97 {
98 for (String ndkArchitecture : AndroidNdk.NDK_ARCHITECTURES)
99 {
100 Artifact artifact = new DefaultArtifact("acme", "acme", "1.0", "runtime", Const.ArtifactType.NATIVE_SYMBOL_OBJECT, ndkArchitecture + "-acme", null);
101 String architecture = NativeHelper.extractArchitectureFromArtifact(artifact, "armeabi");
102 assertNotNull("unexpected null architecture", architecture);
103 assertEquals("unexpected architecture", ndkArchitecture, architecture);
104 }
105 }
106
107 @Test
108 public void architectureResolutionForDefaultLegacyArchitectureClassifier() throws Exception
109 {
110 Artifact artifact = new DefaultArtifact("acme", "acme", "1.0", "runtime", Const.ArtifactType.NATIVE_SYMBOL_OBJECT, "acme", null);
111 String architecture = NativeHelper.extractArchitectureFromArtifact(artifact, "armeabi");
112 assertNotNull("unexpected null architecture", architecture);
113 assertEquals("unexpected architecture", "armeabi", architecture);
114 }
115
116 @Test
117 public void artifactHasHardwareArchitecture() throws Exception
118 {
119 for (String ndkArchitecture : AndroidNdk.NDK_ARCHITECTURES)
120 {
121 Artifact artifact = new DefaultArtifact("acme", "acme", "1.0", "runtime", Const.ArtifactType.NATIVE_SYMBOL_OBJECT, ndkArchitecture, null);
122 boolean value = NativeHelper.artifactHasHardwareArchitecture(artifact, ndkArchitecture, "armeabi");
123 assertTrue("unexpected value", value);
124 }
125 }
126
127 @Test
128 public void artifactHasHardwareArchitectureWithClassifier() throws Exception
129 {
130 for (String ndkArchitecture : AndroidNdk.NDK_ARCHITECTURES)
131 {
132 Artifact artifact = new DefaultArtifact("acme", "acme", "1.0", "runtime", Const.ArtifactType.NATIVE_SYMBOL_OBJECT, ndkArchitecture + "-acme", null);
133 boolean value = NativeHelper.artifactHasHardwareArchitecture(artifact, ndkArchitecture, "armeabi");
134 assertTrue("unexpected value", value);
135 }
136 }
137
138 @Test
139 public void artifactHasHardwareArchitectureWithDefaultLegacyClassifier() throws Exception
140 {
141 Artifact artifact = new DefaultArtifact("acme", "acme", "1.0", "runtime", Const.ArtifactType.NATIVE_SYMBOL_OBJECT, "acme", null);
142 boolean value = NativeHelper.artifactHasHardwareArchitecture(artifact, "armeabi", "armeabi");
143 assertTrue("unexpected value", value);
144 }
145
146 @Test
147 public void artifactHasHardwareArchitectureNotNativeLibrary() throws Exception
148 {
149 Artifact artifact = new DefaultArtifact("acme", "acme", "1.0", "runtime", "jar", "armeabi", null);
150 boolean value = NativeHelper.artifactHasHardwareArchitecture(artifact, "armeabi", "armeabi");
151 assertFalse("unexpected value", value);
152 }
153
154 }