Cleanup ScannedDependencyTest 85/76585/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 2 Oct 2018 18:17:10 +0000 (20:17 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 2 Oct 2018 19:55:49 +0000 (19:55 +0000)
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 <robert.varga@pantheon.tech>
yang/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependencyTest.java
yang/yang-maven-plugin/src/test/resources/tests/META-INF/yang/test.yang [new file with mode: 0644]
yang/yang-maven-plugin/src/test/resources/tests/META-INF/yang/test2.yang [new file with mode: 0644]

index b7483638a59016e96f7a222d90596d01de6d9e83..cd95324b7ce35fb3c3eed1ec3b8401b8c7559a9c 100644 (file)
@@ -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<Artifact> 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 (file)
index 0000000..a01d7dd
--- /dev/null
@@ -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 (file)
index 0000000..913cca8
--- /dev/null
@@ -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;
+        }
+    }
+}