1// don't forget to set the proper hostname 2var fs = new davlib.DavFS(); 3fs.initialize(); 4 5function writeToDiv(line, emphasize) { 6 var div = document.getElementById('testdiv'); 7 var textnode = document.createTextNode(line); 8 var newdiv = document.createElement('div'); 9 newdiv.appendChild(textnode); 10 if (emphasize) { 11 newdiv.style.color = 'red'; 12 } else { 13 newdiv.style.color = 'blue'; 14 }; 15 div.appendChild(newdiv); 16}; 17 18function assert(statement, debugprint) { 19 if (!statement) { 20 writeToDiv('FAILURE: ' + debugprint, 1); 21 throw(new exceptions.AssertionError('failure ' + debugprint)); 22 } else { 23 writeToDiv('success'); 24 }; 25}; 26 27// since the lib is async I wrote the functions in the order 28// they are executed to give a bit of an overview 29function runTests() { 30 testMkDir(); 31}; 32 33function testMkDir() { 34 function handler(error) { 35 assert(!error, 'MkDir test failed, expected no error, got ' + error); 36 testWrite(); 37 }; 38 writeToDiv('Going to create dir /davtests/foodir/:'); 39 fs.mkDir('/davtests/foodir/', handler); 40}; 41 42function testWrite() { 43 function handler(error) { 44 assert(!error, 'Write test failed, expected no error, got ' + error); 45 testCopy(); 46 }; 47 writeToDiv('Going to write to file /davtests/foodir/foo:'); 48 fs.write('/davtests/foodir/foo', 'foo!', handler); 49}; 50 51function testCopy() { 52 function handler(error) { 53 assert(!error, 'Copy test failed, expected no error, got ' + error); 54 testListDir(); 55 }; 56 writeToDiv('Going to copy file /davtests/foodir/foo to ' + 57 '/davtests/foodir/bar:'); 58 fs.copy('/davtests/foodir/foo', '/davtests/foodir/bar', handler); 59}; 60 61function testListDir() { 62 function handler(error, content) { 63 assert(!error, 'ListDir test failed, expected no error, got ' + 64 error); 65 if (!error) { 66 writeToDiv('content: ' + 67 (content.toSource ? content.toSource() : content)); 68 }; 69 testMove(); 70 }; 71 writeToDiv('Going to list dir /davtests/foodir/:'); 72 fs.listDir('/davtests/foodir/', handler); 73}; 74 75function testMove() { 76 function handler(error) { 77 assert(!error, 'Move test failed, expected no error, got ' + error); 78 testGetProps(); 79 }; 80 writeToDiv('Going to move file /davtests/foodir/foo to ' + 81 '/davtests/foodir/bar:'); 82 fs.move('/davtests/foodir/foo', '/davtests/foodir/bar', handler); 83}; 84 85function testGetProps() { 86 function handler(error, content) { 87 assert(!error, 'GetProps test failed, expected no error, got ' + 88 error); 89 if (!error) { 90 writeToDiv('DAV: properties:'); 91 for (var name in content['DAV:']) { 92 writeToDiv(name + ': ' + content['DAV:'][name].toXML()); 93 }; 94 }; 95 testLocked1(); 96 }; 97 writeToDiv('Going to get properties of file /davtests/foodir/bar:'); 98 fs.getProps('/davtests/foodir/bar', handler); 99}; 100 101function testLocked1() { 102 function handler(error, content) { 103 assert((!error && !content), 104 'Locked test 1 failed, expected no error and false as return ' + 105 'value, received ' + error + ' as error and ' + content + ' ' + 106 'as content'); 107 testLock(); 108 }; 109 writeToDiv('Going to check whether file /davtests/foodir/bar is unlocked:' 110 ); 111 fs.isLocked('/davtests/foodir/bar', handler); 112}; 113 114function testLock() { 115 function handler(error, locktoken) { 116 assert(!error, 'Lock test failed, expected no error, got ' + error); 117 testLocked2(locktoken); 118 }; 119 writeToDiv('Going to lock file /davtests/foodir/bar:'); 120 fs.lock('/davtests/foodir/bar', 'johnny', handler); 121}; 122 123function testLocked2(locktoken) { 124 function handler(error, content) { 125 assert((!error && content), 126 'Locked test 2 failed, expected no error and true as return ' + 127 'value, received ' + error + ' as error and ' + content + ' ' + 128 'as content'); 129 testRemoveLocked(locktoken); 130 }; 131 writeToDiv('Going to check whether file /davtests/foodir/bar is locked:'); 132 fs.isLocked('/davtests/foodir/bar', handler); 133}; 134 135function testRemoveLocked(locktoken) { 136 function handler(error) { 137 assert(error, 'RemoveLocked test failed, expected a locked error, ' + 138 'but did not occur'); 139 testUnlock(locktoken); 140 }; 141 writeToDiv('Going to try to remove locked file /davtests/foodir/bar ' + 142 '(success means file could not be removed):'); 143 fs.remove('/davtests/foodir/bar', handler); 144}; 145 146function testUnlock(locktoken) { 147 function handler(error) { 148 assert(!error, 'Unlock test failed, expected no error, got ' + error); 149 testRemove(); 150 }; 151 writeToDiv('Going to remove file /davtests/foodir/bar:'); 152 fs.unlock('/davtests/foodir/bar', locktoken, handler); 153}; 154 155function testRemove() { 156 function handler(error) { 157 assert(!error, 'Remove test failed, expected no error, got ' + error); 158 testRemoveDir(); 159 }; 160 writeToDiv('Going to remove file /davtests/foodir/bar:'); 161 fs.remove('/davtests/foodir/bar', handler); 162}; 163 164function testRemoveDir() { 165 function handler(error) { 166 assert(!error, 'Remove test failed, expected no error, got ' + error); 167 }; 168 writeToDiv('Going to remove dir /davtests/foodir/:'); 169 fs.remove('/davtests/foodir/', handler); 170}; 171 172