/** * Verify bad file object is handled correctly. */ @Test public void testBadFileObject() throws Exception { File testSrc = new File(System.getProperty("test.src")); File srcFile = new File(testSrc, "pkg/C.class"); // unacceptable file kind DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(srcFile); try { DocumentationTask t = tool.getTask(null, fm, null, null, null, files); error("getTask succeeded, no exception thrown"); } catch (IllegalArgumentException e) { System.err.println("exception caught as expected: " + e); } } }
/** * Verify null is handled correctly. */ @Test public void testNull() throws Exception { DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = Arrays.asList((JavaFileObject) null); try { DocumentationTask t = tool.getTask(null, fm, null, null, null, files); error("getTask succeeded, no exception thrown"); } catch (NullPointerException e) { System.err.println("exception caught as expected: " + e); } } }
@Test public void testDirectAccess1() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); Context c = new Context(); Messager.preRegister(c, "javadoc"); try (StandardJavaFileManager fm = new JavacFileManager(c, true, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); DocumentationTask t = new JavadocTaskImpl(c, null, null, files); if (t.call()) { System.err.println("task succeeded"); } else { throw new Exception("task failed"); } } }
/** * Verify that expected output files are written via the file manager, * for an in-memory file object. */ @Test public void testMemoryFileObject() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); DocumentationTask t = tool.getTask(null, fm, null, null, null, files); if (t.call()) { System.err.println("task succeeded"); checkFiles(outDir, standardExpectFiles); } else { throw new Exception("task failed"); } } }
/** * Verify null is handled correctly. */ @Test public void testNull() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<String> options = Arrays.asList((String) null); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); try { DocumentationTask t = tool.getTask(null, fm, null, null, options, files); error("getTask succeeded, no exception thrown"); } catch (NullPointerException e) { System.err.println("exception caught as expected: " + e); } } }
/** * Verify that an alternate doclet can be specified. * * There is no standard interface or superclass for a doclet; * the only requirement is that it provides static methods that * can be invoked via reflection. So, for now, the doclet is * specified as a class. * Because we cannot create and use a unique instance of the class, * we verify that the doclet has been called by having it record * (in a static field!) the comment from the last time it was invoked, * which is randomly generated each time the test is run. */ @Test public void testDoclet() throws Exception { Random r = new Random(); int key = r.nextInt(); JavaFileObject srcFile = createSimpleJavaFileObject( "pkg/C", "package pkg; /** " + key + "*/ public class C { }"); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); DocumentationTask t = tool.getTask(null, fm, null, TestDoclet.class, null, files); if (t.call()) { System.err.println("task succeeded"); if (TestDoclet.lastCaller.equals(String.valueOf(key))) System.err.println("found expected key: " + key); else error("Expected key not found"); checkFiles(outDir, Collections.<String>emptySet()); } else { throw new Exception("task failed"); } } }
@Test public void testRawCall() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); @SuppressWarnings("rawtypes") Callable t = tool.getTask(null, fm, null, null, null, files); if (t.call() == Boolean.TRUE) { System.err.println("task succeeded"); } else { throw new Exception("task failed"); } } }
@Test public void testDirectAccess2() throws Exception { JavaFileObject srcFile = null; // error, provokes NPE Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); Context c = new Context(); Messager.preRegister(c, "javadoc"); try (StandardJavaFileManager fm = new JavacFileManager(c, true, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); try { DocumentationTask t = new JavadocTaskImpl(c, null, null, files);; error("getTask succeeded, no exception thrown"); } catch (NullPointerException e) { System.err.println("exception caught as expected: " + e); } } }
/** * Verify that expected output files are written via the file manager, * for a source file read from the file system with StandardJavaFileManager. */ @Test public void testStandardFileObject() throws Exception { File testSrc = new File(System.getProperty("test.src")); File srcFile = new File(testSrc, "pkg/C.java"); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(srcFile); DocumentationTask t = tool.getTask(null, fm, null, null, null, files); if (t.call()) { System.err.println("task succeeded"); checkFiles(outDir, standardExpectFiles); } else { throw new Exception("task failed"); } } }
/** * Verify that expected output files are written for given options. */ @Test public void testNoIndex() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); Iterable<String> options = Arrays.asList("-noindex"); DocumentationTask t = tool.getTask(null, fm, null, null, options, files); if (t.call()) { System.err.println("task succeeded"); Set<String> expectFiles = new TreeSet<String>(noIndexFiles); checkFiles(outDir, expectFiles); } else { error("task failed"); } } }
/** * Read the "package-list" file which is available locally. * * @param path URL or directory path to the packages. * @param pkgListPath Path to the local "package-list" file. */ private void readPackageListFromFile(String path, DocFile pkgListPath) throws Fault { DocFile file = pkgListPath.resolve(DocPaths.PACKAGE_LIST); if (! (file.isAbsolute() || linkoffline)){ file = file.resolveAgainst(DocumentationTool.Location.DOCUMENTATION_OUTPUT); } try { if (file.exists() && file.canRead()) { boolean pathIsRelative = !DocFile.createFileForInput(configuration, path).isAbsolute() && !isUrl(path); readPackageList(file.openInputStream(), path, pathIsRelative); } else { throw new Fault(configuration.getText("doclet.File_error", file.getPath()), null); } } catch (IOException exc) { throw new Fault(configuration.getText("doclet.File_error", file.getPath()), exc); } }
public PathDocFileFactory(Configuration configuration) { super(configuration); fileManager = (PathFileManager) configuration.getFileManager(); if (!configuration.destDirName.isEmpty() || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) { try { String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName; Path dir = fileManager.getDefaultFileSystem().getPath(dirName); fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir)); } catch (IOException e) { throw new DocletAbortException(e); } } destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next(); }
private File getDestDir() { if (destDir == null) { if (!configuration.destDirName.isEmpty() || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) { try { String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName; File dir = new File(dirName); fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir)); } catch (IOException e) { throw new DocletAbortException(e); } } destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next(); } return destDir; }
/** * Read the "package-list" file which is available locally. * * @param path URL or directory path to the packages. * @param pkgListPath Path to the local "package-list" file. * @throws Fault if an error occurs that can be treated as a warning * @throws DocFileIOException if there is a problem opening the package list file */ private void readPackageListFromFile(String path, DocFile pkgListPath) throws Fault, DocFileIOException { DocFile file = pkgListPath.resolve(DocPaths.PACKAGE_LIST); if (! (file.isAbsolute() || linkoffline)){ file = file.resolveAgainst(DocumentationTool.Location.DOCUMENTATION_OUTPUT); } try { if (file.exists() && file.canRead()) { boolean pathIsRelative = !isUrl(path) && !DocFile.createFileForInput(configuration, path).isAbsolute(); readPackageList(file.openInputStream(), path, pathIsRelative); } else { throw new Fault(configuration.getText("doclet.File_error", file.getPath()), null); } } catch (IOException exc) { throw new Fault(configuration.getText("doclet.File_error", file.getPath()), exc); } }
@Override public void setDestDir(String destDirName) throws SimpleDocletException { if (destDir != null) throw new AssertionError("destDir already initialized: " + destDir); if (!destDirName.isEmpty() || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) { try { String dirName = destDirName.isEmpty() ? "." : destDirName; Path dir = Paths.get(dirName); fileManager.setLocationFromPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir)); } catch (IOException e) { // generic IOException from file manager, setting location, e.g. file not a directory String message = configuration.getResources().getText("doclet.error.initializing.dest.dir", e); throw new SimpleDocletException(message, e); } } destDir = fileManager.getLocationAsPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next(); }
private DocumentationTask getAndRunTask() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); DocumentationTask t = tool.getTask(null, fm, null, null, null, files); if (t.call()) { System.err.println("task succeeded"); return t; } else { throw new Exception("task failed"); } } }
/** * Verify that exceptions from a doclet are thrown as expected. */ @Test public void testBadDoclet() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); DocumentationTask t = tool.getTask(null, fm, null, BadDoclet.class, null, files); try { t.call(); error("call completed without exception"); } catch (RuntimeException e) { e.printStackTrace(); Throwable c = e.getCause(); if (c.getClass() == UnexpectedError.class) System.err.println("exception caught as expected: " + c); else throw e; } } }
/** * Verify that an alternate file manager can be specified: * in this case, a TestFileManager. */ @Test public void testFileManager() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); StandardJavaFileManager fm = new TestFileManager(); File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); DocumentationTask t = tool.getTask(null, fm, null, null, Arrays.asList("-verbose"), files); if (t.call()) { System.err.println("task succeeded"); checkFiles(outDir, standardExpectFiles); } else { throw new Exception("task failed"); } }
/** * Verify that exceptions from a doclet are thrown as expected. */ @Test public void testBadDoclet() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { File outDir = getOutDir(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); DocumentationTask t = tool.getTask(null, fm, null, BadDoclet.class, null, files); try { t.call(); error("call completed without exception"); } catch (RuntimeException e) { Throwable c = e.getCause(); if (c.getClass() == UnexpectedError.class) System.err.println("exception caught as expected: " + c); else throw e; } } }
/** * Read the "package-list" file which is available locally. * * @param path URL or directory path to the packages. * @param pkgListPath Path to the local "package-list" file. */ private void readPackageListFromFile(String path, DocFile pkgListPath) throws Fault { DocFile file = pkgListPath.resolve(DocPaths.PACKAGE_LIST); if (! (file.isAbsolute() || linkoffline)){ file = file.resolveAgainst(DocumentationTool.Location.DOCUMENTATION_OUTPUT); } try { if (file.exists() && file.canRead()) { boolean pathIsRelative = !isUrl(path) && !DocFile.createFileForInput(configuration, path).isAbsolute(); readPackageList(file.openInputStream(), path, pathIsRelative); } else { throw new Fault(configuration.getText("doclet.File_error", file.getPath()), null); } } catch (IOException exc) { throw new Fault(configuration.getText("doclet.File_error", file.getPath()), exc); } }
private Path getDestDir() { if (destDir == null) { if (!configuration.destDirName.isEmpty() || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) { try { String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName; Path dir = Paths.get(dirName); fileManager.setLocationFromPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir)); } catch (IOException e) { throw new DocletAbortException(e); } } destDir = fileManager.getLocationAsPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next(); } return destDir; }
/** * Verify that an alternate file manager can be specified: * in this case, a PathFileManager. */ @Test public void testFileManager() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); PathFileManager fm = new JavacPathFileManager(new Context(), false, null); Path outDir = getOutDir().toPath(); fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); DocumentationTask t = tool.getTask(null, fm, null, null, null, files); if (t.call()) { System.err.println("task succeeded"); checkFiles(outDir, standardExpectFiles); } else { throw new Exception("task failed"); } }