View Javadoc
1   /*
2    * #%L
3    * Nuiton Decorator
4    * %%
5    * Copyright (C) 2011 CodeLutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package org.nuiton.decorator;
23  
24  import org.junit.After;
25  import org.junit.Test;
26  
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.Comparator;
30  import java.util.List;
31  
32  import static org.junit.Assert.assertEquals;
33  
34  /**
35   * @author tchemit <chemit@codelutin.com>
36   * @since 2.3
37   */
38  public class JXPathDecoratorTest {
39  
40      protected JXPathDecorator<?> decorator;
41  
42      protected String expected;
43  
44      protected String result;
45  
46      @After
47      public void after() {
48          decorator = null;
49      }
50  
51      @Test(expected = NullPointerException.class)
52      public void testNullInternalClass() throws Exception {
53          decorator = DecoratorUtil.newJXPathDecorator(null, "hello");
54      }
55  
56      @Test(expected = IllegalArgumentException.class)
57      public void testMissingRightBrace() throws Exception {
58          decorator = DecoratorUtil.newJXPathDecorator(Object.class, "${haha");
59      }
60  
61      @Test(expected = IllegalArgumentException.class)
62      public void testMissingRightBrace2() throws Exception {
63          decorator = DecoratorUtil.newJXPathDecorator(Object.class, "${haha${hum}");
64      }
65  
66      @Test
67      public void testNullBean() throws Exception {
68          decorator = DecoratorUtil.newJXPathDecorator(Object.class, "hello");
69          expected = "hello";
70          assertEquals(expected, decorator.getExpression());
71          assertEquals(0, decorator.nbToken);
72          assertEquals(0, decorator.getTokens().length);
73  
74          result = decorator.toString(null);
75          assertEquals(null, result);
76      }
77  
78      @Test
79      public void testNoJXPath() throws Exception {
80          decorator = DecoratorUtil.newJXPathDecorator(Object.class, "hello");
81          expected = "hello";
82          assertEquals(expected, decorator.getExpression());
83          assertEquals(0, decorator.nbToken);
84          assertEquals(0, decorator.getTokens().length);
85  
86          result = decorator.toString(this);
87          assertEquals(expected, result);
88      }
89  
90      @Test
91      public void testDecorator() throws Exception {
92  
93          decorator = DecoratorUtil.newJXPathDecorator(
94                  JXPathDecorator.class, "${expression}$s - ${nbToken}$d");
95          assertEquals("%1$s - %2$d", decorator.getExpression());
96          assertDecoratorInternal();
97  
98          decorator = DecoratorUtil.newJXPathDecorator(
99                  JXPathDecorator.class, "${expression}${nbToken}");
100         assertEquals("%1%2", decorator.getExpression());
101         assertDecoratorInternal();
102 
103         decorator = DecoratorUtil.newJXPathDecorator(
104                 JXPathDecorator.class,
105                 "before ${expression}$s - ${nbToken}$d after");
106         assertEquals("before %1$s - %2$d after", decorator.getExpression());
107         assertDecoratorInternal();
108 
109         decorator = DecoratorUtil.newJXPathDecorator(
110                 JXPathDecorator.class,
111                 "before${expression}$s-${nbToken}$dafter");
112         assertEquals("before%1$s-%2$dafter", decorator.getExpression());
113         assertDecoratorInternal();
114     }
115 
116     @Test
117     public void testDecoratorEspcapeCharacters() throws Exception {
118 
119         decorator = DecoratorUtil.newJXPathDecorator(
120                 JXPathDecorator.class, "(${expression}$s) - ${nbToken}$d");
121         assertEquals("(%1$s) - %2$d", decorator.getExpression());
122         String s = decorator.toString(decorator);
123         assertDecoratorInternal();
124 
125     }
126 
127     @Test
128     public void testSort() throws Exception {
129 
130         List<Data> datas = Data.generate(10);
131 
132         JXPathDecorator<Data> d = DecoratorUtil.newJXPathDecorator(
133                 Data.class, "${pos}$d ${name}$s");
134 
135         List<Data> sortData = new ArrayList<Data>(datas);
136         DecoratorUtil.sort(d, sortData, 0);
137         for (int i = 0; i < datas.size(); i++) {
138             Data data = datas.get(i);
139             Data sData = sortData.get(i);
140             assertEquals(data, sData);
141         }
142         Collections.sort(datas, new Comparator<Data>() {
143             @Override
144             public int compare(Data o1, Data o2) {
145                 return o1.name.compareTo(o2.name);
146             }
147         });
148         JXPathDecorator.Context<Data> context = d.context;
149         context.setComparator(null);
150         DecoratorUtil.sort(d, sortData, 1);
151         for (int i = 0; i < datas.size(); i++) {
152             Data data = datas.get(i);
153             Data sData = sortData.get(i);
154             assertEquals(data, sData);
155         }
156     }
157 
158 
159     public void assertDecoratorInternal(String... tokens) {
160         assertTokens(tokens);
161         expected = String.format(decorator.getExpression(),
162                                  decorator.getExpression(),
163                                  decorator.getNbToken());
164         result = decorator.toString(decorator);
165         assertEquals(expected, result);
166     }
167 
168     private void assertTokens(String... tokens) {
169         if (tokens.length == 0) {
170             tokens = new String[]{"expression", "nbToken"};
171         }
172         assertEquals(2, decorator.nbToken);
173         assertEquals(2, decorator.getTokens().length);
174         assertEquals(tokens[0], decorator.getTokens()[0]);
175         assertEquals(tokens[1], decorator.getTokens()[1]);
176     }
177 
178 }