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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import java.io.BufferedInputStream;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
34 import java.util.jar.Attributes;
35 import java.util.jar.JarEntry;
36 import java.util.jar.JarOutputStream;
37 import java.util.jar.Manifest;
38 import org.apache.maven.artifact.Artifact;
39 import org.apache.maven.artifact.repository.ArtifactRepository;
40 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
41 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
42 import org.apache.maven.model.Dependency;
43 import org.apache.maven.model.Plugin;
44 import org.apache.maven.project.MavenProject;
45 import org.apache.maven.repository.RepositorySystem;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.runners.MockitoJUnitRunner;
49 import org.opendaylight.yangtools.yang.model.api.Module;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
52 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
53 import org.opendaylight.yangtools.yang2sources.plugin.Util.ContextHolder;
55 @RunWith(MockitoJUnitRunner.class)
56 public class UtilTest {
59 public void getClassPathTest() {
60 final MavenProject project = mock(MavenProject.class);
61 final File file = mock(File.class);
62 final File file2 = mock(File.class);
63 final Artifact artifact = mock(Artifact.class);
64 final Artifact artifact2 = mock(Artifact.class);
66 final Set<Artifact> artifacts = new HashSet<>();
67 artifacts.add(artifact);
68 artifacts.add(artifact2);
70 when(project.getArtifacts()).thenReturn(artifacts);
71 when(artifact.getFile()).thenReturn(file);
72 when(file.isFile()).thenReturn(true);
73 when(file.getName()).thenReturn("iamjar.jar");
74 when(artifact2.getFile()).thenReturn(file2);
75 when(file2.isDirectory()).thenReturn(true);
77 final List<File> files = Util.getClassPath(project);
78 assertEquals(2, files.size());
79 assertTrue(files.contains(file) && files.contains(file2));
83 public void checkClasspathTest() throws Exception {
84 final MavenProject project = mock(MavenProject.class);
85 final Plugin plugin = mock(Plugin.class);
86 final RepositorySystem repoSystem = mock(RepositorySystem.class);
87 final ArtifactRepository localRepo = mock(ArtifactRepository.class);
88 final ArtifactResolutionResult artifactResolResult = mock(ArtifactResolutionResult.class);
89 final Artifact artifact = mock(Artifact.class);
90 final Dependency dep = mock(Dependency.class);
92 final List<ArtifactRepository> remoteRepos = new ArrayList<>();
93 remoteRepos.add(localRepo);
95 final Set<Artifact> artifacts = new HashSet<>();
96 artifacts.add(artifact);
98 final List<Dependency> listDepcy = new ArrayList<>();
101 when(project.getPlugin(anyString())).thenReturn(plugin);
102 when(plugin.getDependencies()).thenReturn(listDepcy);
103 when(artifact.getArtifactId()).thenReturn("artifactId");
104 when(artifact.getGroupId()).thenReturn("groupId");
105 when(artifact.getVersion()).thenReturn("SNAPSHOT");
106 when(repoSystem.createDependencyArtifact(dep)).thenReturn(artifact);
107 when(repoSystem.resolve(any(ArtifactResolutionRequest.class))).thenReturn(artifactResolResult);
108 when(artifactResolResult.getArtifacts()).thenReturn(artifacts);
109 when(project.getDependencyArtifacts()).thenReturn(artifacts);
111 Util.checkClasspath(project, repoSystem, localRepo, remoteRepos);
112 assertEquals(1, artifacts.size());
113 assertEquals(1, remoteRepos.size());
114 assertEquals(1, listDepcy.size());
118 public void findYangFilesInDependenciesAsStream() throws Exception {
119 final MavenProject project = mock(MavenProject.class);
120 prepareProject(project);
122 final List<YangTextSchemaSource> yangzip = Util.findYangFilesInDependenciesAsStream(project);
123 assertNotNull(yangzip);
124 assertEquals(2, yangzip.size());
128 public void findYangFilesInDependencies() throws Exception {
129 final MavenProject project = mock(MavenProject.class);
130 prepareProject(project);
132 final Collection<File> files = Util.findYangFilesInDependencies(project);
133 assertNotNull(files);
134 assertEquals(2, files.size());
138 public void contextHolderTest() throws Exception {
139 final File testYang1 = new File(getClass().getResource("/test.yang").toURI());
140 final File testYang2 = new File(getClass().getResource("/test2.yang").toURI());
141 final SchemaContext context = YangParserTestUtils.parseYangSources(testYang1, testYang2);
142 final Map<Module, String> yangModules = new HashMap<>();
143 final Util.ContextHolder cxH = new ContextHolder(context, yangModules.keySet(), yangModules);
144 assertEquals(context, cxH.getContext());
145 assertEquals(yangModules.keySet(), cxH.getYangModules());
148 private URL makeMetaInf() throws Exception {
149 final String path = getClass().getResource("/").getPath();
150 final String metaInfPath = path + "tests/META-INF/yang";
151 final Path createDirectories = Files.createDirectories(Paths.get(metaInfPath));
152 assertNotNull(createDirectories);
153 assertEquals(metaInfPath, createDirectories.toString());
154 Runtime.getRuntime().exec("cp " + path + "/test.yang " + metaInfPath + "/");
155 Runtime.getRuntime().exec("cp " + path + "/test2.yang " + metaInfPath + "/");
156 return getClass().getResource("/tests");
159 private void prepareProject(final MavenProject project) throws Exception {
160 URL url = getClass().getResource("/tests");
165 final File testFile = new File(getClass().getResource("/tests").toURI());
166 File testFile2 = new File(getClass().getResource("/").getPath(), "test.jar");
167 testFile2.createNewFile();
168 testFile2 = new File(getClass().getResource("/test.jar").getFile());
170 final Manifest manifest = new Manifest();
171 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
172 final JarOutputStream target = new JarOutputStream(new FileOutputStream(testFile2), manifest);
173 addSourceFileToTargetJar(new File(getClass().getResource("/tests/META-INF").getPath()), target);
176 final Artifact artifact = mock(Artifact.class);
177 final Artifact artifact2 = mock(Artifact.class);
179 final Set<Artifact> artifacts = new HashSet<>();
180 artifacts.add(artifact);
181 artifacts.add(artifact2);
183 when(project.getArtifacts()).thenReturn(artifacts);
184 when(artifact.getFile()).thenReturn(testFile);
185 when(artifact2.getFile()).thenReturn(testFile2);
188 private void addSourceFileToTargetJar(final File source, final JarOutputStream target) throws IOException {
189 BufferedInputStream in = null;
191 if (source.isDirectory()) {
192 String name = source.getPath().replace("\\", "/");
193 if (!name.isEmpty()) {
194 if (!name.endsWith("/")) {
197 final JarEntry entry = new JarEntry(name);
198 entry.setTime(source.lastModified());
199 target.putNextEntry(entry);
202 for (final File nestedFile : source.listFiles()) {
203 addSourceFileToTargetJar(nestedFile, target);
208 final JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
209 entry.setTime(source.lastModified());
210 target.putNextEntry(entry);
211 in = new BufferedInputStream(new FileInputStream(source));
213 final byte[] buffer = new byte[1024];
215 final int count = in.read(buffer);
219 target.write(buffer, 0, count);