1 /*
   2  * Copyright (c) 2002, 2016, 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 package test.astro;
  25 
  26 import static java.lang.String.valueOf;
  27 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  28 import static org.testng.Assert.assertEquals;
  29 import static test.astro.AstroConstants.ASTROCAT;
  30 import static test.astro.AstroConstants.GOLDEN_DIR;
  31 
  32 import java.io.IOException;
  33 import java.nio.file.Files;
  34 import java.nio.file.Paths;
  35 import java.util.List;
  36 
  37 import javax.xml.transform.sax.TransformerHandler;
  38 
  39 import org.testng.annotations.BeforeClass;
  40 import org.testng.annotations.DataProvider;
  41 import org.testng.annotations.Listeners;
  42 import org.testng.annotations.Test;
  43 
  44 /*
  45  * @test
  46  * @library /javax/xml/jaxp/libs
  47  * @run testng/othervm -DrunSecMngr=true test.astro.AstroTest
  48  * @run testng/othervm test.astro.AstroTest
  49  * @summary run astro application, test xslt
  50  *
  51  * There are vast amounts of textual astronomical data, typically user is
  52  * interested in a small subset, which is the result from carrying out a query.
  53  * A query can be composed of one or more filters, for example, the user could
  54  * query the database for all stars of visual magnitude down to 6.5 that lie
  55  * between right ascensions 0 h to 2 h, and between declinations of 45 to 90 degrees.
  56  *
  57  * Astro application uses JAXP to query astronomical data saved in an XML dataset.
  58  * A FilterFactory implementation creates filter(A filter is an instance of a JAXP
  59  * TransformerHandler) from an XSL stylesheet.
  60  * A InputSourceFactory implementation creates a new sax input source from an XML file.
  61  * AstroProcessor leverages InputSourceFactory to parse catalog.xml, which saves
  62  * textual astronomical data, and then creates filters with specified parameters
  63  * from FilterFactory, all of the filters are chained together, AstroProcessor
  64  * appends the HTML filter at the end of filter chain, and hooks up the chain to
  65  * the input source, finally processes and outputs to the user specified output file.
  66  *
  67  * AstroTest drives AstroProcessor to run the specified queries(total 4 in setup),
  68  * and then compares the output with the golden files to determine PASS or FAIL.
  69  * It provides variant implementations of FilterFactory and InputSourceFactory to
  70  * AstroProcessor to test different JAXP classes and features.
  71  *
  72  */
  73 @Listeners({jaxp.library.FilePolicy.class})
  74 public class AstroTest {
  75     private FiltersAndGolden[] data;
  76 
  77     @BeforeClass
  78     public void setup() throws Exception {
  79         data = new FiltersAndGolden[4];
  80         data[0] = new FiltersAndGolden(getGoldenFileContent(1), astro -> astro.getRAFilter(0.106, 0.108));
  81         data[1] = new FiltersAndGolden(getGoldenFileContent(2), astro -> astro.getStellarTypeFilter("K0IIIbCN-0.5"));
  82         data[2] = new FiltersAndGolden(getGoldenFileContent(3), astro -> astro.getStellarTypeFilter("G"), astro -> astro.getDecFilter(-5.0, 60.0));
  83         data[3] = new FiltersAndGolden(getGoldenFileContent(4), astro -> astro.getRADECFilter(0.084, 0.096, -5.75, 14.0));
  84     }
  85 
  86     /*
  87      * Provide permutations of InputSourceFactory and FilterFactory for test.
  88      */
  89     @DataProvider(name = "factories")
  90     public Object[][] getQueryFactories() {
  91         return new Object[][] {
  92                 { StreamFilterFactoryImpl.class, InputSourceFactoryImpl.class },
  93                 { SAXFilterFactoryImpl.class, InputSourceFactoryImpl.class },
  94                 { DOMFilterFactoryImpl.class, InputSourceFactoryImpl.class },
  95                 { TemplatesFilterFactoryImpl.class, InputSourceFactoryImpl.class },
  96                 { StreamFilterFactoryImpl.class, DOML3InputSourceFactoryImpl.class } };
  97     }
  98 
  99     @Test(dataProvider = "factories")
 100     public void test(Class<FilterFactory> fFactClass, Class<InputSourceFactory> isFactClass) throws Exception {
 101         System.out.println(fFactClass.getName() +" : " + isFactClass.getName());
 102         AstroProcessor astro = new AstroProcessor(fFactClass, ASTROCAT, isFactClass);
 103 
 104         for (int i = 0; i < data.length; i++) {
 105             runProcess(astro, valueOf(i + 1), data[i].getGolden(), data[i].getFilters());
 106         }
 107     }
 108 
 109     private void runProcess(AstroProcessor astro, String processNum, List<String> goldenfileContent, FilterCreator... filterCreators) throws Exception {
 110         System.out.println("run process " + processNum);
 111         TransformerHandler[] filters = new TransformerHandler[filterCreators.length];
 112         for (int i = 0; i < filterCreators.length; i++)
 113             filters[i] = filterCreators[i].createFilter(astro);
 114 
 115         String outputfile = Files.createTempFile(Paths.get(USER_DIR), "query" + processNum + ".out.", null).toString();
 116         System.out.println("output file: " + outputfile);
 117         astro.process(outputfile, filters);
 118         assertEquals(Files.readAllLines(Paths.get(outputfile)), goldenfileContent);
 119     }
 120 
 121     private List<String>  getGoldenFileContent(int num) throws IOException {
 122         return Files.readAllLines(Paths.get(GOLDEN_DIR + "query" + num + ".out"));
 123     }
 124 
 125     @FunctionalInterface
 126     private interface FilterCreator {
 127         TransformerHandler createFilter(AstroProcessor astro) throws Exception;
 128     }
 129 
 130     private static class FiltersAndGolden {
 131         private FilterCreator[] filters;
 132         private List<String> golden;
 133 
 134         FiltersAndGolden(List<String> golden, FilterCreator... filters) {
 135             this.filters = filters;
 136             this.golden = golden;
 137         }
 138 
 139         FilterCreator[] getFilters() {
 140             return filters;
 141         }
 142 
 143         List<String> getGolden() {
 144             return golden;
 145         }
 146     }
 147 }
 148