View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.tests.remoting.jaxws;
18  
19  //Checkstyle: EmptyBlock off
20  //Checkstyle: MagicNumber off
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  import static org.junit.Assert.fail;
33  
34  import ch.elca.el4j.tests.remoting.jaxws.service.Calculator;
35  import ch.elca.el4j.tests.remoting.jaxws.service.CalculatorException;
36  import ch.elca.el4j.tests.remoting.jaxws.service.CalculatorValueObject;
37  import ch.elca.el4j.tests.remoting.jaxws.service.SomeIntValue;
38  
39  /**
40   *
41   * This class is a test for JAX-WS using proxies on the generated classes
42   * to get the same interfaces as on the server.
43   *
44   * @svnLink $Revision: 4181 $;$Date: 2010-10-28 09:12:24 +0200 (Do, 28. Okt 2010) $;$Author: sstelca $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/tests/remoting_jaxws/functional-tests/src/test/java/ch/elca/el4j/tests/remoting/jaxws/CalculatorJaxwsTest.java $
45   *
46   * @author Philippe Jacot (PJA)
47   * @author Stefan Wismer (SWI)
48   */
49  public class CalculatorJaxwsTest extends AbstractJaxwsTest {
50  	/**
51  	 * Is the delta to doubles can have to be equal.
52  	 */
53  	private static final double DOUBLE_TOLERANCE = 0.000000001;
54  		
55  	/** {@inheritDoc} */
56  	@Override
57  	protected String[] getIncludeConfigLocations() {
58  		return new String[] {"classpath*:mandatory/*.xml",
59  			"classpath*:scenarios/db/raw/*.xml",
60  			"classpath*:scenarios/dataaccess/*.xml",
61  			"classpath*:scenarios/dataaccess/hibernate/*.xml",
62  			"classpath*:scenarios/dataaccess/hibernate/refdb/*.xml",
63  			"scenarios/client/remotingtests-jaxws-client-config.xml"};
64  	}
65  
66  	/**
67  	 * This test tests the area calculation method.
68  	 */
69  	@Test
70  	public void testAreaCalculation() {
71  		final double VALUE_A = 2.3;
72  		final double VALUE_B = 5.7;
73  		final double FAULT_DELTA = 0.00000001;
74  		double result = getCalc().getArea(VALUE_A, VALUE_B);
75  		assertEquals("The area is not correctly calculated.", result,
76  			VALUE_A * VALUE_B, FAULT_DELTA);
77  	}
78  	
79  	/**
80  	 * This test tests the exception handling.
81  	 */
82  	@Test
83  	public void testExceptionBehaviour() {
84  		// Checkstyle: EmptyBlock off
85  		try {
86  			getCalc().throwMeAnException();
87  			fail("No exception was thrown.");
88  		} catch (CalculatorException e) {
89  			// This is expected
90  		}
91  	}
92  	
93  	/**
94  	 * This test tests the counting of uppercase letters.
95  	 */
96  	@Test
97  	public void testNumberOfUppercaseCharacters() {
98  		String message = "Hans Müller likes to pay with Euro.";
99  		int numberOfUppercaseLetters = 3;
100 		int result = getCalc().countNumberOfUppercaseLetters(message);
101 		assertEquals("The number of uppercase letter was not "
102 			+ "counted correctly.", result, numberOfUppercaseLetters);
103 	}
104 	
105 	/**
106 	 * This test is used to test if a value object will be serialized and
107 	 * deserialized correctly.
108 	 */
109 	@Test
110 	public void testEchoOfValueObject() {
111 		final int MY_INT = 449312154;
112 		final long MY_LONG = 3121846575454654L;
113 		final double MY_DOUBLE = 6994.641368469;
114 		final String MY_STRING
115 			= "I can not find any ä, ö, ü, é, è or à character on my keyboard.";
116 		final byte[] MY_BYTE_ARRAY = MY_STRING.getBytes();
117 		
118 		CalculatorValueObject o = new CalculatorValueObject();
119 		o.setMyInt(MY_INT);
120 		o.setMyLong(MY_LONG);
121 		o.setMyDouble(MY_DOUBLE);
122 		o.setMyString(MY_STRING);
123 		o.setMyByteArray(MY_BYTE_ARRAY);
124 		
125 		// test int[]
126 		o.setMyIntArray(new int[] {9, 2});
127 		
128 		// test int[][]
129 		o.setMyIntMatrix(new int[][]{new int[] {5, 8}, new int[] {1, -4}});
130 		
131 		SomeIntValue v = new SomeIntValue(MY_INT);
132 		o.setSomeValue(v);
133 		
134 		List<SomeIntValue> intValues = new ArrayList<SomeIntValue>();
135 		intValues.add(v);
136 		o.setMyNestedObjectList(intValues);
137 		
138 		List<Integer> list = new ArrayList<Integer>();
139 		list.add(4);
140 		list.add(7);
141 		o.setMyIntegerList(list);
142 		
143 		Set<Integer> set = new HashSet<Integer>();
144 		set.add(2);
145 		set.add(5);
146 		o.setMyIntegerSet(set);
147 		
148 		CalculatorValueObject echo = getCalc().echoValueObject(o);
149 		
150 		assertEquals("Int values are not equal.",
151 			o.getMyInt(), echo.getMyInt());
152 		assertEquals("Long values are not equal.",
153 			o.getMyLong(), echo.getMyLong());
154 		assertEquals("Double values are not equal.",
155 			o.getMyDouble(), echo.getMyDouble(), DOUBLE_TOLERANCE);
156 		assertEquals("Strings are not equal.",
157 			o.getMyString(), echo.getMyString());
158 		assertTrue("Byte arrays are not equal.",
159 			Arrays.equals(o.getMyByteArray(), echo.getMyByteArray()));
160 		assertEquals("SomeIntValue are not equal.",
161 			o.getSomeValue().getSomeValue(),
162 			echo.getSomeValue().getSomeValue());
163 		
164 		assertEquals("MyNestedObjectList is not equal.",
165 			o.getMyNestedObjectList().get(0).getSomeValue(),
166 			echo.getMyNestedObjectList().get(0).getSomeValue());
167 		
168 		if (echo.getMyIntegerList().size() == list.size()) {
169 			assertTrue("List items are not equal.",
170 				echo.getMyIntegerList().get(0).equals(Integer.valueOf(4))
171 				&& echo.getMyIntegerList().get(1).equals(Integer.valueOf(7)));
172 		} else {
173 			fail("List size is not equal.");
174 		}
175 		
176 		if (echo.getMyIntegerSet().size() == set.size()) {
177 			assertTrue("Set items are not equal.",
178 				echo.getMyIntegerSet().contains(Integer.valueOf(2))
179 				&& echo.getMyIntegerSet().contains(Integer.valueOf(5)));
180 		} else {
181 			fail("Set size is not equal.");
182 		}
183 		
184 		assertTrue("int[] is not equal.",
185 			echo.getMyIntArray()[0] == 9
186 			&& echo.getMyIntArray()[1] == 2);
187 		
188 		assertTrue("int[][] is not equal.",
189 			echo.getMyIntMatrix()[0][0] == 5
190 			&& echo.getMyIntMatrix()[0][1] == 8
191 			&& echo.getMyIntMatrix()[1][0] == 1
192 			&& echo.getMyIntMatrix()[1][1] == -4);
193 	}
194 	
195 	/**
196 	 * Get the calculator to use.
197 	 * @return Calculator to use
198 	 */
199 	public Calculator getCalc() {
200 		return (Calculator) getApplicationContext().getBean("calculator");
201 	}
202 }