@AndroidIncompatible // Path (for symlink creation) public void testScanDirectory_symlinkToRootCycle() throws IOException { ClassLoader loader = ClassPathTest.class.getClassLoader(); // directory with a cycle, // /root // /child // /[grandchild -> root] java.nio.file.Path root = createTempDirectory("ClassPathTest"); try { createFile(root.resolve("some.txt")); java.nio.file.Path child = createDirectory(root.resolve("child")); createSymbolicLink(child.resolve("grandchild"), root); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(root.toFile(), loader); assertEquals(ImmutableSet.of(new ResourceInfo("some.txt", loader)), scanner.getResources()); } finally { deleteRecursively(root); } }
public void testGetResources() throws Exception { Map<String, ResourceInfo> byName = Maps.newHashMap(); Map<String, ResourceInfo> byToString = Maps.newHashMap(); ClassPath classpath = ClassPath.from(getClass().getClassLoader()); for (ResourceInfo resource : classpath.getResources()) { assertThat(resource.getResourceName()).isNotEqualTo(JarFile.MANIFEST_NAME); assertThat(resource.toString()).isNotEqualTo(JarFile.MANIFEST_NAME); byName.put(resource.getResourceName(), resource); byToString.put(resource.toString(), resource); assertNotNull(resource.url()); } String testResourceName = "com/google/common/reflect/test.txt"; assertThat(byName.keySet()).containsAllOf( "com/google/common/reflect/ClassPath.class", "com/google/common/reflect/ClassPathTest.class", "com/google/common/reflect/ClassPathTest$Nested.class", testResourceName); assertThat(byToString.keySet()).containsAllOf( "com.google.common.reflect.ClassPath", "com.google.common.reflect.ClassPathTest", "com.google.common.reflect.ClassPathTest$Nested", testResourceName); assertEquals(getClass().getClassLoader().getResource(testResourceName), byName.get("com/google/common/reflect/test.txt").url()); }
@AndroidIncompatible // Path (for symlink creation) public void testScanDirectory_symlinkToRootCycle() throws IOException { ClassLoader loader = ClassPathTest.class.getClassLoader(); // directory with a cycle, // /root // /child // /[grandchild -> root] java.nio.file.Path root = createTempDirectory("ClassPathTest"); try { createFile(root.resolve("some.txt")); java.nio.file.Path child = createDirectory(root.resolve("child")); createSymbolicLink(child.resolve("grandchild"), root); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(root.toFile(), loader); assertEquals(ImmutableSet.of(new ResourceInfo("some.txt", loader)), scanner.getResources()); } finally { deleteRecursivelyOrLog(root); } }
public static Set< String > determineInstrumentableClasses() throws Throwable { TaxonomyLoader loader = new AgentTaxonomyLoader(); Set< ResourceInfo > resources = ClassPath.from( loader.getClassLoader() ).getResources(); Set< Class< ? > > viewedClasses = new HashSet< Class< ? > >(); resources.stream() .filter( resourceInfo -> resourceInfo.getResourceName().endsWith( ".tax" ) ) .map( resourceInfo -> getTaxonomy( loader, resourceInfo.getResourceName() ) ) .filter( taxonomy -> taxonomy != null ) .forEach( taxonomy -> viewedClasses.addAll( getViewedClasses( taxonomy ) ) ); Set< String > instrumentableClassNames = viewedClasses.stream() .filter( viewedClass -> isTopLevelClass( viewedClass, viewedClasses ) ) .map( viewedClass -> viewedClass.getName() ) .collect( Collectors.toSet() ); return instrumentableClassNames; }
public void testEquals() { new EqualsTester() .addEqualityGroup(classInfo(ClassPathTest.class), classInfo(ClassPathTest.class)) .addEqualityGroup(classInfo(Test.class), classInfo(Test.class, getClass().getClassLoader())) .addEqualityGroup( new ResourceInfo("a/b/c.txt", getClass().getClassLoader()), new ResourceInfo("a/b/c.txt", getClass().getClassLoader())) .addEqualityGroup( new ResourceInfo("x.txt", getClass().getClassLoader())) .testEquals(); }
@AndroidIncompatible // Path (for symlink creation) public void testScanDirectory_symlinkCycle() throws IOException { ClassLoader loader = ClassPathTest.class.getClassLoader(); // directory with a cycle, // /root // /left // /[sibling -> right] // /right // /[sibling -> left] java.nio.file.Path root = createTempDirectory("ClassPathTest"); try { java.nio.file.Path left = createDirectory(root.resolve("left")); createFile(left.resolve("some.txt")); java.nio.file.Path right = createDirectory(root.resolve("right")); createFile(right.resolve("another.txt")); createSymbolicLink(left.resolve("sibling"), right); createSymbolicLink(right.resolve("sibling"), left); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(root.toFile(), loader); assertEquals( ImmutableSet.of( new ResourceInfo("left/some.txt", loader), new ResourceInfo("left/sibling/another.txt", loader), new ResourceInfo("right/another.txt", loader), new ResourceInfo("right/sibling/some.txt", loader)), scanner.getResources()); } finally { deleteRecursively(root); } }
private void registerVanillaPasses() { try { for(ResourceInfo info : ClassPath.from(Thread.currentThread().getContextClassLoader()).getResources()) { if(info.getResourceName().startsWith("assets/minecraft/shaders/program/") && info.getResourceName().endsWith(".json")) { String[] parts = info.getResourceName().split("/"); String file = parts[parts.length - 1]; String name = file.substring(0, file.indexOf(".")); if(name.equals("phosphor")) { PassRegistry.register(name, new PhosphorPass()); } else if(name.equals("notch")) { PassRegistry.register(name, new NotchPass()); } else PassRegistry.register(name, new VanillaPass(name)); } } } catch(IOException e) { e.printStackTrace(); } }
/** * * @param document The resource pointing to the document to be represented * @param docSuffix The portion of the filename that should be removed for Title generation */ public ApiDocumentMetadata(ResourceInfo document, String docSuffix) { this.document = document; String name = document.getResourceName(); String title = name; if (name.contains("/") && !name.endsWith("/")) { name = name.substring(name.lastIndexOf("/") + 1); title = StringUtils.capitalize(name).replace(docSuffix, ""); } this.path = name; this.title = title; }
public void testEquals() { new EqualsTester() .addEqualityGroup(classInfo(ClassPathTest.class), classInfo(ClassPathTest.class)) .addEqualityGroup(classInfo(Test.class), classInfo(Test.class, getClass().getClassLoader())) .addEqualityGroup( new ResourceInfo("a/b/c.txt", getClass().getClassLoader()), new ResourceInfo("a/b/c.txt", getClass().getClassLoader())) .addEqualityGroup(new ResourceInfo("x.txt", getClass().getClassLoader())) .testEquals(); }
private void doTestExistsThrowsSecurityException() throws IOException, URISyntaxException { File file = null; // In Java 9, Logger may read the TZ database. Only disallow reading the class path URLs. final PermissionCollection readClassPathFiles = new FilePermission("", "read").newPermissionCollection(); for (URL url : ClassPath.Scanner.parseJavaClassPath()) { if (url.getProtocol().equalsIgnoreCase("file")) { file = new File(url.toURI()); readClassPathFiles.add(new FilePermission(file.getAbsolutePath(), "read")); } } assertThat(file).isNotNull(); SecurityManager disallowFilesSecurityManager = new SecurityManager() { @Override public void checkPermission(Permission p) { if (readClassPathFiles.implies(p)) { throw new SecurityException("Disallowed: " + p); } } }; System.setSecurityManager(disallowFilesSecurityManager); try { file.exists(); fail("Did not get expected SecurityException"); } catch (SecurityException expected) { } ClassPath classPath = ClassPath.from(getClass().getClassLoader()); // ClassPath may contain resources from the boot class loader; just not from the class path. for (ResourceInfo resource : classPath.getResources()) { assertThat(resource.getResourceName()).doesNotContain("com/google/common/reflect/"); } }
@AndroidIncompatible // Path (for symlink creation) public void testScanDirectory_symlinkCycle() throws IOException { ClassLoader loader = ClassPathTest.class.getClassLoader(); // directory with a cycle, // /root // /left // /[sibling -> right] // /right // /[sibling -> left] java.nio.file.Path root = createTempDirectory("ClassPathTest"); try { java.nio.file.Path left = createDirectory(root.resolve("left")); createFile(left.resolve("some.txt")); java.nio.file.Path right = createDirectory(root.resolve("right")); createFile(right.resolve("another.txt")); createSymbolicLink(left.resolve("sibling"), right); createSymbolicLink(right.resolve("sibling"), left); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(root.toFile(), loader); assertEquals( ImmutableSet.of( new ResourceInfo("left/some.txt", loader), new ResourceInfo("left/sibling/another.txt", loader), new ResourceInfo("right/another.txt", loader), new ResourceInfo("right/sibling/some.txt", loader)), scanner.getResources()); } finally { deleteRecursivelyOrLog(root); } }
private static ResourceInfo resourceInfo(Class<?> cls) { String resource = cls.getName().replace('.', '/') + ".class"; ClassLoader loader = cls.getClassLoader(); return ResourceInfo.of(resource, loader); }
private static ResourceInfo resourceInfo(Class<?> cls) { return ResourceInfo.of(cls.getName().replace('.', '/') + ".class", cls.getClassLoader()); }
@BeforeMethod public void setup() throws IOException { dataContext = new AnnotationConfigApplicationContext(); dataContext.getEnvironment().setActiveProfiles(Profiles.UNIT_TEST); dataContext.scan("org.diqube"); dataContext.refresh(); tableRegistry = dataContext.getBean(TableRegistry.class); metadataManagerMock = Mockito.mock(TableMetadataManager.class); ServerTableMetadataPublisher metadataPublisher = ServerTableMetadataPublisherTestUtil.create(dataContext.getBean(TableRegistry.class), dataContext.getBean(TableShardMetadataBuilderFactory.class), metadataManagerMock); controlFileFactory = new Function<File, ControlFileLoader>() { @Override public ControlFileLoader apply(File controlFile) { return new ControlFileLoader( // tableRegistry, // dataContext.getBean(TableFactory.class), // dataContext.getBean(CsvLoader.class), // dataContext.getBean(JsonLoader.class), // dataContext.getBean(DiqubeLoader.class), // dataContext.getBean(ClusterFlattenServiceHandler.class), // metadataPublisher, // controlFile); } }; testDir = File.createTempFile(ControlFileLoaderTest.class.getSimpleName(), Long.toString(System.nanoTime())); testDir.delete(); testDir.mkdir(); for (ResourceInfo resInfo : ClassPath.from(this.getClass().getClassLoader()).getResources()) { if (resInfo.getResourceName().startsWith(TESTDATA_CLASSPATH)) { String targetFileName = resInfo.getResourceName().substring(TESTDATA_CLASSPATH.length()); try (FileOutputStream fos = new FileOutputStream(new File(testDir, targetFileName))) { InputStream is = this.getClass().getClassLoader().getResourceAsStream(resInfo.getResourceName()); ByteStreams.copy(is, fos); } } } }
@Parameters(name = "{index}: {0}") public static Iterable<Object[]> data() throws IOException { Path testDataPath = Paths.get("com/google/googlejavaformat/java/testdata"); ClassLoader classLoader = FormatterIntegrationTest.class.getClassLoader(); Map<String, String> inputs = new TreeMap<>(); Map<String, String> outputs = new TreeMap<>(); for (ResourceInfo resourceInfo : ClassPath.from(classLoader).getResources()) { String resourceName = resourceInfo.getResourceName(); Path resourceNamePath = Paths.get(resourceName); if (resourceNamePath.startsWith(testDataPath)) { Path subPath = testDataPath.relativize(resourceNamePath); assertEquals("bad testdata file names", 1, subPath.getNameCount()); String baseName = getNameWithoutExtension(subPath.getFileName().toString()); String extension = getFileExtension(subPath.getFileName().toString()); String contents; try (InputStream stream = FormatterIntegrationTest.class.getClassLoader().getResourceAsStream(resourceName)) { contents = CharStreams.toString(new InputStreamReader(stream, UTF_8)); } switch (extension) { case "input": inputs.put(baseName, contents); break; case "output": outputs.put(baseName, contents); break; default: } } } List<Object[]> testInputs = new ArrayList<>(); assertEquals("unmatched inputs and outputs", inputs.size(), outputs.size()); for (Map.Entry<String, String> entry : inputs.entrySet()) { String fileName = entry.getKey(); String input = inputs.get(fileName); assertTrue("unmatched input", outputs.containsKey(fileName)); String expectedOutput = outputs.get(fileName); testInputs.add(new Object[] {fileName, input, expectedOutput}); } return testInputs; }