<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>validates_presence_of :purpose : </title>
    <link>http://lenaherrmann.net</link>
    <language>en_US</language>
    <ttl>40</ttl>
    <description>Lena Herrmann</description>
    <item>
      <title>Ta-da! Here is my thesis.</title>
      <description>&lt;p&gt;I&amp;#8217;m happy to announce the immediate availability of my thesis! It was handed in in July 2010 as my diplom thesis in the academic program of Media Informatics at &lt;a href="http://www.beuth-hochschule.de/"&gt;Beuth Hochschule f&#252;r Technik Berlin&lt;/a&gt; (University of Applied Sciences). A &lt;a href="http://en.wikipedia.org/wiki/Diplom"&gt;German Diplom&lt;/a&gt; is comparable to a combined bachelor&amp;#8217;s and master&amp;#8217;s degree. Originally it has been written in German, so I&amp;#8217;m very happy it&amp;#8217;s also available for English speaking readers now: thanks a lot to the awesome &lt;a title="Copywriting, Translation" href="http://www.sanderstruck.com/"&gt;Sander Van de Moortel&lt;/a&gt; for translating it into English, and to &lt;a href="http://blog.couchone.com/post/1415269773/im-happy-to-announce-the-immediate-availability"&gt;CouchOne&lt;/a&gt; for sponsoring the translation. Read also &lt;a href="http://lenaherrmann.net/2010/08/05/thesis-is-done-thanks-for-your-support"&gt;my thank you notes&lt;/a&gt; for more gratefulness.&lt;/p&gt;


&lt;p&gt;I hope the thesis is an interesting read and useful for your research or development!&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Contents&lt;/h3&gt;


&lt;p&gt;The title is &lt;b&gt;&amp;#8220;Implementation of a distributed application using the
document-oriented database CouchDB &#8212; An outliner as a replicable distributed system&amp;#8221;&lt;/b&gt; (originally &lt;b&gt;&amp;#8220;Umsetzung einer verteilten Anwendung mit der dokumentenorientierten Datenbank &#8212; CouchDB Ein Gliederungseditor als replizierbares Verteiltes System&amp;#8221;&lt;/b&gt;).&lt;/p&gt; 


&lt;p&gt;What is it about? &lt;a href="http://upstre.am/blog/2010/10/diplom-thesis-a-distributed-application-with-couchdb-and-javascript/"&gt;I wrote a couple of words about that&lt;/a&gt; for the Upstream blog. Here is also the abstract &#8212; all the contents in ten sentences:&lt;/p&gt;


&lt;p&gt;&lt;i&gt;Modern web browsers and mobile devices are capable of running complex applications that allow collaboration and data exchange between their users. Laptops and mobile phones, however, cannot be expected to keep their internet connections alive at all times. This problem can be sidestepped using data replication, which means that data are regularly synchronised and kept consistent. This thesis describes the drafting and prototypical development of a JavaScript application that uses the document-oriented database CouchDB to form distributed outliner software. Outliners can be used to record thoughts or concepts in a hierarchically structured manner. Apart from categorising the system to be developed and analysing possible approaches, the thesis will also examine the technologies used. Special focus lies on CouchDB with its built-in master-master replication and its ability to implement complex applications without the use of middleware. The final application runs locally in the browser and is therefore also usable when off-line. Conflicts are resolved when the system is synchronised, sometimes steered by user input. The thesis also evaluates the applicability of CouchDB in distributed applications with particular regard to the use case at hand.&lt;/i&gt;&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Download&lt;/h3&gt;


&lt;p&gt;  
  &lt;ul&gt;
    &lt;li&gt;Read &lt;a href="http://www.scribd.com/doc/40221592/Implementation-of-a-distributed-application-using-the-document-oriented-database-CouchDB"&gt;the English version&lt;/a&gt; and &lt;a href="http://www.scribd.com/doc/40221773/Umsetzung-einer-verteilten-Anwendung-mit-der-dokumentenorientierten-Datenbank-CouchDB"&gt;the German version&lt;/a&gt; online,
    &lt;/li&gt;
    &lt;li&gt;check out the &lt;a href="https://github.com/lenalena/thesis_en"&gt;English LaTeX sources&lt;/a&gt; or the &lt;a href="https://github.com/lenalena/thesis_de"&gt;German LaTeX sources&lt;/a&gt; on github, or
    &lt;/li&gt;  
    &lt;li&gt;download the pdf &lt;a href="https://github.com/lenalena/thesis_en/raw/master/Lena_Herrmann_CouchDB_thesis.pdf"&gt;in English&lt;/a&gt; or &lt;a href="https://github.com/lenalena/thesis_de/raw/master/Lena_Herrmann_CouchDB_Diplomarbeit.pdf"&gt;in German&lt;/a&gt;.&lt;/li&gt;
  &lt;/ul&gt;
&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;BibTeX&lt;/h3&gt;


&lt;p&gt;
  In case you want to quote me in your paper/thesis/homework, here&amp;#8217;s the BibTeX entry:
  
  &lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;@mastersthesis{herrmann:couchdb,
  author = {Lena Herrmann},
  title = {Realisation of a Distributed Application Using the Document-Oriented Database CouchDB - An Outliner as a Replicable Distributed System},
  school = {University of Applied Sciences Berlin},
  year = {2010},
  month = {July}
}

@mastersthesis{herrmann:couchdb,
  author = {Lena Herrmann},
  title = {Umsetzung einer verteilten Anwendung mit der dokumentenorientierten Datenbank CouchDB - Ein Gliederungseditor als replizierbares Verteiltes System},
  school = {Beuth Hochschule f&#252;r Technik Berlin},
  year = {2010},
  month = {Juli}
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;The Application&lt;/h3&gt;


&lt;p&gt;The hands-on part is an outliner that allows people to collaborate on documents even when they don&amp;#8217;t have continuous internet access. If you want to read/clone/try out/improve/take apart/whatever the source code, &lt;a href="https://github.com/lenalena/doingnotes"&gt;it&amp;#8217;s here on github&lt;/a&gt;. &lt;a href="https://github.com/lenalena/doingnotes/blob/master/README.md"&gt;The github README&lt;/a&gt; contains installation and usage instructions, for more details read chapter 9, &amp;#8220;Application&amp;#8221;, in the thesis.&lt;/p&gt;


&lt;p&gt;You can also &lt;a href="http://lena.couchone.com:5984/doingnotes/_design/doingnotes/index.html"&gt;try out the outliner online&lt;/a&gt;. Though, in order to enjoy the replication and conflict resolution features, you have to (also) install it locally. But it&amp;#8217;s a couchapp - when you have CouchDB running you&amp;#8217;re almost there already.&lt;/p&gt;


&lt;p&gt;I hope you all like it!&lt;/p&gt;
</description>
      <pubDate>Wed, 27 Oct 2010 14:02:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:cd3cb76b-52bb-4dec-92a3-53c8b71cd46c</guid>
      <comments>http://lenaherrmann.net/2010/10/27/ta-da-here-is-my-thesis#comments</comments>
      <category>thesis</category>
      <category>couchdb</category>
      <category>javascript</category>
      <category>sammyjs</category>
      <category>couchapp</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=19</trackback:ping>
      <link>http://lenaherrmann.net/2010/10/27/ta-da-here-is-my-thesis</link>
    </item>
    <item>
      <title>Pretty URLs for you couchapp, the howto</title>
      <description>&lt;p&gt;If you have a couchapp and want to have nice URLS, go &lt;a href="http://blog.couchone.com/post/443028592/whats-new-in-apache-couchdb-0-11-part-one-nice-urls"&gt;read this blogpost&lt;/a&gt;. If you still don&amp;#8217;t get it, here is how to do it. It doesn&amp;#8217;t work with my outliner app, because it is so entangled with old couchapp and jquery.couch.js versions and I&amp;#8217;m not up to upgrade hell right now. But I&amp;#8217;ll write it down for my next project and for you, yay.&lt;/p&gt;


&lt;p&gt;Let&amp;#8217;s say you have an app running at http://account.couchone.com/mydb/_design/doingnotes/index.html and want to add the additional URL http://account.couchone.com/index.html. First you have to set up a virtual host. (Want to know why? Go back &lt;a href="http://blog.couchone.com/post/443028592/whats-new-in-apache-couchdb-0-11-part-one-nice-urls"&gt;read the blogpost&lt;/a&gt;). If you are using a hosted CouchDB instance and don&amp;#8217;t have access to the local.ini file, you can also do this remotely:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;curl -X PUT http://username:password@account.couchone.com/_config/vhosts/account.couchone.com -d '&amp;quot;/mydb/_design/doingnotes/_rewrite&amp;quot;'&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Check at the bottom of http://account.couchone.com/_utils/config.html if there is a key value pair &amp;#8220;account.couchone.com&amp;#8221; =&gt; &amp;#8220;/mydb/_design/doingnotes/_rewrite&amp;#8221;.&lt;/p&gt;


&lt;p&gt;Now it won&amp;#8217;t work yet, because you still have to add the rewrite(s) to your design document. Do this by adding a file &amp;#8220;rewrites.json&amp;#8221; in your project folder. I has to contain an array of json objects with a &amp;#8220;from&amp;#8221; and a &amp;#8220;to&amp;#8221; attribute each. They work down in the path starting with the design document (the &amp;#8220;doingnotes&amp;#8221; in the URL). You can use wildcards. &amp;#8220;from&amp;#8221; specifies where it should map from, &amp;#8220;to&amp;#8221; which file it should map to. This is how rewrites.json looks for my application:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;[{
  &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
  &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;index.html&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
},
{
  &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;vendor/*&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
  &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;vendor/*&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
},
{
  &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;app/*&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
  &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;app/*&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
},
{
  &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;config/*&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
  &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;config/*&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
},
{
  &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;_view/*&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
  &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;_view/*&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
}]&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;If you just put the first from/to pair in, you can already call the application, but it won&amp;#8217;t work because the code in the design document needs access to all the folders that are stored in the application&amp;#8217;s root level (that is not in the &amp;#8220;_attachments&amp;#8221; folder). The wildcards map everything down from that path. If you have code in other locations as well, you have to add rewrites for those folders too. That&amp;#8217;s it.&lt;/p&gt;
</description>
      <pubDate>Mon, 11 Oct 2010 15:34:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8f61cf51-61c6-4e2e-a604-fe8277848cd1</guid>
      <comments>http://lenaherrmann.net/2010/10/11/pretty-urls-for-you-couchapp-the-howto#comments</comments>
      <category>couchdb</category>
      <category>couchapp</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=18</trackback:ping>
      <link>http://lenaherrmann.net/2010/10/11/pretty-urls-for-you-couchapp-the-howto</link>
    </item>
    <item>
      <title>Thesis is done - thanks for your support!</title>
      <description>&lt;p&gt;I&amp;#8217;m done!! Three weeks ago &lt;a href="http://blog.couch.io/post/903223195/diplom-thesis-realisation-of-a-distributed-application"&gt;I handed in my diplom thesis&lt;/a&gt;. The practical part was the design and implementation of a distributed, replicatable &lt;a href="http://en.wikipedia.org/wiki/Outliner"&gt;outliner&lt;/a&gt; with the NoSQL database &lt;a href="http://couchdb.apache.org/"&gt;CouchDB&lt;/a&gt;. As theoretical work I evaluated Distributed Systems, CouchDB, various web technologies and programming strategies. I still have to pass the oral exam, but my degree is finally very close - soon I can call myself a graduated computer scientist. Yay!&lt;/p&gt;


&lt;p&gt;As soon as academic procedures allow it, I&amp;#8217;ll publish the text and the source code of the thesis here. Until then I&amp;#8217;d like to say thank you to a few people. As the foreword of thesis won&amp;#8217;t get as much attention as this blog, I&amp;#8217;ll do it here.&lt;/p&gt;


&lt;p&gt;First first first of all, my appreciation goes to my boys at home and my parents for backing me up by spending loads of time with my child. Not only my professional life is based upon their support. THANKS.&lt;/p&gt;


&lt;p&gt;Further thanks go to:&lt;/p&gt;


&lt;p&gt;&lt;a href="http://upstre.am/"&gt;Upstream&lt;/a&gt; for generously sponsoring a part of the time I spent researching - especially &lt;a href="http://www.twitter.com/langalex"&gt;Alex Lang&lt;/a&gt; for his support and coming up with the initial idea. Without them I probably would have done some usability study for the drawer, or I would have had lots of fun in some enterprisey Java company. Also I felt I wanted to strive hard to write good code in my thesis to match Upstream&amp;#8217;s high standards for code quality and test coverage - not a bad thing.&lt;/p&gt;


&lt;p&gt;Allround guru &lt;a href="http://jan.prima.de/"&gt;Jan Lehnardt&lt;/a&gt; - he not only knew and happily provided the answers to all questions I had about CouchDB, databases in general, JavaScript, LaTeX, typography and virtually everything else; he was also constantly encouraging and guiding me to contribute to open source projects, trusting in my skills and doing &lt;i&gt;awesome&lt;/i&gt; things. One day I&amp;#8217;ll also submit a talk to one of your conferences, word :-)&lt;/p&gt;


&lt;p style="font-size:0.9em;"&gt;&lt;img width="532"  src="/files/thesis_sofa.jpg" alt="Jan Lehnardt, myself and Alex Lang on the couch with thesis."/&gt;&lt;br/&gt;

Jan Lehnardt, myself and Alex Lang on the couch with thesis.&lt;/p&gt;


&lt;p&gt;My professor &lt;a href="http://prof.beuth-hochschule.de/edlich/"&gt;Stefan Edlich&lt;/a&gt; for doing a great job in mentoring me. He is also the university professor with the most phenomenal email response times I have ever heard of.&lt;/p&gt;


&lt;p&gt;&lt;a href="http://github.com/ur5"&gt;Urs&lt;/a&gt;, Andy and Ischa advised me on the structuring and helped to make the result look quite scientific. They patiently explained to me how to go about writing such a long academic text. Ischa also is one of the very few people who actually read the whole thing in one piece.&lt;/p&gt;


&lt;p&gt;Several lovely people who helped at certain points during the research or the programming, listened with interest and/or assisted figuring out specific problems: &lt;a href="http://freelancing-gods.com/"&gt;Pat Allan&lt;/a&gt; (getting started was the hardest part); Urs again (many small and big issues all along the way); &lt;a href="http://github.com/endor"&gt;Frank Pr&#246;&#223;dorf&lt;/a&gt; and &lt;a href="http://twitter.com/overbryd"&gt;Lukas Rieder&lt;/a&gt; (figuring out with me what a Couchapp actually is); &lt;a href="http://till.klampaeckel.de/"&gt;Till Klamp&#228;ckel&lt;/a&gt; (skilled advice about Lounge and EC2); &lt;a href="http://www.kristinaschneider.com/"&gt;Kristina Schneider&lt;/a&gt; (tricky CSS shit); &lt;a href="http://twitter.com/freaklikeme"&gt;Thilo Utke&lt;/a&gt; and &lt;a href="http://twitter.com/svenfuchs"&gt;Sven Fuchs&lt;/a&gt; (die HTMLUnit bug, die);  &lt;a href="http://twitter.com/i2w"&gt;Ian White&lt;/a&gt; (this one bug in the code you had no idea what it was for but figured it out anyway); and various people from the Ruby and Javascript communities who helped me to shape my ideas by asking basic or challenging questions.&lt;/p&gt;


&lt;p&gt;Some more people who wrote great open source programs, did great support and were always open to ideas: my thanks go to a good part of the CouchDB community here, and also to &lt;a href="http://quirkey.com/"&gt;Aaron Quint&lt;/a&gt; and &lt;a href="http://twitter.com/tjholowaychuk"&gt;TJ Holowaychuk&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Finally my army of proofreaders! I split the text up in very small parts so that also people with a limited time budget had no excuse - I can really recommend this approach ;-). The list includes almost everyone mentioned above, and also: &lt;a href="http://twitter.com/adrianlang"&gt;Adrian&lt;/a&gt;, Anne, &lt;a href="https://www.xing.com/profile/Birgit_Behringer"&gt;Birgit&lt;/a&gt;, &lt;a href="http://twitter.com/bumi"&gt;Bumi&lt;/a&gt;, Diana, &lt;a href="http://ekedase.blogspot.com/"&gt;Eike&lt;/a&gt;, &lt;a href="http://twitter.com/flofff"&gt;Flo&lt;/a&gt;, Grushi, &lt;a href="https://www.xing.com/profile/Helge_NeuschwanderLutz"&gt;Helge&lt;/a&gt;, Jan S., Micha, Neels, Ole and &lt;a href="http://www.huesler-informatik.ch/"&gt;Patrick&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;The very last one goes to my sweetheart for brightening my life in those last stressful months.&lt;/p&gt;


&lt;p&gt;Thank you everyone! I&amp;#8217;m glad to know you all!!&lt;/p&gt;
</description>
      <pubDate>Thu, 05 Aug 2010 10:41:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8b192edb-c0eb-431c-ba7e-112050446cc0</guid>
      <comments>http://lenaherrmann.net/2010/08/05/thesis-is-done-thanks-for-your-support#comments</comments>
      <category>thesis</category>
      <category>thoughts</category>
      <category>couchdb</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=17</trackback:ping>
      <link>http://lenaherrmann.net/2010/08/05/thesis-is-done-thanks-for-your-support</link>
    </item>
    <item>
      <title>JavaScript syntax highlighting in the LaTeX listings package</title>
      <description>&lt;p&gt;I&amp;#8217;m currently writing my thesis, using &lt;a href="http://www.latex-project.org/"&gt;LaTeX&lt;/a&gt;. As the programming part of my thesis was mostly done in Javascript, I have to include some bits of Javascript source code in the LaTeX document. For this I&amp;#8217;m using the &lt;a href="http://en.wikibooks.org/wiki/LaTeX/Packages/Listings"&gt;listings package&lt;/a&gt;. With this package you can &amp;#8220;include the source code of any programming language within your document&amp;#8221;. Unfortunately Javascript is not in the list of supported programming languages. (In return, the list contains languages like Oberon-2, Promela or RSL, which are probably being used way more often than Javascript &amp;#8230; er.)&lt;/p&gt;


&lt;p&gt;Luckily &lt;a href="http://twitter.com/awendt"&gt;@awendt&lt;/a&gt; pointed me to these links: &lt;a href="http://vis4.net/blog/2009/08/javascript-quelltexte-in-latex/"&gt;Javascript Quelltexte in LaTeX&lt;/a&gt; and &lt;a href="http://mail.asta.uni-potsdam.de/pipermail/uplug/2006-June/003595.html"&gt;a thread on a mailinglist&lt;/a&gt;. I changed those examples only a bit, I&amp;#8217;m posting my result here mainly in the hope it will produce a Google hit for &amp;#8220;javascript latex source code&amp;#8221; :-)&lt;/p&gt;


&lt;p&gt;Put this at the beginning of your document. It will define support for the Javascript language. I included the keywords that show up in my code most frequently.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="er"&gt;\&lt;/span&gt;usepackage{listings}
&lt;span class="er"&gt;\&lt;/span&gt;usepackage{color}
&lt;span class="er"&gt;\&lt;/span&gt;definecolor{lightgray}{rgb}{.&lt;span class="i"&gt;9&lt;/span&gt;,.&lt;span class="i"&gt;9&lt;/span&gt;,.&lt;span class="i"&gt;9&lt;/span&gt;}
&lt;span class="er"&gt;\&lt;/span&gt;definecolor{darkgray}{rgb}{.&lt;span class="i"&gt;4&lt;/span&gt;,.&lt;span class="i"&gt;4&lt;/span&gt;,.&lt;span class="i"&gt;4&lt;/span&gt;}
&lt;span class="er"&gt;\&lt;/span&gt;definecolor{purple}{rgb}{&lt;span class="fl"&gt;0.65&lt;/span&gt;, &lt;span class="fl"&gt;0.12&lt;/span&gt;, &lt;span class="fl"&gt;0.82&lt;/span&gt;}

&lt;span class="er"&gt;\&lt;/span&gt;lstdefinelanguage{&lt;span class="co"&gt;JavaScript&lt;/span&gt;}{
  keywords={typeof, new, &lt;span class="pc"&gt;true&lt;/span&gt;, &lt;span class="pc"&gt;false&lt;/span&gt;, catch, function, &lt;span class="r"&gt;return&lt;/span&gt;, null, catch, switch, var, &lt;span class="r"&gt;if&lt;/span&gt;, &lt;span class="r"&gt;in&lt;/span&gt;, &lt;span class="r"&gt;while&lt;/span&gt;, &lt;span class="r"&gt;do&lt;/span&gt;, &lt;span class="r"&gt;else&lt;/span&gt;, &lt;span class="r"&gt;case&lt;/span&gt;, &lt;span class="r"&gt;break&lt;/span&gt;},
  keywordstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{blue}&lt;span class="er"&gt;\&lt;/span&gt;bfseries,
  ndkeywords={&lt;span class="r"&gt;class&lt;/span&gt;, export, boolean, throw, implements, import, this},
  ndkeywordstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{darkgray}&lt;span class="er"&gt;\&lt;/span&gt;bfseries,
  identifierstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{black},
  sensitive=&lt;span class="pc"&gt;false&lt;/span&gt;,
  comment=[l]{&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;},
  morecomment=[s]{&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;*}{*&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;},
  commentstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{purple}&lt;span class="er"&gt;\&lt;/span&gt;ttfamily,
  stringstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{red}&lt;span class="er"&gt;\&lt;/span&gt;ttfamily,
  morestring=[b]&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;,
  morestring=[b]&amp;quot;
}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Here you define the style for the newly defined language. This also belongs in the preamble of your LaTeX file.
  
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="er"&gt;\&lt;/span&gt;lstset{
   language=&lt;span class="co"&gt;JavaScript&lt;/span&gt;,
   backgroundcolor=&lt;span class="er"&gt;\&lt;/span&gt;color{lightgray},
   extendedchars=&lt;span class="pc"&gt;true&lt;/span&gt;,
   basicstyle=&lt;span class="er"&gt;\&lt;/span&gt;footnotesize&lt;span class="er"&gt;\&lt;/span&gt;ttfamily,
   showstringspaces=&lt;span class="pc"&gt;false&lt;/span&gt;,
   showspaces=&lt;span class="pc"&gt;false&lt;/span&gt;,
   numbers=left,
   numberstyle=&lt;span class="er"&gt;\&lt;/span&gt;footnotesize,
   numbersep=&lt;span class="i"&gt;9&lt;/span&gt;pt,
   tabsize=&lt;span class="i"&gt;2&lt;/span&gt;,
   breaklines=&lt;span class="pc"&gt;true&lt;/span&gt;,
   showtabs=&lt;span class="pc"&gt;false&lt;/span&gt;,
   captionpos=b
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The colours are just an example, read how to define more colours &lt;a href="http://en.wikibooks.org/wiki/LaTeX/Colors#Defining_new_colors"&gt;in the wikibook&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Now you can insert code like this. The example is nonsense, I tried to include as many keywords as possible. The &lt;i&gt;medskip&lt;/i&gt; command before the listing inserts a linebreak, a not very nice way to have some distance between the text and the listing.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="er"&gt;\&lt;/span&gt;medskip
&lt;span class="er"&gt;\&lt;/span&gt;begin{lstlisting}[caption=My Javascript Example]
Name.prototype = {
  &lt;span class="ke"&gt;methodName&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;(params){
    &lt;span class="kw"&gt;var&lt;/span&gt; doubleQuoteString = &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;some text&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;
    &lt;span class="kw"&gt;var&lt;/span&gt; singleQuoteString = &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;some more text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;;
    &lt;span class="c"&gt;// this is a comment&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(&lt;span class="lv"&gt;this&lt;/span&gt;.confirmed != &lt;span class="kw"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="kw"&gt;typeof&lt;/span&gt;(&lt;span class="lv"&gt;this&lt;/span&gt;.confirmed) == Boolean &amp;amp;&amp;amp; &lt;span class="lv"&gt;this&lt;/span&gt;.confirmed == &lt;span class="kw"&gt;true&lt;/span&gt;){
      document.createElement(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;h3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;);
      &lt;span class="pd"&gt;$&lt;/span&gt;(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;#system&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).append(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;This looks great&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
      &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="kw"&gt;false&lt;/span&gt;;
    } &lt;span class="kw"&gt;else&lt;/span&gt; {
      &lt;span class="kw"&gt;throw&lt;/span&gt; &lt;span class="kw"&gt;new&lt;/span&gt; Error;
    }
  }
}
&lt;span class="er"&gt;\&lt;/span&gt;end{lstlisting}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The result looks like this:&lt;/p&gt;


&lt;p&gt;&lt;img width="532"  src="/files/latex.png" alt="Javascript source code in LaTeX"/&gt;&lt;/p&gt;


&lt;p&gt;Update: &lt;a href="http://twitter.com/perkee"&gt;@perkee&lt;/a&gt; pointed out you can also use the &lt;a href="http://code.google.com/p/minted/"&gt;minted package&lt;/a&gt; for syntax highlighting.&lt;/p&gt; 
</description>
      <pubDate>Thu, 20 May 2010 11:20:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:53c2e2e5-5f54-4169-9b9a-d48e347b4cf3</guid>
      <comments>http://lenaherrmann.net/2010/05/20/javascript-syntax-highlighting-in-the-latex-listings-package#comments</comments>
      <category>javascript</category>
      <category>latex</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=16</trackback:ping>
      <link>http://lenaherrmann.net/2010/05/20/javascript-syntax-highlighting-in-the-latex-listings-package</link>
    </item>
    <item>
      <title>Security in CouchDB: changing the authentication db</title>
      <description>&lt;p&gt;I recently &lt;a href="http://lenaherrmann.net/2010/04/28/writing-a-testsuite-for-the-couchdb-api"&gt;wrote a testsuite for CouchDB&amp;#8217;s Javascript HTTP API&lt;/a&gt;. Among the things I tested were the security methods. That&amp;#8217;s those that deal with authentication - like signup, login, logout and so on. The challenge was to make sure my tests wouldn&amp;#8217;t interfere with the CouchDB setup of the person running the tests.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Background&lt;/h3&gt;


&lt;p&gt;CouchDB stores the information about the database users in a special database, the authentication_db. The default authentication_db is called &amp;#8220;_users&amp;#8221;. So this is where your user data is saved when you sign up without taking care about the details, e.g. when you use &lt;a href="http://github.com/lenalena/couchdb/blob/testsuite/share/www/script/jquery.couch.js#L112-119"&gt;the signup method provided by the JQuery HTTP API&lt;/a&gt;. Read more about the ways to interact with the authentication_db in &lt;a href="http://github.com/lenalena/couchdb-http-api-docs/blob/master/couch_js_security_methods.markdown"&gt;the HTTP API documentation&lt;/a&gt; (&lt;a href="http://github.com/lenalena/couchdb-http-api-docs/blob/master/jquery_couch_js_security_methods.markdown"&gt;JQuery methods&lt;/a&gt;), and read more about general CouchDB security &lt;a href="http://wiki.apache.org/couchdb/Security_Features_Overview"&gt;in the CouchDB Wiki&lt;/a&gt; or in &lt;a href="http://books.couchdb.org/relax/reference/security"&gt;The Definitive Guide&lt;/a&gt;.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;How to set the authentication_db&lt;/h3&gt;


&lt;p&gt;When you e.g. create database users from within your tests, how can you make sure they don&amp;#8217;t mess up the actual authentication_db, the one the unsuspecting user might use in production? Of course you have to create a custom authentication_db for storing the user information, and make CouchDB use that one. And after your tests are finished, you want CouchDB to use the original authentication_db again, so that only your testsuite uses the custom users database.&lt;/p&gt;  


&lt;p&gt;So there is one function you call right at the beginning of the part of the testsuite that&amp;#8217;s concerned with security features:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;function&lt;/span&gt; useTestUserDb(){
  test_users_db = &lt;span class="kw"&gt;new&lt;/span&gt; CouchDB(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;test_users_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
  &lt;span class="kw"&gt;var&lt;/span&gt; xhr = CouchDB.request(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;PUT&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;/_config/couch_httpd_auth/authentication_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, {
    &lt;span class="ke"&gt;body&lt;/span&gt;: JSON.stringify(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;test_users_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
  });
  &lt;span class="kw"&gt;if&lt;/span&gt;(&lt;span class="kw"&gt;typeof&lt;/span&gt;(old_auth_db) == &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;){
    old_auth_db = xhr.responseText.replace(&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="ch"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;,&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).replace(&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;&amp;quot;&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="mod"&gt;g&lt;/span&gt;&lt;/span&gt;,&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;);
  }
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;We create a custom authentication_db (&amp;#8220;test_users_db&amp;#8221;) and write its name into the CouchDB configuration. If we don&amp;#8217;t have the name of the old authentication_db stored yet (the one that&amp;#8217;s called &amp;#8220;_users&amp;#8221; per default, but might be called differently), we strip this name from special characters and store it in a variable, so we can restore it later. This is the function we call at the very end of the testsuite:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;function&lt;/span&gt; useOldUserDb(){
  CouchDB.request(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;PUT&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;/_config/couch_httpd_auth/authentication_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, {
    &lt;span class="ke"&gt;body&lt;/span&gt;: JSON.stringify(old_auth_db)
  });
  test_users_db.deleteDb();
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;In the last line, we delete our custom authentication_db. Everything is back to normal, there is no trace of our tests left. You can have a look at &lt;a href="http://github.com/lenalena/couchdb/blob/testsuite/share/www/spec/couch_js_class_methods_spec.js"&gt;how it looks like in production.&lt;/a&gt;&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;How to work with the authentication_db&lt;/h3&gt;


&lt;p&gt;You can create a new user like this:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; userDoc = CouchDB.prepareUserDoc({&lt;span class="ke"&gt;name&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Gaius Baltar&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;roles&lt;/span&gt;: [&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;president&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;]}, &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;secretpassword&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
test_users_db.save(userDoc);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Then you are logged in and can do whatever you want. About working with users and roles, read the &lt;a href="http://books.couchdb.org/relax/reference/security"&gt;chapter in The Definitive Guide&lt;/a&gt;. When you&amp;#8217;re done, delete the userDoc from the authentication_db, as the authentication_db has to be empty before it can be deleted:
  
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;test_users_db.deleteDoc({&lt;span class="ke"&gt;_id&lt;/span&gt; : userDoc.id, &lt;span class="ke"&gt;_rev&lt;/span&gt; : userDoc.rev})&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;
</description>
      <pubDate>Thu, 29 Apr 2010 10:40:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f3260d78-d5a7-4750-9429-b21c2cef7f0a</guid>
      <comments>http://lenaherrmann.net/2010/04/29/security-in-couchdb-changing-the-authentication-db#comments</comments>
      <category>couchdb</category>
      <category>javascript</category>
      <category>authentication</category>
      <category>stubbing</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=14</trackback:ping>
      <link>http://lenaherrmann.net/2010/04/29/security-in-couchdb-changing-the-authentication-db</link>
    </item>
    <item>
      <title>Writing a testsuite for the CouchDB API</title>
      <description>&lt;p&gt;&lt;a href="http://couchdb.apache.org"&gt;CouchDB&lt;/a&gt; is a database that I quite like. In fact, I haven&amp;#8217;t done much else this winter, as the programming part of my thesis involved diving into many of CouchDB&amp;#8217;s features. I had just finished my little program and was desperately looking for an excuse not to start writing the thesis. Mercifully &lt;a href="http://twitter.com/janl"&gt;Jan&lt;/a&gt; from &lt;a href="http://couch.io"&gt;Couchio&lt;/a&gt; came along and asked if I wouldn&amp;#8217;t like to write a testsuite for CouchDB&amp;#8217;s Javascript HTTP API, and the documentation with it. I wanted!&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;What is this HTTP API?&lt;/h3&gt; 


&lt;p&gt;If you have worked with CouchDB from a Javascript program before (&lt;a href="http://www.quirkey.com/blog/2009/09/15/sammy-js-couchdb-and-the-new-web-architecture/"&gt;eg Sammy.js is a good companion for a simple web application&lt;/a&gt;), you have probably used the methods &lt;a href="http://github.com/apache/couchdb/blob/trunk/share/www/script/couch.js"&gt;a file named couch.js&lt;/a&gt; provides. With this interface you can easily access the most common CouchDB functionality without having to handle the XMLHttpRequest yourself. It allows you to create a &amp;#8220;database object&amp;#8221; and then perform things on this object, for example save or delete documents:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; db = &lt;span class="kw"&gt;new&lt;/span&gt; CouchDB(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;test_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
db.createDb();

&lt;span class="kw"&gt;var&lt;/span&gt; doc = {&lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Starbuck&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Kara Thrace&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;};
&lt;span class="kw"&gt;var&lt;/span&gt; saved_doc = db.save(doc);

&lt;span class="c"&gt;// do something&lt;/span&gt;
db.deleteDoc({&lt;span class="ke"&gt;_id&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;_rev&lt;/span&gt; : saved_doc.rev});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;If you&amp;#8217;re using JQuery, you can use the JQuery interface in &lt;a href="http://github.com/apache/couchdb/blob/trunk/share/www/script/jquery.couch.js"&gt;jquery.couch.js&lt;/a&gt;. It covers basically the same things as couch.js, but it uses, you guessed it, JQuery. That means you can send your requests asynchronously and handle the response in the callback. Same example:&lt;/p&gt;


&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; db = &lt;span class="pd"&gt;$&lt;/span&gt;.couch.db(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;spec_db&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;);
db.create();

&lt;span class="kw"&gt;var&lt;/span&gt; doc = {&lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Starbuck&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Kara Thrace&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;};
db.saveDoc(doc, {
  &lt;span class="ke"&gt;success&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;(response){
    &lt;span class="c"&gt;// do something&lt;/span&gt;
    db.removeDoc({&lt;span class="ke"&gt;_id&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;_rev&lt;/span&gt; : response.rev});
  }
});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The code looks a bit more complicated, but with this API you have the full power of &lt;a href="http://lenaherrmann.net/2010/02/15/refactoring-asynchronous-code"&gt;asynchronicity&lt;/a&gt; in your head, where spelling the word asynchronicity is the worst part. &lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Writing the testsuite&lt;/h3&gt;


&lt;p&gt;I was free to choose which testing framework I wanted to use. The tests in CouchDB use a kind of home grown testing framework, which does its job, but is not really the state of the art of Unit Testing. The API tests aren&amp;#8217;t supposed to run within the normal CouchDB testsuite anyway. That is the one you always run in Futon when you installed CouchDB. So it was possible to use something different &amp;#8230; like &lt;a href="http://jspec.info/"&gt;JSpec&lt;/a&gt;. (I have also &lt;a href="http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be"&gt;blogged&lt;/a&gt; and &lt;a href="http://lenaherrmann.net/2010/03/22/jspec-slides-of-my-talk-at-berlin-js-usergroup"&gt;talked&lt;/a&gt; about it in the past.)&lt;/p&gt;


&lt;p&gt;How to run JSpec tests? If you are familiar with JSpec tests, you know you define your tests in Javascript files, reference those from a HTML file and open that in a browser. When you want your tests to perform calls to the database, this won&amp;#8217;t work - you get the error message &amp;#8220;Access to restricted URI denied&amp;#8221;. Shortly this is because AJAX only allows connections to the same resource it is called from. Therefore you have to run the tests from within the database. CouchDB has a folder &lt;a href="http://github.com/apache/couchdb/blob/trunk/share/www/"&gt;share/www&lt;/a&gt;, which gets linked to the &amp;#8220;/_utils/&amp;#8221; path of your local installation. In this folder you find Futon. That&amp;#8217;s where you have to put your tests and all the helper scripts. Then you can run the tests directly from the couch by browsing to &lt;a href="http://127.0.0.1:5984/_utils/spec/run.html"&gt;/_utils/spec/run.html&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Writing the testsuite was pretty straightforward. Along the way I found some bugs in the code (no surprise as it was written untested). One of the problems I had to solve I&amp;#8217;ve explained in another blogpost: &lt;a href="http://lenaherrmann.net/2010/04/29/security-in-couchdb-changing-the-authentication-db"&gt;How to overwrite the authentication_db so the tests don&amp;#8217;t interfere with the CouchDB setup.&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;There&amp;#8217;s one thing I&amp;#8217;m not completely happy with: The tests do really need kind of long to run, up to 3 minutes, depends a lot on the browser and CPU load. The other CouchDB Javascript tests are a lot faster. I think the new ones are a bit more comprehensive. And I preferred a very clean setup to how it&amp;#8217;s done in the old tests: I have a database created before every single assertion and have it dropped afterwards. So you know exactly in which state your database is during the test. I guess that&amp;#8217;s worth the longer runtime.&lt;/p&gt;


&lt;p&gt;So. If you want to see a lot of JSpec code in action, and want to learn more about CouchDB, &lt;a href="http://github.com/lenalena/couchdb/tree/testsuite/share/www/spec/"&gt;have a look at the code&lt;/a&gt;. At the moment it&amp;#8217;s in my fork only, the community (someone - you?) has yet to integrate it into Futon. For all the details, read &lt;a href="http://mail-archives.apache.org/mod_mbox/couchdb-dev/201004.mbox/%3C4BB74086.4010302@zeromail.org%3E"&gt;my rundown and left TODOs&lt;/a&gt;.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Documentation&lt;/h3&gt; 


&lt;p&gt;I also wrote API Docs for all the methods. It &lt;a href="http://github.com/lenalena/couchdb-http-api-docs"&gt;can be found here&lt;/a&gt;. Later you will find it nicely formatted in the &lt;a href="http://wiki.apache.org/couchdb/Reference"&gt;CouchDB Wiki&lt;/a&gt; and &lt;a href="http://www.couch.io/docs"&gt;on the Couchio website&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;If you find anything is missing or you don&amp;#8217;t understand it, feel free to drop me a line. I&amp;#8217;m happy to clarify it as long as it&amp;#8217;s all freshly in my brain.&lt;/p&gt;


&lt;p style="font-size:0.9em;"&gt;PS - I consider example data in tests important, so I took all the data from the SciFi series &lt;a href="http://en.battlestarwiki.org/wiki/Portal:Battlestar_Galactica_%28RDM%29"&gt;Battlestar Galactica&lt;/a&gt;, currently on my playlist and I luv it. And it gave me an excuse to read in the Battlestar Wiki every now and then when I was looking for correct spelling :)&lt;/p&gt;
</description>
      <pubDate>Wed, 28 Apr 2010 12:07:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e603b6cb-97da-442f-91a0-7f91f70fb52d</guid>
      <comments>http://lenaherrmann.net/2010/04/28/writing-a-testsuite-for-the-couchdb-api#comments</comments>
      <category>jquery</category>
      <category>jspec</category>
      <category>couchdb</category>
      <category>testing</category>
      <category>documentation</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=13</trackback:ping>
      <link>http://lenaherrmann.net/2010/04/28/writing-a-testsuite-for-the-couchdb-api</link>
    </item>
    <item>
      <title>JSpec: Slides of my talk at Berlin JS Usergroup</title>
      <description>&lt;p&gt;Last thursday I gave a talk at the monthly meeting of the &lt;a href="http://twitter.com/berlinjs"&gt;Berlin JS Usergroup&lt;/a&gt;. I was talking about &lt;a href="http://github.com/visionmedia/jspec"&gt;JSpec&lt;/a&gt;, a Javascript Unit Testing framework. Thanks to all who were there to listen and ask questions!&lt;/p&gt;


&lt;p&gt;Here are &lt;a href="http://www.slideshare.net/kilaulena/jspec-a-javascript-unit-testing-framework"&gt;the slides&lt;/a&gt;. When you have a look at them, make sure to also read the notes (next to the comments), so the slides make sense to you.
&lt;/p&gt;


&lt;p&gt;To find more information about JSpec, check out &lt;a href="http://github.com/visionmedia/jspec"&gt;the README on github&lt;/a&gt;, or read the &lt;a href="http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be"&gt;blog post I wrote about it recently&lt;/a&gt;.&lt;p&gt;

&lt;p&gt;Thanks also to &lt;a href="http://twitter.com/gisikw"&gt;@gisikw&lt;/a&gt; and &lt;a href="http://twitter.com/tjholowaychuk"&gt;@tjholowaychuk&lt;/a&gt; for &lt;a href="http://slides.kevingisi.com/TestYourJavaScript/"&gt;this talk&lt;/a&gt; from which I borrowed a few grammar examples :)&lt;/p&gt;
</description>
      <pubDate>Mon, 22 Mar 2010 12:52:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8e64ebfd-22ee-4b7b-885d-96a488801c87</guid>
      <comments>http://lenaherrmann.net/2010/03/22/jspec-slides-of-my-talk-at-berlin-js-usergroup#comments</comments>
      <category>jspec</category>
      <category>talk</category>
      <category>slides</category>
      <category>testing</category>
      <category>javascript</category>
      <category>berlinjs</category>
      <category>usergroup</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=12</trackback:ping>
      <link>http://lenaherrmann.net/2010/03/22/jspec-slides-of-my-talk-at-berlin-js-usergroup</link>
    </item>
    <item>
      <title>Refactoring asynchronous code</title>
      <description>&lt;p&gt;How to refactor a long chunk of asynchronous code is one thing I learned during my Javascript &amp; CouchDB project. It&amp;#8217;s not a difficult thing, but I thought it was before I figured out how to do it, so I guess it might be interesting for Javascript newbies.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Asynchronous code&lt;/h3&gt;


&lt;p&gt;In the first couple of weeks I struggled with this new style of coding before I got the hang of it. If you&amp;#8217;re fairly new to Javascript, you&amp;#8217;re probably used to just assign a variable in one line and use it in the next line. In the asynchronous world you query for a value and do stuff with it &lt;i&gt;in the callback function&lt;/i&gt; of the query.&lt;/p&gt;


&lt;p&gt;To illustrate - &amp;#8220;normal&amp;#8221; code looks like this:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; result = db.query(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;select * from T&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
&lt;span class="c"&gt;// use result&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Although writing web applications just screams for doing it like this instead:
  
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;
db.query(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;select..&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="kw"&gt;function&lt;/span&gt; (result) {
&lt;span class="c"&gt;// use result&lt;/span&gt;
});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p style="font-size:0.8em"&gt;(Examples stolen from &lt;a href="http://nodejs.org/"&gt;Ryan Dahl&amp;#8217;s Presentation about Node.js&lt;/a&gt;.)&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;The Problem&lt;/h3&gt;


&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="http://lenaherrmann.net/files/async-bad.png" alt="Bad asynchronous code" /&gt;&lt;/p&gt;


&lt;p&gt;I have a method that checks if I have to display a notification message. For this it makes a lot of requests to the database, the received data has to be compared with other data, there&amp;#8217;s lots of if/else clauses to consider all kinds of conditions and edge cases. It started simple, but then more and more conditions came in, unless I had the &lt;a href="http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef0128766398f7970c-pi"&gt;much feared diagonal closing bracket line&lt;/a&gt; on my screen. &lt;/p&gt;


&lt;p&gt;You don&amp;#8217;t need to know the details, just look at the picture and you&amp;#8217;ll get the impression.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;clear:left"&gt;Refactoring&lt;/h3&gt;


&lt;p&gt;By way of illustration I made up a much shorter example:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;
checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar == whatever){
      &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
      openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
        &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
          &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
        }
      });
    }
  });
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;It is time to refactor after you are sure this actually is the way to go and the code basically works (&amp;#8220;make it work, then make it pretty&amp;#8221;). First you group the code into methods: You split the lines by what they do, and then you name each section. I think around 8 LOC is a good length for a method. &lt;/p&gt;


&lt;p&gt;If this was synchronous code, you could just call these methods after each other. But this doesn&amp;#8217;t work here, because &lt;i&gt;result&lt;/i&gt; and &lt;i&gt;result2&lt;/i&gt; are not available outside the function that retrieves them. &lt;/p&gt;


&lt;p&gt;My first approach was to call the functions from within each other. This works - but to see what &lt;i&gt;checkForConflicts&lt;/i&gt; does at its very heart, you have to scroll down through all the methods - not much gained:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;);
}

doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;){
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar = whatever){
      doSomethingElse(result);
    }
  });
}

doSomethingElse(result){
  &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
  openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
    &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
      showMessage(result2);
    }
  });
}

showMessage = &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
  &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The way to do it is to use &lt;i&gt;callbacks&lt;/i&gt;. Each method gets an anonymous function as (additional) argument: in the method declaration this function has the name &lt;i&gt;callback&lt;/i&gt;. This callback is called if all the conditions within the method are met. The callback gets its arguments from within the method that calls it. You have to specify these arguments again in &lt;i&gt;checkForConflicts&lt;/i&gt; to have a valid function definition.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    doSomethingElse(result, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
      showMessage(result2); 
    });
  });
}

doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;, callback){
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar = whatever){
      callback(result);
    }
  });
}

doSomethingElse(result, callback){
  &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
  openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
    &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
      callback(result2);
    }
  });
}

showMessage = &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
  &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;When you look at &lt;i&gt;checkForConflicts&lt;/i&gt;, you now immediately see what it does, without being bothered by the details. As a side effect, deciding which code needs access to which arguments helps you to understand your code better. In my case I was able to optimize the use of passed variables a lot.&lt;/p&gt;


&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="http://lenaherrmann.net/files/async-good.png" alt="Refactored asynchronous code" /&gt;&lt;/p&gt;


&lt;p&gt;You might argue that the code is much longer now. First, longer is not worse if it is more readable. Second, this is only an example - if there was code instead of &lt;i&gt;//do stuff&lt;/i&gt; the few extra lines wouldn&amp;#8217;t carry weight.&lt;/p&gt;


&lt;p&gt;Finally here is a screenshot of my refactored code. All the methods don&amp;#8217;t fit on one screen, but you can see what &lt;i&gt;checkForConflicts&lt;/i&gt; does very quickly.&lt;/p&gt;


&lt;p style="clear:left"/&gt;
</description>
      <pubDate>Mon, 15 Feb 2010 16:17:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8e9117d9-b37a-467b-99ac-b6f035ed1a9e</guid>
      <comments>http://lenaherrmann.net/2010/02/15/refactoring-asynchronous-code#comments</comments>
      <category>javascript</category>
      <category>refactoring</category>
      <category>ajax</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=11</trackback:ping>
      <link>http://lenaherrmann.net/2010/02/15/refactoring-asynchronous-code</link>
    </item>
    <item>
      <title>Testing PUT requests with Culerity</title>
      <description>&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="/files/Image/Bildschirmfoto%202010-02-02%20um%2012.38.41.png" alt="All green!" /&gt;&lt;/p&gt;


&lt;p&gt;One major pain at testing with culerity so far was a HtmlUnit bug that causes a ScriptException with PUT AJAX requests in jQuery. When you tried to test an update you got a &amp;#8220;java.lang.IllegalArgumentException: The content cannot be null&amp;#8221;. Fortunately, this bug is fixed now. With latest HtmlUnit build culerity should run fine, and I tell you how.&lt;/p&gt;


&lt;p&gt;I assume you have culerity installed already. If not, follow &lt;a href="http://github.com/langalex/culerity"&gt;these instructions&lt;/a&gt; for testing Rails apps &lt;a href="http://upstre.am/2009/10/25/testing-couchapps-with-cucumber-and-culerity/"&gt;or these&lt;/a&gt; for testing Couchapps.&lt;/p&gt;


&lt;p&gt;1. Make sure you have up-to-date versions of celerity and culerity:&lt;br/&gt;
$ jruby -S gem install celerity #(gives you celerity-0.7.8)&lt;br/&gt;
$ gem install culerity &amp;#8211;source http://gemcutter.org #(gives you culerity-0.2.7)&lt;/p&gt;


&lt;p&gt;2. Get the latest HtmlUnit nightly build:&lt;br/&gt;
$ wget http://build.canoo.com/htmlunit/artifacts/htmlunit-2.7-SNAPSHOT-with-dependencies.zip&lt;br/&gt;
$ unzip htmlunit-2.7-SNAPSHOT-with-dependencies.zip &lt;br/&gt;
$ cd htmlunit-2.7-SNAPSHOT-with-dependencies/lib&lt;br/&gt;
These are the jar files you need to copy into your celerity gem.&lt;/p&gt;


&lt;p&gt;3. Find your celerity gem and copy the files:&lt;br/&gt;
$ jruby -S gem which celerity&lt;br/&gt;
$ cp * PATH_OF_JRUBY_INSTALLATION/lib/ruby/gems/1.8/gems/celerity-0.7.7/lib/celerity/htmlunit&lt;/p&gt;


&lt;p&gt;4. The new HtmlUnit needs JQuery 1.4, so you &lt;a href="http://docs.jquery.com/Downloading_jQuery#Current_Release"&gt;need to upgrade&lt;/a&gt; if you haven&amp;#8217;t done already. There&amp;#8217;s a &lt;a href="http://github.com/jquery/jquery-compat-1.3"&gt;backwards compatibility plugin for 1.3&lt;/a&gt; if this gives you trouble.
&lt;/p&gt;


&lt;p&gt;Have fun!&lt;/p&gt;
</description>
      <pubDate>Sun, 31 Jan 2010 21:22:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:40dcb9ab-9661-4888-bdb1-d9ac36fb7fc2</guid>
      <comments>http://lenaherrmann.net/2010/01/31/testing-put-requests-with-culerity#comments</comments>
      <category>testing</category>
      <category>culerity</category>
      <category>ajax</category>
      <category>htmlunit</category>
      <category>bug</category>
      <category>celerity</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=10</trackback:ping>
      <link>http://lenaherrmann.net/2010/01/31/testing-put-requests-with-culerity</link>
    </item>
    <item>
      <title>JSpec - JavaScript Unit testing how it should be</title>
      <description>&lt;p&gt;Selecting a unit test framework for a JavaScript-only application - not a task with an obvious answer! Coming from the Rails world, where most people work with either TestUnit or RSpec, I was a bit lost when I started researching. &lt;a href="http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript"&gt;This table on wikipedia&lt;/a&gt; was quite helpful for a first overview. But: JsUnit, JSTest, jsUnitTest, jsUnity - these are actually different frameworks, and there&amp;#8217;s even many more &amp;#8230;&lt;/p&gt;


&lt;p&gt;I narrowed the selection down to those being currently developed, or are in use in &amp;#8220;big&amp;#8221; projects. &lt;a href="http://www.jsunit.net/"&gt;JsUnit&lt;/a&gt; seems to be one of the major players, but I found almost no documentation, and the code base hasn&amp;#8217;t changed a lot in the last years. That&amp;#8217;s not a negative thing in itself, but having an active community around is a good thing. &lt;a href="http://docs.jquery.com/QUnit"&gt;QUnit&lt;/a&gt; is used for testing JQuery, it looked like you could work with it as well, but its syntax and setup didn&amp;#8217;t make me go &amp;#8220;wow&amp;#8221; exactly. Same goes for &lt;a href="http://code.google.com/p/jqunit/"&gt;JQUnit&lt;/a&gt;, about which I had read good stuff in several blog posts. It&amp;#8217;s also what Sammy.js is tested with.&lt;/p&gt;


&lt;p&gt;I like RSpec, so when I had my first look at &lt;a href="http://github.com/nkallen/screw-unit"&gt;ScrewUnit&lt;/a&gt;, I was delighted to find there&amp;#8217;s BDD syntax for JavaScript too. ScrewUnit is also a part of &lt;a href="http://github.com/relevance/blue-ridge"&gt;Blue Ridge&lt;/a&gt;, which is sadly still only available as a Rails plugin. So, ScrewUnit looked nice.&lt;/p&gt;


&lt;p&gt;And THEN &amp;#8230; I found &lt;a href="http://visionmedia.github.com/jspec/"&gt;JSpec&lt;/a&gt;. I was thrilled! It&amp;#8217;s a JavaScript framework, but it doesn&amp;#8217;t look like JavaScript at all: no curly brackets, no semicolons, just plain domain language. It just looks like RSpec. Here is a random snippet from my test suite, just have a look:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;NoteCollection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
  before_each
    note3 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;8c8&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;three&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;});
    note1 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;ae9&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;next_id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;107&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;});
    note2 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;107&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;two&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;next_id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;8c8&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;});
      
    notes = &lt;span class="kw"&gt;new&lt;/span&gt; NoteCollection([note3, note1, note2]);
  end
  
  describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;firstNote&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should return a note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      notes.firstNote().should.be_an_instance_of Note
    end
    
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should return the first note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      notes.firstNote().id.should.eql note1.id
    end

    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should throw an error if there is more than one note that could be the first note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      note4 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;fff&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;rev&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;2-420&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;});
      notes.notes.push(note4);
      -{notes.firstNote()}.should.throw_error &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;More than one first note found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    end  
  end
end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;And that are just 3 out of over 45 core matchers. There are also matchers for JQuery, see the next example, where I put the DOM I wanted to test in a fixture called outline.html. In Rails I don&amp;#8217;t use fixtures, but if you want to test an application where all the markup is generated out of many different sources, it makes sense to test little pieces of the DOM in isolation. In ScrewUnit you have only one file for your all your mocked DOM.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;NoteElement&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
  before_each
    outline = elements(fixture(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;outline&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;))
    notes = outline.find(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;li&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
  
    parent_note = &lt;span class="kw"&gt;new&lt;/span&gt; NoteElement(&lt;span class="pd"&gt;$&lt;/span&gt;(notes.get(&lt;span class="i"&gt;0&lt;/span&gt;)))
    child_note = &lt;span class="kw"&gt;new&lt;/span&gt; NoteElement(&lt;span class="pd"&gt;$&lt;/span&gt;(notes.get(&lt;span class="i"&gt;1&lt;/span&gt;)))
  end
  
  describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;focusNextTextarea&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should focus the first child note if there is one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      parent_note.focusNextTextarea();
      parent_note.noteLi().should.not.have_attr(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;data-focus&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
      child_note.noteLi().attr(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;data-focus&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;).should.eql &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    end
  end

  describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;insertUpdateNotePointers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;    
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should call setNextPointerToNewlyInsertedNote&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      inserted_note = {&lt;span class="ke"&gt;id&lt;/span&gt;:&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;inserted!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;};
      parent_note.should.receive(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;setNextPointerToNewlyInsertedNote&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).with_args(inserted_note)
      parent_note.insertUpdateNotePointers(inserted_note);
    end
  end
end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Note, if you don&amp;#8217;t like the RSpec-like Syntax, you can also skip the JSpec DSL and just use plain Javascript syntax.&lt;/p&gt;


&lt;p&gt;More JSpec goodness: You can either run the tests in the browser, or in the terminal, so you can include it in your CI. In the terminal you can specify the browsers it should be tested with: The browsers will be opened in the background, but you won&amp;#8217;t see your stuff being run like in Selenium. You can mock timers to test asynchronous calls. You can stub methods, mock AJAX requests, specify shared behaviours, hook into JSpec to specify your own matchers or do whatever else, and &amp;#8230; just &lt;a href="http://github.com/visionmedia/jspec/"&gt;see for yourself&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Installation is easy too: just add JSpec.js and JSpec.css to your application. If you need them, add the files for jquery, timers or xhr support too. In spec/index.html you include your application files and specify which tests you want to run. Then open it in the browser, wait a few milliseconds, and see the result. If you want to run JSpec tests in the terminal, just install the JSpec Ruby gem. For this you also have to install &lt;a href="http://www.mozilla.org/rhino/"&gt;Rhino&lt;/a&gt; (a Java-based JavaScript interpreter) - when you are on OS X, &lt;a href="http://peter.michaux.ca/articles/installing-rhino-on-os-x"&gt;this should get you started&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;What I don&amp;#8217;t like about JSpec &amp;#8230; the BlueRidge browser output is much more clear and pretty. I thought really hard about more things but that&amp;#8217;s it.&lt;/p&gt;


&lt;p&gt;Make sure to read the documentation on &lt;a href="http://jspec.info/"&gt;the JSpec website&lt;/a&gt; as well as the &lt;a href="http://github.com/visionmedia/jspec/"&gt;github Readme&lt;/a&gt;. Both ressources look similar, but at the time of writing there were different things on the two pages.&lt;/p&gt;
</description>
      <pubDate>Mon, 04 Jan 2010 12:07:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c6222c7f-ab27-4e0b-a744-09dde4f35970</guid>
      <comments>http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be#comments</comments>
      <category>javascript</category>
      <category>jspec</category>
      <category>jquery</category>
      <category>testing</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=9</trackback:ping>
      <link>http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be</link>
    </item>
  </channel>
</rss>
