openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] OpenJPA > Testing
Date Mon, 09 Aug 2010 19:14:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/5/_/styles/combined.css?spaceKey=openjpa&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/openjpa/Testing">Testing</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~drwoods">Donald
Woods</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        added Eclipse steps for junits<br />
    </div>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br># Run --&gt; Debug
Configurations...  Create a new JUnit configuration <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">!eclipse_dbgcfg_1.png|border=1!
<br># For the Test tab - Give the configuration a Name, select a Project and Test class,
and you&#39;ll probably want to select the &quot;Keep JUnit running...&quot; option.
<br>!eclipse_dbgcfg_2.png|border=1! <br># For the Arguments tab - Press the Variables...
button. This will allow us to define variables that can be shared across other Release/Debug
Configurations within this workspace. <br>!eclipse_dbgcfg_3.png|border=1! <br>#
For the Select Variable window - Press the Edit Variables... button. <br>!eclipse_dbgcfg_4.png|border=1!
<br># For the Preferences windows - Press New... to add two variables. <br>!eclipse_dbgcfg_5.png|border=1!
<br>{code} <br>Name = openjpa.trace <br>Value = -Dopenjpa.Log=DefaultLevel=TRACE
<br>Description = Set OpenJPA logging to TRACE <br>{code} <br>!eclipse_dbgcfg_6.png|border=1!
<br>{code} <br>Name = connect.derby <br>Value = -Dopenjpa.ConnectionDriverName=org.apache.derby.jdbc.EmbeddedDriver
-Dopenjpa.ConnectionURL=jdbc:derby:target/database/openjpa-derby-database;create=true <br>Description
= Connection properties for Derby <br>{code} <br>!eclipse_dbgcfg_7.png|border=1!
<br># After the above are created and saved, add the new variables as VM Arguments.
<br>{code} <br>${openjpa.trace} <br>${connect.derby} <br>{code} <br>!eclipse_dbgcfg_8.png|border=1!
<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        

<h1><a name="Testing-RunningandWritingWritingTestCasesForOpenJPATestswithOpenJPA"></a>Running
and <a href="/confluence/display/openjpa/Writing+Test+Cases+For+OpenJPA" title="Writing
Test Cases For OpenJPA">Writing</a> Tests with OpenJPA</h1>

<p>OpenJPA's unit tests are written using JUnit. For a template for a simple test case,
see the code for <a href="http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestPersistence.java?view=markup"
class="external-link" rel="nofollow">TestPersistence.java</a>.</p>

<p>If you want to contribute your own test case then follow the <a href="/confluence/display/openjpa/Writing+Test+Cases+For+OpenJPA"
title="Writing Test Cases For OpenJPA">guidelines</a>.</p>

<p>Once you have downloaded and built OpenJPA (see <a href="/confluence/display/openjpa/Building"
title="Building">Building</a>), you can run individual tests using the "test" goal
to maven. For example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">mvn test -DfailIfNoTests=<span class="code-keyword">false</span>
-Dtest=TestPersistence</pre>
</div></div>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>By default Maven will fail if
there are no testcases found in any module you build. The examples solve this by specifying
-DfailIfNoTests=false but you can also change your personal default by adding the following
to ${user.home}/.m2/settings.xml : 
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
        <span class="code-tag">&lt;profile&gt;</span>
            <span class="code-tag">&lt;id&gt;</span>enable-no-tests<span
class="code-tag">&lt;/id&gt;</span>
            <span class="code-tag">&lt;activation&gt;</span>
                <span class="code-tag">&lt;activeByDefault&gt;</span>true<span
class="code-tag">&lt;/activeByDefault&gt;</span>
            <span class="code-tag">&lt;/activation&gt;</span>
            <span class="code-tag">&lt;properties&gt;</span>
                <span class="code-tag">&lt;failIfNoTests&gt;</span>false<span
class="code-tag">&lt;/failIfNoTests&gt;</span>
            <span class="code-tag">&lt;/properties&gt;</span>
        <span class="code-tag">&lt;/profile&gt;</span>
</pre>
</div></div></td></tr></table></div> 

<p>To get more debugging information (e.g., to see the SQL that is being executed against
the database), you can enable trace-level logging from the command line using the "openjpa.Log"
system property. For example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
$ mvn test -DfailIfNoTests=<span class="code-keyword">false</span> -Dtest=TestPersistence
-Dopenjpa.Log=DefaultLevel=TRACE

[INFO] Scanning <span class="code-keyword">for</span> projects...
[INFO] Reactor build order: 
...
690  test  TRACE  [main] openjpa.jdbc.SQL - &lt;t 4261185, conn 3061987&gt; executing
prepstmnt 12659709 
   INSERT INTO AllFieldTypes (id, arrayOfStrings, booleanField, byteField, charField, dateField,

   doubleField, floatField, intField, longField, shortField, stringField) 
   VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [params=(<span class="code-object">long</span>)
601, (<span class="code-keyword">null</span>) <span class="code-keyword">null</span>,
(<span class="code-object">int</span>) 0, (<span class="code-object">byte</span>)
0, 
   (<span class="code-object">int</span>) 0, (<span class="code-keyword">null</span>)
<span class="code-keyword">null</span>, (<span class="code-object">double</span>)
0.0, (<span class="code-object">float</span>) 0.0, (<span class="code-object">int</span>)
0, (<span class="code-object">long</span>) 0, (<span class="code-object">short</span>)
0, (<span class="code-keyword">null</span>) <span class="code-keyword">null</span>]
701  test  TRACE  [main] openjpa.jdbc.SQL - &lt;t 4261185, conn 3061987&gt; [11 ms]
spent
701  test  TRACE  [main] openjpa.jdbc.JDBC - &lt;t 4261185, conn 3061987&gt; [0 ms]
commit
702  test  TRACE  [main] openjpa.jdbc.JDBC - &lt;t 4261185, conn 0&gt; [0 ms] close
...
$
</pre>
</div></div>

<h2><a name="Testing-SettingadditionalLogparameters"></a>Setting additional
Log parameters</h2>

<p>There are several parameters that are used by OpenJPA Log. Here are some that might
be of value when debugging test cases. Note that the logger names are case sensitive while
the log levels are not.</p>

<ul>
	<li>DefaultLevel sets the level for all loggers that are not otherwise set</li>
	<li>file sets the file name for logging output</li>
	<li>Enhance logs messages from the enhancer</li>
	<li>Metadata logs messages from the metadata processor</li>
	<li>Tool logs messages from the schema synchronization tool</li>
	<li>SQL logs messages from the SQL generator</li>
	<li>JDBC logs messages related to JDBC</li>
	<li>Schema logs messages related to schema</li>
	<li>Runtime logs messages related to runtime</li>
	<li>Query logs messages related to queried</li>
	<li>DataCache logs messages related to caching</li>
</ul>


<p>Log levels specify the minimum log message that is output. All log messages with
a level higher than the log level set for a logger are output. So if INFO is specified, log
messages of level INFO, WARN, ERROR, and SEVERE are output.</p>

<ul>
	<li>TRACE</li>
	<li>INFO</li>
	<li>WARN</li>
	<li>ERROR</li>
	<li>FATAL</li>
</ul>


<p>For example, to avoid enhancement warnings, get detailed SQL information, and write
the log data to a file:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">mvn test -Dtest=TestPersistence -Dopenjpa.Log=Enhance=ERROR,SQL=TRACE,file=openjpa.log</pre>
</div></div>

<h1><a name="Testing-Testingagainstalternatedatabases"></a>Testing against
alternate databases</h1>

<p>By default, OpenJPA uses the <a href="http://db.apache.org/derby/" class="external-link"
rel="nofollow">Derby</a> database for testing. The <tt>openjpa-persistence-jdbc/pom.xml</tt>
POM declares various pre-defined databases against which tests can be executed. For example,
to test against the stand-alone <a href="http://hsqldb.org/" class="external-link" rel="nofollow">HSQLDB</a>
database, you can run with the "test-hsqldb" profile:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">mvn test -DfailIfNoTests=<span class="code-keyword">false</span>
-Dtest=TestPersistence -Ptest-hsqldb</pre>
</div></div>

<p>For databases that are not in the pre-defined list, you can manually specify connection
parameters to use for testing under the "test-custom" profile. You will need to manually provide
the driver class and specify all of the connection parameters. For example, to test against
Oracle, you might run:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
mvn test -DfailIfNoTests=<span class="code-keyword">false</span> -Dtest=TestPersistence
-Ptest-custom \
  -Dopenjpa.custom.driverjar=$(pwd)/drivers/jdbc-oracle-10_2_0_1_0.jar \
  -Dopenjpa.custom.driverclass=oracle.jdbc.driver.OracleDriver \
  -Dopenjpa.custom.url=jdbc:oracle:thin:@HOST:PORT:DBNAME \
  -Dopenjpa.custom.username=USERNAME \
  -Dopenjpa.custom.password=PASSWORD
</pre>
</div></div>

<p>If you frequently need to test against another database, you can permanently declare
the database connection parameters in the <tt>~/.m2/settings.xml</tt> file under
a custom profile. For example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;settings&gt;
    &lt;profiles&gt;
        &lt;profile&gt;
            &lt;id&gt;test-oracle&lt;/id&gt;
            &lt;properties&gt;
                &lt;test-custom&gt;<span class="code-keyword">true</span>&lt;/test-custom&gt;
                &lt;openjpa.custom.driverjar&gt;${user.home}/.m2/privaterepos/jdbc-oracle-10_2_0_1_0.jar&lt;/openjpa.custom.driverjar&gt;
                &lt;openjpa.custom.driverclass&gt;oracle.jdbc.driver.OracleDriver&lt;/openjpa.custom.driverclass&gt;
                &lt;openjpa.custom.url&gt;jdbc:oracle:thin:@HOST:PORT:DBNAME&lt;/openjpa.custom.url&gt;
                &lt;openjpa.custom.username&gt;USERNAME&lt;/openjpa.custom.username&gt;
                &lt;openjpa.custom.password&gt;PASSWORD&lt;/openjpa.custom.password&gt;
            &lt;/properties&gt;
        &lt;/profile&gt;
    &lt;/profiles&gt;
&lt;/settings&gt;
</pre>
</div></div>

<p>This profile can then be executed by running:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
mvn test -DfailIfNoTests=<span class="code-keyword">false</span> -Dtest=TestPersistence
-Ptest-custom,test-oracle
</pre>
</div></div>


<h1><a name="Testing-RunningJUnitsinEclipse"></a>Running JUnits in Eclipse</h1>

<p>You'll probably want to setup a Debug Configuration in Eclipse for running the JUnit
testcases.</p>

<ol>
	<li>Run --&gt; Debug Configurations...  Create a new JUnit configuration<br/>
<span class="image-wrap" style=""><img src="/confluence/download/attachments/34610/eclipse_dbgcfg_1.png?version=1&amp;modificationDate=1281380901300"
style="border: 1px solid black" /></span></li>
	<li>For the Test tab - Give the configuration a Name, select a Project and Test class,
and you'll probably want to select the "Keep JUnit running..." option.<br/>
<span class="image-wrap" style=""><img src="/confluence/download/attachments/34610/eclipse_dbgcfg_2.png?version=1&amp;modificationDate=1281380901410"
style="border: 1px solid black" /></span></li>
	<li>For the Arguments tab - Press the Variables... button. This will allow us to define
variables that can be shared across other Release/Debug Configurations within this workspace.<br/>
<span class="image-wrap" style=""><img src="/confluence/download/attachments/34610/eclipse_dbgcfg_3.png?version=1&amp;modificationDate=1281380901520"
style="border: 1px solid black" /></span></li>
	<li>For the Select Variable window - Press the Edit Variables... button.<br/>
<span class="image-wrap" style=""><img src="/confluence/download/attachments/34610/eclipse_dbgcfg_4.png?version=1&amp;modificationDate=1281380899689"
style="border: 1px solid black" /></span></li>
	<li>For the Preferences windows - Press New... to add two variables.<br/>
<span class="image-wrap" style=""><img src="/confluence/download/attachments/34610/eclipse_dbgcfg_5.png?version=1&amp;modificationDate=1281380928296"
style="border: 1px solid black" /></span>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Name = openjpa.trace
Value = -Dopenjpa.Log=DefaultLevel=TRACE
Description = Set OpenJPA logging to TRACE
</pre>
</div></div>
<p><span class="image-wrap" style=""><img src="/confluence/download/attachments/34610/eclipse_dbgcfg_6.png?version=1&amp;modificationDate=1281380928405"
style="border: 1px solid black" /></span></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Name = connect.derby
Value = -Dopenjpa.ConnectionDriverName=org.apache.derby.jdbc.EmbeddedDriver -Dopenjpa.ConnectionURL=jdbc:derby:target/database/openjpa-derby-database;create=<span
class="code-keyword">true</span>
Description = Connection properties <span class="code-keyword">for</span> Derby
</pre>
</div></div>
<p><span class="image-wrap" style=""><img src="/confluence/download/attachments/34610/eclipse_dbgcfg_7.png?version=1&amp;modificationDate=1281380928511"
style="border: 1px solid black" /></span></p></li>
	<li>After the above are created and saved, add the new variables as VM Arguments.
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
${openjpa.trace}
${connect.derby}
</pre>
</div></div>
<p><span class="image-wrap" style=""><img src="/confluence/download/attachments/34610/eclipse_dbgcfg_8.png?version=1&amp;modificationDate=1281380928158"
style="border: 1px solid black" /></span></p></li>
</ol>


    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/openjpa/Testing">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=34610&revisedVersion=15&originalVersion=14">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/openjpa/Testing?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message