⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 teststackobjectpool.html

📁 优秀的文档,可以学习java之用 0006728337 00000 n 0006728424 00000 n 0006728600 00000 n
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="272" href="#272">272</a>     <strong>public</strong> <strong>void</strong> testReturnObjectDiscardOrder() throws Exception {<a name="273" href="#273">273</a>         <em class="comment">// setup</em><a name="274" href="#274">274</a>         <em class="comment">// We need a factory that tracks what was discarded.</em><a name="275" href="#275">275</a>         PoolableObjectFactory pof = <strong>new</strong> PoolableObjectFactory() {<a name="276" href="#276">276</a>             <strong>int</strong> i = 0;<a name="277" href="#277">277</a>             <strong>public</strong> Object makeObject() throws Exception {<a name="278" href="#278">278</a>                 <strong>return</strong> <strong>new</strong> Integer(i++);<a name="279" href="#279">279</a>             }<a name="280" href="#280">280</a> <a name="281" href="#281">281</a>             <strong>public</strong> <strong>void</strong> destroyObject(Object obj) throws Exception {<a name="282" href="#282">282</a>                 destroyed.add(obj);<a name="283" href="#283">283</a>             }<a name="284" href="#284">284</a> <a name="285" href="#285">285</a>             <strong>public</strong> <strong>boolean</strong> validateObject(Object obj) {<a name="286" href="#286">286</a>                 <strong>return</strong> obj instanceof Integer;<a name="287" href="#287">287</a>             }<a name="288" href="#288">288</a> <a name="289" href="#289">289</a>             <strong>public</strong> <strong>void</strong> activateObject(Object obj) throws Exception {<a name="290" href="#290">290</a>             }<a name="291" href="#291">291</a> <a name="292" href="#292">292</a>             <strong>public</strong> <strong>void</strong> passivateObject(Object obj) throws Exception {<a name="293" href="#293">293</a>             }<a name="294" href="#294">294</a>         };<a name="295" href="#295">295</a>         ObjectPool pool = <strong>new</strong> StackObjectPool(pof, 3);<a name="296" href="#296">296</a> <a name="297" href="#297">297</a>         <em class="comment">// borrow more objects than the pool can hold</em><a name="298" href="#298">298</a>         Integer i0 = (Integer)pool.borrowObject();<a name="299" href="#299">299</a>         Integer i1 = (Integer)pool.borrowObject();<a name="300" href="#300">300</a>         Integer i2 = (Integer)pool.borrowObject();<a name="301" href="#301">301</a>         Integer i3 = (Integer)pool.borrowObject();<a name="302" href="#302">302</a> <a name="303" href="#303">303</a>         <em class="comment">// tests</em><a name="304" href="#304">304</a>         <em class="comment">// return as many as the pool will hold.</em><a name="305" href="#305">305</a>         pool.returnObject(i0);<a name="306" href="#306">306</a>         pool.returnObject(i1);<a name="307" href="#307">307</a>         pool.returnObject(i2);<a name="308" href="#308">308</a> <a name="309" href="#309">309</a>         <em class="comment">// the pool should now be full.</em><a name="310" href="#310">310</a>         assertEquals(<span class="string">"No returned objects should have been destroyed yet."</span>,0, destroyed.size());<a name="311" href="#311">311</a> <a name="312" href="#312">312</a>         <em class="comment">// cause the pool to discard a returned object.</em><a name="313" href="#313">313</a>         pool.returnObject(i3);<a name="314" href="#314">314</a>         assertEquals(<span class="string">"One object should have been destroyed."</span>, 1, destroyed.size());<a name="315" href="#315">315</a> <a name="316" href="#316">316</a>         <em class="comment">// check to see what object was destroyed</em><a name="317" href="#317">317</a>         Integer d = (Integer)destroyed.get(0);<a name="318" href="#318">318</a>         assertEquals(<span class="string">"Destoryed objects should have the stalest object."</span>, i0, d);<a name="319" href="#319">319</a>     }<a name="320" href="#320">320</a> <a name="321" href="#321">321</a>     <strong>private</strong> List testFactorySequenceStates = <strong>new</strong> ArrayList(5);<a name="322" href="#322">322</a>     <strong>public</strong> <strong>void</strong> testFactorySequence() throws Exception {<a name="323" href="#323">323</a>         <em class="comment">// setup</em><a name="324" href="#324">324</a>         <em class="comment">// We need a factory that tracks method call sequence.</em><a name="325" href="#325">325</a>         PoolableObjectFactory pof = <strong>new</strong> PoolableObjectFactory() {<a name="326" href="#326">326</a>             <strong>public</strong> Object makeObject() throws Exception {<a name="327" href="#327">327</a>                 testFactorySequenceStates.add(<span class="string">"makeObject"</span>);<a name="328" href="#328">328</a>                 <strong>return</strong> <strong>new</strong> Object();<a name="329" href="#329">329</a>             }<a name="330" href="#330">330</a> <a name="331" href="#331">331</a>             <strong>public</strong> <strong>void</strong> activateObject(Object obj) throws Exception {<a name="332" href="#332">332</a>                 testFactorySequenceStates.add(<span class="string">"activateObject"</span>);<a name="333" href="#333">333</a>             }<a name="334" href="#334">334</a> <a name="335" href="#335">335</a>             <strong>public</strong> <strong>boolean</strong> validateObject(Object obj) {<a name="336" href="#336">336</a>                 testFactorySequenceStates.add(<span class="string">"validateObject"</span>);<a name="337" href="#337">337</a>                 <strong>return</strong> <strong>true</strong>;<a name="338" href="#338">338</a>             }<a name="339" href="#339">339</a> <a name="340" href="#340">340</a>             <strong>public</strong> <strong>void</strong> passivateObject(Object obj) throws Exception {<a name="341" href="#341">341</a>                 testFactorySequenceStates.add(<span class="string">"passivateObject"</span>);<a name="342" href="#342">342</a>             }<a name="343" href="#343">343</a> <a name="344" href="#344">344</a>             <strong>public</strong> <strong>void</strong> destroyObject(Object obj) throws Exception {<a name="345" href="#345">345</a>                 testFactorySequenceStates.add(<span class="string">"destroyObject"</span>);<a name="346" href="#346">346</a>             }<a name="347" href="#347">347</a>         };<a name="348" href="#348">348</a> <a name="349" href="#349">349</a>         ObjectPool pool = <strong>new</strong> StackObjectPool(pof, 1);<a name="350" href="#350">350</a> <a name="351" href="#351">351</a>         <em class="comment">// check the order in which the factory is called during borrow</em><a name="352" href="#352">352</a>         testFactorySequenceStates.clear();<a name="353" href="#353">353</a>         Object o = pool.borrowObject();<a name="354" href="#354">354</a>         List desiredSequence = Arrays.asList(<strong>new</strong> String[] {<a name="355" href="#355">355</a>                 <span class="string">"makeObject"</span>,<a name="356" href="#356">356</a>                 <span class="string">"activateObject"</span>,<a name="357" href="#357">357</a>                 <span class="string">"validateObject"</span><a name="358" href="#358">358</a>         });<a name="359" href="#359">359</a>         assertEquals(<span class="string">"Wrong sequence"</span>, desiredSequence, testFactorySequenceStates);<a name="360" href="#360">360</a> <a name="361" href="#361">361</a>         <em class="comment">// check the order in which the factory is called when returning an object</em><a name="362" href="#362">362</a>         testFactorySequenceStates.clear();<a name="363" href="#363">363</a>         pool.returnObject(o);<a name="364" href="#364">364</a>         desiredSequence = Arrays.asList(<strong>new</strong> String[] {<a name="365" href="#365">365</a>                 <span class="string">"validateObject"</span>,<a name="366" href="#366">366</a>                 <span class="string">"passivateObject"</span><a name="367" href="#367">367</a>         });<a name="368" href="#368">368</a>         assertEquals(<span class="string">"Wrong sequence"</span>, desiredSequence, testFactorySequenceStates);<a name="369" href="#369">369</a> <a name="370" href="#370">370</a>         <em class="comment">// check the order in which the factory is called during borrow again</em><a name="371" href="#371">371</a>         testFactorySequenceStates.clear();<a name="372" href="#372">372</a>         o = pool.borrowObject();<a name="373" href="#373">373</a>         desiredSequence = Arrays.asList(<strong>new</strong> String[] {<a name="374" href="#374">374</a>                 <span class="string">"activateObject"</span>,<a name="375" href="#375">375</a>                 <span class="string">"validateObject"</span><a name="376" href="#376">376</a>         });<a name="377" href="#377">377</a>         assertEquals(<span class="string">"Wrong sequence"</span>, desiredSequence, testFactorySequenceStates);<a name="378" href="#378">378</a> <a name="379" href="#379">379</a>         <em class="comment">// check the order in which the factory is called when invalidating an object</em><a name="380" href="#380">380</a>         testFactorySequenceStates.clear();<a name="381" href="#381">381</a>         pool.invalidateObject(o);<a name="382" href="#382">382</a>         desiredSequence = Arrays.asList(<strong>new</strong> String[] {<a name="383" href="#383">383</a>                 <span class="string">"destroyObject"</span><a name="384" href="#384">384</a>         });<a name="385" href="#385">385</a>         assertEquals(<span class="string">"Wrong sequence"</span>, desiredSequence, testFactorySequenceStates);<a name="386" href="#386">386</a>     }<a name="387" href="#387">387</a> <a name="388" href="#388">388</a>     <strong>static</strong> <strong>class</strong> SimpleFactory implements PoolableObjectFactory {<a name="389" href="#389">389</a>         <strong>int</strong> counter = 0;<a name="390" href="#390">390</a>         <strong>public</strong> Object makeObject() { <strong>return</strong> String.valueOf(counter++); }<a name="391" href="#391">391</a>         <strong>public</strong> <strong>void</strong> destroyObject(Object obj) { }<a name="392" href="#392">392</a>         <strong>public</strong> <strong>boolean</strong> validateObject(Object obj) { <strong>return</strong> <strong>true</strong>; }<a name="393" href="#393">393</a>         <strong>public</strong> <strong>void</strong> activateObject(Object obj) { }<a name="394" href="#394">394</a>         <strong>public</strong> <strong>void</strong> passivateObject(Object obj) { }<a name="395" href="#395">395</a>     }<a name="396" href="#396">396</a> <a name="397" href="#397">397</a>     <strong>protected</strong> <strong>boolean</strong> isLifo() {<a name="398" href="#398">398</a>         <strong>return</strong> <strong>true</strong>;<a name="399" href="#399">399</a>     }<a name="400" href="#400">400</a> <a name="401" href="#401">401</a>     <strong>protected</strong> <strong>boolean</strong> isFifo() {<a name="402" href="#402">402</a>         <strong>return</strong> false;<a name="403" href="#403">403</a>     }<a name="404" href="#404">404</a> }<a name="405" href="#405">405</a> </pre><hr/><div id="footer">This page was automatically generated by <a href="http://maven.apache.org/">Maven</a></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -