Skip to content

Commit

Permalink
fix some warnings (#14)
Browse files Browse the repository at this point in the history
* fix some warnings
  • Loading branch information
elharo authored Feb 19, 2020
1 parent deacec0 commit ac64c42
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,19 @@ protected HtmlPage htmlPage( String htmlFile )
assertTrue( file.exists() );

// HtmlUnit
WebClient webClient = new WebClient();
webClient.getOptions().setCssEnabled( false );

return (HtmlPage) webClient.getPage( file.toURI().toURL() );
try ( WebClient webClient = new WebClient() ) {
webClient.getOptions().setCssEnabled( false );

return (HtmlPage) webClient.getPage( file.toURI().toURL() );
}
}

/**
* Verify a HtmlPage.
*
* @param file the file to verify.
*
* @throws java.lang.Exception if something goes wrong;
* @throws java.lang.Exception if something goes wrong
*/
public abstract void verify( String file )
throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
import com.gargoylesoftware.htmlunit.html.HtmlSection;
import com.gargoylesoftware.htmlunit.html.HtmlTeletype;

import java.util.Iterator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -233,7 +234,10 @@ public void testVelocityToolManager()
renderer.mergeDocumentIntoSite( writer, sink, siteRenderingContext );

String renderResult = writer.toString();
String expectedResult = IOUtils.toString( getClass().getResourceAsStream( "velocity-toolmanager.expected.txt" ) );
String expectedResult =
IOUtils.toString(
getClass().getResourceAsStream( "velocity-toolmanager.expected.txt" ),
StandardCharsets.UTF_8 );
expectedResult = StringUtils.unifyLineSeparators( expectedResult );
assertEquals( expectedResult, renderResult );
}
Expand Down Expand Up @@ -261,8 +265,8 @@ public void testVelocityToolManagerForTemplate()
renderer.mergeDocumentIntoSite( writer, sink, siteRenderingContext );

String renderResult = writer.toString();
String expectedResult = IOUtils.toString( getClass().getResourceAsStream( "velocity-toolmanager.expected.txt" ) );
expectedResult = StringUtils.unifyLineSeparators( expectedResult );
InputStream in = getClass().getResourceAsStream( "velocity-toolmanager.expected.txt" );
String expectedResult = StringUtils.unifyLineSeparators( IOUtils.toString( in, StandardCharsets.UTF_8 ) );
assertEquals( expectedResult, renderResult );
}

Expand Down Expand Up @@ -290,8 +294,10 @@ public void testVelocityToolManagerForSkin()
SiteRendererSink sink = new SiteRendererSink( context );
renderer.mergeDocumentIntoSite( writer, sink, siteRenderingContext );
String renderResult = writer.toString();
String expectedResult = IOUtils.toString( getClass().getResourceAsStream( "velocity-toolmanager.expected.txt" ) );
expectedResult = StringUtils.unifyLineSeparators( expectedResult );
String expectedResult = StringUtils.unifyLineSeparators(
IOUtils.toString(
getClass().getResourceAsStream( "velocity-toolmanager.expected.txt" ),
StandardCharsets.UTF_8 ) );
assertEquals( expectedResult, renderResult );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.gargoylesoftware.htmlunit.html.HtmlLink;
import com.gargoylesoftware.htmlunit.html.HtmlMeta;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlStyle;
import com.gargoylesoftware.htmlunit.html.HtmlTitle;

import java.util.Iterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,46 +60,47 @@ public void verify( String file )
assertTrue( jsTest.exists() );

// HtmlUnit
WebClient webClient = new WebClient();
webClient.getOptions().setCssEnabled( false );

final List<String> collectedAlerts = new ArrayList<String>( 4 );
webClient.setAlertHandler( new CollectingAlertHandler( collectedAlerts ) );

HtmlPage page = (HtmlPage) webClient.getPage( jsTest.toURI().toURL() );
assertNotNull( page );

HtmlElement element = page.getHtmlElementById( "contentBox" );
assertNotNull( element );
HtmlDivision division = (HtmlDivision) element;
assertNotNull( division );

Iterator<HtmlElement> elementIterator = division.getHtmlElementDescendants().iterator();

// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------

HtmlSection section = (HtmlSection) elementIterator.next();
assertNotNull( section );

HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
assertNotNull( h2 );
assertEquals( "Test", h2.asText().trim() );

HtmlAnchor a = (HtmlAnchor) elementIterator.next();
assertNotNull( a );
assertEquals( "Test", a.getAttribute( "name" ) );

HtmlParagraph p = (HtmlParagraph) elementIterator.next();
assertNotNull( p );
assertEquals( "You should see a JavaScript alert...", p.asText().trim() );

HtmlScript script = (HtmlScript) elementIterator.next();
assertNotNull( script );
assertEquals( "text/javascript", script.getAttribute( "type" ) );
assertEquals( "", script.asText().trim() );
final List<String> expectedAlerts = Collections.singletonList( "Hello!" );
assertEquals( expectedAlerts, collectedAlerts );
try ( WebClient webClient = new WebClient() ) {
webClient.getOptions().setCssEnabled( false );

final List<String> collectedAlerts = new ArrayList<String>( 4 );
webClient.setAlertHandler( new CollectingAlertHandler( collectedAlerts ) );

HtmlPage page = (HtmlPage) webClient.getPage( jsTest.toURI().toURL() );
assertNotNull( page );

HtmlElement element = page.getHtmlElementById( "contentBox" );
assertNotNull( element );
HtmlDivision division = (HtmlDivision) element;
assertNotNull( division );

Iterator<HtmlElement> elementIterator = division.getHtmlElementDescendants().iterator();

// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------

HtmlSection section = (HtmlSection) elementIterator.next();
assertNotNull( section );

HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
assertNotNull( h2 );
assertEquals( "Test", h2.asText().trim() );

HtmlAnchor a = (HtmlAnchor) elementIterator.next();
assertNotNull( a );
assertEquals( "Test", a.getAttribute( "name" ) );

HtmlParagraph p = (HtmlParagraph) elementIterator.next();
assertNotNull( p );
assertEquals( "You should see a JavaScript alert...", p.asText().trim() );

HtmlScript script = (HtmlScript) elementIterator.next();
assertNotNull( script );
assertEquals( "text/javascript", script.getAttribute( "type" ) );
assertEquals( "", script.asText().trim() );
List<String> expectedAlerts = Collections.singletonList( "Hello!" );
assertEquals( expectedAlerts, collectedAlerts );
}
}
}

0 comments on commit ac64c42

Please # to comment.