📄 arcwritertest.html
字号:
<a name="260" href="#260">260</a> rec.close();<a name="261" href="#261">261</a> <strong>if</strong> (count != 0) {<a name="262" href="#262">262</a> assertTrue(<span class="string">"Unexpected URL "</span> + rec.getMetaData().getUrl(),<a name="263" href="#263">263</a> rec.getMetaData().getUrl().equals(SOME_URL));<a name="264" href="#264">264</a> }<a name="265" href="#265">265</a> count++;<a name="266" href="#266">266</a> }<a name="267" href="#267">267</a> <strong>return</strong> count;<a name="268" href="#268">268</a> }<a name="269" href="#269">269</a> <a name="270" href="#270">270</a> <strong>protected</strong> <a href="../../../../org/archive/io/arc/ARCWriter.html">ARCWriter</a> createArcWithOneRecord(String name,<a name="271" href="#271">271</a> <strong>boolean</strong> compressed)<a name="272" href="#272">272</a> throws IOException {<a name="273" href="#273">273</a> ARCWriter writer = createARCWriter(name, compressed);<a name="274" href="#274">274</a> String content = getContent();<a name="275" href="#275">275</a> writeRecord(writer, SOME_URL, <span class="string">"text/html"</span>,<a name="276" href="#276">276</a> content.length(), getBaos(content));<a name="277" href="#277">277</a> <strong>return</strong> writer;<a name="278" href="#278">278</a> }<a name="279" href="#279">279</a> <a name="280" href="#280">280</a> <strong>public</strong> <strong>void</strong> testSpaceInURL() {<a name="281" href="#281">281</a> String eMessage = <strong>null</strong>;<a name="282" href="#282">282</a> <strong>try</strong> {<a name="283" href="#283">283</a> holeyUrl(<span class="string">"testSpaceInURL-"</span> + PREFIX, false, <span class="string">" "</span>);<a name="284" href="#284">284</a> } <strong>catch</strong> (IOException e) {<a name="285" href="#285">285</a> eMessage = e.getMessage();<a name="286" href="#286">286</a> }<a name="287" href="#287">287</a> assertTrue(<span class="string">"Didn't get expected exception: "</span> + eMessage,<a name="288" href="#288">288</a> eMessage.startsWith(<span class="string">"Metadata line doesn't match"</span>));<a name="289" href="#289">289</a> }<a name="290" href="#290">290</a> <a name="291" href="#291">291</a> <strong>public</strong> <strong>void</strong> testTabInURL() { <a name="292" href="#292">292</a> String eMessage = <strong>null</strong>;<a name="293" href="#293">293</a> <strong>try</strong> {<a name="294" href="#294">294</a> holeyUrl(<span class="string">"testTabInURL-"</span> + PREFIX, false, <span class="string">"\t"</span>);<a name="295" href="#295">295</a> } <strong>catch</strong> (IOException e) {<a name="296" href="#296">296</a> eMessage = e.getMessage();<a name="297" href="#297">297</a> }<a name="298" href="#298">298</a> assertTrue(<span class="string">"Didn't get expected exception: "</span> + eMessage,<a name="299" href="#299">299</a> eMessage.startsWith(<span class="string">"Metadata line doesn't match"</span>));<a name="300" href="#300">300</a> }<a name="301" href="#301">301</a> <a name="302" href="#302">302</a> <strong>protected</strong> <strong>void</strong> holeyUrl(String name, <strong>boolean</strong> compress, String urlInsert)<a name="303" href="#303">303</a> throws IOException {<a name="304" href="#304">304</a> ARCWriter writer = createArcWithOneRecord(name, compress);<a name="305" href="#305">305</a> <em class="comment">// Add some bytes on the end to mess up the record.</em><a name="306" href="#306">306</a> String content = getContent();<a name="307" href="#307">307</a> ByteArrayOutputStream baos = getBaos(content);<a name="308" href="#308">308</a> writeRecord(writer, SOME_URL + urlInsert + <span class="string">"/index.html"</span>, <span class="string">"text/html"</span>,<a name="309" href="#309">309</a> content.length(), baos);<a name="310" href="#310">310</a> writer.close();<a name="311" href="#311">311</a> }<a name="312" href="#312">312</a> <a name="313" href="#313">313</a> <em class="comment">// If uncompressed, length has to be right or parse will fail.</em><a name="314" href="#314">314</a> <em class="comment">//</em><a name="315" href="#315">315</a> <em class="comment">// public void testLengthTooShort() throws IOException {</em><a name="316" href="#316">316</a> <em class="comment">// lengthTooShort("testLengthTooShort-" + PREFIX, false);</em><a name="317" href="#317">317</a> <em class="comment">// }</em><a name="318" href="#318">318</a> <a name="319" href="#319">319</a> <strong>public</strong> <strong>void</strong> testLengthTooShortCompressed() throws IOException {<a name="320" href="#320">320</a> lengthTooShort(<span class="string">"testLengthTooShortCompressed-"</span> + PREFIX, <strong>true</strong>, false);<a name="321" href="#321">321</a> }<a name="322" href="#322">322</a> <a name="323" href="#323">323</a> <strong>public</strong> <strong>void</strong> testLengthTooShortCompressedStrict()<a name="324" href="#324">324</a> throws IOException { <a name="325" href="#325">325</a> String eMessage = <strong>null</strong>;<a name="326" href="#326">326</a> <strong>try</strong> {<a name="327" href="#327">327</a> lengthTooShort(<span class="string">"testLengthTooShortCompressedStrict-"</span> + PREFIX,<a name="328" href="#328">328</a> <strong>true</strong>, <strong>true</strong>);<a name="329" href="#329">329</a> } <strong>catch</strong> (RuntimeException e) {<a name="330" href="#330">330</a> eMessage = e.getMessage();<a name="331" href="#331">331</a> }<a name="332" href="#332">332</a> assertTrue(<span class="string">"Didn't get expected exception: "</span> + eMessage,<a name="333" href="#333">333</a> eMessage.startsWith(<span class="string">"java.io.IOException: Record ENDING at"</span>));<a name="334" href="#334">334</a> }<a name="335" href="#335">335</a> <a name="336" href="#336">336</a> <strong>protected</strong> <strong>void</strong> lengthTooShort(String name, <strong>boolean</strong> compress, <strong>boolean</strong> strict)<a name="337" href="#337">337</a> throws IOException {<a name="338" href="#338">338</a> ARCWriter writer = createArcWithOneRecord(name, compress);<a name="339" href="#339">339</a> <em class="comment">// Add some bytes on the end to mess up the record.</em><a name="340" href="#340">340</a> String content = getContent();<a name="341" href="#341">341</a> ByteArrayOutputStream baos = getBaos(content);<a name="342" href="#342">342</a> baos.write(<span class="string">"SOME TRAILING BYTES"</span>.getBytes());<a name="343" href="#343">343</a> writeRecord(writer, SOME_URL, <span class="string">"text/html"</span>,<a name="344" href="#344">344</a> content.length(), baos);<a name="345" href="#345">345</a> writeRecord(writer, SOME_URL, <span class="string">"text/html"</span>,<a name="346" href="#346">346</a> content.length(), getBaos(content));<a name="347" href="#347">347</a> writer.close();<a name="348" href="#348">348</a> <a name="349" href="#349">349</a> <em class="comment">// Catch System.err into a byte stream.</em><a name="350" href="#350">350</a> ByteArrayOutputStream os = <strong>new</strong> ByteArrayOutputStream();<a name="351" href="#351">351</a> System.setErr(<strong>new</strong> PrintStream(os));<a name="352" href="#352">352</a> <a name="353" href="#353">353</a> <a href="../../../../org/archive/io/arc/ARCReader.html">ARCReader</a> r = ARCReaderFactory.get(writer.getFile());<a name="354" href="#354">354</a> r.setStrict(strict);<a name="355" href="#355">355</a> <strong>int</strong> count = iterateRecords(r);<a name="356" href="#356">356</a> assertTrue(<span class="string">"Count wrong "</span> + count, count == 4);<a name="357" href="#357">357</a> <a name="358" href="#358">358</a> <em class="comment">// Make sure we get the warning string which complains about the</em><a name="359" href="#359">359</a> <em class="comment">// trailing bytes.</em><a name="360" href="#360">360</a> String err = os.toString();<a name="361" href="#361">361</a> assertTrue(<span class="string">"No message "</span> + err, err.startsWith(<span class="string">"WARNING"</span>) &&<a name="362" href="#362">362</a> (err.indexOf(<span class="string">"Record ENDING at"</span>) > 0));<a name="363" href="#363">363</a> }<a name="364" href="#364">364</a> <a name="365" href="#365">365</a> <em class="comment">// If uncompressed, length has to be right or parse will fail.</em><a name="366" href="#366">366</a> <em class="comment">//</em><a name="367" href="#367">367</a> <em class="comment">// public void testLengthTooLong()</em><a name="368" href="#368">368</a> <em class="comment">// throws IOException {</em><a name="369" href="#369">369</a> <em class="comment">// lengthTooLong("testLengthTooLongCompressed-" + PREFIX,</em><a name="370" href="#370">370</a> <em class="comment">// false, false);</em><a name="371" href="#371">371</a> <em class="comment">// }</em><a name="372" href="#372">372</a> <a name="373" href="#373">373</a> <strong>public</strong> <strong>void</strong> testLengthTooLongCompressed()<a name="374" href="#374">374</a> throws IOException {<a name="375" href="#375">375</a> lengthTooLong(<span class="string">"testLengthTooLongCompressed-"</span> + PREFIX,<a name="376" href="#376">376</a> <strong>true</strong>, false);<a name="377" href="#377">377</a> }<a name="378" href="#378">378</a> <a name="379" href="#379">379</a> <strong>public</strong> <strong>void</strong> testLengthTooLongCompressedStrict() {<a name="380" href="#380">380</a> String eMessage = <strong>null</strong>;<a name="381" href="#381">381</a> <strong>try</strong> {<a name="382" href="#382">382</a> lengthTooLong(<span class="string">"testLengthTooLongCompressed-"</span> + PREFIX,<a name="383" href="#383">383</a> <strong>true</strong>, <strong>true</strong>);<a name="384" href="#384">384</a> } <strong>catch</strong> (IOException e) {<a name="385" href="#385">385</a> eMessage = e.getMessage();<a name="386" href="#386">386</a> }<a name="387" href="#387">387</a> assertTrue(<span class="string">"Didn't get expected exception: "</span> + eMessage,<a name="388" href="#388">388</a> eMessage.startsWith(<span class="string">"Premature EOF before end-of-record"</span>));<a name="389" href="#389">389</a> }<a name="390" href="#390">390</a> <a name="391" href="#391">391</a> <strong>protected</strong> <strong>void</strong> lengthTooLong(String name, <strong>boolean</strong> compress,<a name="392" href="#392">392</a> <strong>boolean</strong> strict)<a name="393" href="#393">393</a> throws IOException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -