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