Skip to content

Commit 22e9e6f

Browse files
committed
powermock
1 parent 48373db commit 22e9e6f

File tree

6 files changed

+118
-7
lines changed

6 files changed

+118
-7
lines changed

pom.xml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
<version>1.0-SNAPSHOT</version>
1010

1111
<dependencies>
12+
<dependency>
13+
<groupId>org.apache.commons</groupId>
14+
<artifactId>commons-lang3</artifactId>
15+
<version>3.1</version>
16+
</dependency>
1217
<dependency>
1318
<groupId>junit</groupId>
1419
<artifactId>junit</artifactId>
@@ -19,17 +24,25 @@
1924
<groupId>org.hamcrest</groupId>
2025
<artifactId>hamcrest-all</artifactId>
2126
<version>1.3</version>
22-
</dependency>
23-
<dependency>
24-
<groupId>org.apache.commons</groupId>
25-
<artifactId>commons-lang3</artifactId>
26-
<version>3.1</version>
27+
<scope>test</scope>
2728
</dependency>
2829
<dependency>
2930
<groupId>org.mockito</groupId>
3031
<artifactId>mockito-all</artifactId>
3132
<version>1.9.5</version>
3233
</dependency>
34+
<dependency>
35+
<groupId>org.powermock</groupId>
36+
<artifactId>powermock-module-junit4</artifactId>
37+
<version>1.5.2</version>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.powermock</groupId>
42+
<artifactId>powermock-api-mockito</artifactId>
43+
<version>1.5.2</version>
44+
<scope>test</scope>
45+
</dependency>
3346
</dependencies>
3447

3548

src/main/java/helloworld/Main.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import helloworld.behavioral.command.Command;
77
import helloworld.behavioral.command.HelloWorldPrintCommand;
88
import helloworld.behavioral.interpreter.HelloWorldInterpreter;
9+
import helloworld.behavioral.iterator.HelloWorldCharacterIterator;
910
import helloworld.behavioral.mediator.HelloWorldMediator;
1011
import helloworld.behavioral.mediator.HelloWorldSlogan;
1112
import helloworld.behavioral.mediator.HelloWorldTarget;
@@ -93,6 +94,12 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
9394
HelloWorldInterpreter helloWorldInterpreter = new HelloWorldInterpreter();
9495
helloWorldInterpreter.interpret("println('Hello Interpreter!')");
9596

97+
HelloWorldCharacterIterator helloWorldCharacterIterator = new HelloWorldCharacterIterator("Hello Iterator!".toCharArray());
98+
while (helloWorldCharacterIterator.hasNext()){
99+
System.out.print(helloWorldCharacterIterator.next());
100+
}
101+
System.out.println();
102+
96103
HelloWorldSlogan helloWorldSlogan = new HelloWorldSlogan();
97104
HelloWorldTarget helloWorldTarget = new HelloWorldTarget();
98105
HelloWorldMediator helloWorldMediator = new HelloWorldMediator(helloWorldSlogan,helloWorldTarget);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package helloworld.behavioral.iterator;
2+
3+
import java.util.Iterator;
4+
import java.util.List;
5+
6+
/**
7+
* @author yihua.huang@dianping.com
8+
*/
9+
public class HelloWorldCharacterIterator implements Iterator<Character> {
10+
11+
private char[] characters;
12+
13+
private int size;
14+
15+
private int index;
16+
17+
public HelloWorldCharacterIterator(char[] characters) {
18+
this.characters = characters;
19+
this.size = characters.length;
20+
this.index = 0;
21+
}
22+
23+
@Override
24+
public boolean hasNext() {
25+
return index < size;
26+
}
27+
28+
@Override
29+
public Character next() {
30+
return characters[index++];
31+
}
32+
33+
@Override
34+
public void remove() {
35+
throw new UnsupportedOperationException("remove not support!");
36+
}
37+
}

src/main/java/helloworld/structural/composite/CompositeHelloWorld.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CompositeHelloWorld implements HelloWorld {
1414

1515
private List<HelloWorld> helloWorlds;
1616

17-
private String lineSeparetor = System.getProperty("line.separator");
17+
private String lineSeparator = System.getProperty("line.separator");
1818

1919
public CompositeHelloWorld(HelloWorld... helloWorlds) {
2020
this.helloWorlds = Arrays.asList(helloWorlds);
@@ -26,7 +26,7 @@ public String helloWorld() {
2626
for (HelloWorld helloWorld : helloWorlds) {
2727
helloWorldOuts.add(helloWorld.helloWorld());
2828
}
29-
return StringUtils.join(helloWorldOuts, lineSeparetor);
29+
return StringUtils.join(helloWorldOuts, lineSeparator);
3030
}
3131

3232
public static class DefaultHelloWorld implements HelloWorld{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package helloworld.behavioral;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.powermock.core.classloader.annotations.PrepareForTest;
6+
import org.powermock.modules.junit4.PowerMockRunner;
7+
8+
import java.io.PrintStream;
9+
10+
import static org.mockito.Mockito.*;
11+
12+
/**
13+
* @author yihua.huang@dianping.com
14+
*/
15+
public class PrinterTest {
16+
17+
@Test
18+
public void testPrinter() {
19+
Printer printer = Printer.instance();
20+
PrintStream mockPrintStream = mock(PrintStream.class);
21+
when(System.out).thenReturn(mockPrintStream);
22+
printer.println("Hello World!");
23+
verify(mockPrintStream,times(1)).println("Hello World!");
24+
}
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package helloworld.behavioral.iterator;
2+
3+
import org.junit.Test;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
import static org.hamcrest.Matchers.is;
6+
7+
/**
8+
* @author yihua.huang@dianping.com
9+
*/
10+
public class HelloWorldIteratorTest {
11+
12+
@Test
13+
public void testHelloWorldIterator(){
14+
final String helloIterator = "Hello Iterator!";
15+
HelloWorldCharacterIterator helloWorldCharacterIterator = new HelloWorldCharacterIterator(helloIterator.toCharArray());
16+
StringBuffer stringBuffer = new StringBuffer();
17+
while (helloWorldCharacterIterator.hasNext()){
18+
stringBuffer.append(helloWorldCharacterIterator.next());
19+
}
20+
assertThat(stringBuffer.toString(),is(helloIterator));
21+
}
22+
23+
@Test(expected = UnsupportedOperationException.class)
24+
public void testHelloWorldIteratorRemove(){
25+
final String helloIterator = "Hello Iterator!";
26+
HelloWorldCharacterIterator helloWorldCharacterIterator = new HelloWorldCharacterIterator(helloIterator.toCharArray());
27+
helloWorldCharacterIterator.remove();
28+
}
29+
}

0 commit comments

Comments
 (0)