2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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;
9 import static org.junit.Assert.assertEquals;
10 import static org.junit.Assert.assertNotNull;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
14 import java.io.BufferedInputStream;
16 import java.io.FileInputStream;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
20 import java.nio.file.Files;
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23 import java.util.Collection;
24 import java.util.HashSet;
26 import java.util.jar.Attributes;
27 import java.util.jar.JarEntry;
28 import java.util.jar.JarOutputStream;
29 import java.util.jar.Manifest;
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.project.MavenProject;
32 import org.junit.Test;
34 public class ScannedDependencyTest {
37 public void findYangFilesInDependenciesAsStream() throws Exception {
38 final MavenProject project = mock(MavenProject.class);
39 prepareProject(project);
41 final Collection<ScannedDependency> yangzip = ScannedDependency.scanDependencies(project);
42 assertNotNull(yangzip);
43 assertEquals(2, yangzip.size());
47 public void findYangFilesInDependencies() throws Exception {
48 final MavenProject project = mock(MavenProject.class);
49 prepareProject(project);
51 final Collection<ScannedDependency> files = ScannedDependency.scanDependencies(project);
53 assertEquals(2, files.size());
56 private static URL makeMetaInf() throws Exception {
57 final String path = ScannedDependencyTest.class.getResource("/").getPath();
58 final String metaInfPath = path + "tests/META-INF/yang";
59 final Path createDirectories = Files.createDirectories(Paths.get(metaInfPath));
60 assertNotNull(createDirectories);
61 assertEquals(metaInfPath, createDirectories.toString());
62 Runtime.getRuntime().exec("cp " + path + "/test.yang " + metaInfPath + "/");
63 Runtime.getRuntime().exec("cp " + path + "/test2.yang " + metaInfPath + "/");
64 return ScannedDependencyTest.class.getResource("/tests");
67 private static void prepareProject(final MavenProject project) throws Exception {
68 URL url = ScannedDependencyTest.class.getResource("/tests");
73 final File testFile = new File(ScannedDependencyTest.class.getResource("/tests").toURI());
74 File testFile2 = new File(ScannedDependencyTest.class.getResource("/").getPath(), "test.jar");
75 testFile2.createNewFile();
76 testFile2 = new File(ScannedDependencyTest.class.getResource("/test.jar").getFile());
78 final Manifest manifest = new Manifest();
79 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
80 final JarOutputStream target = new JarOutputStream(new FileOutputStream(testFile2), manifest);
81 addSourceFileToTargetJar(new File(ScannedDependencyTest.class.getResource("/tests/META-INF").getPath()),
85 final Artifact artifact = mock(Artifact.class);
86 final Artifact artifact2 = mock(Artifact.class);
88 final Set<Artifact> artifacts = new HashSet<>();
89 artifacts.add(artifact);
90 artifacts.add(artifact2);
92 when(project.getArtifacts()).thenReturn(artifacts);
93 when(artifact.getFile()).thenReturn(testFile);
94 when(artifact2.getFile()).thenReturn(testFile2);
97 private static void addSourceFileToTargetJar(final File source, final JarOutputStream target) throws IOException {
98 BufferedInputStream in = null;
100 if (source.isDirectory()) {
101 String name = source.getPath().replace("\\", "/");
102 if (!name.isEmpty()) {
103 if (!name.endsWith("/")) {
106 final JarEntry entry = new JarEntry(name);
107 entry.setTime(source.lastModified());
108 target.putNextEntry(entry);
111 for (final File nestedFile : source.listFiles()) {
112 addSourceFileToTargetJar(nestedFile, target);
117 final JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
118 entry.setTime(source.lastModified());
119 target.putNextEntry(entry);
120 in = new BufferedInputStream(new FileInputStream(source));
122 final byte[] buffer = new byte[1024];
124 final int count = in.read(buffer);
128 target.write(buffer, 0, count);