View Javadoc
1   package org.codehaus.mojo.javancss;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  
24  import org.dom4j.Node;
25  import org.easymock.MockControl;
26  
27  /**
28   * Test for NumericNodeComparator class.
29   *
30   * @author <a href="jeanlaurent@gmail.com">Jean-Laurent de Morlhon</a>
31   */
32  public class NumericNodeComparatorTest
33      extends TestCase
34  {
35      private static final String NODE_PROPERTY = "foobar";
36  
37      private static final Integer SMALL_VALUE = new Integer( 10 );
38  
39      private static final Integer BIG_VALUE = new Integer( 42 );
40  
41      private MockControl control;
42  
43      private Node bigNodeMock;
44  
45      private Node smallNodeMock;
46  
47      private NumericNodeComparator nnc;
48  
49      public void setUp()
50      {
51          control = MockControl.createControl( Node.class );
52          nnc = new NumericNodeComparator( NODE_PROPERTY );
53          bigNodeMock = (Node) control.getMock();
54          smallNodeMock = (Node) control.getMock();
55      }
56  
57      public void testComparePositive()
58      {
59          bigNodeMock.numberValueOf( NODE_PROPERTY );
60          control.setReturnValue( BIG_VALUE );
61          smallNodeMock.numberValueOf( NODE_PROPERTY );
62          control.setReturnValue( SMALL_VALUE );
63          control.replay();
64          assertTrue( nnc.compare( smallNodeMock, bigNodeMock ) > 0 );
65          control.verify();
66      }
67  
68      public void testCompareNegative()
69      {
70          bigNodeMock.numberValueOf( NODE_PROPERTY );
71          control.setReturnValue( SMALL_VALUE );
72          smallNodeMock.numberValueOf( NODE_PROPERTY );
73          control.setReturnValue( BIG_VALUE );
74          control.replay();
75          assertTrue( nnc.compare( smallNodeMock, bigNodeMock ) < 0 );
76          control.verify();
77      }
78  
79      public void testCompareEqual()
80      {
81          bigNodeMock.numberValueOf( NODE_PROPERTY );
82          control.setReturnValue( SMALL_VALUE );
83          smallNodeMock.numberValueOf( NODE_PROPERTY );
84          control.setReturnValue( SMALL_VALUE );
85          control.replay();
86          assertEquals( 0, nnc.compare( smallNodeMock, bigNodeMock ) );
87          control.verify();
88      }
89  
90      // should throw npe whenever one of the node is null
91      public void testCompareWithBigNull()
92      {
93          smallNodeMock.numberValueOf( NODE_PROPERTY );
94          control.setReturnValue( SMALL_VALUE );
95          control.replay();
96          boolean caught = false;
97          try
98          {
99              nnc.compare( null, smallNodeMock );
100         }
101         catch ( NullPointerException npe )
102         {
103             caught = true;
104         }
105         assertTrue( caught );
106         control.verify();
107     }
108 
109     public void testCompareWithSmallNull()
110     {
111         bigNodeMock.numberValueOf( NODE_PROPERTY );
112         control.setReturnValue( BIG_VALUE, MockControl.ZERO_OR_MORE );
113         control.replay();
114         boolean caught = false;
115         try
116         {
117             nnc.compare( bigNodeMock, null );
118         }
119         catch ( NullPointerException npe )
120         {
121             caught = true;
122         }
123         assertTrue( caught );
124         control.verify();
125     }
126 
127 }