4040import org .w3c .dom .Document ;
4141import org .xml .sax .InputSource ;
4242
43+ import com .fasterxml .jackson .databind .JsonNode ;
4344import com .fasterxml .jackson .databind .ObjectMapper ;
4445import com .marklogic .client .DatabaseClient ;
4546import com .marklogic .client .DatabaseClientFactory ;
@@ -351,6 +352,7 @@ public void testJSReturningDifferentTypesOrder1() throws KeyManagementException,
351352 System .out .println ("Something went wrong" );
352353 }
353354 }
355+ evr .close ();
354356 }
355357
356358 // This test is intended to test eval(T handle), passing input stream handle
@@ -390,6 +392,7 @@ public void testJSReturningDifferentTypesOrder2() throws KeyManagementException,
390392 System .out .println ("Something went wrong" );
391393 }
392394 }
395+ evr .close ();
393396 } catch (Exception e ) {
394397 throw e ;
395398 } finally {
@@ -439,6 +442,7 @@ public void testJSDifferentVariableTypes() throws KeyManagementException, NoSuch
439442 System .out .println (new ObjectMapper ().createObjectNode ().nullNode ().toString ());
440443 EvalResultIterator evr = evl .eval ();
441444 this .validateReturnTypes (evr );
445+ evr .close ();
442446
443447 } catch (Exception e ) {
444448 throw e ;
@@ -486,6 +490,7 @@ public void testJSDifferentVariableTypesWithNulls() throws KeyManagementExceptio
486490 System .out .println (new ObjectMapper ().createObjectNode ().nullNode ().toString ());
487491 EvalResultIterator evr = evl .eval ();
488492 this .validateReturnTypes (evr );
493+ evr .close ();
489494
490495 } catch (Exception e ) {
491496 throw e ;
@@ -528,6 +533,7 @@ public void testJSDifferentVariableTypesNoNullNodes() throws KeyManagementExcept
528533 System .out .println (new ObjectMapper ().createObjectNode ().nullNode ().toString ());
529534 EvalResultIterator evr = evl .eval ();
530535 this .validateReturnTypes (evr );
536+ evr .close ();
531537
532538 } catch (Exception e ) {
533539 throw e ;
@@ -551,7 +557,7 @@ public void testMultipleJSfnOnServerEval() {
551557 .evalAs (String .class );
552558 System .out .println (response3 );
553559 }
554-
560+
555561 // Issue 30209 ,external variable passing is not working so I have test cases
556562 // where it test to see we can invoke a module
557563 @ Test
@@ -605,6 +611,7 @@ public void testJSReturningDifferentTypesOrder3fromModules() throws Exception {
605611 System .out .println ("Something went wrong" );
606612 }
607613 }
614+ evr .close ();
608615 } catch (Exception e ) {
609616 throw e ;
610617 } finally {
@@ -614,4 +621,69 @@ public void testJSReturningDifferentTypesOrder3fromModules() throws Exception {
614621 moduleClient .release ();
615622 }
616623 }
624+
625+ /*
626+ * Test is intended to test ServerEvaluationCall.evalAs(Class<T>) closing underlying streams prematurely
627+ * Git Issue 725
628+ */
629+ @ Test
630+ public void testStreamClosingWithEvalAs () throws KeyManagementException , NoSuchAlgorithmException , Exception {
631+ try {
632+ String query1 =
633+ "var phrase = 'MarkLogic Corp';"
634+ + "var repeat = 10;"
635+ + "for (var i = 0; i < repeat; i++) {"
636+ + "phrase += phrase.concat(phrase);"
637+ + "}"
638+ + "phrase;" ;
639+
640+ String query2 =
641+ "var phrase2 = 'Java Client API';"
642+ + "var repeat = 10;"
643+ + "for (var i = 0; i < repeat; i++) {"
644+ + "phrase2 += phrase2.concat(phrase2);"
645+ + "}"
646+ + "var jsStr = JSON.stringify(phrase2);"
647+ + "jsStr;" ;
648+
649+ ServerEvaluationCall evl = client .newServerEval ().javascript (query1 );
650+
651+ // Using evalAs(String.class)
652+ String evr = evl .evalAs (String .class );
653+
654+ int nLen = evr .length ();
655+ String firstFiftyChars = evr .substring (0 , 50 );
656+ String lastTenChars = evr .substring (evr .length () - 10 );
657+
658+ // Make sure we can access results that are greater than 17,400 chars. See Git Issue 725.
659+ System .out .println ("String length is " + nLen );
660+ System .out .println ("First Fifty String output is " + firstFiftyChars );
661+ System .out .println ("Last Ten String output is " + lastTenChars );
662+
663+ assertTrue ("String length incorrect" , nLen ==826686 );
664+ assertTrue ("First Fifty String output incorrect " , firstFiftyChars .equalsIgnoreCase ("MarkLogic CorpMarkLogic CorpMarkLogic CorpMarkLogi" ));
665+ assertTrue ("Last Ten String output incorrect " , lastTenChars .equalsIgnoreCase ("Logic Corp" ));
666+
667+ // Using evalAs(JsonNode.class)
668+ ServerEvaluationCall ev2 = client .newServerEval ().javascript (query2 );
669+ JsonNode jNode = ev2 .evalAs (JsonNode .class );
670+
671+ String jsonStr = jNode .asText ();
672+ int nJLen = jsonStr .length ();
673+ String firstFiftyCharsJson = jsonStr .substring (0 , 50 );
674+ String lastTenCharsJson = jsonStr .substring (jsonStr .length () - 10 );
675+
676+ // Make sure we can access results that are greater than 17,400 chars. See Git Issue 725.
677+ System .out .println ("JSON String length is " + nJLen );
678+ System .out .println ("First Fifty String JSON output is " + firstFiftyCharsJson );
679+ System .out .println ("Last Ten String JSON output is " + lastTenCharsJson );
680+
681+ assertTrue ("JSON String length incorrect" , nJLen ==885735 );
682+ assertTrue ("First Fifty JSON String output incorrect " , firstFiftyCharsJson .equalsIgnoreCase ("Java Client APIJava Client APIJava Client APIJava " ));
683+ assertTrue ("Last Ten JSON String output incorrect " , lastTenCharsJson .equalsIgnoreCase ("Client API" ));
684+
685+ } catch (Exception e ) {
686+ throw e ;
687+ }
688+ }
617689}
0 commit comments