Skip to content

Commit 853ff39

Browse files
georgeajitgeorgeajit
authored andcommitted
#725 - Tests for evalAs(Class) that return some non-streaming responses
at least as long as 17,400 chars.
1 parent e8c4ea5 commit 853ff39

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

test-complete/src/test/java/com/marklogic/client/functionaltest/TestEvalJavaScript.java

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.w3c.dom.Document;
4141
import org.xml.sax.InputSource;
4242

43+
import com.fasterxml.jackson.databind.JsonNode;
4344
import com.fasterxml.jackson.databind.ObjectMapper;
4445
import com.marklogic.client.DatabaseClient;
4546
import 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
}

test-complete/src/test/java/com/marklogic/client/functionaltest/TestEvalXquery.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ else if (er.getType().equals(Type.STRING)) {
229229
assertFalse("getting in else part, missing a type ", true);
230230
}
231231
}
232+
evr.close();
232233
}
233234

234235
// This test intended to verify a simple xquery for inserting and reading
@@ -291,8 +292,8 @@ else if (er.getType().equals(Type.TEXTNODE)) {
291292
} else {
292293
System.out.println("Something went wrong");
293294
}
294-
295295
}
296+
evr.close();
296297
}
297298

298299
// This test is intended to test eval(T handle), passing input stream handle
@@ -303,14 +304,16 @@ public void testXqueryReturningDifferentTypesAndFormatsWithHandle() throws KeyMa
303304
InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/xqueries.txt");
304305
InputStreamHandle ish = new InputStreamHandle();
305306
ish.set(inputStream);
307+
EvalResultIterator evr = null;
306308

307309
try {
308-
EvalResultIterator evr = client.newServerEval().xquery(ish).eval();
310+
evr = client.newServerEval().xquery(ish).eval();
309311
this.validateReturnTypes(evr);
310312
} catch (Exception e) {
311313
throw e;
312314
} finally {
313315
inputStream.close();
316+
evr.close();
314317
}
315318
}
316319

@@ -412,6 +415,7 @@ else if (er.getType().equals(Type.TEXTNODE)) {
412415
System.out.println("No corresponding type found for :" + er.getType());
413416
}
414417
}
418+
evr.close();
415419

416420
} catch (Exception e) {
417421
throw e;
@@ -455,6 +459,7 @@ public void testXqueryWithExtVarAsNode() throws KeyManagementException, NoSuchAl
455459
// System.out.println("Type XML :"+convertXMLDocumentToString(dh.get()));
456460
assertEquals("document has content", "<foo attr=\"attribute\"><?processing instruction?><!--comment-->test1</foo>", convertXMLDocumentToString(dh.get()));
457461
}
462+
evr.close();
458463
} catch (Exception e) {
459464
throw e;
460465
}
@@ -493,6 +498,7 @@ public void testXqueryInvokeModuleRetDiffTypes() throws KeyManagementException,
493498
.addVariableAs("myNull", (String) null);
494499
EvalResultIterator evr = evl.eval();
495500
this.validateReturnTypes(evr);
501+
evr.close();
496502

497503
} catch (Exception e) {
498504
throw e;

0 commit comments

Comments
 (0)