Skip to content

Commit d4c869a

Browse files
committed
Merge remote-tracking branch 'origin/newRegressionTests' into dev
2 parents 9c7b561 + 9f14e33 commit d4c869a

File tree

4 files changed

+137
-42
lines changed

4 files changed

+137
-42
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
{

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ public class TestBytesHandle extends BasicJavaClientREST{
2323
private static String dbName = "BytesHandleDB";
2424
private static String [] fNames = {"BytesHandleDB-1"};
2525
private static String restServerName = "REST-Java-Client-API-Server";
26+
//Additional port to test for Uber port
27+
private static int uberPort = 8000;
28+
2629
@BeforeClass
2730
public static void setUp() throws Exception{
2831
System.out.println("In setup");
2932
setupJavaRESTServer(dbName, fNames[0], restServerName,8011);
33+
createUserRolesWithPrevilages("test-eval","xdbc:eval", "xdbc:eval-in","xdmp:eval-in","any-uri","xdbc:invoke");
34+
createRESTUser("eval-user", "x", "test-eval","rest-admin","rest-writer","rest-reader");
3035
}
3136

3237
@Test
@@ -39,10 +44,10 @@ public void testXmlCRUD() throws IOException , SAXException, ParserConfiguration
3944
XMLUnit.setNormalizeWhitespace(true);
4045

4146
// connect the client
42-
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x",Authentication.DIGEST);
47+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x",Authentication.DIGEST);
4348

4449
// write docs
45-
writeDocumentUsingBytesHandle(client, filename, uri, null,"XML");//***********
50+
writeDocumentUsingBytesHandle(client, filename, uri, null,"XML");
4651

4752
//read docs
4853
BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"XML");
@@ -101,7 +106,7 @@ public void testTextCRUD() throws IOException, ParserConfigurationException, SAX
101106
System.out.println("Runing test TextCRUD");
102107

103108
// connect the client
104-
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
109+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
105110

106111
// write docs
107112
writeDocumentUsingBytesHandle(client, filename, uri, "Text");
@@ -161,7 +166,7 @@ public void testJsonCRUD() throws IOException, ParserConfigurationException, SAX
161166
ObjectMapper mapper = new ObjectMapper();
162167

163168
// connect the client
164-
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
169+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
165170

166171
// write docs
167172
writeDocumentUsingBytesHandle(client, filename, uri, "JSON");
@@ -220,7 +225,7 @@ public void testBinaryCRUD() throws IOException, ParserConfigurationException, S
220225
System.out.println("Running testBinaryCRUD");
221226

222227
// connect the client
223-
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
228+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
224229

225230
// write docs
226231
writeDocumentUsingBytesHandle(client, filename, uri, "Binary");
@@ -250,8 +255,8 @@ public void testBinaryCRUD() throws IOException, ParserConfigurationException, S
250255

251256
// get the binary size
252257
long sizeUpdate = getBinarySizeFromByte(fileReadUpdate);
253-
long expectedSizeUpdate = 3290;
254-
//long expectedSizeUpdate = 3322;
258+
//long expectedSizeUpdate = 3290;
259+
long expectedSizeUpdate = 3322;
255260
assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate);
256261

257262
// delete the document

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,16 @@
1616
import com.marklogic.client.DatabaseClient;
1717
import com.marklogic.client.DatabaseClientFactory;
1818
import com.marklogic.client.DatabaseClientFactory.Authentication;
19-
import com.marklogic.client.MarkLogicIOException;
19+
import com.marklogic.client.ResourceNotFoundException;
2020
import com.marklogic.client.document.DocumentManager.Metadata;
21-
import com.marklogic.client.document.DocumentPatchBuilder;
2221
import com.marklogic.client.document.DocumentWriteSet;
2322
import com.marklogic.client.document.JSONDocumentManager;
2423
import com.marklogic.client.io.DocumentMetadataHandle;
2524
import com.marklogic.client.io.DocumentMetadataHandle.Capability;
26-
import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections;
27-
import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions;
28-
import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties;
2925
import com.marklogic.client.io.Format;
3026
import com.marklogic.client.io.JacksonHandle;
3127
import com.marklogic.client.pojo.PojoRepository;
3228
import com.marklogic.client.pojo.annotation.Id;
33-
import com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod.SmallArtifactMissingGetter;
3429

3530
public class TestPOJOWithDocsStoredByOthers extends BasicJavaClientREST {
3631

@@ -251,7 +246,7 @@ public void validateSmallArtifact(SmallArtifactIdInSuper artifact) {
251246
public void validateSmallArtifactSuper(SmallArtifactNoId artifact) {
252247
assertNotNull("Artifact object should never be Null", artifact);
253248
assertNotNull("Id should never be Null", artifact.getId());
254-
assertEquals("Id of the object is ", -99, artifact.getId());
249+
assertEquals("Id of the object is ", 0, artifact.getId());
255250
assertEquals("Name of the object is ", "SmallArtifactInSuperOnly",
256251
artifact.getName());
257252
assertEquals("Inventory of the object is ", 1000,
@@ -333,7 +328,7 @@ public void validateSubObjReferencedbySuperClassvariableOne(SmallArtifactIdInSup
333328
* Issue 136 might solve this also.
334329
*/
335330

336-
@Test
331+
@Test(expected=IllegalArgumentException.class)
337332
public void testPOJOReadDocStoredWithInvalidContent() throws Exception {
338333

339334
String docId[] = { "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifactIdInSuper/SmallArtifactIdInSuper.json" };
@@ -420,11 +415,11 @@ public void testPOJOReadDocStoredWithNoBeanProperty() throws Exception {
420415
* Purpose : This test is to validate read documents with valid POJO
421416
* specific URI and has invalid data-types for one of the bean property.
422417
* Uses SmallArtifact class which has @Id on the name methods. Test result
423-
* expectations are: read should return exception.
418+
* expectations are: read should return ResourceNotFoundException exception.
424419
* Field inventory has a String
425420
*/
426421

427-
@Test(/*expected=MarkLogicIOException.class*/)
422+
@Test(expected=ResourceNotFoundException.class)
428423
public void testPOJOReadDocStoredWithInvalidDataType() throws Exception {
429424

430425
String docId[] = { "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact/SmallArtifact.json" };

0 commit comments

Comments
 (0)