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