﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-aldream-文章分类-java</title><link>http://www.blogjava.net/aldreamlau/category/27235.html</link><description>me</description><language>zh-cn</language><lastBuildDate>Mon, 12 Nov 2007 22:04:13 GMT</lastBuildDate><pubDate>Mon, 12 Nov 2007 22:04:13 GMT</pubDate><ttl>60</ttl><item><title>eclipse Junit测试向导</title><link>http://www.blogjava.net/aldreamlau/articles/159974.html</link><dc:creator>aldream</dc:creator><author>aldream</author><pubDate>Mon, 12 Nov 2007 07:10:00 GMT</pubDate><guid>http://www.blogjava.net/aldreamlau/articles/159974.html</guid><wfw:comment>http://www.blogjava.net/aldreamlau/comments/159974.html</wfw:comment><comments>http://www.blogjava.net/aldreamlau/articles/159974.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/aldreamlau/comments/commentRss/159974.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aldreamlau/services/trackbacks/159974.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">
原文出处：<a href="http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf">http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf</a></span>
<p>&nbsp;</p>
<p>JUnit is a simple Java testing framework to
write tests for you Java application. This tutorial gives you an
overview of the features of JUnit and shows a little example how you
can write tests for your Java application.</p>
<h1>General</h1>
<p><strong>Author</strong>:
</p>
<p>Sascha
Wolski</p>
<p>Sebastian
Hennebrueder</p>
<p><a href="http://www.laliluna.de/tutorials.html">http://www.laliluna.de/tutorials.html</a>
? Tutorials for Struts, EJB, xdoclet and eclipse.</p>
<p><br />
</p>
<p><strong>Date</strong>:
</p>
<p>April,
12 2005</p>
<p><br />
</p>
<p><strong>Software:</strong></p>
<p>Eclipse
3.x</p>
<p>Junit
2.x</p>
<p><br />
</p>
<p><strong>Source
code:</strong></p>
<p><a href="http://www.laliluna.de/assets/tutorials/junit-testing-source.zip">http://www.laliluna.de/assets/tutorials/junit-testing-source.zip</a></p>
<p><strong>PDF
Version</strong></p>
<p><a href="http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf">http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf</a></p>
<h1>What is JUnit</h1>
<p>JUnit is a simple open source Java testing
framework used to write and run repeatable automated tests. It is an
instance of the xUnit architecture for unit testing framework.
Eclipse supports creating test cases and running test suites, so it
is easy to use for your Java applications.</p>
<p>JUnit features include:</p>
<ul>
    <li>
    <p>Assertions for testing expected results</p>
    </li>
    <li>
    <p>Test fixtures for sharing common test
    data</p>
    </li>
    <li>
    <p>Test suites for easily organizing and
    running tests</p>
    </li>
    <li>
    <p>Graphical and textual test runners</p>
    </li>
</ul>
<h1>What is a test case</h1>
<p>A test case is a class which holds a number
of test methods. For example if you want to test some  methods of a
class <em>Book</em> you create a class <em>BookTest</em> which extends
the JUnit <em>TestCase</em> class and place your test methods in there.</p>
<h1>How you write and run a simple test</h1>
<ol>
    <li>
    <p>Create
    a subclass of TestCase:</p>
    </li>
</ol>
<p><br />
</p>
<pre>public class BookTest extends TestCase{ <br />
//..<br />
}</pre>
<p>
<br />
</p>
<ol start="2">
    <li>
    <p>Write
    a test method to assert expected results on the object under test:</p>
    </li>
</ol>
<p>Note: The naming convention for a test
method is testXXX()</p>
<pre>public void testCollection() {<br />
Collection collection = new ArrayList();<br />
assertTrue(collection.isEmpty());<br />
} </pre>
<p>
<br />
</p>
<ol start="3">
    <li>
    <p>Write
    a <em>suite()</em> method that uses reflection to dynamically create a
    test suite containing all the <em>testXXX()</em> methods:</p>
    </li>
</ol>
<p><br />
</p>
<pre>public static Test suite(){<br />
return new TestSuite(BookTest.class);<br />
}</pre>
<p>
<br />
<br />
</p>
<ol start="4">
    <li>
    <p>Activate the JUnit view in Eclipse
    (<em>Window &gt; Show View &gt; Other.. &gt; Java &gt; JUnit</em>).</p>
    </li>
</ol>
<h1><img name="Graphic1" src="http://www.laliluna.de/assets/images/tutorials/junit-testing/junit-view.gif" alt="" align="left" border="0" height="356" width="402" /><br clear="left" />
<br />
<br />
</h1>
<p>You
find the <em>JUnit</em> tab near the <em>Package Explorer</em> tab. You
can change the position of the tab by drag and drop it.</p>
<p><img name="Graphic2" src="http://www.laliluna.de/assets/images/tutorials/junit-testing/junit-view2.gif" alt="" align="left" border="0" height="360" width="336" /><br clear="left" />
<br />
<br />
</p>
<ol start="5">
    <li>
    <p style="page-break-before: always;" class="ttext-western">
    Right click on the subclass of
    <em>TestCase</em> and choose <em>Run &gt; JUnit Test</em> to run the
    test.</p>
    </li>
</ol>
<h1>Using a test fixture</h1>
<p>A test fixture is useful if you have two or
more tests for a common set of objects. Using a test fixture avoids
duplicating the test code necessary to initialize and cleanup those
common objects for each test.</p>
<p>To create a test fixture, define a <em>setUp()</em>
method that initializes common object and a <em>tearDown()</em> method
to cleanup those objects. The JUnit framework automatically invokes
the <em>setUp()</em> method before a each test is run and the
<em>tearDown()</em> method after each test is run.</p>
<p>The following test uses a test fixture:</p>
<pre>public class BookTest2 extends TestCase {<br />
<br />
private Collection collection;<br />
<br />
protected void setUp() {<br />
collection = new ArrayList();<br />
}<br />
<br />
protected void tearDown() {<br />
collection.clear(); <br />
}<br />
<br />
public void testEmptyCollection(){<br />
assertTrue(collection.isEmpty());<br />
}<br />
}</pre>
<h1>
Dynamic and static way of running single tests</h1>
<p>JUnit supports two ways (static and dynamic)
of running single tests.
</p>
<p>In static way you override the <em>runTest()</em>
method inherited form TestCase class and call the desired test case.
A convenient way to do this is with an anonymous inner class.
</p>
<p>Note: Each test must be given a name, so
you can identify it if it fails.
</p>
<pre>TestCase test = new BookTest("equals test") {<br />
public void runTest() {<br />
testEquals();<br />
}<br />
};</pre>
<p>
<br />
<br />
</p>
<p>The dynamic way to create a test case to be
run uses reflection to implement <em>runTest</em>. It assumes the name
of the test is the name of the test case method to invoke. It
dynamically finds and invokes the test method. The dynamic way is
more compact to write but it is less static type safe. An error in
the name of the test case goes unnoticed until you run it and get a
<em>NoSuchMethodException</em>. We leave the choice of which to use up
to you.</p>
<pre>TestCast test = new BookTest("testEquals");</pre>
<h1>
What is a TestSuite</h1>
<p>If you have two tests and you'll run them
together you could run the tests one at a time yourself, but you
would quickly grow tired of that. Instead, JUnit provides an object
<em>TestSuite</em> which runs any number of test cases together. The
suite method is like a main method that is specialized to run tests.
</p>
<p>Create a suite and add each test case you
want to execute:</p>
<pre>public static void suite(){<br />
TestSuite suite = new TestSuite();<br />
suite.addTest(new BookTest("testEquals"));<br />
suite.addTest(new BookTest("testBookAdd"));<br />
return suite;<br />
}</pre>
<p>
<br />
<br />
</p>
<p>Since JUnit 2.0 there is an even simpler way
to create a test suite, which holds all testXXX() methods. You only
pass the class with the tests to a TestSuite and it extracts the test
methods automatically.</p>
<p>Note: If you use this way to create a
TestSuite all test methods will be added. If you do not want all test
methods in the TestSuite use the normal way to create it.</p>
<p>Example:</p>
<pre>public static void suite(){<br />
return new TestSuite(BookTest.class);<br />
}</pre>
<h1>
A little example</h1>
<p>Create a new Java project named
JUnitExample.
</p>
<p>Add a package
<em>de.laliluna.tutorial.junitexample</em> where you place the example
classes and a package <em>test.laliluna.tutorial.junitexample</em>
where you place your test classes.
</p>
<h2>The class Book</h2>
<p>Create a new class <em>Book</em> in the
package <em>de.laliluna.tutorial.junitexample</em>.</p>
<p>Add two properties <em>title</em> of type
<em>String</em> and <em>price</em> of type <em>double</em>.</p>
<p>Add a constructor to set the two properties.</p>
<p>Provide a getter- and setter-method for each
of them.</p>
<p>Add a method trunk for a method
<em>equals(Object object)</em> which checks if the object is an
instance of the class Book and the values of the object are equal.
The method return a boolean value.</p>
<p>Note: Do not write the logic of the
<em>equals(..)</em> method, we do it after finish creating the test
method.</p>
<p>The following source code shows the class
Book.</p>
<pre>public class Book {<br />
<br />
private String title;<br />
private double price;<br />
<br />
/**<br />
* Constructor <br />
* <br />
* @param title<br />
* @param price<br />
*/<br />
public Book(String title,<br />
double price) {<br />
this.title = title;<br />
this.price = price;<br />
}<br />
<br />
/**<br />
* Check if an object is an instance of book<br />
* and the values of title and price are equal<br />
* then return true, otherwise return false<br />
*/<br />
public boolean equals(Object object) {<br />
<br />
return false;<br />
}<br />
<br />
public double getPrice() {<br />
return price;<br />
}<br />
<br />
public void setPrice(double price) {<br />
this.price = price;<br />
}<br />
<br />
public String getTitle() {<br />
return title;<br />
}<br />
<br />
public void setTitle(String title) {<br />
this.title = title;<br />
}<br />
}</pre>
<h2>
The test case BookTest</h2>
<p>Create a new test case <em>BookTest</em> in
the package <em>test.laliluna.tutorial.junitexample</em><span style="font-style: normal;">
Right click on the package and choose </span><em>New &gt; JUnit Test
Case</em><span style="font-style: normal;">. </span>
</p>
<p style="font-style: normal;" class="ttext-western">In the wizard
choose the methods stubs <em>setUp()</em>, <em>tearDown()</em> and
<em>constructor()</em>.</p>
<p><img name="Graphic3" src="http://www.laliluna.de/assets/images/tutorials/junit-testing/new-test-case.gif" alt="" align="left" border="0" height="365" width="464" /><br clear="left" />
<br />
<br />
</p>
<p><br />
<br />
</p>
<p>The
following source code shows the class BookTest</p>
<pre>public class BookTest extends TestCase {<br />
<br />
/**<br />
* setUp() method that initializes common objects<br />
*/<br />
protected void setUp() throws Exception {<br />
super.setUp();<br />
}<br />
<br />
/**<br />
* tearDown() method that cleanup the common objects<br />
*/<br />
protected void tearDown() throws Exception {<br />
super.tearDown();<br />
}<br />
<br />
/**<br />
* Constructor for BookTest.<br />
* @param name<br />
*/<br />
public BookTest(String name) {<br />
super(name);<br />
}<br />
<br />
}</pre>
<p>
<br />
<br />
</p>
<p>Now we want to write a test for the
<em>equals(..)</em> method of the class <em>Book</em>. We provide three
private properties, book1, book2 and book3 of type <em>Book. </em>
</p>
<pre>private Book book1;<br />
private Book book2;<br />
private Book book3;</pre>
<p>
<br />
<br />
</p>
<p>Within the <em>setUp()</em> method we
initializes the three properties with some values. Property book1 and
book3 are the same.</p>
<pre>protected void setUp() throws Exception {<br />
super.setUp();<br />
book1 = new Book("ES", 12.99);<br />
book2 = new Book("The Gate", 11.99);<br />
book3 = new Book("ES", 12.99);<br />
}</pre>
<p>
<br />
<br />
</p>
<p>Within the tearDown() method we cleanup the
properties:</p>
<pre>protected void tearDown() throws Exception {<br />
super.tearDown();<br />
book1 = null;<br />
book2 = null;<br />
book3 = null;<br />
}</pre>
<p>
<br />
<br />
</p>
<p>Now, add a test method <em>testEquals()</em>
to the test case. Within the method we use the <em>assertFalse()</em>
method of the JUnit framework to test if the return-value of the
equals(..) method is false, because book1 and book2 are not the same.
If the return-value is false the logic of the equals() method is
correct, otherwise there is a logical problem while comparing the
objects. We want to test if the method compares the objects correctly
by using the assertTrue() method. Book1 and Book3 are the same,
because both are an instance of the class Book and have the same
values.</p>
<p>The following source code shows the
testEquals() method:</p>
<pre>public void testEquals(){<br />
assertFalse(book2.equals(book1));<br />
assertTrue(book1.equals(book1));<br />
}</pre>
<p>
<br />
<br />
</p>
<h2>Writing the logic of the equals() method</h2>
<p>We have finished the test and now we can add
the logic to the <em>equals()</em> method stub. Open the class Book and
add the logic to the <em>equals()</em> method. First we check if the
object given by the method is an instance of <em>Book</em>. Then
compare the properties <em>title</em> and <em>price</em><span style="font-style: normal;">,
if they are equal return true. </span>
</p>
<pre>public boolean equals(Object object) {<br />
if (object instanceof Book) {<br />
Book book = (Book) object;<br />
return getTitle().equals(book.getTitle())<br />
&amp;&amp; getPrice() == book.getPrice();<br />
}<br />
return false;<br />
}</pre>
<h2>
<br />
Create the suite() method</h2>
<p>In order to run the test method <em>testEquals()</em>
add a method <em>suite()</em> to the class <em>BookTest</em>.
</p>
<p>Note: You can also create a separate class
where you add the <em>suite()</em>&nbsp;method.</p>
<p>Within the method create a new instance of
<em>TestSuite</em> and use the method <em>addTest(..)</em> to add a test.
Here we use the dynamically way to add a test to a TestSuite.</p>
<p>The method looks like the follows:</p>
<pre>public static Test suite(){<br />
TestSuite suite = new TestSuite();<br />
suite.addTest(new BookTest("testEquals"));<br />
return suite;<br />
}</pre>
<p>
<br />
<br />
</p>
<h2>Run the test</h2>
<p>After finishing all test methods we want to
run the JUnit test case. Right mouse button on the class BookTest and
choose <em>Run As &gt; JUnit Test</em>.
</p>
<p>On the JUnit view (Menu Windows -&gt; show
view) of Eclipse you can see how many runs, errors and failures
occurred.</p>
<br />
<img src ="http://www.blogjava.net/aldreamlau/aggbug/159974.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aldreamlau/" target="_blank">aldream</a> 2007-11-12 15:10 <a href="http://www.blogjava.net/aldreamlau/articles/159974.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在eclipse调试JSP和java程序</title><link>http://www.blogjava.net/aldreamlau/articles/159969.html</link><dc:creator>aldream</dc:creator><author>aldream</author><pubDate>Mon, 12 Nov 2007 06:53:00 GMT</pubDate><guid>http://www.blogjava.net/aldreamlau/articles/159969.html</guid><wfw:comment>http://www.blogjava.net/aldreamlau/comments/159969.html</wfw:comment><comments>http://www.blogjava.net/aldreamlau/articles/159969.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/aldreamlau/comments/commentRss/159969.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aldreamlau/services/trackbacks/159969.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">原文出处：<a href="http://www.laliluna.de/assets/tutorials/debugging-jsp-java-tutorial-en.pdf">http://www.laliluna.de/assets/tutorials/debugging-jsp-java-tutorial-en.pdf</a><br />
</span><br />
<p>This tutorial gives you an overview of how
to use the debugging feature of eclipse to debug  your web or Java
projects.</p>
<h1>General</h1>
<p><strong>Author</strong>:
</p>
<p>Sascha
Wolski</p>
<p>Sebastian
Hennebrueder</p>
<p><a href="http://www.laliluna.de/tutorial.html">http://www.laliluna.de/tutorial.html</a>
? Tutorials for  Struts, JSF, EJB, Hibernate, xdoclet, eclipse and
more.</p>
<p><strong>Datum</strong>:
</p>
<p>January, 25 2005<br />
<strong>Development
Tools</strong></p>
<p>Eclipse
3.x
</p>
<p>MyEclipse
plugin 3.8
</p>
<p>(A
cheap and quite powerful Extension to Eclipse to develop Web
Applications and EJB (J2EE) Applications. I think that there is a
test version availalable at MyEclipse.)</p>
<p><br />
</p>
<p><strong>PDF
Version des Tutorials</strong></p>
<p><a href="http://www.laliluna.de/assets/tutorials/debugging-jsp-java-tutorial-en.pdf">http://www.laliluna.de/assets/tutorials/debugging-jsp-java-tutorial-en.pdf</a></p>
<h1>Introduction</h1>
<p>The Eclipse Platform features a Java
debugger that provides all standard debugging functionality,
including the ability to perform step execution, setting of
breakpoints and values, inspecting variables and values, and to
suspend and resume threads. With the extension MyEclipse you are
able to debug JSP and included files too.
</p>
<p><br />
<br />
</p>
<h1>The debug perspective</h1>
<p>First we will explain the debug perspective.
You can activate the perspective under <strong>Windows &gt; Open
Perspective &gt; Other...</strong>
</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/select-perspective.gif" name="Graphic1" alt="" align="left" border="0" height="249" width="244" /><br clear="left" />
<br />
<br />
</p>
<p>Alternatively, you can click on the debug
icon of the perspective tool bar.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/select-perspective-toolbar.gif" name="Graphic2" alt="" align="left" border="0" height="33" width="220" /><br clear="left" />
<br />
<br />
</p>
<h2>Debug view</h2>
<p>The Debug view displays the stack trace for
the suspended threads for each target you are debugging. Each entry
is the variable state of a method right when it called the next
method. The view allows you to manage the debugging of a program in
the workbench.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/window-debug.gif" name="Graphic3" alt="" align="left" border="0" height="303" width="474" /><br clear="left" />
<br />
<br />
</p>
<h2>Variables view</h2>
<p>The view displays information about the
variables in the selected class. You can get information about the
variable like value, size and more from this view.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/window-variable.gif" name="Graphic4" alt="" align="left" border="0" height="297" width="400" /><br clear="left" />
<br />
<br />
</p>
<h2 style="page-break-before: always;">Breakpoints
view</h2>
<p>The view lists all breakpoints you have set
in the workbench project. In this view, you can enable or disable
breakpoints, remove them, or add a new ones. You can also
double-click a breakpoint to display its location in the editor.</p>
<p>This view also lists Java exception breakpoints,
which suspend execution at the point where the exception is thrown.
You can add or remove exceptions.
</p>
<p><br />
<br />
</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/window-breakpoints.gif" name="Graphic5" alt="" align="left" border="0" height="131" width="410" /><br clear="left" />
<br />
<br />
</p>
<h2>Expressions view</h2>
<p>You can inspect data from each class of a
suspended thread, and other places in this view. It opens
automatically when an item is added to the view.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/window-expressions.gif" name="Graphic6" alt="" align="left" border="0" height="247" width="404" /><br clear="left" />
<br />
<br />
</p>
<h1>Debug a web project</h1>
<p>In the next steps I will explain how you can
debug a project. I will refer to the project JSP + Servlet you can
download on our site.
<a href="http://www.laliluna.de/assets/tutorials/java-servlets-jsp-tutorial.zip">http://www.laliluna.de/assets/tutorials/java-servlets-jsp-tutorial.zip</a>
</p>
<p>Open the class <strong>BookEdit.java</strong> and set
a breakpoint in the method <strong>doGet(..)</strong> at line 72. You can set a
breakpoint by double-click on the info bar on the left edge of the
editor, or right click <strong>Toggle Breakpoint</strong>.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/set-breakpoint-java.gif" name="Graphic7" alt="" align="left" border="0" height="157" width="584" /><br clear="left" />
<br />
<br />
</p>
<p>Set the second breakpoint in the
<strong>bookEdit.jsp</strong> file on the HTML start tag at line 14.</p>
<p><strong>Note</strong>: If you don't have installed
MyEclipse, you can't set a breakpoint in jsp and included files.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/set-breakpoint-jsp.gif" name="Graphic8" alt="" align="left" border="0" height="203" width="488" /><br clear="left" />
<br />
<br />
</p>
<p>To see all breakpoints you can use the
Breakpoints view in the debug perspective.
</p>
<h2>Breakpoint Properties</h2>
<p>You can apply some properties to each
breakpoint, for example how many times a breakpoint can hit before it
suspends the thread or on which condition it suspends the thread.</p>
<p>Select the breakpoint right mouse button and
choose the option <strong>Breakpoint Properties</strong>.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-breakpoint-properties-open.gif" name="Graphic21" alt="" align="left" border="0" height="100" width="216" /><br clear="left" />
<br />
<br />
</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-breakpoint-properties.gif" name="Graphic20" alt="" align="left" border="0" height="469" width="552" /><br clear="left" />
<br />
<br />
</p>
<h3><br />
<br />
</h3>
<h3>Hit Count</h3>
<p>The hit count sets a number of times the
breakpoint can be executed before the thread suspends. Very helpful
in a loop expression or if you want to know the value of a expression
after some hits.</p>
<h3>Enable Condition</h3>
<p>There are two options to suspend a thread by
using a condition.
</p>
<ul>
    <li>
    <p>if the enabled condition is true</p>
    </li>
    <li>
    <p>if the enabled condition changes</p>
    </li>
</ul>
<p>If you want that the condition suspends the
thread when it is true select the option <strong>condition is true</strong> on
<strong>Suspend when</strong>.</p>
<p>Example:</p>
<pre>action.equals("edit");</pre>
<p>
<br />
<br />
</p>
<p>If you want that the condition suspends the
thread when the value of the condition changes select the option
<strong>value of the condition </strong><span>changes
on </span><strong>Suspend when</strong><span style="font-style: normal;">.</span></p>
<p><br />
<br />
</p>
<h3><br />
<br />
</h3>
<h3 style="page-break-before: always;">Suspend Policy</h3>
<p>You can define if the breakpoint suspends
only the thread or the complete virtual machine.</p>
<h2>Deploy and Debug</h2>
<p>Now we want to debug the project, so deploy
it and call it in your web browser. <br />
Choose <strong>Edit</strong> on the
list of books.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/edit-book.gif" name="Graphic9" alt="" align="left" border="0" height="228" width="456" /><br clear="left" />
<br />
<br />
</p>
<p>After you clicked the <strong>Edit</strong> link,
eclipse shows you the following confirm window.  You can choose if
you want to switch to the debug perspective. Yes, you want it<strong> ;- )</strong></p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/switch-to-debug-perspective.gif" name="Graphic10" alt="" align="left" border="0" height="191" width="457" /><br clear="left" />
<br />
<br />
</p>
<p style="page-break-before: always;">The first
entry in the debug view represents the state of the method where the
breakpoint was set. You can preview the state of a entry simply by
clicking on it, also the file will be open where the method is
placed.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-view-current-frame.gif" name="Graphic11" alt="" align="left" border="0" height="162" width="435" /><br clear="left" />
<br />
<br />
</p>
<p>You can see the values of all variables of
the selected entry in the Variables view.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-variables-view.gif" name="Graphic17" alt="" align="left" border="0" height="256" width="391" /><br clear="left" />
<br />
<br />
</p>
<p>A marked line and an arrow at the breakpoint
shows that the debugger is suspended on this line.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-editor.gif" name="Graphic12" alt="" align="left" border="0" height="183" width="355" /><br clear="left" />
<br />
<br />
</p>
<p>If you've decided that you missed an
important place in the execution process, perhaps the breakpoint was
in the wrong place, or maybe you accidentally stepped over some code
you wanted to inspect earlier in the process. Eclipse has a feature
called <strong>Drop to frame</strong><span>, that
essentially lets you 'rewind' the execution to the beginning of any
method in the stack. This is especially useful when you perform
variable modification or code hot swapping.<br />
Right click on the
frame and choose the option </span><strong>Drop to Frame</strong>.</p>
<p><br />
<br />
</p>
<h2><br />
<br />
</h2>
<h2 style="page-break-before: always;">Inspect
expressions</h2>
<p>To get informations about expression, you
can inspect a expression. Right click on the marked expression, you
want to inspect and choose <strong>Inspect</strong><span>
or press </span><strong>Ctrl + Shift + I</strong><span>.
A pop-up window appears that holds the informations about the
expression. </span>
</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-inspect.gif" name="Graphic13" alt="" align="left" border="0" height="207" width="389" /><br clear="left" />
<br />
<br />
</p>
<h2><br />
<br />
</h2>
<h2>Watch expressions</h2>
<p>Watch is similar to inspect an expression.
Watch means that the expression will be added to the Expressions
view, where you can watch the informations about the expression. Add
two expressions to the Expressions view.
</p>
<p>Mark the expressions, right click and choose
<strong>Watch</strong>.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-watch.gif" name="Graphic14" alt="" align="left" border="0" height="179" width="537" /><br clear="left" />
<br />
<br />
</p>
<p>Now the two expression are in the
Expressions view.</p>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-expression-view.gif" name="Graphic15" alt="" align="left" border="0" height="158" width="428" /><br clear="left" />
<br />
<br />
</p>
<h2><br />
<br />
</h2>
<h2 style="page-break-before: always;">Display
expressions</h2>
<p>If you want to display the type and the
value of an expression, mark the expression and choose  the <strong>Display</strong>
option from the context menu (right mouse button) or press <strong>Ctrl +
Shift + D</strong>.
</p>
<h2><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-display.gif" name="Graphic16" alt="" align="left" border="0" height="127" width="275" /><br clear="left" />
Run
to Line</h2>
<p>If you set a breakpoint and somewhere under
the breakpoint is a line you want to go, you can use the option <strong>Run
to Line</strong>. The code between the breakpoint and the selected line
will be executed. Select the line you want to go, press the right
mouse button and choose  the option <strong>Run to Line</strong><span>
or use the key binding </span><strong>Ctrl + R</strong>.</p>
<h2><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-run-to-line.gif" name="Graphic18" alt="" align="left" border="0" height="302" width="636" /><br clear="left" />
Debugging
JSP files (supported by MyEclipse)</h2>
<p><img src="http://www.laliluna.de/assets/images/tutorials/debugging-jsp-java-tutorial/debug-jsp-variable-view.gif" name="Graphic19" alt="" align="left" border="0" height="208" width="384" /><br clear="left" />
Debugging
a JSP file is the same like debugging a Java class, but the most
features (Watch, Inspect, etc) are not implemented. The only way to
get the values of variables is the Variables view.</p>
<p><br />
<br />
</p>
<p>That's it. You will only need to debug, when
you make mistakes. Avoid them and forget the tutorial.</p>
<br />
<img src ="http://www.blogjava.net/aldreamlau/aggbug/159969.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aldreamlau/" target="_blank">aldream</a> 2007-11-12 14:53 <a href="http://www.blogjava.net/aldreamlau/articles/159969.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>嵌套诊断环境NDC </title><link>http://www.blogjava.net/aldreamlau/articles/159731.html</link><dc:creator>aldream</dc:creator><author>aldream</author><pubDate>Sun, 11 Nov 2007 06:51:00 GMT</pubDate><guid>http://www.blogjava.net/aldreamlau/articles/159731.html</guid><wfw:comment>http://www.blogjava.net/aldreamlau/comments/159731.html</wfw:comment><comments>http://www.blogjava.net/aldreamlau/articles/159731.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/aldreamlau/comments/commentRss/159731.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aldreamlau/services/trackbacks/159731.html</trackback:ping><description><![CDATA[<span id="ArticleContent1_ArticleContent1_lblContent"><span class="myp11"><font id="zoom">在
多用户并发的环境下，通常是由不同的线程分别处理不同的客户端请求。此时要在日志信息中区分出不同的客户端，你可以为每一个线程生成一个Logger，从
而从一堆日志信息中区分出哪些信息是属于哪个线程的，但这种方式并不高效。Log4J巧妙地使用了Neil
Harrison提出的&#8220;NDC（嵌套诊断环境）&#8221;机制来解决这个问题。Log4J为同一类别的线程生成一个Logger，多个线程共享使用，而它仅在日
志信息中添加能够区分不同线程的信息。NDC是什么？举例来说，如果一个Servlet接到并发请求时，为每一个客户端创建一个新的线程，然后分配一个用
于保存该请求上下文的NDC堆栈。该上下文可能是发出请求的主机名、IP地址或其它任何可以用于标识该请求的信息。这样，由于不同的客户端处理线程具有不
同的NDC堆栈，即使这个Servlet同时生成多个线程处理不同的请求，这些日志信息仍然可以区分出来，就好像Log4J为每一个线程都单独生成了一个
Logger实例一样。在Log4J中是通过org.apache.log4j.NDC实现这种机制的。使用NDC的方法也很简单，步骤如下： <br />
<br />
1. 在进入一个环境时调用NDC.push(String)，然后创建一个NDC；
<br />
<br />
2. 所做的日志操作输出中包括了NDC的信息；
<br />
<br />
3. 离开该环境时调用NDC.pop方法；
<br />
<br />
4. 当从一个线程中退出时调用NDC.remove方法，以便释放资源。
<br />
<br />
下面是一个模拟记录来自不同客户端请求事件的例子，代码如下：<br />
</font></span></span>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;org.apache.log4j.Logger;<br />
</span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;org.apache.log4j.NDC;<br />
</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;TestNDC&nbsp;{<br />
&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;Logger&nbsp;log&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Logger.getLogger(TestNDC.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">.getName());<br />
&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;main(String[]&nbsp;args)&nbsp;{<br />
&nbsp;&nbsp;log.info(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Make&nbsp;sure&nbsp;%x&nbsp;is&nbsp;in&nbsp;your&nbsp;layout&nbsp;pattern!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;从客户端获得IP地址的例子</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;String[]&nbsp;ips&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;{</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">192.168.0.10</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">192.168.0.27</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">};<br />
&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;&nbsp;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">ips.length&nbsp;;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;模拟一个运行方法</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;将IP放进&nbsp;NDC中</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;NDC.push(ips[i]);<br />
&nbsp;&nbsp;&nbsp;log.info(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">A&nbsp;NEW&nbsp;client&nbsp;connected,&nbsp;who's&nbsp;ip&nbsp;should&nbsp;appear&nbsp;in&nbsp;this&nbsp;log&nbsp;message.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;NDC.pop();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;NDC.remove();<br />
&nbsp;&nbsp;log.info(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Finished.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;}<br />
}</span></div>
<span id="ArticleContent1_ArticleContent1_lblContent"><span class="myp11"><font id="zoom">注意配置文件中的布局格式中一定要加上%x。系统输出如下：<br />
</font></span></span>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);">INFO&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;Make&nbsp;sure&nbsp;</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">x&nbsp;is&nbsp;in&nbsp;your&nbsp;layout&nbsp;pattern</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);"><br />
INFO&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">192.168</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0);">0.10</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;A&nbsp;NEW&nbsp;client&nbsp;connected,&nbsp;who</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">s&nbsp;ip&nbsp;should&nbsp;appear&nbsp;in&nbsp;this&nbsp;log&nbsp;</span><span style="color: rgb(0, 0, 0);">message.<br />
INFO&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">192.168</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0);">0.27</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;A&nbsp;NEW&nbsp;client&nbsp;connected,&nbsp;who</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">s&nbsp;ip&nbsp;should&nbsp;appear&nbsp;in&nbsp;this&nbsp;log&nbsp;</span><span style="color: rgb(0, 0, 0);">message.<br />
INFO&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;Finished.</span></div>
<br />
<img src ="http://www.blogjava.net/aldreamlau/aggbug/159731.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aldreamlau/" target="_blank">aldream</a> 2007-11-11 14:51 <a href="http://www.blogjava.net/aldreamlau/articles/159731.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个Junit例子(转自caterpillar.onlyfun.net)</title><link>http://www.blogjava.net/aldreamlau/articles/159653.html</link><dc:creator>aldream</dc:creator><author>aldream</author><pubDate>Sat, 10 Nov 2007 16:55:00 GMT</pubDate><guid>http://www.blogjava.net/aldreamlau/articles/159653.html</guid><wfw:comment>http://www.blogjava.net/aldreamlau/comments/159653.html</wfw:comment><comments>http://www.blogjava.net/aldreamlau/articles/159653.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/aldreamlau/comments/commentRss/159653.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aldreamlau/services/trackbacks/159653.html</trackback:ping><description><![CDATA[使用JUnit時，您主要都是透過繼承TestCase類別來撰寫測試案例，預設上您可以使用testXXX() 名稱來撰寫單元測試。<br />
<br />
在測試一個單元方法時，有時您會需要給它一些物件作為運行時的資料，例如您撰寫下面這個測試案例：<br />
<br />
MaxMinTest.java<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyfun.caterpillar.test;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyfun.caterpillar.MaxMinTool;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;junit.framework.TestCase;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;MaxMinTest&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;TestCase&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;testMax()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arr&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;{</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">};<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;MaxMinTool.getMax(arr));<br />
</span><span style="color: rgb(0, 128, 128);">10</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">11</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">12</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;testMin()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">13</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arr&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;{</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">};<br />
</span><span style="color: rgb(0, 128, 128);">14</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;MaxMinTool.getMin(arr));<br />
</span><span style="color: rgb(0, 128, 128);">15</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">16</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">17</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;main(String[]&nbsp;args)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">18</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;junit.swingui.TestRunner.run(MaxMinTest.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />
</span><span style="color: rgb(0, 128, 128);">19</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">20</span>&nbsp;<span style="color: rgb(0, 0, 0);">}</span></div>
<br />
您將設計的MaxMinTool包括靜態方法getMax()與getMin()，當您給它一個整數陣列，它們將個別傳回陣列中的最大值與最小值，顯然
的，您所準備的陣列重複出現在兩個單元測試之中，重複的程式碼在設計中可以減少就儘量減少，在這兩個單元測試中，整數陣列的準備是單元方法所需要的資源，
我們稱之為fixture，也就是一個測試時所需要的資源集合。<br />
<br />
fixture必須與上下文（Context）無關，也就是與程式執行前後無關，這樣才符合單元測試的意涵，為此，通常將所需的fixture撰寫在單元方法之中，如此在單元測試開始時創建fixture，並於結束後銷毀fixture。<br />
<br />
然而對於重複出現在各個單元測試中的fixture，您可以集中加以管理，您可以在繼承TestCase之後，重新定義<span style="font-weight: bold;">setUp()</span>與<span style="font-weight: bold;">tearDown()</span>方法，將數個單元測試所需要的fixture在setUp()中創建，並在tearDown()中銷毀，例如：<br />
<br />
MaxMinTest.java<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyfun.caterpillar.test;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyfun.caterpillar.MaxMinTool;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;junit.framework.TestCase;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;MaxMinTest&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;TestCase&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arr;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;setUp()&nbsp;</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">10</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">.setUp();<br />
</span><span style="color: rgb(0, 128, 128);">11</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]{</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">};<br />
</span><span style="color: rgb(0, 128, 128);">12</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">13</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">14</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;tearDown()&nbsp;</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">15</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">.tearDown();<br />
</span><span style="color: rgb(0, 128, 128);">16</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;<br />
</span><span style="color: rgb(0, 128, 128);">17</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">18</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">19</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;testMax()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">20</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;MaxMinTool.getMax(arr));<br />
</span><span style="color: rgb(0, 128, 128);">21</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">22</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">23</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;testMin()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">24</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;MaxMinTool.getMin(arr));<br />
</span><span style="color: rgb(0, 128, 128);">25</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">26</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">27</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;main(String[]&nbsp;args)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">28</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;junit.swingui.TestRunner.run(MaxMinTest.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />
</span><span style="color: rgb(0, 128, 128);">29</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">30</span>&nbsp;<span style="color: rgb(0, 0, 0);">}</span></div>
<br />
setUp()方法會在每一個單元測試testXXX()方法開始前被呼叫，因而整數陣列會被建立，而tearDown()會在每一個單元測試
testXXX()方法結束後被呼叫，因而整數陣列參考名稱將會參考至null，如此一來，您可以將fixture的管理集中在
setUp()與tearDown()方法之後。<br />
<br />
最後按照測試案例的內容，您完成MaxMinTool類別：
<br />
<br />
MaxMinTool.java<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyfun.caterpillar;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;MaxMinTool&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;getMax(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arr)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;max&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Integer.MIN_VALUE;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;arr.length;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(arr[i]&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;max)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;arr[i];<br />
</span><span style="color: rgb(0, 128, 128);">10</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">11</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">12</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;max;<br />
</span><span style="color: rgb(0, 128, 128);">13</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">14</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">15</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;getMin(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arr)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">16</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;min&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Integer.MAX_VALUE;<br />
</span><span style="color: rgb(0, 128, 128);">17</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">18</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;arr.length;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">19</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(arr[i]&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;min)<br />
</span><span style="color: rgb(0, 128, 128);">20</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;arr[i];<br />
</span><span style="color: rgb(0, 128, 128);">21</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">22</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">23</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;min;<br />
</span><span style="color: rgb(0, 128, 128);">24</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">25</span>&nbsp;<span style="color: rgb(0, 0, 0);">}</span></div>
<br />
Swing介面的TestRunner在測試失敗時會顯示紅色的棒子，而在測試成功後會顯示綠色的棒子，而 <span style="font-weight: bold;">"Keep the bar green to keep the code clean."</span> 正是JUnit的名言，也是測試的最終目的。<br />
<img src ="http://www.blogjava.net/aldreamlau/aggbug/159653.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aldreamlau/" target="_blank">aldream</a> 2007-11-11 00:55 <a href="http://www.blogjava.net/aldreamlau/articles/159653.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Log4j优化</title><link>http://www.blogjava.net/aldreamlau/articles/159642.html</link><dc:creator>aldream</dc:creator><author>aldream</author><pubDate>Sat, 10 Nov 2007 15:31:00 GMT</pubDate><guid>http://www.blogjava.net/aldreamlau/articles/159642.html</guid><wfw:comment>http://www.blogjava.net/aldreamlau/comments/159642.html</wfw:comment><comments>http://www.blogjava.net/aldreamlau/articles/159642.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/aldreamlau/comments/commentRss/159642.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aldreamlau/services/trackbacks/159642.html</trackback:ping><description><![CDATA[一个经常引用的依靠于logging的参数是可以计算的花费。这是一个合理的概念，一个适度的应用程序可能产生成千上万个日志请求。许多努力花在测量和调试logging的优化上。Log4j要求快速和弹性：速度最重要，弹性是其次。<br />
用户应该注意随后的优化建议。<br />
<strong><a target=""><span style="color: rgb(118, 0, 0);">9.1 日志为禁用时，日志的优化</span></a></strong><br />
当日志被彻底的关闭，一个日志请求的花费等于一个方法的调用加上整数的比较时间。<br />
在233mhz的Pentium II 机器上这个花费通常在5-50纳秒之间。<br />
然而，方法调用包括参数构建的隐藏花费。<br />
例如，对于logger cat，<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);">logger.debug(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Entry&nbsp;number:&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;is&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;String.valueOf(entry));</span></div>
引起了构建信息参数的花费，例如，转化整数i和entry到一个string，并且连接中间字符串，不管信息是否被输出。这个参数的构建花费可能是很高，它主要决定于被调用的参数的大小。<br />
避免参数构建的花费应如下，<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(logger.isDebugEnabled()&nbsp;{<br />
logger.debug(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Entry&nbsp;number:&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;is&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;String.valueOf(entry[i]));<br />
}</span></div>
<br />
如果logger的debug被关闭这将不会招致参数构建的花费。另一方面，如果logger是debug的话，它将产生两次判断 logger是否能用的花费。一次是在debugenabled，一次是debug。这是无关紧要的，因为判断日志是否可用只占日志实际花费时间的约1%。<br />
在Log4j里，日志请求在Logger 类的实例里。Logger 是一个类，而不是一个接口。这大量的减少了在方法调用上的弹性化的花费。<br />
当然用户采用预处理或编译时间技术去编译出所有的日志声明。这将导致完美的执行成效。<br />
然而因为二进制应用程序不包括任何的日志声明的结果，日志不可能对那个二进制程序开启。以我的观点，以这种较大的代价来换取较小的性能优化是不值得的。<br />
<strong>9.2 当日志状态为启用时，日志的优化</strong><br />
这是本质上的优化logger的层次。当日志状态为开，Log4j依然需要比较请求的级别与logger的级别。然而，logger可能没有被安排一个级别；它们将从它们的father继承。这样，在继承之前，logger可能需要搜索它的祖先。<br />
这里有一个认真的努力使层次的搜索尽可能的快。例如，子logger仅仅连接到它的存在的father logger。<br />
在先前展示的BasicConfigurator 例子中，名为com.foo.bar 的logger是连接到根logger，因此绕过了不存在的logger com和com.foo。这将显著的改善执行的速度，特别是解析logger的层结构时。<br />
典型的层次结构的解析的花费是logger彻底关闭时的三倍。<br />
<strong>9.3 日志信息的输出时，日志的优化</strong><br />
这是主要花费在日志输出的格式化和发送它到它的输出源上。这里我们再一次的付出努力以使格式化执行的尽可能快。同appender一样。实际上典型的花费大约是100-300毫秒。<br />
详情看org.apache.log4.performance.Logging。<br />
虽然Log4j有许多特点，但是它的第一个设计目标还是速度。一些Log4j的组件已经被重写过很多次以改善性能。不过，投稿者经常提出了新的优化。你应该满意的知道，以SimpleLayout的配置执行测试已经展示了Log4j的输出同System.out.println一样快。<br />
<img src ="http://www.blogjava.net/aldreamlau/aggbug/159642.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aldreamlau/" target="_blank">aldream</a> 2007-11-10 23:31 <a href="http://www.blogjava.net/aldreamlau/articles/159642.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Log4j配置文件</title><link>http://www.blogjava.net/aldreamlau/articles/159483.html</link><dc:creator>aldream</dc:creator><author>aldream</author><pubDate>Fri, 09 Nov 2007 16:43:00 GMT</pubDate><guid>http://www.blogjava.net/aldreamlau/articles/159483.html</guid><wfw:comment>http://www.blogjava.net/aldreamlau/comments/159483.html</wfw:comment><comments>http://www.blogjava.net/aldreamlau/articles/159483.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/aldreamlau/comments/commentRss/159483.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aldreamlau/services/trackbacks/159483.html</trackback:ping><description><![CDATA[<span style="font-family: 宋体;">下面的Log4J配置文件实现了输出到控制台、文件、回滚文件、发送日志邮件、输出到数据库日志表、自定义标签等全套功能。</span>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 128, 128);">&nbsp;1</span><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="color: rgb(0, 0, 0);">log4j.rootLogger</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DEBUG</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">CONSOLE</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">A1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">im<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.addivity.org.apache</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">true<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#&nbsp;应用于控制台<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.CONSOLE</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.ConsoleAppender<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.Threshold</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DEBUG<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.CONSOLE.Target</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">System.out<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.CONSOLE.Encoding</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">GBK<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.CONSOLE.layout</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.PatternLayout<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.CONSOLE.layout.ConversionPattern</span><span style="color: rgb(0, 0, 0);">=</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">framework</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%d&nbsp;-&nbsp;%c&nbsp;-%-4r&nbsp;</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">%t</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%-5p&nbsp;%c&nbsp;%x&nbsp;-&nbsp;%m%n<br />
</span><span style="color: rgb(0, 128, 128);">10</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#log4j.appender.CONSOLE.layout.ConversionPattern</span><span style="color: rgb(0, 0, 0);">=</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">start</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%d{DATE}</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">DATE</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%p</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">PRIORITY</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%x</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">NDC</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%t</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">THREAD</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;n%c</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">CATEGORY</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%m</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">MESSAGE</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%n<br />
</span><span style="color: rgb(0, 128, 128);">11</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#应用于文件<br />
</span><span style="color: rgb(0, 128, 128);">12</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.FILE</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.FileAppender<br />
</span><span style="color: rgb(0, 128, 128);">13</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.FILE.File</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">file.log<br />
</span><span style="color: rgb(0, 128, 128);">14</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.FILE.Append</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">false<br />
</span><span style="color: rgb(0, 128, 128);">15</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.FILE.Encoding</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">GBK<br />
</span><span style="color: rgb(0, 128, 128);">16</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.FILE.layout</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.PatternLayout<br />
</span><span style="color: rgb(0, 128, 128);">17</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.FILE.layout.ConversionPattern</span><span style="color: rgb(0, 0, 0);">=</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">framework</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%d&nbsp;-&nbsp;%c&nbsp;-%-4r&nbsp;</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">%t</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%-5p&nbsp;%c&nbsp;%x&nbsp;-&nbsp;%m%n<br />
</span><span style="color: rgb(0, 128, 128);">18</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#&nbsp;Use&nbsp;this&nbsp;layout&nbsp;for&nbsp;LogFactor&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">&nbsp;analysis<br />
</span><span style="color: rgb(0, 128, 128);">19</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#&nbsp;应用于文件回滚<br />
</span><span style="color: rgb(0, 128, 128);">20</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.ROLLING_FILE</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.RollingFileAppender<br />
</span><span style="color: rgb(0, 128, 128);">21</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.ROLLING_FILE.Threshold</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ERROR<br />
</span><span style="color: rgb(0, 128, 128);">22</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.ROLLING_FILE.File</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">rolling.log<br />
</span><span style="color: rgb(0, 128, 128);">23</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.ROLLING_FILE.Append</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">true<br />
</span><span style="color: rgb(0, 128, 128);">24</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.CONSOLE_FILE.Encoding</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">GBK<br />
</span><span style="color: rgb(0, 128, 128);">25</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.ROLLING_FILE.MaxFileSize</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">10KB<br />
</span><span style="color: rgb(0, 128, 128);">26</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.ROLLING_FILE.MaxBackupIndex</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">27</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.ROLLING_FILE.layout</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.PatternLayout<br />
</span><span style="color: rgb(0, 128, 128);">28</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.ROLLING_FILE.layout.ConversionPattern</span><span style="color: rgb(0, 0, 0);">=</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">framework</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%d&nbsp;-&nbsp;%c&nbsp;-%-4r&nbsp;</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">%t</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%-5p&nbsp;%c&nbsp;%x&nbsp;-&nbsp;%m%n<br />
</span><span style="color: rgb(0, 128, 128);">29</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#应用于socket<br />
</span><span style="color: rgb(0, 128, 128);">30</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.SOCKET</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.RollingFileAppender<br />
</span><span style="color: rgb(0, 128, 128);">31</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.SOCKET.RemoteHost</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">localhost<br />
</span><span style="color: rgb(0, 128, 128);">32</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.SOCKET.Port</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">5001</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">33</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.SOCKET.LocationInfo</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">true<br />
</span><span style="color: rgb(0, 128, 128);">34</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#&nbsp;Set&nbsp;up&nbsp;for&nbsp;Log&nbsp;Facter&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">35</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.SOCKET.layout</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.PatternLayout<br />
</span><span style="color: rgb(0, 128, 128);">36</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.SOCET.layout.ConversionPattern</span><span style="color: rgb(0, 0, 0);">=</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">start</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%d{DATE}</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">DATE</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%p</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">PRIORITY</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%x</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">NDC</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%t</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">THREAD</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%c</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">CATEGORY</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%m</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">MESSAGE</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">%n%n<br />
</span><span style="color: rgb(0, 128, 128);">37</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#&nbsp;Log&nbsp;Factor&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">&nbsp;Appender<br />
</span><span style="color: rgb(0, 128, 128);">38</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.LF5_APPENDER</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.lf5.LF5Appender<br />
</span><span style="color: rgb(0, 128, 128);">39</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.LF5_APPENDER.MaxNumberOfRecords</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">2000</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">40</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#&nbsp;发送日志给邮件<br />
</span><span style="color: rgb(0, 128, 128);">41</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.MAIL</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.net.SMTPAppender<br />
</span><span style="color: rgb(0, 128, 128);">42</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.MAIL.Threshold</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">FATAL<br />
</span><span style="color: rgb(0, 128, 128);">43</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.MAIL.BufferSize</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">44</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.MAIL.From</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">web@www.wuset.com<br />
</span><span style="color: rgb(0, 128, 128);">45</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.MAIL.SMTPHost</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">www.wusetu.com<br />
</span><span style="color: rgb(0, 128, 128);">46</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.MAIL.Subject</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">Log4J&nbsp;Message<br />
</span><span style="color: rgb(0, 128, 128);">47</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.MAIL.To</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">web@www.wusetu.com<br />
</span><span style="color: rgb(0, 128, 128);">48</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.MAIL.layout</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.PatternLayout<br />
</span><span style="color: rgb(0, 128, 128);">49</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.MAIL.layout.ConversionPattern</span><span style="color: rgb(0, 0, 0);">=</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">framework</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%d&nbsp;-&nbsp;%c&nbsp;-%-4r&nbsp;</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">%t</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%-5p&nbsp;%c&nbsp;%x&nbsp;-&nbsp;%m%n<br />
</span><span style="color: rgb(0, 128, 128);">50</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#&nbsp;用于数据库<br />
</span><span style="color: rgb(0, 128, 128);">51</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.DATABASE</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.jdbc.JDBCAppender<br />
</span><span style="color: rgb(0, 128, 128);">52</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.DATABASE.URL</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">jdbc:mysql://localhost:</span><span style="color: rgb(0, 0, 0);">3306</span><span style="color: rgb(0, 0, 0);">/test<br />
</span><span style="color: rgb(0, 128, 128);">53</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.DATABASE.driver</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">com.mysql.jdbc.Driver<br />
</span><span style="color: rgb(0, 128, 128);">54</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.DATABASE.user</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">root<br />
</span><span style="color: rgb(0, 128, 128);">55</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.DATABASE.password</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">56</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.DATABASE.sql</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">INSERT&nbsp;INTO&nbsp;LOG4J&nbsp;(Message)&nbsp;VALUES&nbsp;('</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">framework</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%d&nbsp;-&nbsp;%c&nbsp;-%-4r&nbsp;</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">%t</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%-5p&nbsp;%c&nbsp;%x&nbsp;-&nbsp;%m%n')<br />
</span><span style="color: rgb(0, 128, 128);">57</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.DATABASE.layout</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.PatternLayout<br />
</span><span style="color: rgb(0, 128, 128);">58</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.DATABASE.layout.ConversionPattern</span><span style="color: rgb(0, 0, 0);">=</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">framework</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%d&nbsp;-&nbsp;%c&nbsp;-%-4r&nbsp;</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">%t</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%-5p&nbsp;%c&nbsp;%x&nbsp;-&nbsp;%m%n<br />
</span><span style="color: rgb(0, 128, 128);">59</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#&nbsp;每天新建日志<br />
</span><span style="color: rgb(0, 128, 128);">60</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.A1</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.DailyRollingFileAppender<br />
</span><span style="color: rgb(0, 128, 128);">61</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.A1.File</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">log<br />
</span><span style="color: rgb(0, 128, 128);">62</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.A1.Encoding</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">GBK<br />
</span><span style="color: rgb(0, 128, 128);">63</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.A1.DatePattern</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">'.'yyyy-MM-dd<br />
</span><span style="color: rgb(0, 128, 128);">64</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.A1.layout</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.PatternLayout<br />
</span><span style="color: rgb(0, 128, 128);">65</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.A1.layout.ConversionPattern</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">%d{ABSOLUTE}&nbsp;%5p&nbsp;%c{</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">}:%L&nbsp;:&nbsp;%m%n<br />
</span><span style="color: rgb(0, 128, 128);">66</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />#自定义Appender<br />
</span><span style="color: rgb(0, 128, 128);">67</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.im&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;net.cybercorlin.util.logger.appender.IMAppender<br />
</span><span style="color: rgb(0, 128, 128);">68</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.im.host&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;mail.cybercorlin.net<br />
</span><span style="color: rgb(0, 128, 128);">69</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.im.username&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;username<br />
</span><span style="color: rgb(0, 128, 128);">70</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.im.password&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;password<br />
</span><span style="color: rgb(0, 128, 128);">71</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.im.recipient&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;corlin@cybercorlin.net<br />
</span><span style="color: rgb(0, 128, 128);">72</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.im.layout</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">org.apache.log4j.PatternLayout<br />
</span><span style="color: rgb(0, 128, 128);">73</span><span style="color: rgb(0, 0, 0);"><img alt="" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />log4j.appender.im.layout.ConversionPattern&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">framework</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%d&nbsp;-&nbsp;%c&nbsp;-%-4r&nbsp;</span><span style="font-weight: bold; color: rgb(128, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">%t</span><span style="font-weight: bold; color: rgb(128, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&nbsp;%-5p&nbsp;%c&nbsp;%x&nbsp;-&nbsp;%m%n</span></div>
<img src ="http://www.blogjava.net/aldreamlau/aggbug/159483.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aldreamlau/" target="_blank">aldream</a> 2007-11-10 00:43 <a href="http://www.blogjava.net/aldreamlau/articles/159483.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>