From 5f925a0a0419dad3be5a2d60b3950e941423416f Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 2 Oct 2018 20:17:10 +0200 Subject: [PATCH] Cleanup ScannedDependencyTest Rather than messing with test resources set them up properly in the project so that we can rely on them being present. Also make sure we clean up our resources correctly. Change-Id: Idb1b8a8d87293a255582f7120cdb0e47f7278080 Signed-off-by: Robert Varga --- .../plugin/ScannedDependencyTest.java | 40 +++---------------- .../resources/tests/META-INF/yang/test.yang | 25 ++++++++++++ .../resources/tests/META-INF/yang/test2.yang | 25 ++++++++++++ 3 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 yang/yang-maven-plugin/src/test/resources/tests/META-INF/yang/test.yang create mode 100644 yang/yang-maven-plugin/src/test/resources/tests/META-INF/yang/test2.yang diff --git a/yang/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependencyTest.java b/yang/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependencyTest.java index b7483638a5..cd95324b7c 100644 --- a/yang/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependencyTest.java +++ b/yang/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependencyTest.java @@ -6,23 +6,19 @@ * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang2sources.plugin; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import com.google.common.collect.ImmutableSet; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Collection; -import java.util.HashSet; -import java.util.Set; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; @@ -53,45 +49,21 @@ public class ScannedDependencyTest { assertEquals(2, files.size()); } - private static URL makeMetaInf() throws Exception { - final String path = ScannedDependencyTest.class.getResource("/").getPath(); - final String metaInfPath = path + "tests/META-INF/yang"; - final Path createDirectories = Files.createDirectories(Paths.get(metaInfPath)); - assertNotNull(createDirectories); - assertEquals(metaInfPath, createDirectories.toString()); - Runtime.getRuntime().exec("cp " + path + "/test.yang " + metaInfPath + "/"); - Runtime.getRuntime().exec("cp " + path + "/test2.yang " + metaInfPath + "/"); - return ScannedDependencyTest.class.getResource("/tests"); - } - private static void prepareProject(final MavenProject project) throws Exception { - URL url = ScannedDependencyTest.class.getResource("/tests"); - if (url == null) { - url = makeMetaInf(); - } - assertNotNull(url); - final File testFile = new File(ScannedDependencyTest.class.getResource("/tests").toURI()); - File testFile2 = new File(ScannedDependencyTest.class.getResource("/").getPath(), "test.jar"); - testFile2.createNewFile(); - testFile2 = new File(ScannedDependencyTest.class.getResource("/test.jar").getFile()); - final Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); + final File testFile2 = new File(ScannedDependencyTest.class.getResource("/").getPath(), "test.jar"); final JarOutputStream target = new JarOutputStream(new FileOutputStream(testFile2), manifest); addSourceFileToTargetJar(new File(ScannedDependencyTest.class.getResource("/tests/META-INF").getPath()), target); target.close(); final Artifact artifact = mock(Artifact.class); - final Artifact artifact2 = mock(Artifact.class); + when(artifact.getFile()).thenReturn(new File(ScannedDependencyTest.class.getResource("/tests").toURI())); - final Set artifacts = new HashSet<>(); - artifacts.add(artifact); - artifacts.add(artifact2); - - when(project.getArtifacts()).thenReturn(artifacts); - when(artifact.getFile()).thenReturn(testFile); + final Artifact artifact2 = mock(Artifact.class); when(artifact2.getFile()).thenReturn(testFile2); + when(project.getArtifacts()).thenReturn(ImmutableSet.of(artifact, artifact2)); } private static void addSourceFileToTargetJar(final File source, final JarOutputStream target) throws IOException { diff --git a/yang/yang-maven-plugin/src/test/resources/tests/META-INF/yang/test.yang b/yang/yang-maven-plugin/src/test/resources/tests/META-INF/yang/test.yang new file mode 100644 index 0000000000..a01d7dd004 --- /dev/null +++ b/yang/yang-maven-plugin/src/test/resources/tests/META-INF/yang/test.yang @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +module test { + yang-version 1; + namespace "urn:opendaylight:test"; + prefix "test"; + + organization "Cisco Systems, Inc."; + + revision "2014-03-13" { + description + "Initial revision"; + } + + container cont{ + leaf l { + type string; + } + } +} diff --git a/yang/yang-maven-plugin/src/test/resources/tests/META-INF/yang/test2.yang b/yang/yang-maven-plugin/src/test/resources/tests/META-INF/yang/test2.yang new file mode 100644 index 0000000000..913cca809d --- /dev/null +++ b/yang/yang-maven-plugin/src/test/resources/tests/META-INF/yang/test2.yang @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +module test2 { + yang-version 1; + namespace "urn:opendaylight:test2"; + prefix "test"; + + organization "Cisco Systems, Inc."; + + revision "2014-03-13" { + description + "Initial revision"; + } + + container cont2{ + leaf l { + type string; + } + } +} -- 2.36.6