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;
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;
15 import com.google.common.collect.ImmutableSet;
16 import java.io.BufferedInputStream;
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;
30 public class ScannedDependencyTest {
33 public void findYangFilesInDependenciesAsStream() throws Exception {
34 final MavenProject project = mock(MavenProject.class);
35 prepareProject(project);
37 final Collection<ScannedDependency> yangzip = ScannedDependency.scanDependencies(project);
38 assertNotNull(yangzip);
39 assertEquals(2, yangzip.size());
43 public void findYangFilesInDependencies() throws Exception {
44 final MavenProject project = mock(MavenProject.class);
45 prepareProject(project);
47 final Collection<ScannedDependency> files = ScannedDependency.scanDependencies(project);
49 assertEquals(2, files.size());
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()),
61 final Artifact artifact = mock(Artifact.class);
62 when(artifact.getFile()).thenReturn(new File(ScannedDependencyTest.class.getResource("/tests").toURI()));
64 final Artifact artifact2 = mock(Artifact.class);
65 when(artifact2.getFile()).thenReturn(testFile2);
66 when(project.getArtifacts()).thenReturn(ImmutableSet.of(artifact, artifact2));
69 private static void addSourceFileToTargetJar(final File source, final JarOutputStream target) throws IOException {
70 BufferedInputStream in = null;
72 if (source.isDirectory()) {
73 String name = source.getPath().replace("\\", "/");
74 if (!name.isEmpty()) {
75 if (!name.endsWith("/")) {
78 final JarEntry entry = new JarEntry(name);
79 entry.setTime(source.lastModified());
80 target.putNextEntry(entry);
83 for (final File nestedFile : source.listFiles()) {
84 addSourceFileToTargetJar(nestedFile, target);
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));
94 final byte[] buffer = new byte[1024];
96 final int count = in.read(buffer);
100 target.write(buffer, 0, count);