b7483638a59016e96f7a222d90596d01de6d9e83
[yangtools.git] / yang / yang-maven-plugin / src / test / java / org / opendaylight / yangtools / yang2sources / plugin / ScannedDependencyTest.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
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;
13
14 import java.io.BufferedInputStream;
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.net.URL;
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;
25 import java.util.Set;
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;
33
34 public class ScannedDependencyTest {
35
36     @Test
37     public void findYangFilesInDependenciesAsStream() throws Exception {
38         final MavenProject project = mock(MavenProject.class);
39         prepareProject(project);
40
41         final Collection<ScannedDependency> yangzip = ScannedDependency.scanDependencies(project);
42         assertNotNull(yangzip);
43         assertEquals(2, yangzip.size());
44     }
45
46     @Test
47     public void findYangFilesInDependencies() throws Exception {
48         final MavenProject project = mock(MavenProject.class);
49         prepareProject(project);
50
51         final Collection<ScannedDependency> files = ScannedDependency.scanDependencies(project);
52         assertNotNull(files);
53         assertEquals(2, files.size());
54     }
55
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");
65     }
66
67     private static void prepareProject(final MavenProject project) throws Exception {
68         URL url = ScannedDependencyTest.class.getResource("/tests");
69         if (url == null) {
70             url = makeMetaInf();
71         }
72         assertNotNull(url);
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());
77
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()),
82             target);
83         target.close();
84
85         final Artifact artifact = mock(Artifact.class);
86         final Artifact artifact2 = mock(Artifact.class);
87
88         final Set<Artifact> artifacts = new HashSet<>();
89         artifacts.add(artifact);
90         artifacts.add(artifact2);
91
92         when(project.getArtifacts()).thenReturn(artifacts);
93         when(artifact.getFile()).thenReturn(testFile);
94         when(artifact2.getFile()).thenReturn(testFile2);
95     }
96
97     private static void addSourceFileToTargetJar(final File source, final JarOutputStream target) throws IOException {
98         BufferedInputStream in = null;
99         try {
100             if (source.isDirectory()) {
101                 String name = source.getPath().replace("\\", "/");
102                 if (!name.isEmpty()) {
103                     if (!name.endsWith("/")) {
104                         name += "/";
105                     }
106                     final JarEntry entry = new JarEntry(name);
107                     entry.setTime(source.lastModified());
108                     target.putNextEntry(entry);
109                     target.closeEntry();
110                 }
111                 for (final File nestedFile : source.listFiles()) {
112                     addSourceFileToTargetJar(nestedFile, target);
113                 }
114                 return;
115             }
116
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));
121
122             final byte[] buffer = new byte[1024];
123             while (true) {
124                 final int count = in.read(buffer);
125                 if (count == -1) {
126                     break;
127                 }
128                 target.write(buffer, 0, count);
129             }
130             target.closeEntry();
131         } finally {
132             if (in != null) {
133                 in.close();
134             }
135         }
136     }
137 }