-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Saeid Mirzaei
committed
Jul 28, 2014
1 parent
e703818
commit ffba4ce
Showing
10 changed files
with
251 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,47 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.saeidmirzaei</groupId> | ||
<groupId>org.sdco</groupId> | ||
<artifactId>webServiceMock</artifactId> | ||
<packaging>jar</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>webServiceMock</name> | ||
<url>http://maven.apache.org</url> | ||
<url>http://techblog.saeidmirzaei.com/?p=213</url> | ||
<properties> | ||
|
||
|
||
<cxf.version>2.2.3</cxf.version> | ||
|
||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<version>4.4</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-rt-frontend-jaxws</artifactId> | ||
<version>${cxf.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-rt-transports-http</artifactId> | ||
<version>${cxf.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-rt-transports-http-jetty</artifactId> | ||
<version>${cxf.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.8.4</version> | ||
</dependency> | ||
|
||
|
||
</dependencies> | ||
</project> |
2 changes: 1 addition & 1 deletion
2
...k/src/main/java/org/saeidmirzaei/App.java → ...ain/java/org/sdco/webservicemock/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.saeidmirzaei; | ||
package org.sdco.webservicemock; | ||
|
||
/** | ||
* Hello world! | ||
|
36 changes: 36 additions & 0 deletions
36
webServiceMock/src/main/java/org/sdco/webservicemock/BusinessClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.sdco.webservicemock; | ||
|
||
import org.apache.cxf.interceptor.LoggingInInterceptor; | ||
import org.apache.cxf.interceptor.LoggingOutInterceptor; | ||
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; | ||
|
||
|
||
// This class is going to call Hello World services | ||
public class BusinessClass { | ||
|
||
public void sayGreetings() { | ||
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); | ||
factory.getInInterceptors().add(new LoggingInInterceptor()); | ||
factory.getOutInterceptors().add(new LoggingOutInterceptor()); | ||
|
||
factory.setServiceClass(HelloWorld.class); | ||
|
||
factory.setAddress(TestGreeting.helloWorldUrl); | ||
HelloWorld client = (HelloWorld) factory.create(); | ||
|
||
|
||
// ask first name | ||
|
||
String firstName = client.askName("first"); | ||
String lastName = client.askName("last"); | ||
|
||
client.sayHi( firstName + " " + lastName); | ||
|
||
// we are freinds now, use only first name | ||
client.sayGoodbye( firstName); | ||
|
||
|
||
|
||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
webServiceMock/src/main/java/org/sdco/webservicemock/HelloWorld.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.sdco.webservicemock; | ||
|
||
import javax.jws.WebService; | ||
|
||
|
||
@WebService | ||
public interface HelloWorld { | ||
|
||
// Say the text after hi | ||
void sayHi(String text); | ||
|
||
// if part == "first", firstname, if "last" return lastname, Exception otherwise | ||
String askName(String part); | ||
|
||
|
||
// say the text after goodbye | ||
void sayGoodbye(String text); | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
webServiceMock/src/main/java/org/sdco/webservicemock/HelloWorldImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.sdco.webservicemock; | ||
|
||
public class HelloWorldImpl implements HelloWorld { | ||
|
||
@Override | ||
public void sayHi(String text) { | ||
System.out.println("************************************************"); | ||
System.out.println("hello " + text); | ||
|
||
} | ||
|
||
@Override | ||
public String askName(String part) { | ||
if (part.equals("first")) | ||
return "richard"; | ||
if (part.equals("last")) | ||
return "fineman"; | ||
throw new RuntimeException("not valid name part"); | ||
} | ||
|
||
@Override | ||
public void sayGoodbye(String text) { | ||
System.out.println("goodbye " + text); | ||
|
||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
webServiceMock/src/main/java/org/sdco/webservicemock/RunAll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.sdco.webservicemock; | ||
|
||
import javax.xml.ws.Endpoint; | ||
|
||
public class RunAll { | ||
public static void main(String[] args) { | ||
System.out.println("Starting Server"); | ||
HelloWorldImpl implementor = new HelloWorldImpl(); | ||
String address = "http://localhost:9000/helloWorld"; | ||
Endpoint.publish(address, implementor); | ||
BusinessClass businessClass = new BusinessClass(); | ||
businessClass.sayGreetings(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
webServiceMock/src/main/java/org/sdco/webservicemock/ServiceProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.sdco.webservicemock; | ||
|
||
|
||
|
||
import java.lang.reflect.Method; | ||
|
||
|
||
public class ServiceProxy implements java.lang.reflect.InvocationHandler { | ||
private Object obj; | ||
|
||
public static Object newInstance(Object obj) { | ||
return java.lang.reflect.Proxy.newProxyInstance( | ||
obj.getClass().getClassLoader(), | ||
obj.getClass().getInterfaces(), | ||
new ServiceProxy(obj)); | ||
} | ||
|
||
|
||
private ServiceProxy(Object obj) { | ||
this.obj = obj; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method m, Object[] args) | ||
throws Throwable { | ||
Object result; | ||
try { | ||
|
||
result = m.invoke(obj, args); | ||
} catch (Exception e) { | ||
throw new RuntimeException("unexpected invocation exception: " + | ||
e.getMessage()); | ||
} | ||
|
||
return result; | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
webServiceMock/src/test/java/org/saeidmirzaei/AppTest.java
This file was deleted.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
webServiceMock/src/test/java/org/sdco/webservicemock/TestGreeting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.sdco.webservicemock; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import javax.xml.ws.Endpoint; | ||
|
||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.InOrder; | ||
|
||
import static org.mockito.Mockito.reset; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.inOrder; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.reset; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
|
||
/** | ||
* Unit test for simple App. | ||
*/ | ||
public class TestGreeting | ||
{ | ||
final static String helloWorldUrl = "http://localhost:9000/helloWorldService"; | ||
static HelloWorld helloWorldServiceMock; | ||
static ArgumentCaptor<String> hiCaptor; | ||
static ArgumentCaptor<String> goodbyeCaptor; | ||
|
||
@Before | ||
public void startup() throws Exception { | ||
reset(helloWorldServiceMock); | ||
|
||
|
||
} | ||
@BeforeClass | ||
public static void startupClass() throws Exception { | ||
helloWorldServiceMock = mock(HelloWorld.class); | ||
|
||
|
||
HelloWorld helloWorldService = (HelloWorld) ServiceProxy.newInstance(helloWorldServiceMock); | ||
|
||
Endpoint helloWorldServiceEndpoint = Endpoint.publish(helloWorldUrl, helloWorldService); | ||
hiCaptor = ArgumentCaptor.forClass(String.class);; | ||
goodbyeCaptor = ArgumentCaptor.forClass(String.class);; | ||
|
||
} | ||
|
||
@Test | ||
public void TestGreetings() | ||
{ | ||
when(helloWorldServiceMock.askName("first")).thenReturn("richard"); | ||
when(helloWorldServiceMock.askName("last")).thenReturn("feynman"); | ||
|
||
BusinessClass businessClass = new BusinessClass(); | ||
businessClass.sayGreetings(); | ||
|
||
|
||
|
||
InOrder inOrder = inOrder(helloWorldServiceMock); | ||
|
||
// check that askName is called by "first" and "last" parementers in order | ||
inOrder.verify(helloWorldServiceMock).askName("first"); | ||
inOrder.verify(helloWorldServiceMock).askName("last"); | ||
inOrder.verify(helloWorldServiceMock).sayHi(hiCaptor.capture()); | ||
inOrder.verify(helloWorldServiceMock).sayGoodbye(goodbyeCaptor.capture()); | ||
|
||
assertEquals("richard feynman", hiCaptor.getValue()); | ||
assertEquals("richard", goodbyeCaptor.getValue()); | ||
|
||
|
||
verify(helloWorldServiceMock); | ||
|
||
|
||
|
||
} | ||
|
||
} |