Skip to content

Commit b6803ba

Browse files
committed
Merge remote-tracking branch 'origin/dev' into dev
2 parents 988f5bd + 11b8ed3 commit b6803ba

20 files changed

+589
-214
lines changed

test-complete/src/test/java/com/marklogic/javaclient/BasicJavaClientREST.java

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,49 @@ public void updateDocumentReaderHandle(DatabaseClient client, String filename, S
675675

676676
docStream.close();
677677
}
678+
679+
/* Read a binary file, and return its contents as an array of bytes.
680+
*
681+
*/
682+
683+
byte[] readBinaryFile(String binaryFile)
684+
{
685+
System.out.println("Read binary file : " + binaryFile);
686+
687+
File file = new File(binaryFile);
688+
System.out.println("File size is : " + file.length());
689+
690+
byte[] byteArray = new byte[(int)file.length()];
691+
692+
try {
693+
InputStream input = null;
694+
try {
695+
int totalBytesRead = 0;
696+
input = new BufferedInputStream(new FileInputStream(file));
697+
while(totalBytesRead < byteArray.length){
698+
int bytesRemaining = byteArray.length - totalBytesRead;
699+
//input.read() returns -1, 0, or more :
700+
int bytesRead = input.read(byteArray, totalBytesRead, bytesRemaining);
701+
if (bytesRead > 0){
702+
totalBytesRead = totalBytesRead + bytesRead;
703+
}
704+
}
705+
706+
System.out.println("Number of bytes read: " + totalBytesRead);
707+
}
708+
finally {
709+
System.out.println("Closing input stream.");
710+
input.close();
711+
}
712+
}
713+
catch (FileNotFoundException fnfex) {
714+
System.out.println("File not found Exception.");
715+
}
716+
catch (IOException ioex) {
717+
System.out.println(ioex);
718+
}
719+
return byteArray;
720+
}
678721

679722
/**
680723
* Read document using ReaderHandle
@@ -1070,19 +1113,30 @@ public void updateDocumentUsingByteHandle(DatabaseClient client, String filename
10701113
{
10711114
// create doc manager
10721115
DocumentManager docMgr = null;
1116+
byte[] contentInByte = null;
1117+
// variable to hold QA data directory path
1118+
String dirpath = new String("src/test/java/com/marklogic/javaclient/data/");
1119+
10731120
docMgr = documentManagerSelector(client, docMgr, type);
1074-
1075-
// acquire the content
1076-
FileReader content = new FileReader("src/test/java/com/marklogic/javaclient/data/" + filename);
1077-
//String contentInString = new String(content);
1078-
BufferedReader br = new BufferedReader(content);
1079-
String readContent = "";
1080-
String line = null;
1081-
while ((line = br.readLine()) != null)
1121+
1122+
if(type.equalsIgnoreCase("BINARY"))
1123+
{
1124+
contentInByte = readBinaryFile(dirpath + filename);
1125+
}
1126+
else
1127+
{
1128+
// acquire the content
1129+
FileReader content = new FileReader(dirpath + filename);
1130+
//String contentInString = new String(content);
1131+
BufferedReader br = new BufferedReader(content);
1132+
String readContent = "";
1133+
String line = null;
1134+
while ((line = br.readLine()) != null)
10821135
readContent = readContent + line;
1083-
br.close();
1136+
br.close();
10841137

1085-
byte[] contentInByte = (byte[])readContent.getBytes();
1138+
contentInByte = (byte[])readContent.getBytes();
1139+
}
10861140
// create an identifier for the document
10871141
String docId = uri;
10881142
// create a handle on the content

test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.marklogic.javaclient;
22

33
import java.io.File;
4-
54
import java.util.HashMap;
65

76
import com.marklogic.client.DatabaseClient;
@@ -18,7 +17,6 @@
1817
import com.marklogic.client.io.Format;
1918
import com.marklogic.client.io.StringHandle;
2019
import com.marklogic.client.io.DOMHandle;
21-
2220
import com.marklogic.client.io.JacksonHandle;
2321

2422

@@ -27,6 +25,7 @@
2725
import com.fasterxml.jackson.databind.ObjectMapper;
2826

2927
import org.junit.*;
28+
import org.junit.runners.MethodSorters;
3029

3130
import static org.junit.Assert.*;
3231

@@ -41,7 +40,7 @@
4140
*/
4241

4342

44-
43+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
4544
public class TestBulkReadSample1 extends BasicJavaClientREST {
4645

4746
private static final int BATCH_SIZE=100;
@@ -80,7 +79,7 @@ public void testCleanUp() throws Exception
8079
* Test Bulk Read to see you can read all the documents?
8180
*/
8281
@Test
83-
public void testReadMultipleTextDoc()
82+
public void test1ReadMultipleTextDoc()
8483
{
8584
int count=1;
8685
TextDocumentManager docMgr = client.newTextDocumentManager();
@@ -116,7 +115,7 @@ public void testReadMultipleTextDoc()
116115
* Verified by reading individual documents
117116
*/
118117
@Test
119-
public void testReadMultipleXMLDoc() throws Exception
118+
public void test2ReadMultipleXMLDoc() throws Exception
120119
{
121120
int count=1;
122121
XMLDocumentManager docMgr = client.newXMLDocumentManager();
@@ -158,7 +157,7 @@ public void testReadMultipleXMLDoc() throws Exception
158157
*
159158
*/
160159
@Test
161-
public void testReadMultipleBinaryDoc() throws Exception
160+
public void test3ReadMultipleBinaryDoc() throws Exception
162161
{
163162
String docId[] = {"Sega-4MB.jpg"};
164163
int count=1;
@@ -213,7 +212,7 @@ public void testReadMultipleBinaryDoc() throws Exception
213212
* This test has a bug logged in github with tracking Issue#33
214213
*/
215214
@Test
216-
public void testWriteMultipleJSONDocs() throws Exception
215+
public void test4WriteMultipleJSONDocs() throws Exception
217216
{
218217
int count=1;
219218
JSONDocumentManager docMgr = client.newJSONDocumentManager();
@@ -257,13 +256,16 @@ public void testWriteMultipleJSONDocs() throws Exception
257256

258257

259258
}
259+
260+
261+
260262
/*
261263
* This test uses GenericManager to load all different document types
262264
* This test has a bug logged in github with tracking Issue#33
263265
*
264266
*/
265267
@Test
266-
public void testWriteGenericDocMgr() throws Exception
268+
public void test5WriteGenericDocMgr() throws Exception
267269
{
268270

269271
GenericDocumentManager docMgr = client.newDocumentManager();
@@ -281,13 +283,7 @@ public void testWriteGenericDocMgr() throws Exception
281283
}
282284
// for(String uri:uris){System.out.println(uri);}
283285
DocumentPage page = docMgr.read(uris);
284-
if(!page.hasNext()){
285-
testReadMultipleTextDoc();
286-
testReadMultipleXMLDoc();
287-
testReadMultipleBinaryDoc();
288-
testWriteMultipleJSONDocs();
289-
page = docMgr.read(uris);
290-
}
286+
291287
while(page.hasNext()){
292288
DocumentRecord rec = page.next();
293289
switch(rec.getFormat())
@@ -307,6 +303,51 @@ public void testWriteGenericDocMgr() throws Exception
307303
assertEquals("binary document count", 25,countJpg);
308304
assertEquals("Json document count", 25,countJson);
309305
}
306+
//test for Issue# 107
307+
@Test
308+
public void test6CloseMethodforReadMultipleDoc() throws Exception
309+
{
310+
int count=1;
311+
DocumentPage page;
312+
JSONDocumentManager docMgr = client.newJSONDocumentManager();
313+
DocumentWriteSet writeset =docMgr.newWriteSet();
314+
315+
HashMap<String,String> map= new HashMap<String,String>();
316+
for(int j=0;j<102;j++){
317+
for(int i =0;i<10;i++){
318+
JsonNode jn = new ObjectMapper().readTree("{\"animal"+i+"\":\"dog"+i+"\", \"says\":\"woof\"}");
319+
JacksonHandle jh = new JacksonHandle();
320+
jh.set(jn);
321+
writeset.add(DIRECTORY+j+"/dog"+i+".json",jh);
322+
map.put(DIRECTORY+j+"/dog"+i+".json", jn.toString());
323+
}
324+
docMgr.write(writeset);
325+
writeset = docMgr.newWriteSet();
326+
}
327+
328+
String uris[] = new String[100];
329+
for(int j=0;j<102;j++){
330+
for(int i =0;i<10;i++){
331+
uris[i]=DIRECTORY+j+"/dog"+i+".json";
332+
}
333+
count=0;
334+
page = docMgr.read(uris);
335+
336+
DocumentRecord rec;
337+
JacksonHandle jh = new JacksonHandle();
338+
while(page.hasNext()){
339+
rec = page.next();
340+
validateRecord(rec,Format.JSON);
341+
rec.getContent(jh);
342+
assertEquals("Comparing the content :",map.get(rec.getUri()),jh.get().toString());
343+
count++;
344+
} page.close();
345+
// validateRecord(rec,Format.JSON);
346+
assertEquals("document count", 10,count);
347+
}
348+
349+
}
350+
310351
@AfterClass
311352
public static void tearDown() throws Exception
312353
{

0 commit comments

Comments
 (0)