4747 */
4848public class XSLTest extends NbTestCase {
4949 private static final String CONFIG_FILE1 = "ConfigFile1.xml" ; //NOI18N
50- private static final String CONFIG_FILE2 = "ConfigFile2.xml" ; //NOI18N
51- private static final String CONFIG_FILE3 = "ConfigFile3.xml" ; //NOI18N
52- private static final String CONFIG_EMPTY_CAT = "ConfigFileEmptyCatalog.xml" ; //NOI18N
50+ private static final String CONFIG_FILE2 = "ConfigFile2.xml" ; //NOI18N
51+ private static final String CONFIG_FILE3 = "ConfigFile3.xml" ; //NOI18N
52+ private static final String CONFIG_EMPTY_CAT = "ConfigFileEmptyCatalog.xml" ; //NOI18N
5353 private static final String BUILD_FILE1 = "BuildFile1.xml" ; //NOI18N
54- private static final String BUILD_FILE2 = "BuildFile2.xml" ; //NOI18N
55- private static final String BUILD_FILE3 = "BuildFile3.xml" ; //NOI18N
56- private static final String BUILD_EMPTY_CAT = "BuildFileEmptyCatalog.xml" ; //NOI18N
57-
58- private static final String XSL_FILE =
54+ private static final String BUILD_FILE2 = "BuildFile2.xml" ; //NOI18N
55+ private static final String BUILD_FILE3 = "BuildFile3.xml" ; //NOI18N
56+ private static final String BUILD_EMPTY_CAT = "BuildFileEmptyCatalog.xml" ; //NOI18N
57+
58+ private static final String XSL_FILE =
5959 "/org/netbeans/modules/xml/jaxb/resources/JAXBBuild.xsl" ; //NOI18N
6060 private static final String TEMP_BUILD_FILE = "jaxb_build" ; //NOI18N
61-
61+
6262 public XSLTest (String testName ) {
6363 super (testName );
6464 }
65-
65+
66+ @ Override
6667 public void setUp () throws Exception {
6768 }
68-
69+
70+ @ Override
6971 public void tearDown () throws Exception {
7072 }
71-
73+
7274 private InputStream getFromClasspath (String filePath ){
7375 return this .getClass ().getResourceAsStream (filePath );
7476 }
75-
77+
7678 private InputStream getDatafile (String filename ) throws FileNotFoundException {
7779 String dataFilename = "/org/netbeans/modules/xml/jaxb/util/" + filename ;
7880 return new FileInputStream (new File (getDataDir (), dataFilename ));
7981 }
80-
82+
8183 private String getString (InputStream stream ) throws IOException {
8284 BufferedReader br = new BufferedReader (new InputStreamReader (stream ));
83- StringBuffer sb = new StringBuffer ();
85+ StringBuilder sb = new StringBuilder ();
8486 String line = "" ;
8587 while (line != null ){
8688 line = br .readLine ();
8789 if (line != null ){
8890 sb .append (line );
8991 }
9092 }
91-
93+
9294 return sb .toString ();
9395 }
94-
96+
9597 private void compareStream (InputStream file1 , InputStream file2 ) throws IOException {
96- boolean ret = false ;
9798 String str1 = getString (file1 );
9899 //System.out.println("Str1:" + str1 + ":Str1");
99100 String str2 = getString (file2 );
100- //System.out.println("Str2:" + str2 + ":Str2");
101- //System.out.println("Length:" + str1.length() + ":" + str2.length());
101+ //System.out.println("Str2:" + str2 + ":Str2");
102+ //System.out.println("Length:" + str1.length() + ":" + str2.length());
102103 assertEquals (str1 , str2 );
103104 }
104-
105+
105106 private void transformConfig2Build (String configFile , String buildFile ){
106107 try {
107108 Source xmlSource = new StreamSource (getDatafile (configFile ));
108109 Source xslSource = new StreamSource (getFromClasspath (XSL_FILE ));
109110 File tmpFile = java .io .File .createTempFile (TEMP_BUILD_FILE , ".xml" );
110111 //System.out.println("tmpFile:" + tmpFile.getAbsolutePath());
111112 tmpFile .deleteOnExit ();
112- FileOutputStream fos = new FileOutputStream (tmpFile );
113- Result result = new StreamResult (fos );
114- TransformerFactory fact = TransformerFactory .newInstance ();
115- fact .setAttribute ("indent-number" , 4 ); //NOI18N
116- Transformer xformer = fact .newTransformer (xslSource );
117- xformer .setOutputProperty (OutputKeys .INDENT , "yes" ); //NOI18N
118- xformer .setOutputProperty (OutputKeys .METHOD , "xml" ); //NOI18N
119- xformer .transform (xmlSource , result );
113+ try (FileOutputStream fos = new FileOutputStream (tmpFile )) {
114+ Result result = new StreamResult (fos );
115+ TransformerFactory fact = TransformerFactory .newInstance ();
116+ fact .setAttribute ("indent-number" , 4 ); //NOI18N
117+ Transformer xformer = fact .newTransformer (xslSource );
118+ xformer .setOutputProperty (OutputKeys .INDENT , "yes" ); //NOI18N
119+ xformer .setOutputProperty (OutputKeys .METHOD , "xml" ); //NOI18N
120+ xformer .transform (xmlSource , result );
121+ }
120122 // Compare.
121- fos .close ();
122123 compareStream (getDatafile (buildFile ), new FileInputStream (tmpFile ));
123124 } catch (TransformerConfigurationException ex ) {
124125 Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
@@ -131,7 +132,7 @@ private void transformConfig2Build(String configFile, String buildFile){
131132 fail ("IOException" );
132133 }
133134 }
134-
135+
135136 /**
136137 * Test the XSL style sheet.
137138 **/
@@ -163,5 +164,5 @@ public void testXformConfig2BuildEmptyCatalog(){
163164 transformConfig2Build (CONFIG_EMPTY_CAT , BUILD_EMPTY_CAT );
164165 System .out .println ("testXformConfig2BuildEmptyCatalog done." );
165166 }
166-
167+
167168}
0 commit comments