2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang2sources.plugin;
10 import java.io.BufferedInputStream;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.nio.file.Paths;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.List;
24 import java.util.jar.Attributes;
25 import java.util.jar.JarEntry;
26 import java.util.jar.JarOutputStream;
27 import java.util.jar.Manifest;
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
31 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
32 import org.apache.maven.model.Dependency;
33 import org.apache.maven.model.Plugin;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.repository.RepositorySystem;
36 import org.junit.Assert;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mockito;
40 import org.mockito.runners.MockitoJUnitRunner;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 import org.opendaylight.yangtools.yang2sources.plugin.Util.ContextHolder;
45 import org.opendaylight.yangtools.yang2sources.plugin.Util.YangsInZipsResult;
47 @RunWith(MockitoJUnitRunner.class)
48 public class UtilTest {
51 public void getClassPathTest() {
52 final MavenProject project = Mockito.mock(MavenProject.class);
53 final File file = Mockito.mock(File.class);
54 final File file2 = Mockito.mock(File.class);
55 final Artifact artifact = Mockito.mock(Artifact.class);
56 final Artifact artifact2 = Mockito.mock(Artifact.class);
58 final Set<Artifact> artifacts = new HashSet<>();
59 artifacts.add(artifact);
60 artifacts.add(artifact2);
62 Mockito.when(project.getArtifacts()).thenReturn(artifacts);
63 Mockito.when(artifact.getFile()).thenReturn(file);
64 Mockito.when(file.isFile()).thenReturn(true);
65 Mockito.when(file.getName()).thenReturn("iamjar.jar");
66 Mockito.when(artifact2.getFile()).thenReturn(file2);
67 Mockito.when(file2.isDirectory()).thenReturn(true);
69 final List<File> files = Util.getClassPath(project);
70 Assert.assertEquals(2, files.size());
71 Assert.assertTrue(files.contains(file) && files.contains(file2));
75 public void checkClasspathTest() throws Exception {
76 final MavenProject project = Mockito.mock(MavenProject.class);
77 final Plugin plugin = Mockito.mock(Plugin.class);
78 final RepositorySystem repoSystem = Mockito.mock(RepositorySystem.class);
79 final ArtifactRepository localRepo = Mockito.mock(ArtifactRepository.class);
80 final ArtifactResolutionResult artifactResolResult = Mockito.mock(ArtifactResolutionResult.class);
81 final Artifact artifact = Mockito.mock(Artifact.class);
82 final Dependency dep = Mockito.mock(Dependency.class);
84 final List<ArtifactRepository> remoteRepos = new ArrayList<>();
85 remoteRepos.add(localRepo);
87 final Set<Artifact> artifacts = new HashSet<>();
88 artifacts.add(artifact);
90 final List<Dependency> listDepcy = new ArrayList<>();
93 Mockito.when(project.getPlugin(Mockito.anyString())).thenReturn(plugin);
94 Mockito.when(plugin.getDependencies()).thenReturn(listDepcy);
95 Mockito.when(artifact.getArtifactId()).thenReturn("artifactId");
96 Mockito.when(artifact.getGroupId()).thenReturn("groupId");
97 Mockito.when(artifact.getVersion()).thenReturn("SNAPSHOT");
98 Mockito.when(repoSystem.createDependencyArtifact(dep)).thenReturn(artifact);
99 Mockito.when(repoSystem.resolve(Mockito.any(ArtifactResolutionRequest.class))).thenReturn(artifactResolResult);
100 Mockito.when(artifactResolResult.getArtifacts()).thenReturn(artifacts);
101 Mockito.when(project.getDependencyArtifacts()).thenReturn(artifacts);
103 Util.checkClasspath(project, repoSystem, localRepo, remoteRepos);
104 Assert.assertEquals(1, artifacts.size());
105 Assert.assertEquals(1, remoteRepos.size());
106 Assert.assertEquals(1, listDepcy.size());
110 public void findYangFilesInDependenciesAsStream() throws Exception {
111 final MavenProject project = Mockito.mock(MavenProject.class);
112 prepareProject(project);
114 final YangsInZipsResult yangzip = Util.findYangFilesInDependenciesAsStream(project);
115 Assert.assertNotNull(yangzip);
116 Assert.assertEquals(2, yangzip.getYangStreams().size());
121 public void findYangFilesInDependencies() throws Exception {
122 final MavenProject project = Mockito.mock(MavenProject.class);
123 prepareProject(project);
125 final Collection<File> files = Util.findYangFilesInDependencies(project);
126 Assert.assertNotNull(files);
127 Assert.assertEquals(2, files.size());
131 public void contextHolderTest() throws Exception {
132 final File testYang1 = new File(getClass().getResource("/test.yang").toURI());
133 final File testYang2 = new File(getClass().getResource("/test2.yang").toURI());
134 final SchemaContext context = YangParserTestUtils.parseYangSources(testYang1, testYang2);
135 final Set<Module> yangModules = new HashSet<>();
136 final Util.ContextHolder cxH = new ContextHolder(context, yangModules, yangModules);
137 Assert.assertEquals(context, cxH.getContext());
138 Assert.assertEquals(yangModules, cxH.getYangModules());
141 private URL makeMetaInf() throws Exception {
142 final String path = getClass().getResource("/").getPath();
143 final String metaInfPath = path + "tests/META-INF/yang";
144 final Path createDirectories = Files.createDirectories(Paths.get(metaInfPath));
145 Assert.assertNotNull(createDirectories);
146 Assert.assertEquals(metaInfPath, createDirectories.toString());
147 Runtime.getRuntime().exec("cp " + path + "/test.yang " + metaInfPath + "/");
148 Runtime.getRuntime().exec("cp " + path + "/test2.yang " + metaInfPath + "/");
149 return getClass().getResource("/tests");
152 private void prepareProject(final MavenProject project) throws Exception {
153 URL url = getClass().getResource("/tests");
157 Assert.assertNotNull(url);
158 final File testFile = new File(getClass().getResource("/tests").toURI());
159 File testFile2 = new File(getClass().getResource("/").getPath(), "test.jar");
160 testFile2.createNewFile();
161 testFile2 = new File(getClass().getResource("/test.jar").getFile());
163 final Manifest manifest = new Manifest();
164 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
165 final JarOutputStream target = new JarOutputStream(new FileOutputStream(testFile2), manifest);
166 addSourceFileToTargetJar(new File(getClass().getResource("/tests/META-INF").getPath()), target);
169 final Artifact artifact = Mockito.mock(Artifact.class);
170 final Artifact artifact2 = Mockito.mock(Artifact.class);
172 final Set<Artifact> artifacts = new HashSet<>();
173 artifacts.add(artifact);
174 artifacts.add(artifact2);
176 Mockito.when(project.getArtifacts()).thenReturn(artifacts);
177 Mockito.when(artifact.getFile()).thenReturn(testFile);
178 Mockito.when(artifact2.getFile()).thenReturn(testFile2);
181 private void addSourceFileToTargetJar(final File source, final JarOutputStream target) throws IOException {
182 BufferedInputStream in = null;
184 if (source.isDirectory()) {
185 String name = source.getPath().replace("\\", "/");
186 if (!name.isEmpty()) {
187 if (!name.endsWith("/")) {
190 final JarEntry entry = new JarEntry(name);
191 entry.setTime(source.lastModified());
192 target.putNextEntry(entry);
195 for (final File nestedFile : source.listFiles()) {
196 addSourceFileToTargetJar(nestedFile, target);
201 final JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
202 entry.setTime(source.lastModified());
203 target.putNextEntry(entry);
204 in = new BufferedInputStream(new FileInputStream(source));
206 final byte[] buffer = new byte[1024];
208 final int count = in.read(buffer);
212 target.write(buffer, 0, count);