1 /* 2 * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package org.graalvm.compiler.debug.test; 26 27 import java.io.File; 28 import java.io.FileWriter; 29 import java.io.IOException; 30 import java.net.URI; 31 import java.net.URISyntaxException; 32 import java.nio.file.FileVisitResult; 33 import java.nio.file.FileVisitor; 34 import java.nio.file.Files; 35 import java.nio.file.Path; 36 import java.nio.file.Paths; 37 import java.nio.file.attribute.BasicFileAttributes; 38 import java.util.HashMap; 39 import java.util.Map; 40 import org.graalvm.compiler.debug.Versions; 41 import org.junit.After; 42 import static org.junit.Assert.assertEquals; 43 import static org.junit.Assert.assertNotNull; 44 import static org.junit.Assert.assertSame; 45 import static org.junit.Assert.assertTrue; 46 import static org.junit.Assert.fail; 47 import static org.junit.Assume.assumeTrue; 48 import org.junit.Test; 49 50 public class VersionsTest { 51 private File temporaryDirectory; 52 53 @After 54 public void cleanUp() throws IOException { 55 if (temporaryDirectory != null) { 56 Files.walkFileTree(temporaryDirectory.toPath(), new FileVisitor<Path>() { 57 @Override 58 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { 59 return FileVisitResult.CONTINUE; 60 } 61 62 @Override 63 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 64 Files.delete(file); 65 return FileVisitResult.CONTINUE; 66 } 67 68 @Override 69 public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { 70 Files.delete(file); 71 return FileVisitResult.CONTINUE; 72 } 73 74 @Override 75 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { 76 Files.delete(dir); 77 return FileVisitResult.CONTINUE; 78 } 79 }); 80 } 81 temporaryDirectory = null; 82 } 83 84 @Test 85 public void emptyProperties() throws URISyntaxException { 86 Path root = Paths.get(new URI("file:/")); 87 Versions v = new Versions(root); 88 assertEmpty(v.withVersions(null)); 89 } 90 91 @Test 92 public void emptyWithNullProperties() throws URISyntaxException { 93 Path root = Paths.get(new URI("file:/")); 94 Versions v = new Versions(root); 95 assertEmpty(v.withVersions(null)); 96 } 97 98 @Test 99 public void readFromSameDirNullProps() throws IOException { 100 File dir = prepareReleaseFile(); 101 102 Versions v = new Versions(dir.toPath()); 103 Map<Object, Object> map = v.withVersions(null); 104 assertNonModifiable(map); 105 106 assertEquals("16055f1ffaf736b7b86dcfaea53971983cd9ae0a", map.get("version.sdk")); 107 assertEquals("7930979c3b0af09a910accaaf3e73b2a55d2bade", map.get("version.truffleruby")); 108 } 109 110 @Test 111 public void readFromSameDir() throws IOException { 112 File dir = prepareReleaseFile(); 113 114 Versions v = new Versions(dir.toPath()); 115 116 Map<Object, Object> prepared = new HashMap<>(); 117 prepared.put("test", "best"); 118 119 Map<Object, Object> map = v.withVersions(prepared); 120 assertSame(prepared, map); 121 122 assertEquals("16055f1ffaf736b7b86dcfaea53971983cd9ae0a", map.get("version.sdk")); 123 assertEquals("7930979c3b0af09a910accaaf3e73b2a55d2bade", map.get("version.truffleruby")); 124 assertEquals("best", map.get("test")); 125 } 126 127 @Test 128 public void readFromSubDirNullProps() throws IOException { 129 File dir = prepareSubReleaseFile(); 130 131 Versions v = new Versions(dir.toPath()); 132 Map<Object, Object> map = v.withVersions(null); 133 assertNonModifiable(map); 134 135 assertEquals("16055f1ffaf736b7b86dcfaea53971983cd9ae0a", map.get("version.sdk")); 136 assertEquals("7930979c3b0af09a910accaaf3e73b2a55d2bade", map.get("version.truffleruby")); 137 } 138 139 @Test 140 public void readFromSubDir() throws IOException { 141 File dir = prepareSubReleaseFile(); 142 143 Versions v = new Versions(dir.toPath()); 144 145 Map<Object, Object> prepared = new HashMap<>(); 146 prepared.put("test", "best"); 147 148 Map<Object, Object> map = v.withVersions(prepared); 149 assertSame(prepared, map); 150 151 assertEquals("16055f1ffaf736b7b86dcfaea53971983cd9ae0a", map.get("version.sdk")); 152 assertEquals("7930979c3b0af09a910accaaf3e73b2a55d2bade", map.get("version.truffleruby")); 153 assertEquals("best", map.get("test")); 154 } 155 156 private File prepareReleaseFile() throws IOException { 157 if (temporaryDirectory == null) { 158 temporaryDirectory = File.createTempFile("versions", ".tmp"); 159 temporaryDirectory.delete(); 160 assumeTrue(temporaryDirectory.mkdirs()); 161 try (FileWriter w = new FileWriter(new File(temporaryDirectory, "release"))) { 162 // @formatter:off 163 w.write( 164 "OS_NAME=linux\n" + 165 "OS_ARCH=amd64\n" + 166 "SOURCE=\" truffle:16055f1ffaf736b7b86dcfaea53971983cd9ae0a sdk:16055f1ffaf736b7b86dcfaea53971983cd9ae0a " + 167 "tools-enterprise:fcc1292a05e807a63589e24ce6073aafdef45bb9 graal-js:d374a8fd2733487a9f7518be6a55eb6163a779d1 " + 168 "graal-nodejs:3fcaf6874c9059d5ca5f0615edaa405d66cc1b02 truffleruby:7930979c3b0af09a910accaaf3e73b2a55d2bade " + 169 "fastr:079c6513b46f36abc24bce8aa6022c90576b3eaf graalpython:4cbee7853d460930c4d693970a21b73f811a4703 " + 170 "sulong:2c425f92caa004b12f60428a3e7e6e2715b51f87 substratevm:fcc1292a05e807a63589e24ce6073aafdef45bb9 " + 171 "compiler:16055f1ffaf736b7b86dcfaea53971983cd9ae0a substratevm-enterprise:fcc1292a05e807a63589e24ce6073aafdef45bb9 " + 172 "vm-enterprise:fcc1292a05e807a63589e24ce6073aafdef45bb9 graal-enterprise:fcc1292a05e807a63589e24ce6073aafdef45bb9 \"\n" + 173 "COMMIT_INFO={\"vm-enterprise\":{\"commit.rev\":\"fcc1292a05e807a63589e24ce6073aafdef45bb9\"," + 174 "\"commit.committer\":\"Vojin Jovanovic <vojin.jovanovic@oracle.com>\",}}\n" + 175 "GRAALVM_VERSION=\"0.29-dev\"" 176 ); 177 // @formatter:on 178 } 179 } 180 return temporaryDirectory; 181 } 182 183 private File prepareSubReleaseFile() throws IOException { 184 File subdir = new File(prepareReleaseFile(), "subdir"); 185 assumeTrue(subdir.mkdirs()); 186 return subdir; 187 } 188 189 private static void assertEmpty(Map<?, ?> map) { 190 assertNotNull(map); 191 assertTrue(map.isEmpty()); 192 assertNonModifiable(map); 193 } 194 195 private static void assertNonModifiable(Map<?, ?> map) { 196 try { 197 map.put(null, null); 198 fail("Map shall not be modifiable: " + map); 199 } catch (UnsupportedOperationException ex) { 200 // ok 201 } 202 } 203 }