<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>java &amp;mdash; alan</title>
    <link>https://www.franzoni.eu/alan/tag:java</link>
    <description>Alan&#39;s musings</description>
    <language>en</language>
    <pubDate>Tue, 17 Nov 2026 00:40:51 +0000</pubDate>
    <item>
      <title>Unicode and Encoding: Python vs Java Shootout, part 2</title>
      <link>https://www.franzoni.eu/alan/unicode-and-encoding-python-vs-java-shootout-part-2</link>
      <description>&lt;![CDATA[So, here we come to our second part; if you did miss the first, it&#39;s there. All of the following discussion is relative to Sun Java 6, but most details - if not every detail - is still valid for Java 7 and 8.&#xA;&#xA;Let&#39;s see the way Java handles unicode issues and why it&#39;s usually less problematic than Python - or, at least, while it looks less problematic than Python.&#xA;&#xA;String literals in Java, just like in Python 3, are just Unicode objects; so you don&#39;t need to think whether it&#39;s a &#34;binary string&#34; or a &#34;unicode object&#34;; if it&#39;s a binary blob, you&#39;ll probably get a byte\[\], which should result in a &#39;here be dragons&#39; behaviour about encoding.&#xA;&#xA;Nevertheless, whenever you want to print out a string you actually need binary data, raw bytes! How does Java find out what to do?&#xA;&#xA;System.out, which is an instance of java.io.PrintStream, internally uses a java.io.OutputStreamWriter which gets initialized using \Charset.defaultCharset()\](http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html#defaultCharset%28%5C%29)), and such value will be used for conversions between raw bytes and strings:&#xA;&#xA;Encoding.java&#xA;&#xA;package eu.franzoni.examples.pythonvsjavaencoding;&#xA;&#xA;import java.nio.charset.Charset;&#xA;&#xA;public class Encoding {&#xA;    public static void main(String[] args) throws Exception {&#xA;        String myString = &#34;àèìòù&#34;;&#xA;        System.out.println(System.out.getClass());&#xA;        System.out.println(Charset.defaultCharset());&#xA;        System.out.println(myString);&#xA;        System.out.write(myString.getBytes(Charset.defaultCharset()));&#xA;        System.out.println(&#34;&#34;);&#xA;    }&#xA;}&#xA;&#xA;result:&#xA;&#xA;class java.io.PrintStream&#xA;UTF-8&#xA;àèìòù&#xA;&#xA;The value of the default charset is detected by the JVM at startup, and it can vary on different OSes and because of different language environment settings. On Sun Java you can even set at JVM launch time through the file.encoding property, but that&#39;s discouraged as it can be unpredictable and unreliable. On the contrary, you should always use constructors and methods that accept a charset parameter if you&#39;re converting to/from raw bytes, just like String.getBytes(Charset charset) or \OutputStreamWriter(OutputStream outputStream, Charset charset)\(http://docs.oracle.com/javase/6/docs/api/java/io/OutputStreamWriter.html#OutputStreamWriter%5C%28java.io.OutputStream), java.nio.charset.Charset))&#xA;&#xA;So far, so good. But what happens if the outputstream is unable to handle the chars you&#39;d like it to write?&#xA;&#xA;CantEncode.java&#xA;&#xA;package eu.franzoni.examples.pythonvsjavaencoding;&#xA;&#xA;import java.io.FileOutputStream;&#xA;import java.io.OutputStream;&#xA;import java.io.PrintStream;&#xA;&#xA;public class CantEncode {&#xA;    public static void main(String[] args) throws Exception {&#xA;        // change default output stream&#xA;        OutputStream outputStream = new FileOutputStream(&#34;eu.franzoni.examples/src/eu/franzoni/examples/pythonvsjavaencoding/CantEncode.output&#34;);&#xA;        PrintStream printStream = new PrintStream(outputStream, true, &#34;ASCII&#34;);&#xA;        System.setOut(printStream);&#xA;&#xA;        String myString = &#34;àèìòù&#34;;&#xA;&#xA;        System.out.println(System.out.getClass());&#xA;        System.out.println(myString);&#xA;     }&#xA;}&#xA;&#xA;result:&#xA;&#xA;class java.io.PrintStream&#xA;?????&#xA;&#xA;ASCII encoding cannot possibly output the accented chars, so they&#39;re replaced with question marks instead of throwing the dreaded UnicodeEncodeError.&#xA;&#xA;Which approach is the best? That depends on the context. If you expect somebody to actually read your text, the Java approach might just let an error slip unnoticed for a long time, while the Python approach would trigger an immediate &#39;heads up!&#39;. Otherwise if you&#39;re just logging something to the console (or to any other stream) an exception might disrupt your otherwise well-working program and force an unnecessary quit. The Java approach just looks easier for the non-charset-aware because it throws fewer errors around.&#xA;&#xA;But there&#39;s another situation where Java falls short of Python: source file encoding. While Python allows setting it on a per-file basis and defaults it to iso-8859-1 if the coding directive is not there, on Java you&#39;ll use javac -encoding flag to set it globally, per compile, in order to tell the compiler which kind of encoding the source file uses; if you don&#39;t pass such option, the platform default converter is used&#xA;&#xA;What&#39;s the problem with this approach? Consider a very common situation: you work on a project on Linux, your platform defaults to UTF-8 and everything just works.&#xA;&#xA;Then you checkout your project on Windows; you compile it, the default charset is Windows-1252 and all your files print garbage. That&#39;s what happens, in fact:&#xA;&#xA;echo &#34;utf8 compiled:&#34;&#xA;javac -encoding utf8 eu/franzoni/examples/pythonvsjavaencoding/Encoding.java &amp;&amp; java eu.franzoni.examples.pythonvsjavaencoding.Encoding&#xA;echo &#34;windows-1252 compiled:&#34;&#xA;javac -encoding windows-1252 eu/franzoni/examples/pythonvsjavaencoding/Encoding.java &amp;&amp; java eu.franzoni.examples.pythonvsjavaencoding.Encoding&#xA;&#xA;result:&#xA;&#xA;utf8 compiled:&#xA;class java.io.PrintStream&#xA;UTF-8&#xA;àèìòù&#xA;àèìòù&#xA;windows-1252 compiled:&#xA;class java.io.PrintStream&#xA;UTF-8&#xA;Ã Ã¨Ã¬Ã²Ã¹&#xA;Ã Ã¨Ã¬Ã²Ã¹&#xA;&#xA;As you can see there, it&#39;s not that the JVM encoding has any issue at runtime; it&#39;s the parser, at compile time, which is told a wrong encoding about the string literals in your source code. There&#39;s no way around this!&#xA;&#xA;What I can recommend you is to ALWAYS set the project.build.sourceEncoding property when using Maven to compile, and to ALWAYS set the encoding attribute whenever you&#39;re using Ant. This will save you a lot, lot, lot of headaches.&#xA;&#xA;#Ollivander #java #python #unicode #encodings&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>So, here we come to our second part; if you did miss the first, it&#39;s <a href="/alan/unicode-and-encoding-python-vs-java-shootout-part-1" rel="nofollow">there</a>. All of the following discussion is relative to Sun Java 6, but most details – if not every detail – is still valid for Java 7 and 8.</p>

<p>Let&#39;s see the way Java handles unicode issues and why it&#39;s usually less problematic than Python – or, at least, while it <strong>looks</strong> less problematic than Python.</p>

<p>String literals in Java, just like in Python 3, are just Unicode objects; so you don&#39;t need to think whether it&#39;s a “binary string” or a “unicode object”; if it&#39;s a binary blob, you&#39;ll probably get a byte[], which should result in a &#39;here be dragons&#39; behaviour about encoding.</p>

<p>Nevertheless, whenever you want to print out a string you actually need binary data, raw bytes! How does Java find out what to do?</p>

<p><a href="http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#out" rel="nofollow">System.out</a>, which is an instance of <a href="http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html" rel="nofollow">java.io.PrintStream</a>, internally uses a <a href="http://docs.oracle.com/javase/6/docs/api/java/io/OutputStreamWriter.html" rel="nofollow">java.io.OutputStreamWriter</a> which gets initialized using [Charset.defaultCharset()](<a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html#defaultCharset%28%5C%29" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html#defaultCharset(\)</a>), and such value will be used for conversions between raw bytes and strings:</p>

<p><em>Encoding.java</em></p>

<pre><code class="language-java">package eu.franzoni.examples.pythonvsjavaencoding;

import java.nio.charset.Charset;

public class Encoding {
    public static void main(String[] args) throws Exception {
        String myString = &#34;àèìòù&#34;;
        System.out.println(System.out.getClass());
        System.out.println(Charset.defaultCharset());
        System.out.println(myString);
        System.out.write(myString.getBytes(Charset.defaultCharset()));
        System.out.println(&#34;&#34;);
    }
}
</code></pre>

<p>result:</p>

<pre><code>class java.io.PrintStream
UTF-8
àèìòù
</code></pre>

<p>The value of the default charset is detected by the JVM at startup, and it can vary on different OSes and because of different language environment settings. On Sun Java you can even set at JVM launch time through the file.encoding property, but that&#39;s discouraged as it can be unpredictable and unreliable. On the contrary, <strong>you should always use constructors and methods that accept a charset parameter</strong> if you&#39;re converting to/from raw bytes, just like <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#getBytes%28java.nio.charset.Charset%29" rel="nofollow">String.getBytes(Charset charset)</a> or [OutputStreamWriter(OutputStream outputStream, Charset charset)](<a href="http://docs.oracle.com/javase/6/docs/api/java/io/OutputStreamWriter.html#OutputStreamWriter%5C%28java.io.OutputStream" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/io/OutputStreamWriter.html#OutputStreamWriter\(java.io.OutputStream</a>, java.nio.charset.Charset))</p>

<p>So far, so good. But what happens if the outputstream is unable to handle the chars you&#39;d like it to write?</p>

<p><em>CantEncode.java</em></p>

<pre><code>package eu.franzoni.examples.pythonvsjavaencoding;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class CantEncode {
    public static void main(String[] args) throws Exception {
        // change default output stream
        OutputStream outputStream = new FileOutputStream(&#34;eu.franzoni.examples/src/eu/franzoni/examples/pythonvsjavaencoding/CantEncode.output&#34;);
        PrintStream printStream = new PrintStream(outputStream, true, &#34;ASCII&#34;);
        System.setOut(printStream);

        String myString = &#34;àèìòù&#34;;

        System.out.println(System.out.getClass());
        System.out.println(myString);
     }
}
</code></pre>

<p>result:</p>

<pre><code>class java.io.PrintStream
?????
</code></pre>

<p>ASCII encoding cannot possibly output the accented chars, so they&#39;re replaced with question marks instead of throwing the dreaded UnicodeEncodeError.</p>

<p>Which approach is the best? That depends on the context. If you expect somebody to actually read your text, the Java approach might just let an error slip unnoticed for a long time, while the Python approach would trigger an immediate &#39;heads up!&#39;. Otherwise if you&#39;re just logging something to the console (or to any other stream) an exception might disrupt your otherwise well-working program and force an unnecessary quit. The Java approach just looks easier for the non-charset-aware because it throws fewer errors around.</p>

<p>But there&#39;s another situation where Java falls short of Python: source file encoding. While Python allows setting it on a per-file basis and defaults it to iso-8859-1 if the coding directive is not there, on Java you&#39;ll use <a href="http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/javac.html" rel="nofollow">javac</a> <strong>-encoding</strong> flag to set it globally, per compile, in order to tell the compiler which kind of encoding the source file uses; <strong>if you don&#39;t pass such option, the platform default converter is used</strong></p>

<p>What&#39;s the problem with this approach? Consider a very common situation: you work on a project on Linux, your platform defaults to UTF-8 and everything just works.</p>

<p>Then you checkout your project on Windows; you compile it, the default charset is Windows-1252 and all your files print garbage. That&#39;s what happens, in fact:</p>

<pre><code>echo &#34;utf8 compiled:&#34;
javac -encoding utf8 eu/franzoni/examples/pythonvsjavaencoding/Encoding.java &amp;&amp; java eu.franzoni.examples.pythonvsjavaencoding.Encoding
echo &#34;windows-1252 compiled:&#34;
javac -encoding windows-1252 eu/franzoni/examples/pythonvsjavaencoding/Encoding.java &amp;&amp; java eu.franzoni.examples.pythonvsjavaencoding.Encoding
</code></pre>

<p>result:</p>

<pre><code>utf8 compiled:
class java.io.PrintStream
UTF-8
àèìòù
àèìòù
windows-1252 compiled:
class java.io.PrintStream
UTF-8
Ã Ã¨Ã¬Ã²Ã¹
Ã Ã¨Ã¬Ã²Ã¹
</code></pre>

<p>As you can see there, it&#39;s not that the JVM encoding has any issue at runtime; it&#39;s the parser, at compile time, which is told a wrong encoding about the string literals in your source code. There&#39;s no way around this!</p>

<p>What I can recommend you is to ALWAYS set the <a href="http://maven.apache.org/general.html#encoding-warning" rel="nofollow">project.build.sourceEncoding</a> property when using Maven to compile, and to ALWAYS set the <a href="http://ant.apache.org/manual/Tasks/javac.html" rel="nofollow">encoding attribute</a> whenever you&#39;re using Ant. This will save you a lot, lot, lot of headaches.</p>

<p><a href="/alan/tag:Ollivander" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Ollivander</span></a> <a href="/alan/tag:java" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">java</span></a> <a href="/alan/tag:python" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">python</span></a> <a href="/alan/tag:unicode" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">unicode</span></a> <a href="/alan/tag:encodings" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">encodings</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/unicode-and-encoding-python-vs-java-shootout-part-2</guid>
      <pubDate>Mon, 23 Jul 2012 19:25:00 +0000</pubDate>
    </item>
    <item>
      <title>Unicode and encoding:  Python vs Java shootout, part 1</title>
      <link>https://www.franzoni.eu/alan/unicode-and-encoding-python-vs-java-shootout-part-1</link>
      <description>&lt;![CDATA[Before going on with this post, be sure you&#39;ve read a href=&#34;http://www.joelonsoftware.com/articles/Unicode.html&#34; target=&#34;blank&#34;The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)/a - otherwise, I won&#39;t be able to solve your issues.br /br /Sometimes Java fans just say that Python Unicode support sucks. While there&#39;s a bit of truth in that assertion - for Python 2.x at least, since 3.x solved the problem at its root - the real problem lies in the fact that many programmers don&#39;t know what&#39;s going on &#34;under the hood&#34;, and that python default behaviour is a bit unforgiving.br /br /First things first: Python 2.x has got ba href=&#34;http://docs.python.org/library/stdtypes.html#string-methods&#34;two distinct string types/a: /bthe so-called &#34;byte strings&#34; (str type) are one, and unicode objects (unicode type) are another one. Unicode objects are much like Java strings; they&#39;re an internal abstraction of Python, and need to be interpreted to/from byte strings whenever printing, reading/writing from/to a file, etc.br /br /Sometimes such interpreting &#34;just works&#34; (most probably if you&#39;re dealing with ascii-only text), but if such conversion fails, errors occur: Python infamous and dreaded UnicodeDecodeError can happen in many places and might sometime just puzzle the programmer:br /br /pre class=&#34;brush: py&#34;\# -- coding: utf-8 --br /import sysbr /br /print &#34;current default encoding: &#34; + sys.getdefaultencoding()br /br /&#34;àèìòù&#34; + u&#34;asd&#34;br //prebr /pre class=&#34;brush: plain&#34;javapythonunicode$ python unicode\concat.pybr /current default encoding: asciibr /Traceback (most recent call last):br / File &#34;unicode\concat.py&#34;, line 6, in \module\br / &#34;àèìòù&#34; + u&#34;asd&#34;br /UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xc3 in position 0: ordinal not in range(128)br //prebr / This happens when trying to concatenate an unicode string to string object. What really happens here, behind the scenes, is explained in unicode\concat\explained.py:br /br /pre class=&#34;brush: py&#34;\# -- coding: utf-8 --br /import sysbr /br /print &#34;current default encoding: &#34; + sys.getdefaultencoding()br /br /&#34;àèìòù&#34;.decode(sys.getdefaultencoding()) + u&#34;asd&#34;br //prebr /When using a byte string (not a unicode object) python tries to convert that string to unicode before concatenating to a unicode object. Since an encoding wasn&#39;t specified, python just uses the default.br /br /So, if you&#39;re planning to mix unicode and bytestring objects (which is not a good idea), balways remember to explicitly convert string objects to unicode by their decode() method./b Or, if you&#39;re sure the encoding is always the same, you could add a a href=&#34;http://docs.python.org/library/site.html&#34;sitecustomize.py/a to your interpreter or your project PYTHONPATH, and set such encoding. Beware that setting it system wide may lead to unexpected results when running your project on another machine.br /br /Also, just remember that the a href=&#34;http://www.python.org/dev/peps/pep-0263/&#34;coding directive/a at the begininning of the file does not change unicode encoding/decoding behaviour: that&#39;s a directive to the parser, which is used to instruct it about the current encoding of the file. That information is not retained nor used at runtime.br /br /br /Let&#39;s go for another much dreaded error now: UnicodeEncodeError!br /br /pre class=&#34;brush: py&#34;\# -- coding: utf-8 --br /import sysbr /print &#34;current stdout encoding: &#34; + str(sys.stdout.encoding)br /print &#34;current default encoding: &#34; + sys.getdefaultencoding()br /br /print u&#34;àèìòù&#34;br //prebr /results in this output: br /br /pre class=&#34;brush: plain&#34;javapythonunicode$ python unicode\print.pybr /current stdout encoding: UTF-8br /àèìòùbr //prebr /Everything seems to work (as long as your terminal supports a charset, like UTF-8, or iso-8859-1, which can display accented chars) ! But now let&#39;s go for some &#34;black magic&#34;:br /br /script class=&#34;brush: plain&#34; type=&#34;syntaxhighlighter&#34;\\![CDATA\[ javapythonunicode$ python unicode\print.py \| tee Traceback (most recent call last): File &#34;unicode\print.py&#34;, line 6, in &lt;module print u&#34;àèìòù&#34; UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position 0-4: ordinal not in range(128) current stdout encoding: None current default encoding: ascii \]\] /scriptbr /That may be puzzling, but the sys.stdout printout should give you an hint.br /br /Whenever output is to a terminal, Python does perform autodetection of the encoding of your terminal, and subsequently sets sys.stdout accordingly. When sys.stdout encoding is set, any unicode object that gets printed is encoded accordingly. When output is NOT to a terminal, python does not autodetect sys.stdout encoding and, when sys.stdout has no encoding set any unicode object that is printed is a href=&#34;http://docs.python.org/library/stdtypes.html#file-objects&#34; target=&#34;blank&#34;converted according to sys.getdefaultencoding()/abr /br /So, what really happens here is:br /br /pre class=&#34;brush: py&#34;\# -- coding: utf-8 --br /import sysbr /print &#34;current stdout encoding: &#34; + str(sys.stdout.encoding)br /print &#34;current default encoding: &#34; + sys.getdefaultencoding()br /br /sys.stdout.write(br / u&#34;àèìòù&#34;.encode(sys.stdout.encoding or sys.getdefaultencoding())br / )br //prebr /Since accented characters can be encoded to UTF-8 but not to ASCII, such UnicodeEncodeErrors arise.br /br /Also beware of a href=&#34;http://bugs.python.org/issue4947&#34; target=&#34;blank&#34;bug 4947/a - it may hit you if you&#39;re using Python 2.6 or or older.br /br /&#xA;You can find the second part of this article a href=&#34;/alan/unicode-and-encoding-python-vs-java-shootout-part-2&#34;here/a&#xA;br /&#xA;&#xA;#Ollivander #python #java #unicode #encodings&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>Before going on with this post, be sure you&#39;ve read <a href="http://www.joelonsoftware.com/articles/Unicode.html" target="_blank" rel="nofollow noopener">The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)</a> - otherwise, I won&#39;t be able to solve your issues.<br/><br/>Sometimes Java fans just say that Python Unicode support sucks. While there&#39;s a bit of truth in that assertion – for Python 2.x at least, since 3.x solved the problem at its root – the real problem lies in the fact that many programmers don&#39;t know what&#39;s going on “under the hood”, and that python default behaviour is a bit unforgiving.<br/><br/>First things first: Python 2.x has got <b><a href="http://docs.python.org/library/stdtypes.html#string-methods" rel="nofollow">two distinct string types</a>: </b>the so-called “byte strings” (str type) are one, and unicode objects (unicode type) are another one. Unicode objects are much like Java strings; they&#39;re an internal abstraction of Python, and need to be interpreted to/from byte strings whenever printing, reading/writing from/to a file, etc.<br/><br/>Sometimes such interpreting “just works” (most probably if you&#39;re dealing with ascii-only text), but if such conversion fails, errors occur: Python infamous and dreaded UnicodeDecodeError can happen in many places and might sometime just puzzle the programmer:<br/><br/><pre class="brush: py"># -<em>– coding: utf-8 -</em>-<br/>import sys<br/><br/>print “current default encoding: “ + sys.getdefaultencoding()<br/><br/>“àèìòù” + u”asd”<br/></pre><br/><pre class="brush: plain">javapythonunicode$ python unicode_concat.py<br/>current default encoding: ascii<br/>Traceback (most recent call last):<br/> File “unicode_concat.py”, line 6, in &lt;module&gt;<br/> “àèìòù” + u”asd”<br/>UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xc3 in position 0: ordinal not in range(128)<br/></pre><br/> This happens when trying to concatenate an unicode string to string object. What really happens here, behind the scenes, is explained in unicode_concat_explained.py:<br/><br/><pre class="brush: py"># -<em>– coding: utf-8 -</em>-<br/>import sys<br/><br/>print “current default encoding: “ + sys.getdefaultencoding()<br/><br/>“àèìòù”.decode(sys.getdefaultencoding()) + u”asd”<br/></pre><br/>When using a byte string (not a unicode object) python tries to convert that string to unicode before concatenating to a unicode object. Since an encoding wasn&#39;t specified, python just uses the default.<br/><br/>So, if you&#39;re planning to mix unicode and bytestring objects (which is not a good idea), <b>always remember to explicitly convert string objects to unicode by their decode() method.</b> Or, if you&#39;re sure the encoding is always the same, you could add a <a href="http://docs.python.org/library/site.html" rel="nofollow">sitecustomize.py</a> to your interpreter or your project PYTHONPATH, and set such encoding. Beware that setting it system wide may lead to unexpected results when running your project on another machine.<br/><br/>Also, just remember that the <a href="http://www.python.org/dev/peps/pep-0263/" rel="nofollow">coding directive</a> at the begininning of the file does not change unicode encoding/decoding behaviour: that&#39;s a directive to the parser, which is used to instruct it about the current encoding of the file. That information is not retained nor used at runtime.<br/><br/><br/>Let&#39;s go for another much dreaded error now: UnicodeEncodeError!<br/><br/><pre class="brush: py"># -<em>– coding: utf-8 -</em>-<br/>import sys<br/>print “current stdout encoding: “ + str(sys.stdout.encoding)<br/>print “current default encoding: “ + sys.getdefaultencoding()<br/><br/>print u”àèìòù”<br/></pre><br/>results in this output: <br/><br/><pre class="brush: plain">javapythonunicode$ python unicode_print.py<br/>current stdout encoding: UTF-8<br/>àèìòù<br/></pre><br/>Everything seems to work (as long as your terminal supports a charset, like UTF-8, or iso-8859-1, which can display accented chars) ! But now let&#39;s go for some “black magic”:<br/><br/><br/>That may be puzzling, but the sys.stdout printout should give you an hint.<br/><br/>Whenever output is to a terminal, Python does perform autodetection of the encoding of your terminal, and subsequently sets sys.stdout accordingly. When sys.stdout encoding is set, any unicode object that gets printed is encoded accordingly. When output is NOT to a terminal, python does not autodetect sys.stdout encoding and, when sys.stdout has no encoding set any unicode object that is printed is <a href="http://docs.python.org/library/stdtypes.html#file-objects" target="_blank" rel="nofollow noopener">converted according to sys.getdefaultencoding()</a><br/><br/>So, what really happens here is:<br/><br/><pre class="brush: py"># -<em>– coding: utf-8 -</em>-<br/>import sys<br/>print “current stdout encoding: “ + str(sys.stdout.encoding)<br/>print “current default encoding: “ + sys.getdefaultencoding()<br/><br/>sys.stdout.write(<br/> u”àèìòù”.encode(sys.stdout.encoding or sys.getdefaultencoding())<br/> )<br/></pre><br/>Since accented characters can be encoded to UTF-8 but not to ASCII, such UnicodeEncodeErrors arise.<br/><br/>Also beware of <a href="http://bugs.python.org/issue4947" target="_blank" rel="nofollow noopener">bug 4947</a> - it may hit you if you&#39;re using Python 2.6 or or older.<br/><br/>
You can find the second part of this article <a href="/alan/unicode-and-encoding-python-vs-java-shootout-part-2" rel="nofollow">here</a>
<br/></p>

<p><a href="/alan/tag:Ollivander" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Ollivander</span></a> <a href="/alan/tag:python" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">python</span></a> <a href="/alan/tag:java" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">java</span></a> <a href="/alan/tag:unicode" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">unicode</span></a> <a href="/alan/tag:encodings" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">encodings</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/unicode-and-encoding-python-vs-java-shootout-part-1</guid>
      <pubDate>Wed, 22 Feb 2012 20:23:00 +0000</pubDate>
    </item>
    <item>
      <title>Deploy any Java webapp via war to Heroku</title>
      <link>https://www.franzoni.eu/alan/deploy-any-java-webapp-via-war-to-heroku</link>
      <description>&lt;![CDATA[a href=&#34;http://www.heroku.com/java&#34;Heroku Java/a support is something I longed for, but they a href=&#34;http://devcenter.heroku.com/articles/java-faq#canideployanapplicationpackagedasawarfileto_heroku&#34;apparently don&#39;t support/a deploying a plain, old war file to Heroku. br /br /I don&#39;t like that; I&#39;ve got my own continuous integration setup, and I want to compile my files on my own box - this allows me to keep my own Maven proxy with my own dependencies - which sometimes are patched versions of public ones -  so that I don&#39;t need to push everything on a public repo. I don&#39;t want to be forced to build everything on Heroku, and I don&#39;t want to compile anything I push - I&#39;d usually push to Heroku stable versions only, or maybe integration versions that pass unit, integration and acceptance tests.br /br /So what? Don&#39;t worry, deployment is possible.br /br /script src=&#34;https://gist.github.com/1301100.js&#34; /scriptbr/br/Setup an heroku-aware git repository and a a href=&#34;http://devcenter.heroku.com/articles/java&#34;Cedar stack app,/a and add the files you see above; your dir structure should look like:br /br /blockquotepom.xmlbr /Procfilebr /src/br /     main/br /             java/br /                    root/br /                           StartWebApp.javabr /             resources/br /                             mywebapp.war/blockquotebr /br /Now a simplebr /br /blockquotegit push heroku master/blockquotebr /br /Should do the trick, and let you see your fully deployed war app.br /br /br /There&#39;re still things you need to be aware of; file persistence may not work as expected, since filesystem is ephemeral, and I still need to figure out things related to database configuration, but those should not come at a great expense.br /br /I&#39;ll post updates in the future as soon as I get them right.br /br /br /br /&#xA;&#xA;#Ollivander #java #heroku #war&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="http://www.heroku.com/java" rel="nofollow">Heroku Java</a> support is something I longed for, but they <a href="http://devcenter.heroku.com/articles/java-faq#can_i_deploy_an_application_packaged_as_a_war_file_to_heroku" rel="nofollow">apparently don&#39;t support</a> deploying a plain, old war file to Heroku. <br/><br/>I don&#39;t like that; I&#39;ve got my own continuous integration setup, and I want to compile my files on my own box – this allows me to keep my own Maven proxy with my own dependencies – which sometimes are patched versions of public ones –  so that I don&#39;t need to push everything on a public repo. I don&#39;t want to be forced to build everything on Heroku, and I don&#39;t want to compile anything I push – I&#39;d usually push to Heroku stable versions only, or maybe integration versions that pass unit, integration and acceptance tests.<br/><br/>So what? Don&#39;t worry, deployment is possible.<br/><br/><br/><br/>Setup an heroku-aware git repository and a <a href="http://devcenter.heroku.com/articles/java" rel="nofollow">Cedar stack app,</a> and add the files you see above; your dir structure should look like:<br/><br/><blockquote>pom.xml<br/>Procfile<br/>src/<br/>     main/<br/>             java/<br/>                    root/<br/>                           StartWebApp.java<br/>             resources/<br/>                             mywebapp.war</blockquote><br/><br/>Now a simple<br/><br/><blockquote>git push heroku master</blockquote><br/><br/>Should do the trick, and let you see your fully deployed war app.<br/><br/><br/>There&#39;re still things you need to be aware of; file persistence may not work as expected, since filesystem is ephemeral, and I still need to figure out things related to database configuration, but those should not come at a great expense.<br/><br/>I&#39;ll post updates in the future as soon as I get them right.<br/><br/><br/><br/></p>

<p><a href="/alan/tag:Ollivander" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Ollivander</span></a> <a href="/alan/tag:java" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">java</span></a> <a href="/alan/tag:heroku" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">heroku</span></a> <a href="/alan/tag:war" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">war</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/deploy-any-java-webapp-via-war-to-heroku</guid>
      <pubDate>Thu, 20 Oct 2011 19:15:00 +0000</pubDate>
    </item>
    <item>
      <title>Mock Javamail primer</title>
      <link>https://www.franzoni.eu/alan/mock-javamail-primer</link>
      <description>&lt;![CDATA[Small primer for mock javamail, a pretty useful project that will be handy whenever you need to test code that uses POP3/IMAP/SMTP and you wouldn&#39;t like to build wrappers for anything you need to test - javamail does not really offer interfaces that can be implemented by mock objects, but different implementations can be used at runtime depending on which jars are on the classpath.br /br /Take a look at the project homepage:br /br /a href=&#34;http://java.net/projects/mock-javamail&#34;http://java.net/projects/mock-javamail/abr /br /Be sure you&#39;ve enabled java.net Maven repo, and add this dependency to your pom.xml:br /br /pre style=&#34;background-color: #002b36; border-color: #000000; border-style: solid; border-width: 0.01mm; font-family: monospace; font-size: 13pt; line-height: 100%; padding: 4px;&#34;span style=&#34;background-color: #073642; color: #586e75;&#34;1 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\&lt;/spanspan style=&#34;background-color: #002b36; color: #268bd2; font-weight: bold;&#34;dependency/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\  br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;2 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\&lt;/spanspan style=&#34;background-color: #002b36; color: #268bd2; font-weight: bold;&#34;groupId/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\  /spanspan style=&#34;background-color: #002b36; color: #839496;&#34;org.jvnet.mock-javamail/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\/&lt;/spanspan style=&#34;background-color: #002b36; color: #268bd2; font-weight: bold;&#34;groupId/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\  br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;3 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\&lt;/spanspan style=&#34;background-color: #002b36; color: #268bd2; font-weight: bold;&#34;artifactId/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\  /spanspan style=&#34;background-color: #002b36; color: #839496;&#34;mock-javamail/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\/&lt;/spanspan style=&#34;background-color: #002b36; color: #268bd2; font-weight: bold;&#34;artifactId/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\  br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;4 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\&lt;/spanspan style=&#34;background-color: #002b36; color: #268bd2; font-weight: bold;&#34;version/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\  /spanspan style=&#34;background-color: #002b36; color: #839496;&#34;1.9/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\/&lt;/spanspan style=&#34;background-color: #002b36; color: #268bd2; font-weight: bold;&#34;version/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\  br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;5 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\&lt;/spanspan style=&#34;background-color: #002b36; color: #268bd2; font-weight: bold;&#34;scope/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\  /spanspan style=&#34;background-color: #002b36; color: #839496;&#34;test/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\/&lt;/spanspan style=&#34;background-color: #002b36; color: #268bd2; font-weight: bold;&#34;scope/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\  br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;6 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\/&lt;/spanspan style=&#34;background-color: #002b36; color: #268bd2; font-weight: bold;&#34;dependency/spanspan style=&#34;background-color: #002b36; color: #586e75;&#34;\  /span/prebr /You&#39;re almost ready; now setup your test code this way:br /br /pre style=&#34;background-color: #002b36; border-color: #000000; border-style: solid; border-width: 0.01mm; font-family: monospace; font-size: 13pt; line-height: 100%; padding: 4px;&#34;span style=&#34;background-color: #073642; color: #586e75;&#34; 1 /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;public/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;class/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; IMAPMailRepositoryTest {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 2 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #859900;&#34;@Before&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 3 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;public/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;void/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; setUp() /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;throws/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Exception {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 4 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;final/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Session session = Session.getInstance(&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 5 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; System.getProperties());&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 6&#xA;br / 7 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; MimeMessage msg = /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;new/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; MimeMessage(&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 8 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; session);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 9 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; msg.setRecipients(Message.RecipientType./spanspan style=&#34;background-color: #002b36; color: #d33682;&#34;TO/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;,&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;10 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;testuser@mockserver.com&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;11 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; msg.setSubject(/spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;Some Subject&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;12 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; msg.setText(/spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;sometext&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;13 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Transport.send(msg);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;14 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; }&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;15&#xA;br /16 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #859900;&#34;@After&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;17 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;public/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;void/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; tearDown() /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;throws/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Exception {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;18 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Mailbox.clearAll();&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;19 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; }/span/prebr /br /br /Now you&#39;re ready to use your mock javamail:br /br /pre style=&#34;background-color: #002b36; border-color: #000000; border-style: solid; border-width: 0.01mm; font-family: monospace; font-size: 13pt; line-height: 100%; padding: 4px;&#34;span style=&#34;background-color: #073642; color: #586e75;&#34; 1 /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;public/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;class/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; SomeImapClient {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 2&#xA;br / 3 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;private/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;static/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Logger /spanspan style=&#34;background-color: #002b36; color: #d33682;&#34;log/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; = Logger.getLogger(SomeImapClient./spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;class/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 4&#xA;br / 5 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;public/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;void/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; processMail() {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 6 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;try/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 7 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Session session = getMailSession();&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 8 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Store store = connect(session);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34; 9 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Folder folder = openMailFolder(store);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;10 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; findContent(folder);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;11 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; } /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;catch/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; (MessagingException e) {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;12 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;throw/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;new/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; RuntimeException(e);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;13 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; } /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;catch/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; (IOException e) {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;14 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;throw/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;new/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; RuntimeException(e);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;15 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; }&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;16&#xA;br /17 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; }&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;18&#xA;br /19 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;public/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Session getMailSession() {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;20 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Properties props = System.getProperties();&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;21 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; props.setProperty(/spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;mail.store.protocol&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;, /spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;imaps&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;22 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; props.setProperty(/spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;mail.imap.partialfetch&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;, /spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;0&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;23&#xA;br /24 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #d33682;&#34;log/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;.debug(/spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;Getting session&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;25 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;return/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Session.getDefaultInstance(props, /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;null/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;26&#xA;br /27 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; }&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;28&#xA;br /29 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;public/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Store connect(Session session) /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;throws/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; MessagingException {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;30 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #d33682;&#34;log/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;.debug(/spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;getting the session for accessing email.&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;31 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Store store = session.getStore(/spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;imap&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;32&#xA;br /33 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; store.connect(/spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;mockserver.com&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;, /spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;testuser&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;, /spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;somepassword&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;34 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #d33682;&#34;log/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;.debug(/spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;Connection established with IMAP server.&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;35 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;return/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; store;&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;36 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; }&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;37&#xA;br /38 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;public/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Folder openMailFolder(Store store) /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;throws/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; MessagingException {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;39 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; Folder folder = store.getDefaultFolder();&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;40 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; folder = folder.getFolder(/spanspan style=&#34;background-color: #002b36; color: #2aa198;&#34;&#34;inbox&#34;/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;41 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; folder.open(Folder./spanspan style=&#34;background-color: #002b36; color: #d33682;&#34;READ\_ONLY/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;);&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;42 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;return/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; folder;&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;43 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; }&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;44&#xA;br /45 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;public/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;void/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; findContent(Folder folder) /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;throws/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; MessagingException, IOException {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;46 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #b58900;&#34;for/spanspan style=&#34;background-color: #002b36; color: #839496;&#34; (Message m : folder.getMessages()) {&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;47 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; /spanspan style=&#34;background-color: #002b36; color: #d33682;&#34;log/spanspan style=&#34;background-color: #002b36; color: #839496;&#34;.debug(m.getSubject());&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;48&#xA;br /49 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; }&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;50&#xA;br /51&#xA;br /52 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34; }&#xA;br //spanspan style=&#34;background-color: #073642; color: #586e75;&#34;53&#xA;br /54&#xA;br /55 /spanspan style=&#34;background-color: #002b36; color: #839496;&#34;}/span/prebr /br /Output is:br /br /blockquoteblockquoteDEBUG 30 Aug 2011 19:25:25 SomeImapClient:32 - Getting session/blockquoteblockquoteDEBUG 30 Aug 2011 19:25:25 SomeImapClient:38 - getting the session for accessing email./blockquoteblockquoteDEBUG 30 Aug 2011 19:25:25 SomeImapClient:42 - Connection established with IMAP server./blockquoteblockquoteDEBUG 30 Aug 2011 19:25:25 SomeImapClient:55 - Some Subject/blockquoteblockquotebr //blockquote/blockquotebr /Some small gotchas:br /br /ulliyour store.connect(&#34;example.com&#34;, &#34;username&#34;, &#34;anything&#34;) must match your &#34;username@example.com&#34; addressee in msg.setRecipients; different addressees will yield multiple mailboxes./li&#xA;&#xA; liOnly works for plain imap &amp;amp; pop3 mailboxes. Even though it doesn&#39;t make a lot of sense to use SSL on a mock mailbox, if you try getting the &#34;imaps&#34; store you&#39;ll get an error as a real implementation of javamail will be employed./li&#xA; liI&#39;d always advise to use such tearDown() when doing unit tests, since the Session seems global and different tests could interfere with different contents - unless you use a different fake user/test server url in each of your tests./li&#xA; /ulbr /br /br /&#xA;&#xA;#Ollivander #java #javamail #smtp&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>Small primer for mock javamail, a pretty useful project that will be handy whenever you need to test code that uses POP3/IMAP/SMTP and you wouldn&#39;t like to build wrappers for anything you need to test – javamail does not really offer interfaces that can be implemented by mock objects, but different implementations can be used at runtime depending on which jars are on the classpath.<br/><br/>Take a look at the project homepage:<br/><br/><a href="http://java.net/projects/mock-javamail" rel="nofollow">http://java.net/projects/mock-javamail</a><br/><br/>Be sure you&#39;ve enabled java.net Maven repo, and add this dependency to your pom.xml:<br/><br/><pre style="background-color: #002b36; border-color: #000000; border-style: solid; border-width: 0.01mm; font-family: monospace; font-size: 13pt; line-height: 100%; padding: 4px;"><span style="background-color: #073642; color: #586e75;">1 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #586e75;">&lt;</span><span style="background-color: #002b36; color: #268bd2; font-weight: bold;">dependency</span><span style="background-color: #002b36; color: #586e75;">&gt;
<br/></span><span style="background-color: #073642; color: #586e75;">2 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #586e75;">&lt;</span><span style="background-color: #002b36; color: #268bd2; font-weight: bold;">groupId</span><span style="background-color: #002b36; color: #586e75;">&gt;</span><span style="background-color: #002b36; color: #839496;">org.jvnet.mock-javamail</span><span style="background-color: #002b36; color: #586e75;">&lt;/</span><span style="background-color: #002b36; color: #268bd2; font-weight: bold;">groupId</span><span style="background-color: #002b36; color: #586e75;">&gt;
<br/></span><span style="background-color: #073642; color: #586e75;">3 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #586e75;">&lt;</span><span style="background-color: #002b36; color: #268bd2; font-weight: bold;">artifactId</span><span style="background-color: #002b36; color: #586e75;">&gt;</span><span style="background-color: #002b36; color: #839496;">mock-javamail</span><span style="background-color: #002b36; color: #586e75;">&lt;/</span><span style="background-color: #002b36; color: #268bd2; font-weight: bold;">artifactId</span><span style="background-color: #002b36; color: #586e75;">&gt;
<br/></span><span style="background-color: #073642; color: #586e75;">4 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #586e75;">&lt;</span><span style="background-color: #002b36; color: #268bd2; font-weight: bold;">version</span><span style="background-color: #002b36; color: #586e75;">&gt;</span><span style="background-color: #002b36; color: #839496;">1.9</span><span style="background-color: #002b36; color: #586e75;">&lt;/</span><span style="background-color: #002b36; color: #268bd2; font-weight: bold;">version</span><span style="background-color: #002b36; color: #586e75;">&gt;
<br/></span><span style="background-color: #073642; color: #586e75;">5 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #586e75;">&lt;</span><span style="background-color: #002b36; color: #268bd2; font-weight: bold;">scope</span><span style="background-color: #002b36; color: #586e75;">&gt;</span><span style="background-color: #002b36; color: #839496;">test</span><span style="background-color: #002b36; color: #586e75;">&lt;/</span><span style="background-color: #002b36; color: #268bd2; font-weight: bold;">scope</span><span style="background-color: #002b36; color: #586e75;">&gt;
<br/></span><span style="background-color: #073642; color: #586e75;">6 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #586e75;">&lt;/</span><span style="background-color: #002b36; color: #268bd2; font-weight: bold;">dependency</span><span style="background-color: #002b36; color: #586e75;">&gt;</span></pre><br/>You&#39;re almost ready; now setup your test code this way:<br/><br/><pre style="background-color: #002b36; border-color: #000000; border-style: solid; border-width: 0.01mm; font-family: monospace; font-size: 13pt; line-height: 100%; padding: 4px;"><span style="background-color: #073642; color: #586e75;"> 1 </span><span style="background-color: #002b36; color: #b58900;">public</span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">class</span><span style="background-color: #002b36; color: #839496;"> IMAPMailRepositoryTest {
<br/></span><span style="background-color: #073642; color: #586e75;"> 2 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #859900;">@Before
<br/></span><span style="background-color: #073642; color: #586e75;"> 3 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">public</span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">void</span><span style="background-color: #002b36; color: #839496;"> setUp() </span><span style="background-color: #002b36; color: #b58900;">throws</span><span style="background-color: #002b36; color: #839496;"> Exception {
<br/></span><span style="background-color: #073642; color: #586e75;"> 4 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">final</span><span style="background-color: #002b36; color: #839496;"> Session session = Session.getInstance(
<br/></span><span style="background-color: #073642; color: #586e75;"> 5 </span><span style="background-color: #002b36; color: #839496;"> System.getProperties());
<br/></span><span style="background-color: #073642; color: #586e75;"> 6
<br/> 7 </span><span style="background-color: #002b36; color: #839496;"> MimeMessage msg = </span><span style="background-color: #002b36; color: #b58900;">new</span><span style="background-color: #002b36; color: #839496;"> MimeMessage(
<br/></span><span style="background-color: #073642; color: #586e75;"> 8 </span><span style="background-color: #002b36; color: #839496;"> session);
<br/></span><span style="background-color: #073642; color: #586e75;"> 9 </span><span style="background-color: #002b36; color: #839496;"> msg.setRecipients(Message.RecipientType.</span><span style="background-color: #002b36; color: #d33682;">TO</span><span style="background-color: #002b36; color: #839496;">,
<br/></span><span style="background-color: #073642; color: #586e75;">10 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #2aa198;">“<a href="mailto:testuser@mockserver.com" rel="nofollow">testuser@mockserver.com</a>“</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">11 </span><span style="background-color: #002b36; color: #839496;"> msg.setSubject(</span><span style="background-color: #002b36; color: #2aa198;">“Some Subject”</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">12 </span><span style="background-color: #002b36; color: #839496;"> msg.setText(</span><span style="background-color: #002b36; color: #2aa198;">“sometext”</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">13 </span><span style="background-color: #002b36; color: #839496;"> Transport.send(msg);
<br/></span><span style="background-color: #073642; color: #586e75;">14 </span><span style="background-color: #002b36; color: #839496;"> }
<br/></span><span style="background-color: #073642; color: #586e75;">15
<br/>16 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #859900;">@After
<br/></span><span style="background-color: #073642; color: #586e75;">17 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">public</span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">void</span><span style="background-color: #002b36; color: #839496;"> tearDown() </span><span style="background-color: #002b36; color: #b58900;">throws</span><span style="background-color: #002b36; color: #839496;"> Exception {
<br/></span><span style="background-color: #073642; color: #586e75;">18 </span><span style="background-color: #002b36; color: #839496;"> Mailbox.clearAll();
<br/></span><span style="background-color: #073642; color: #586e75;">19 </span><span style="background-color: #002b36; color: #839496;"> }</span></pre><br/><br/><br/>Now you&#39;re ready to use your mock javamail:<br/><br/><pre style="background-color: #002b36; border-color: #000000; border-style: solid; border-width: 0.01mm; font-family: monospace; font-size: 13pt; line-height: 100%; padding: 4px;"><span style="background-color: #073642; color: #586e75;"> 1 </span><span style="background-color: #002b36; color: #b58900;">public</span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">class</span><span style="background-color: #002b36; color: #839496;"> SomeImapClient {
<br/></span><span style="background-color: #073642; color: #586e75;"> 2
<br/> 3 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">private</span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">static</span><span style="background-color: #002b36; color: #839496;"> Logger </span><span style="background-color: #002b36; color: #d33682;">log</span><span style="background-color: #002b36; color: #839496;"> = Logger.getLogger(SomeImapClient.</span><span style="background-color: #002b36; color: #b58900;">class</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;"> 4
<br/> 5 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">public</span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">void</span><span style="background-color: #002b36; color: #839496;"> processMail() {
<br/></span><span style="background-color: #073642; color: #586e75;"> 6 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">try</span><span style="background-color: #002b36; color: #839496;"> {
<br/></span><span style="background-color: #073642; color: #586e75;"> 7 </span><span style="background-color: #002b36; color: #839496;"> Session session = getMailSession();
<br/></span><span style="background-color: #073642; color: #586e75;"> 8 </span><span style="background-color: #002b36; color: #839496;"> Store store = connect(session);
<br/></span><span style="background-color: #073642; color: #586e75;"> 9 </span><span style="background-color: #002b36; color: #839496;"> Folder folder = openMailFolder(store);
<br/></span><span style="background-color: #073642; color: #586e75;">10 </span><span style="background-color: #002b36; color: #839496;"> findContent(folder);
<br/></span><span style="background-color: #073642; color: #586e75;">11 </span><span style="background-color: #002b36; color: #839496;"> } </span><span style="background-color: #002b36; color: #b58900;">catch</span><span style="background-color: #002b36; color: #839496;"> (MessagingException e) {
<br/></span><span style="background-color: #073642; color: #586e75;">12 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">throw</span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">new</span><span style="background-color: #002b36; color: #839496;"> RuntimeException(e);
<br/></span><span style="background-color: #073642; color: #586e75;">13 </span><span style="background-color: #002b36; color: #839496;"> } </span><span style="background-color: #002b36; color: #b58900;">catch</span><span style="background-color: #002b36; color: #839496;"> (IOException e) {
<br/></span><span style="background-color: #073642; color: #586e75;">14 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">throw</span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">new</span><span style="background-color: #002b36; color: #839496;"> RuntimeException(e);
<br/></span><span style="background-color: #073642; color: #586e75;">15 </span><span style="background-color: #002b36; color: #839496;"> }
<br/></span><span style="background-color: #073642; color: #586e75;">16
<br/>17 </span><span style="background-color: #002b36; color: #839496;"> }
<br/></span><span style="background-color: #073642; color: #586e75;">18
<br/>19 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">public</span><span style="background-color: #002b36; color: #839496;"> Session getMailSession() {
<br/></span><span style="background-color: #073642; color: #586e75;">20 </span><span style="background-color: #002b36; color: #839496;"> Properties props = System.getProperties();
<br/></span><span style="background-color: #073642; color: #586e75;">21 </span><span style="background-color: #002b36; color: #839496;"> props.setProperty(</span><span style="background-color: #002b36; color: #2aa198;">“mail.store.protocol”</span><span style="background-color: #002b36; color: #839496;">, </span><span style="background-color: #002b36; color: #2aa198;">“imaps”</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">22 </span><span style="background-color: #002b36; color: #839496;"> props.setProperty(</span><span style="background-color: #002b36; color: #2aa198;">“mail.imap.partialfetch”</span><span style="background-color: #002b36; color: #839496;">, </span><span style="background-color: #002b36; color: #2aa198;">“0”</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">23
<br/>24 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #d33682;">log</span><span style="background-color: #002b36; color: #839496;">.debug(</span><span style="background-color: #002b36; color: #2aa198;">“Getting session”</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">25 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">return</span><span style="background-color: #002b36; color: #839496;"> Session.getDefaultInstance(props, </span><span style="background-color: #002b36; color: #b58900;">null</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">26
<br/>27 </span><span style="background-color: #002b36; color: #839496;"> }
<br/></span><span style="background-color: #073642; color: #586e75;">28
<br/>29 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">public</span><span style="background-color: #002b36; color: #839496;"> Store connect(Session session) </span><span style="background-color: #002b36; color: #b58900;">throws</span><span style="background-color: #002b36; color: #839496;"> MessagingException {
<br/></span><span style="background-color: #073642; color: #586e75;">30 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #d33682;">log</span><span style="background-color: #002b36; color: #839496;">.debug(</span><span style="background-color: #002b36; color: #2aa198;">“getting the session for accessing email.”</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">31 </span><span style="background-color: #002b36; color: #839496;"> Store store = session.getStore(</span><span style="background-color: #002b36; color: #2aa198;">“imap”</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">32
<br/>33 </span><span style="background-color: #002b36; color: #839496;"> store.connect(</span><span style="background-color: #002b36; color: #2aa198;">“mockserver.com”</span><span style="background-color: #002b36; color: #839496;">, </span><span style="background-color: #002b36; color: #2aa198;">“testuser”</span><span style="background-color: #002b36; color: #839496;">, </span><span style="background-color: #002b36; color: #2aa198;">“somepassword”</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">34 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #d33682;">log</span><span style="background-color: #002b36; color: #839496;">.debug(</span><span style="background-color: #002b36; color: #2aa198;">“Connection established with IMAP server.”</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">35 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">return</span><span style="background-color: #002b36; color: #839496;"> store;
<br/></span><span style="background-color: #073642; color: #586e75;">36 </span><span style="background-color: #002b36; color: #839496;"> }
<br/></span><span style="background-color: #073642; color: #586e75;">37
<br/>38 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">public</span><span style="background-color: #002b36; color: #839496;"> Folder openMailFolder(Store store) </span><span style="background-color: #002b36; color: #b58900;">throws</span><span style="background-color: #002b36; color: #839496;"> MessagingException {
<br/></span><span style="background-color: #073642; color: #586e75;">39 </span><span style="background-color: #002b36; color: #839496;"> Folder folder = store.getDefaultFolder();
<br/></span><span style="background-color: #073642; color: #586e75;">40 </span><span style="background-color: #002b36; color: #839496;"> folder = folder.getFolder(</span><span style="background-color: #002b36; color: #2aa198;">“inbox”</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">41 </span><span style="background-color: #002b36; color: #839496;"> folder.open(Folder.</span><span style="background-color: #002b36; color: #d33682;">READ_ONLY</span><span style="background-color: #002b36; color: #839496;">);
<br/></span><span style="background-color: #073642; color: #586e75;">42 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">return</span><span style="background-color: #002b36; color: #839496;"> folder;
<br/></span><span style="background-color: #073642; color: #586e75;">43 </span><span style="background-color: #002b36; color: #839496;"> }
<br/></span><span style="background-color: #073642; color: #586e75;">44
<br/>45 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">public</span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">void</span><span style="background-color: #002b36; color: #839496;"> findContent(Folder folder) </span><span style="background-color: #002b36; color: #b58900;">throws</span><span style="background-color: #002b36; color: #839496;"> MessagingException, IOException {
<br/></span><span style="background-color: #073642; color: #586e75;">46 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #b58900;">for</span><span style="background-color: #002b36; color: #839496;"> (Message m : folder.getMessages()) {
<br/></span><span style="background-color: #073642; color: #586e75;">47 </span><span style="background-color: #002b36; color: #839496;"> </span><span style="background-color: #002b36; color: #d33682;">log</span><span style="background-color: #002b36; color: #839496;">.debug(m.getSubject());
<br/></span><span style="background-color: #073642; color: #586e75;">48
<br/>49 </span><span style="background-color: #002b36; color: #839496;"> }
<br/></span><span style="background-color: #073642; color: #586e75;">50
<br/>51
<br/>52 </span><span style="background-color: #002b36; color: #839496;"> }
<br/></span><span style="background-color: #073642; color: #586e75;">53
<br/>54
<br/>55 </span><span style="background-color: #002b36; color: #839496;">}</span></pre><br/><br/>Output is:<br/><br/><blockquote><blockquote>DEBUG 30 Aug 2011 19:25:25 SomeImapClient:32 – Getting session</blockquote><blockquote>DEBUG 30 Aug 2011 19:25:25 SomeImapClient:38 – getting the session for accessing email.</blockquote><blockquote>DEBUG 30 Aug 2011 19:25:25 SomeImapClient:42 – Connection established with IMAP server.</blockquote><blockquote>DEBUG 30 Aug 2011 19:25:25 SomeImapClient:55 – Some Subject</blockquote><blockquote><br/></blockquote></blockquote><br/>Some small gotchas:<br/><br/><ul><li>your store.connect(“example.com”, “username”, “anything”) must match your “<a href="mailto:username@example.com" rel="nofollow">username@example.com</a>” addressee in msg.setRecipients; different addressees will yield multiple mailboxes.</li></p>

<p> <li>Only works for plain imap &amp; pop3 mailboxes. Even though it doesn&#39;t make a lot of sense to use SSL on a mock mailbox, if you try getting the “imaps” store you&#39;ll get an error as a real implementation of javamail will be employed.</li>
 <li>I&#39;d always advise to use such tearDown() when doing unit tests, since the Session seems global and different tests could interfere with different contents – unless you use a different fake user/test server url in each of your tests.</li>
 </ul><br/><br/><br/></p>

<p><a href="/alan/tag:Ollivander" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Ollivander</span></a> <a href="/alan/tag:java" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">java</span></a> <a href="/alan/tag:javamail" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">javamail</span></a> <a href="/alan/tag:smtp" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">smtp</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/mock-javamail-primer</guid>
      <pubDate>Tue, 30 Aug 2011 19:10:00 +0000</pubDate>
    </item>
    <item>
      <title>Apple and OpenJDK: was I wrong?</title>
      <link>https://www.franzoni.eu/alan/apple-and-openjdk-was-i-wrong</link>
      <description>&lt;![CDATA[Apple has a href=&#34;http://www.apple.com/pr/library/2010/11/12openjdk.html&#34;announced/a it will contribute to the OpenJDK project, so there&#39;s probably no risk that MacOS-specific pieces of the current JDK will be lost. Probably the next, Oracle-backed JDK will be just fine on Mac and will probably be released on time.br /br /There&#39;re two things I don&#39;t understand:br /br /ulli why such a bad timing? Apple could have deprecated his Java and announced OpenJDK participation at the same time - and that would have been good news. I don&#39;t understand if Apple had already chosen to contribute to OpenJDK or it had been pushed in that direction once it saw what happened in the dev community after their deprecation./liliIt&#39;s still not clear why I can&#39;t use Java in a software I&#39;d like to distribute within the Mac App Store. If it&#39;s Oracle backed it&#39;s not a &#34;deprecated technology&#34; anymore, right? The language restriction was lifted even in iOS apps, why should it come back on Mac apps?/li/ul/div&#xA;&#xA;#Ollivander #apple #java&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>Apple has <a href="http://www.apple.com/pr/library/2010/11/12openjdk.html" rel="nofollow">announced</a> it will contribute to the OpenJDK project, so there&#39;s probably no risk that MacOS-specific pieces of the current JDK will be lost. Probably the next, Oracle-backed JDK will be just fine on Mac and will probably be released on time.<br/><br/>There&#39;re two things I don&#39;t understand:<br/><br/><ul><li> why such a bad timing? Apple could have deprecated his Java and announced OpenJDK participation at the same time – and that would have been good news. I don&#39;t understand if Apple had already chosen to contribute to OpenJDK or it had been pushed in that direction once it saw what happened in the dev community after their deprecation.</li><li>It&#39;s still not clear why I can&#39;t use Java in a software I&#39;d like to distribute within the Mac App Store. If it&#39;s Oracle backed it&#39;s not a “deprecated technology” anymore, right? The language restriction was lifted even in iOS apps, why should it come back on Mac apps?</li></ul></div></p>

<p><a href="/alan/tag:Ollivander" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Ollivander</span></a> <a href="/alan/tag:apple" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">apple</span></a> <a href="/alan/tag:java" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">java</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/apple-and-openjdk-was-i-wrong</guid>
      <pubDate>Sun, 21 Nov 2010 21:20:00 +0000</pubDate>
    </item>
    <item>
      <title>Why Apple Should Not Ditch Java</title>
      <link>https://www.franzoni.eu/alan/why-apple-should-not-ditch-java</link>
      <description>&lt;![CDATA[I&#39;m not an hardcore Apple user. My first Apple product ever was a Macbook Pro I bought about one year ago.br /br /But I fell in love with the Mac platform, and I think its really strong point is that it&#39;s bversatile/b. It&#39;s got plenty of useful proprietary apps that sometimes I want to use or I am forced to use, like Microsoft Office or Adobe Photoshop, a pretty decent Adobe Flash implementation, but since it&#39;s a BSD derivative it&#39;s got a lot of nifty tools I&#39;m used to and that I really like.br /br /Sure, Windows might be faster at many tasks, especially if you&#39;re gonna spend as much money as you need to invest in Apple hardware, but my feelings towards Windows are mixed. I liked XP at the beginning, but it grew old and tired; Vista was a mess; Seven seems fine, but Microsoft still needs to conquer back my trust. Also, Windows is fine as a development platform as long as you use Microsoft tools - which are quite good - but it can get squeaky as soon as you adventure into the a href=&#34;http://en.wikipedia.org/wiki/FreeandOpenSourceSoftware&#34;FLOSS/a land - I can remember some madness I incurred in while trying to use git under Windows.br /br /What about Linux? I&#39;s a great development environment. I simply love my a href=&#34;http://www.ubuntu.com/&#34;Ubuntu/a box, it&#39;s exactly as I want it. Eclipse, Netbeans, everything is set up as I want, all the key bindings, all the details. But my Ubuntu box is a tower form factor workstation with ethernet connectivity and two monitors - I think Ubuntu fails pretty hard at the common desktop experience, especially on a notebook - wireless connectivity is often an issue, suspend and hibernate are unreliable, the battery doesn&#39;t last as long as I&#39;d like, and Flash on full screen is flaky. I don&#39;t think it&#39;s Canonical&#39;s own fault - it&#39;s really hard to have an OS work on each and every hardware and giving it away from free - but from the user&#39;s point of view this is a failure.  Some key apps are also missing - a href=&#34;http://www.openoffice.org/&#34;OpenOffice/a might not be enough a replacement for Office, and a href=&#34;http://www.gimp.org/&#34;GIMP/a is not quite as effective as Adobe Photoshop.br /br /But the Mac has it all, and that means it&#39;s got a blot/b of appeal on developers - we usually want the eye candy and the dark console, we like our computer to &#39;just work&#39; and yet we like to be able to tune every detail. Whenever I go to a conference I can see there&#39;re far more Macs, iPads, iPhones than in any other typical business environment. A blot/b of developers from many communities simply love using Macs for their development work -b I simply don&#39;t think that developers don&#39;t count in Mac market as someone stated. /bMaybe many of them are not Java developers, but as soon as they&#39;re unable to install Eclipse they&#39;ll complain as well.br /br /Apple&#39;s Java sports its own Cocoa bindings, which are very useful to achieve a native look-and-feel, OS integration and general responsiveness for all java based software. a href=&#34;http://www.eclipse.org/&#34;Eclipse/a is java based and it&#39;s a widely used platform, a href=&#34;http://www.jetbrains.com/ruby/&#34;RubyMine/a is one of the great favourites for Ruby coders, a href=&#34;http://www.netbeans.org/&#34;Netbeans/a is evolving at a great pace and supports Java, Ruby, Python, PHP and many others. Not to mention the fact that while I write those lines it&#39;s the bonly/b JDK available and working out of the box on Snow Leopard.br /br /Now this is going to a href=&#34;http://developer.apple.com/library/mac/#releasenotes/Java/JavaSnowLeopardUpdate3LeopardUpdate8RN/NewandNoteworthy/NewandNoteworthy.html#//apple_ref/doc/uid/TP40010380-CH4-SW1&#34;change/a. Java is deprecated on Snow Leopard, won&#39;t be included in Lion and can&#39;t be used for Mac App Store.br /br /If Mac OS X were to be left with a third-party Java implementation - and let&#39;s hope a href=&#34;http://landonf.bikemonkey.org/static/soylatte/&#34;SoyLatte/a can cope with such burden - we&#39;ll probably lose native OS integration, everything that&#39;s Swing or SWT based will need X11 to run, Swing will probably use its native Metal widget set while SWT will probably rely on GTK+ bindings; Apple doesn&#39;t even seem to be willing to contribute its own patches to the OpenJDK project, even though it would be meaningful if they just didn&#39;t want to waste resources on what they think it&#39;s a dead technology.br /br /It&#39;s not a great scenario, but the real question here is: bwhy?/bbr /bbr //bbr /Why should Apple have done all that? Is it that difficult to bupdate /bthe current JDK to work with  Lion? JDK7 might be a different topic, but hey - they&#39;re basically dropping what they&#39;ve already done! And they&#39;re bnot/b letting anyone to continue their job easily. I think Apple is perfectly aware that the Mac OS X ecosystem is fairly different from the walled garden of IOS -  Windows is still got a much larger market, and limiting what Mac OS X can do will just feed Microsoft&#39;s pockets again.br /br /I don&#39;t think there will be an official statement on that, but I bet Oracle has a part on this. Mac JDK seems to have been built by Apple but to have sprung from Sun&#39;s own JDK - is Oracle not willing to let this deal to go on? Maybe Apple needed to renegotiate its license and it turned out to be too expensive? Even though Apple may think Java is dead, it has no apparent reason to silently stab it again.br /br /Their decision is causing a lot of a href=&#34;http://lists.apple.com/archives/Java-dev/2010/Oct/index.html&#34;noise/a in the Mac/Java community, let&#39;s see if Steve comments on this as well&#xA;&#xA;#Ollivander #apple #java&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>I&#39;m not an hardcore Apple user. My first Apple product ever was a Macbook Pro I bought about one year ago.<br/><br/>But I fell in love with the Mac platform, and I think its really strong point is that it&#39;s <b>versatile</b>. It&#39;s got plenty of useful proprietary apps that sometimes I want to use or I am forced to use, like Microsoft Office or Adobe Photoshop, a pretty decent Adobe Flash implementation, but since it&#39;s a BSD derivative it&#39;s got a lot of nifty tools I&#39;m used to and that I really like.<br/><br/>Sure, Windows might be faster at many tasks, especially if you&#39;re gonna spend as much money as you need to invest in Apple hardware, but my feelings towards Windows are mixed. I liked XP at the beginning, but it grew old and tired; Vista was a mess; Seven seems fine, but Microsoft still needs to conquer back my trust. Also, Windows is fine as a development platform as long as you use Microsoft tools – which are quite good – but it can get squeaky as soon as you adventure into the <a href="http://en.wikipedia.org/wiki/Free_and_Open_Source_Software" rel="nofollow">FLOSS</a> land – I can remember some madness I incurred in while trying to use git under Windows.<br/><br/>What about Linux? I&#39;s a great development environment. I simply love my <a href="http://www.ubuntu.com/" rel="nofollow">Ubuntu</a> box, it&#39;s exactly as I want it. Eclipse, Netbeans, everything is set up as I want, all the key bindings, all the details. But my Ubuntu box is a tower form factor workstation with ethernet connectivity and two monitors – I think Ubuntu fails pretty hard at the common desktop experience, especially on a notebook – wireless connectivity is often an issue, suspend and hibernate are unreliable, the battery doesn&#39;t last as long as I&#39;d like, and Flash on full screen is flaky. I don&#39;t think it&#39;s Canonical&#39;s own fault – it&#39;s really hard to have an OS work on each and every hardware and giving it away from free – but from the user&#39;s point of view this is a failure.  Some key apps are also missing – <a href="http://www.openoffice.org/" rel="nofollow">OpenOffice</a> might not be enough a replacement for Office, and <a href="http://www.gimp.org/" rel="nofollow">GIMP</a> is not quite as effective as Adobe Photoshop.<br/><br/>But the Mac has it all, and that means it&#39;s got a <b>lot</b> of appeal on developers – we usually want the eye candy and the dark console, we like our computer to &#39;just work&#39; and yet we like to be able to tune every detail. Whenever I go to a conference I can see there&#39;re far more Macs, iPads, iPhones than in any other typical business environment. A <b>lot</b> of developers from many communities simply love using Macs for their development work -<b> I simply don&#39;t think that developers don&#39;t count in Mac market as someone stated. </b>Maybe many of them are not Java developers, but as soon as they&#39;re unable to install Eclipse they&#39;ll complain as well.<br/><br/>Apple&#39;s Java sports its own Cocoa bindings, which are very useful to achieve a native look-and-feel, OS integration and general responsiveness for all java based software. <a href="http://www.eclipse.org/" rel="nofollow">Eclipse</a> is java based and it&#39;s a widely used platform, <a href="http://www.jetbrains.com/ruby/" rel="nofollow">RubyMine</a> is one of the great favourites for Ruby coders, <a href="http://www.netbeans.org/" rel="nofollow">Netbeans</a> is evolving at a great pace and supports Java, Ruby, Python, PHP and many others. Not to mention the fact that while I write those lines it&#39;s the <b>only</b> JDK available and working out of the box on Snow Leopard.<br/><br/>Now this is going to <a href="http://developer.apple.com/library/mac/#releasenotes/Java/JavaSnowLeopardUpdate3LeopardUpdate8RN/NewandNoteworthy/NewandNoteworthy.html%23//apple_ref/doc/uid/TP40010380-CH4-SW1" rel="nofollow">change</a>. Java is deprecated on Snow Leopard, won&#39;t be included in Lion and can&#39;t be used for Mac App Store.<br/><br/>If Mac OS X were to be left with a third-party Java implementation – and let&#39;s hope <a href="http://landonf.bikemonkey.org/static/soylatte/" rel="nofollow">SoyLatte</a> can cope with such burden – we&#39;ll probably lose native OS integration, everything that&#39;s Swing or SWT based will need X11 to run, Swing will probably use its native Metal widget set while SWT will probably rely on GTK+ bindings; Apple doesn&#39;t even seem to be willing to contribute its own patches to the OpenJDK project, even though it would be meaningful if they just didn&#39;t want to waste resources on what they think it&#39;s a dead technology.<br/><br/>It&#39;s not a great scenario, but the real question here is: <b>why?</b><br/><b><br/></b><br/>Why should Apple have done all that? Is it that difficult to <b>update </b>the current JDK to work with  Lion? JDK7 might be a different topic, but hey – they&#39;re basically dropping what they&#39;ve already done! And they&#39;re <b>not</b> letting anyone to continue their job easily. I think Apple is perfectly aware that the Mac OS X ecosystem is fairly different from the walled garden of IOS –  Windows is still got a much larger market, and limiting what Mac OS X can do will just feed Microsoft&#39;s pockets again.<br/><br/>I don&#39;t think there will be an official statement on that, but I bet Oracle has a part on this. Mac JDK seems to have been built by Apple but to have sprung from Sun&#39;s own JDK – is Oracle not willing to let this deal to go on? Maybe Apple needed to renegotiate its license and it turned out to be too expensive? Even though Apple may think Java is dead, it has no apparent reason to silently stab it again.<br/><br/>Their decision is causing a lot of <a href="http://lists.apple.com/archives/Java-dev/2010/Oct/index.html" rel="nofollow">noise</a> in the Mac/Java community, let&#39;s see if Steve comments on this as well</p>

<p><a href="/alan/tag:Ollivander" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Ollivander</span></a> <a href="/alan/tag:apple" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">apple</span></a> <a href="/alan/tag:java" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">java</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/why-apple-should-not-ditch-java</guid>
      <pubDate>Thu, 21 Oct 2010 20:12:00 +0000</pubDate>
    </item>
  </channel>
</rss>