📄 compressionfiltertest.html
字号:
<a name="107" href="#107">107</a> <a name="108" href="#108">108</a> <em class="jxr_comment">// create instances of the deflater and inflater to help test the output</em><a name="109" href="#109">109</a> actualDeflater = <strong class="jxr_keyword">new</strong> Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);<a name="110" href="#110">110</a> actualInflater = <strong class="jxr_keyword">new</strong> Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);<a name="111" href="#111">111</a> }<a name="112" href="#112">112</a> <a name="113" href="#113">113</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> testCompression() <strong class="jxr_keyword">throws</strong> Exception {<a name="114" href="#114">114</a> <em class="jxr_comment">// prepare the input data</em><a name="115" href="#115">115</a> IoBuffer buf = IoBuffer.wrap(strCompress.getBytes(<span class="jxr_string">"UTF8"</span>));<a name="116" href="#116">116</a> IoBuffer actualOutput = actualDeflater.deflate(buf);<a name="117" href="#117">117</a> WriteRequest writeRequest = <strong class="jxr_keyword">new</strong> DefaultWriteRequest(buf);<a name="118" href="#118">118</a> <a name="119" href="#119">119</a> <em class="jxr_comment">// record all the mock calls</em><a name="120" href="#120">120</a> ioFilterChain.contains(CompressionFilter.<strong class="jxr_keyword">class</strong>);<a name="121" href="#121">121</a> mockIoFilterChain.setReturnValue(false);<a name="122" href="#122">122</a> <a name="123" href="#123">123</a> ioFilterChain.getSession();<a name="124" href="#124">124</a> mockIoFilterChain.setReturnValue(session);<a name="125" href="#125">125</a> <a name="126" href="#126">126</a> session.setAttribute(CompressionFilter.<strong class="jxr_keyword">class</strong>.getName() + <span class="jxr_string">".Deflater"</span>,<a name="127" href="#127">127</a> deflater);<a name="128" href="#128">128</a> mockSession.setDefaultMatcher(<strong class="jxr_keyword">new</strong> <a href="../../../../../org/apache/mina/filter/compression/CompressionFilterTest.html">DataMatcher</a>());<a name="129" href="#129">129</a> mockSession.setReturnValue(<strong class="jxr_keyword">null</strong>, MockControl.ONE);<a name="130" href="#130">130</a> <a name="131" href="#131">131</a> session.setAttribute(CompressionFilter.<strong class="jxr_keyword">class</strong>.getName() + <span class="jxr_string">".Inflater"</span>,<a name="132" href="#132">132</a> inflater);<a name="133" href="#133">133</a> mockSession.setReturnValue(<strong class="jxr_keyword">null</strong>, MockControl.ONE);<a name="134" href="#134">134</a> <a name="135" href="#135">135</a> session.containsAttribute(CompressionFilter.DISABLE_COMPRESSION_ONCE);<a name="136" href="#136">136</a> mockSession.setReturnValue(false);<a name="137" href="#137">137</a> <a name="138" href="#138">138</a> session.getAttribute(CompressionFilter.<strong class="jxr_keyword">class</strong>.getName() + <span class="jxr_string">".Deflater"</span>);<a name="139" href="#139">139</a> mockSession.setReturnValue(deflater);<a name="140" href="#140">140</a> <a name="141" href="#141">141</a> nextFilter.filterWrite(session, <strong class="jxr_keyword">new</strong> DefaultWriteRequest(actualOutput));<a name="142" href="#142">142</a> <a name="143" href="#143">143</a> <em class="jxr_comment">// switch to playback mode</em><a name="144" href="#144">144</a> mockSession.replay();<a name="145" href="#145">145</a> mockIoFilterChain.replay();<a name="146" href="#146">146</a> mockNextFilter.replay();<a name="147" href="#147">147</a> <a name="148" href="#148">148</a> <em class="jxr_comment">// make the actual calls on the filter</em><a name="149" href="#149">149</a> filter.onPreAdd(ioFilterChain, <span class="jxr_string">"CompressionFilter"</span>, nextFilter);<a name="150" href="#150">150</a> filter.filterWrite(nextFilter, session, writeRequest);<a name="151" href="#151">151</a> <a name="152" href="#152">152</a> <em class="jxr_comment">// verify that all the calls happened as recorded</em><a name="153" href="#153">153</a> mockNextFilter.verify();<a name="154" href="#154">154</a> <a name="155" href="#155">155</a> assertTrue(<strong class="jxr_keyword">true</strong>);<a name="156" href="#156">156</a> }<a name="157" href="#157">157</a> <a name="158" href="#158">158</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> testDecompression() <strong class="jxr_keyword">throws</strong> Exception {<a name="159" href="#159">159</a> <em class="jxr_comment">// prepare the input data</em><a name="160" href="#160">160</a> IoBuffer buf = IoBuffer.wrap(strCompress.getBytes(<span class="jxr_string">"UTF8"</span>));<a name="161" href="#161">161</a> IoBuffer byteInput = actualDeflater.deflate(buf);<a name="162" href="#162">162</a> IoBuffer actualOutput = actualInflater.inflate(byteInput);<a name="163" href="#163">163</a> <a name="164" href="#164">164</a> <em class="jxr_comment">// record all the mock calls</em><a name="165" href="#165">165</a> ioFilterChain.contains(CompressionFilter.<strong class="jxr_keyword">class</strong>);<a name="166" href="#166">166</a> mockIoFilterChain.setReturnValue(false);<a name="167" href="#167">167</a> <a name="168" href="#168">168</a> ioFilterChain.getSession();<a name="169" href="#169">169</a> mockIoFilterChain.setReturnValue(session);<a name="170" href="#170">170</a> <a name="171" href="#171">171</a> session.setAttribute(CompressionFilter.<strong class="jxr_keyword">class</strong>.getName() + <span class="jxr_string">".Deflater"</span>,<a name="172" href="#172">172</a> deflater);<a name="173" href="#173">173</a> mockSession.setDefaultMatcher(<strong class="jxr_keyword">new</strong> <a href="../../../../../org/apache/mina/filter/compression/CompressionFilterTest.html">DataMatcher</a>());<a name="174" href="#174">174</a> mockSession.setReturnValue(<strong class="jxr_keyword">null</strong>, MockControl.ONE);<a name="175" href="#175">175</a> <a name="176" href="#176">176</a> session.setAttribute(CompressionFilter.<strong class="jxr_keyword">class</strong>.getName() + <span class="jxr_string">".Inflater"</span>,<a name="177" href="#177">177</a> inflater);<a name="178" href="#178">178</a> mockSession.setReturnValue(<strong class="jxr_keyword">null</strong>, MockControl.ONE);<a name="179" href="#179">179</a> <a name="180" href="#180">180</a> session.getAttribute(CompressionFilter.<strong class="jxr_keyword">class</strong>.getName() + <span class="jxr_string">".Inflater"</span>);<a name="181" href="#181">181</a> mockSession.setReturnValue(inflater);<a name="182" href="#182">182</a> <a name="183" href="#183">183</a> nextFilter.messageReceived(session, actualOutput);<a name="184" href="#184">184</a> <a name="185" href="#185">185</a> <em class="jxr_comment">// switch to playback mode</em><a name="186" href="#186">186</a> mockSession.replay();<a name="187" href="#187">187</a> mockIoFilterChain.replay();<a name="188" href="#188">188</a> mockNextFilter.replay();<a name="189" href="#189">189</a> <a name="190" href="#190">190</a> <em class="jxr_comment">// make the actual calls on the filter</em><a name="191" href="#191">191</a> filter.onPreAdd(ioFilterChain, <span class="jxr_string">"CompressionFilter"</span>, nextFilter);<a name="192" href="#192">192</a> filter.messageReceived(nextFilter, session, byteInput);<a name="193" href="#193">193</a> <a name="194" href="#194">194</a> <em class="jxr_comment">// verify that all the calls happened as recorded</em><a name="195" href="#195">195</a> mockNextFilter.verify();<a name="196" href="#196">196</a> <a name="197" href="#197">197</a> assertTrue(<strong class="jxr_keyword">true</strong>);<a name="198" href="#198">198</a> }<a name="199" href="#199">199</a> <a name="200" href="#200">200</a> <em class="jxr_javadoccomment">/**</em><a name="201" href="#201">201</a> <em class="jxr_javadoccomment"> * A matcher used to check if the actual and expected outputs matched</em><a name="202" href="#202">202</a> <em class="jxr_javadoccomment"> */</em><a name="203" href="#203">203</a> <strong class="jxr_keyword">class</strong> <a href="../../../../../org/apache/mina/filter/compression/CompressionFilterTest.html">DataMatcher</a> <strong class="jxr_keyword">extends</strong> AbstractMatcher {<a name="204" href="#204">204</a> @Override<a name="205" href="#205">205</a> <strong class="jxr_keyword">protected</strong> <strong class="jxr_keyword">boolean</strong> argumentMatches(Object arg0, Object arg1) {<a name="206" href="#206">206</a> <em class="jxr_comment">// we need to only verify the ByteBuffer output</em><a name="207" href="#207">207</a> <strong class="jxr_keyword">if</strong> (arg0 instanceof WriteRequest) {<a name="208" href="#208">208</a> WriteRequest expected = (WriteRequest) arg0;<a name="209" href="#209">209</a> WriteRequest actual = (WriteRequest) arg1;<a name="210" href="#210">210</a> IoBuffer bExpected = (IoBuffer) expected.getMessage();<a name="211" href="#211">211</a> IoBuffer bActual = (IoBuffer) actual.getMessage();<a name="212" href="#212">212</a> <strong class="jxr_keyword">return</strong> bExpected.equals(bActual);<a name="213" href="#213">213</a> }<a name="214" href="#214">214</a> <strong class="jxr_keyword">return</strong> <strong class="jxr_keyword">true</strong>;<a name="215" href="#215">215</a> }<a name="216" href="#216">216</a> }<a name="217" href="#217">217</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 + -