1 /*
   2  * Copyright (c) 2012, 2015, 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  * @test
  26  * @bug 6493690
  27  * @summary javadoc should have a javax.tools.Tool service provider
  28  * @modules java.compiler
  29  *          jdk.compiler
  30  * @build APITest
  31  * @run main Task_reuseTest
  32  */
  33 
  34 import java.io.File;
  35 import java.util.Arrays;
  36 import java.util.Locale;
  37 import javax.tools.DocumentationTool;
  38 import javax.tools.DocumentationTool.DocumentationTask;
  39 import javax.tools.JavaFileObject;
  40 import javax.tools.StandardJavaFileManager;
  41 import javax.tools.ToolProvider;
  42 
  43 /**
  44  * Tests for reusing a documentation task.
  45  */
  46 public class Task_reuseTest extends APITest {
  47     public static void main(String... args) throws Exception {
  48         new Task_reuseTest().run();
  49     }
  50 
  51     /**
  52      * Verify that call can only be called once.
  53      */
  54     @Test
  55     public void testReuse() throws Exception {
  56         DocumentationTask t = getAndRunTask();
  57         try {
  58             t.call();
  59             error("task was reused without exception");
  60         } catch (IllegalStateException e) {
  61             System.err.println("caught exception " + e);
  62         }
  63     }
  64 
  65     /**
  66      * Verify that cannot update task after call
  67      */
  68     @Test
  69     public void testUpdateSetLocale() throws Exception {
  70         DocumentationTask t = getAndRunTask();
  71         try {
  72             t.setLocale(Locale.getDefault());
  73             error("task was reused without exception");
  74         } catch (IllegalStateException e) {
  75             System.err.println("caught exception " + e);
  76         }
  77     }
  78 
  79     private DocumentationTask getAndRunTask() throws Exception {
  80         JavaFileObject srcFile = createSimpleJavaFileObject();
  81         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
  82         try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
  83             File outDir = getOutDir();
  84             fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
  85             Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
  86             DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
  87             if (t.call()) {
  88                 System.err.println("task succeeded");
  89                 return t;
  90             } else {
  91                 throw new Exception("task failed");
  92             }
  93         }
  94     }
  95 }
  96