Cleanup ScannedDependencyTest
[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
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import com.google.common.collect.ImmutableSet;
16 import java.io.BufferedInputStream;
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.jar.Attributes;
23 import java.util.jar.JarEntry;
24 import java.util.jar.JarOutputStream;
25 import java.util.jar.Manifest;
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.project.MavenProject;
28 import org.junit.Test;
29
30 public class ScannedDependencyTest {
31
32     @Test
33     public void findYangFilesInDependenciesAsStream() throws Exception {
34         final MavenProject project = mock(MavenProject.class);
35         prepareProject(project);
36
37         final Collection<ScannedDependency> yangzip = ScannedDependency.scanDependencies(project);
38         assertNotNull(yangzip);
39         assertEquals(2, yangzip.size());
40     }
41
42     @Test
43     public void findYangFilesInDependencies() throws Exception {
44         final MavenProject project = mock(MavenProject.class);
45         prepareProject(project);
46
47         final Collection<ScannedDependency> files = ScannedDependency.scanDependencies(project);
48         assertNotNull(files);
49         assertEquals(2, files.size());
50     }
51
52     private static void prepareProject(final MavenProject project) throws Exception {
53         final Manifest manifest = new Manifest();
54         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
55         final File testFile2 = new File(ScannedDependencyTest.class.getResource("/").getPath(), "test.jar");
56         final JarOutputStream target = new JarOutputStream(new FileOutputStream(testFile2), manifest);
57         addSourceFileToTargetJar(new File(ScannedDependencyTest.class.getResource("/tests/META-INF").getPath()),
58             target);
59         target.close();
60
61         final Artifact artifact = mock(Artifact.class);
62         when(artifact.getFile()).thenReturn(new File(ScannedDependencyTest.class.getResource("/tests").toURI()));
63
64         final Artifact artifact2 = mock(Artifact.class);
65         when(artifact2.getFile()).thenReturn(testFile2);
66         when(project.getArtifacts()).thenReturn(ImmutableSet.of(artifact, artifact2));
67     }
68
69     private static void addSourceFileToTargetJar(final File source, final JarOutputStream target) throws IOException {
70         BufferedInputStream in = null;
71         try {
72             if (source.isDirectory()) {
73                 String name = source.getPath().replace("\\", "/");
74                 if (!name.isEmpty()) {
75                     if (!name.endsWith("/")) {
76                         name += "/";
77                     }
78                     final JarEntry entry = new JarEntry(name);
79                     entry.setTime(source.lastModified());
80                     target.putNextEntry(entry);
81                     target.closeEntry();
82                 }
83                 for (final File nestedFile : source.listFiles()) {
84                     addSourceFileToTargetJar(nestedFile, target);
85                 }
86                 return;
87             }
88
89             final JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
90             entry.setTime(source.lastModified());
91             target.putNextEntry(entry);
92             in = new BufferedInputStream(new FileInputStream(source));
93
94             final byte[] buffer = new byte[1024];
95             while (true) {
96                 final int count = in.read(buffer);
97                 if (count == -1) {
98                     break;
99                 }
100                 target.write(buffer, 0, count);
101             }
102             target.closeEntry();
103         } finally {
104             if (in != null) {
105                 in.close();
106             }
107         }
108     }
109 }