├── .gitignore
├── README.markdown
├── build
├── com.eclipsesource.restfuse.additional.feature
│ ├── .project
│ ├── build.properties
│ ├── feature.properties
│ ├── feature.xml
│ └── pom.xml
├── com.eclipsesource.restfuse.feature
│ ├── .project
│ ├── build.properties
│ ├── feature.properties
│ ├── feature.xml
│ ├── license.html
│ └── pom.xml
├── com.eclipsesource.restfuse.releng
│ ├── .project
│ ├── lib
│ │ ├── jacocoagent.jar
│ │ └── jacocoant.jar
│ ├── misc
│ │ ├── .project
│ │ ├── javadoc.xml
│ │ ├── license.txt
│ │ ├── maven-restfuse.txt
│ │ ├── mvn-submissions
│ │ │ ├── restfuse-maven-1.0.0.zip
│ │ │ ├── restfuse-maven-1.1.0.zip
│ │ │ ├── restfuse-maven-1.1.1.zip
│ │ │ └── restfuse-maven-1.2.0.zip
│ │ └── third-party-license-readme.txt
│ ├── pom.xml
│ ├── restfuse build.launch
│ └── scripts
│ │ ├── comp-repo.xml
│ │ ├── jacoco-ant.xml
│ │ └── repo-tool.sh
├── com.eclipsesource.restfuse.repository
│ ├── .project
│ ├── category.xml
│ └── pom.xml
└── com.eclipsesource.restfuse.target
│ ├── .project
│ ├── restfuse.1.0.0.target
│ ├── restfuse.1.1.0.target
│ └── restfuse.1.2.0.target
├── com.eclipsesource.restfuse.example
├── .classpath
├── .project
├── .settings
│ ├── org.eclipse.jdt.core.prefs
│ └── org.eclipse.pde.core.prefs
├── META-INF
│ └── MANIFEST.MF
├── build.properties
└── src
│ └── com
│ └── eclipsesource
│ └── restfuse
│ └── example
│ ├── AuthenticationTest.java
│ ├── DynamicHeaderTest.java
│ ├── DynamicPathTest.java
│ ├── PollTest.java
│ └── SimpleHttpTest.java
├── com.eclipsesource.restfuse.test
├── .classpath
├── .project
├── .settings
│ ├── org.eclipse.jdt.core.prefs
│ └── org.eclipse.pde.core.prefs
├── META-INF
│ └── MANIFEST.MF
├── build.properties
├── pom.xml
└── src
│ └── com
│ └── eclipsesource
│ └── restfuse
│ ├── Assert_Test.java
│ ├── CallbackResource_Test.java
│ ├── DefaultCallbackResource_Test.java
│ ├── Destination_Test.java
│ ├── HttpJUnitRunner_Test.java
│ ├── Poll_Test.java
│ ├── Status_Test.java
│ ├── internal
│ ├── AuthenticationInfo_Test.java
│ ├── HttpTestStatementOrder_Test.java
│ ├── HttpTestStatement_Test.java
│ ├── InternalRequest_Test.java
│ ├── RequestConfiguration_Test.java
│ ├── RequestContextConfiguration_Test.java
│ ├── RequestImpl_Test.java
│ ├── Response_Test.java
│ ├── callback
│ │ ├── CallbackServer_Test.java
│ │ └── CallbackServlet_Test.java
│ └── poll
│ │ └── PollStateImpl_Test.java
│ └── test
│ └── AllRestfuseTestSuite.java
└── com.eclipsesource.restfuse
├── .classpath
├── .project
├── .settings
├── org.eclipse.jdt.core.prefs
├── org.eclipse.jdt.ui.prefs
└── org.eclipse.pde.core.prefs
├── META-INF
└── MANIFEST.MF
├── about.html
├── build.properties
├── pom.xml
└── src
└── com
├── eclipsesource
└── restfuse
│ ├── Assert.java
│ ├── AuthenticationType.java
│ ├── CallbackResource.java
│ ├── DefaultCallbackResource.java
│ ├── Destination.java
│ ├── HttpJUnitRunner.java
│ ├── HttpOrderComparator.java
│ ├── MediaType.java
│ ├── Method.java
│ ├── PollState.java
│ ├── Request.java
│ ├── RequestContext.java
│ ├── Response.java
│ ├── Status.java
│ ├── annotation
│ ├── Authentication.java
│ ├── Callback.java
│ ├── Context.java
│ ├── Header.java
│ ├── HttpTest.java
│ └── Poll.java
│ └── internal
│ ├── AuthenticationInfo.java
│ ├── BasicStatement.java
│ ├── HttpTestStatement.java
│ ├── InternalRequest.java
│ ├── RequestConfiguration.java
│ ├── RequestImpl.java
│ ├── ResponseImpl.java
│ ├── callback
│ ├── CallbackSerlvet.java
│ ├── CallbackServer.java
│ └── CallbackStatement.java
│ └── poll
│ ├── PollStateImpl.java
│ └── PollStatement.java
└── github
└── kevinsawicki
└── http
└── HttpRequest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | bin
2 | target
3 | .DS_Store
4 |
--------------------------------------------------------------------------------
/README.markdown:
--------------------------------------------------------------------------------
1 | *Please note: This project is no longer maintained. Resources outside of the GitHub repository have been archived.*
2 |
3 | **restfuse**
4 | An open-source [JUnit](http://junit.org) extension for testing HTTP/REST services. Restfuse can also be used to test [asynchronous services](http://developer.eclipsesource.com/restfuse/).
5 |
6 | **Website**
7 | Checkout http://developer.eclipsesource.com/restfuse/
8 |
9 | **License**
10 | All files are licensed under the [Eclipse Public License - v 1.0](http://www.eclipse.org/legal/epl-v10.html)
11 |
12 | **Documentation**
13 | Checkout http://developer.eclipsesource.com/restfuse/docs/
14 |
15 | **Download**
16 | Checkout http://developer.eclipsesource.com/restfuse/downloads/
17 |
18 | **FAQ**
19 | Checkout https://github.com/eclipsesource/restfuse/wiki/FAQ
20 |
21 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.additional.feature/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.eclipsesource.restfuse.additional.feature
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.pde.FeatureBuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.pde.FeatureNature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.additional.feature/build.properties:
--------------------------------------------------------------------------------
1 | bin.includes = feature.xml,\
2 | feature.properties
3 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.additional.feature/feature.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 | %description
10 |
11 |
12 |
13 | %copyright
14 |
15 |
16 |
17 | %license
18 |
19 |
20 |
25 |
26 |
32 |
33 |
39 |
40 |
46 |
47 |
53 |
54 |
60 |
61 |
67 |
68 |
74 |
75 |
81 |
82 |
88 |
89 |
95 |
96 |
102 |
103 |
109 |
110 |
111 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.additional.feature/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.eclipsesource
7 | restfuse
8 | 1.0.0-SNAPSHOT
9 | ../com.eclipsesource.restfuse.releng
10 |
11 |
12 | com.eclipsesource
13 | com.eclipsesource.restfuse.additional.feature
14 | 1.2.0-SNAPSHOT
15 | eclipse-feature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.feature/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.eclipsesource.restfuse.feature
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.pde.FeatureBuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.pde.FeatureNature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.feature/build.properties:
--------------------------------------------------------------------------------
1 | bin.includes = feature.xml,\
2 | feature.properties,\
3 | license.html
4 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.feature/feature.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 | %description
10 |
11 |
12 |
13 | %copyright
14 |
15 |
16 |
17 | %license
18 |
19 |
20 |
23 |
24 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.feature/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.eclipsesource
7 | restfuse
8 | 1.0.0-SNAPSHOT
9 | ../com.eclipsesource.restfuse.releng
10 |
11 |
12 | com.eclipsesource
13 | com.eclipsesource.restfuse.feature
14 | 1.2.0-SNAPSHOT
15 | eclipse-feature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.eclipsesource.restfuse.releng
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/lib/jacocoagent.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eclipsesource/restfuse/81907ee261bb05f1857aa659bccf6346d9b4c597/build/com.eclipsesource.restfuse.releng/lib/jacocoagent.jar
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/lib/jacocoant.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eclipsesource/restfuse/81907ee261bb05f1857aa659bccf6346d9b4c597/build/com.eclipsesource.restfuse.releng/lib/jacocoant.jar
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/misc/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.eclipsesource.restfuse.releng
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/misc/javadoc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/misc/maven-restfuse.txt:
--------------------------------------------------------------------------------
1 |
2 | 1) Check localy
3 | mvn install:install-file -Dfile=com.eclipsesource.restfuse_1.1.1.201208301350.jar -DgroupId=com.restfuse -DartifactId=com.eclipsesource.restfuse -Dversion=1.1.1 -Dpackaging=jar -DpomFile=com.eclipsesource.restfuse_1.1.1.201208301350.xml
4 |
5 | 2) Upload to Nexus:
6 | mvn gpg:sign-and-deploy-file -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=sonatype-nexus-staging -DpomFile=com.eclipsesource.restfuse_1.1.1.201208301350.xml -Dfile=com.eclipsesource.restfuse_1.1.1.201208301350.jar
7 |
8 | mvn gpg:sign-and-deploy-file -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=sonatype-nexus-staging -DpomFile=com.eclipsesource.restfuse_1.1.1.201208301350.xml -Dfile=com.eclipsesource.restfuse_1.1.1.201208301350-sources.jar -Dclassifier=sources
9 |
10 | mvn gpg:sign-and-deploy-file -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=sonatype-nexus-staging -DpomFile=com.eclipsesource.restfuse_1.1.1.201208301350.xml -Dfile=com.eclipsesource.restfuse_1.1.1.201208301350-javadoc.jar -Dclassifier=javadoc
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/misc/mvn-submissions/restfuse-maven-1.0.0.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eclipsesource/restfuse/81907ee261bb05f1857aa659bccf6346d9b4c597/build/com.eclipsesource.restfuse.releng/misc/mvn-submissions/restfuse-maven-1.0.0.zip
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/misc/mvn-submissions/restfuse-maven-1.1.0.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eclipsesource/restfuse/81907ee261bb05f1857aa659bccf6346d9b4c597/build/com.eclipsesource.restfuse.releng/misc/mvn-submissions/restfuse-maven-1.1.0.zip
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/misc/mvn-submissions/restfuse-maven-1.1.1.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eclipsesource/restfuse/81907ee261bb05f1857aa659bccf6346d9b4c597/build/com.eclipsesource.restfuse.releng/misc/mvn-submissions/restfuse-maven-1.1.1.zip
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/misc/mvn-submissions/restfuse-maven-1.2.0.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eclipsesource/restfuse/81907ee261bb05f1857aa659bccf6346d9b4c597/build/com.eclipsesource.restfuse.releng/misc/mvn-submissions/restfuse-maven-1.2.0.zip
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 |
8 | 3.0
9 |
10 |
11 | com.eclipsesource
12 | restfuse
13 | 1.0.0-SNAPSHOT
14 | pom
15 |
16 | restfuse
17 |
18 |
19 | UTF-8
20 | 0.4.0
21 | 0.16.0
22 | 0.16.0
23 | org.eclipse.tycho
24 | 2.12
25 | 4.10
26 | 1.9.0
27 | 2.5.1
28 | ${basedir}/../build/com.eclipsesource.restfuse.releng/lib/jacocoagent.jar
29 | -Xms512m -Xmx512m
30 | -javaagent:${jacocoagent}=destfile=${basedir}/../build/com.eclipsesource.restfuse.releng//target/jacoco.exec,append=true
31 | http://download.eclipsesource.com/technology/restfuse/p2/
32 |
33 |
34 |
35 | ../../com.eclipsesource.restfuse
36 | ../../com.eclipsesource.restfuse.test
37 | ../com.eclipsesource.restfuse.feature
38 | ../com.eclipsesource.restfuse.additional.feature
39 | ../com.eclipsesource.restfuse.repository
40 |
41 |
42 |
50 |
51 |
52 |
53 |
54 | ${tycho-groupid}
55 | tycho-maven-plugin
56 | ${tycho-version}
57 | true
58 |
59 |
60 |
61 | ${tycho-groupid}
62 | tycho-compiler-plugin
63 | ${tycho-version}
64 |
65 | ${project.build.sourceEncoding}
66 |
67 |
68 |
69 |
70 | ${tycho-groupid}
71 | tycho-source-plugin
72 | ${tycho-version}
73 |
74 |
75 | plugin-source
76 |
77 | plugin-source
78 |
79 |
80 |
81 |
82 |
83 |
84 | ${tycho-groupid}
85 | target-platform-configuration
86 | ${tycho-version}
87 |
88 | p2
89 | true
90 |
91 |
92 | ${project.groupId}
93 | ${project.artifactId}
94 | ${project.version}
95 | ${project.parent.relativePath}/../com.eclipsesource.restfuse.target/restfuse.1.2.0
96 |
97 |
98 |
99 |
100 | linux
101 | gtk
102 | x86
103 |
104 |
105 |
106 |
107 |
108 | p2-installable-unit
109 | org.eclipse.equinox.servletbridge.extensionbundle
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 | reproducible-qualifiers
122 |
123 | false
124 |
125 | fullBuild
126 | true
127 |
128 |
129 |
130 |
131 |
132 | ${tycho-groupid}
133 | tycho-packaging-plugin
134 | ${tycho-version}
135 |
136 |
137 | org.eclipse.tycho.extras
138 | tycho-buildtimestamp-jgit
139 | ${tycho-extras-version}
140 |
141 |
142 |
143 | jgit
144 |
145 | pom.xml
146 |
147 | yyyyMMdd-HHmm
148 |
149 |
150 |
151 |
152 | ${tycho-groupid}
153 | tycho-p2-plugin
154 | ${tycho-version}
155 |
156 |
157 |
158 | ${baseline.repository}
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/restfuse build.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/scripts/comp-repo.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/scripts/jacoco-ant.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.releng/scripts/repo-tool.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | #
3 | # Tool to maintain composite repositories
4 |
5 | SCRIPTS_DIR=/Users/holger/dev/gitRepositories/restfuse/com.eclipsesource.restfuse.releng
6 | DOWNLOAD_DIR=/Users/holger/dev/gitRepositories/restfuse-gh-pages
7 |
8 | if [ -z "$RUNTIME_DIR" ]; then
9 | RUNTIME_DIR=/Applications/eclipse/eclipse-3.6.2
10 | fi
11 |
12 | mode=
13 | repoDir=
14 | repoName=
15 |
16 | fail() {
17 | echo Composite Repository Tool
18 | if [ $# -gt 0 ]; then
19 | echo "Error: $1"
20 | fi
21 | echo "Usage:"
22 | echo " $0 repo-dir create "
23 | echo " $0 repo-dir add "
24 | echo " $0 repo-dir remove "
25 | echo
26 | echo "Example:"
27 | echo " $0 1.4/runtime create \"RAP 1.4 Runtime Repository\""
28 | echo " $0 1.4/runtime add M1"
29 | exit 1
30 | }
31 |
32 | # Check command line
33 | if [ $# -ne 3 ]; then
34 | fail "Wrong # of paramters"
35 | fi
36 |
37 | repoDir=$1
38 | if [ ${repoDir:0:1} != "/" ]; then
39 | repoDir="$DOWNLOAD_DIR/$repoDir"
40 | fi
41 | if [ ! -d "$repoDir" ]; then
42 | fail "Repository does not exist: $repoDir"
43 | fi
44 |
45 | mode=$2
46 | if [ "$mode" == "create" ]; then
47 | repoName=$3
48 | elif [ "$mode" == "add" -o "$mode" == "remove" ]; then
49 | repoName=$3
50 | if [ ${repoName:0:7} != "http://" -a ! -d "$repoDir/$repoName" ]; then
51 | fail "Child to add/remove does not exist: $repoDir/$repoName"
52 | fi
53 | else
54 | fail "Illegal mode: $mode"
55 | fi
56 |
57 | # Find PDE build
58 | pdeBuild=`ls -1 $RUNTIME_DIR/plugins | grep pde.build_ | tail -n 1`
59 | echo "Using PDE Build: $pdeBuild"
60 |
61 | # Find Equinox launcher
62 | launcher=$RUNTIME_DIR/plugins/`ls -1 $RUNTIME_DIR/plugins | grep launcher_ | tail -n 1`
63 | echo "Using Equinox launcher: $launcher"
64 |
65 | java -cp $launcher org.eclipse.core.launcher.Main \
66 | -application org.eclipse.ant.core.antRunner \
67 | -buildfile "$SCRIPTS_DIR/comp-repo.xml" \
68 | -DrepoDir="$repoDir" \
69 | -DrepoName="$repoName" \
70 | $mode \
71 | || fail
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.repository/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.eclipsesource.restfuse.repository
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.repository/category.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | An open-source JUnit extension for automated HTTP/REST Tests.
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.repository/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.eclipsesource
7 | restfuse
8 | 1.0.0-SNAPSHOT
9 | ../com.eclipsesource.restfuse.releng
10 |
11 |
12 | com.eclipsesource
13 | com.eclipsesource.restfuse.repository
14 | eclipse-repository
15 |
16 |
17 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.target/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.eclipsesource.restfuse.target
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.target/restfuse.1.0.0.target:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.target/restfuse.1.1.0.target:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/build/com.eclipsesource.restfuse.target/restfuse.1.2.0.target:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.eclipsesource.restfuse.example
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.pde.ManifestBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.pde.SchemaBuilder
20 |
21 |
22 |
23 |
24 |
25 | org.eclipse.pde.PluginNature
26 | org.eclipse.jdt.core.javanature
27 |
28 |
29 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | #Fri Oct 28 20:59:09 CEST 2011
2 | eclipse.preferences.version=1
3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5 | org.eclipse.jdt.core.compiler.compliance=1.6
6 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
7 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
8 | org.eclipse.jdt.core.compiler.source=1.6
9 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/.settings/org.eclipse.pde.core.prefs:
--------------------------------------------------------------------------------
1 | #Fri Oct 28 20:59:09 CEST 2011
2 | eclipse.preferences.version=1
3 | pluginProject.equinox=false
4 | pluginProject.extensions=false
5 | resolve.requirebundle=false
6 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Bundle-ManifestVersion: 2
3 | Bundle-Name: Restfuse Examples
4 | Bundle-SymbolicName: com.eclipsesource.restfuse.example
5 | Bundle-Version: 1.0.0.qualifier
6 | Bundle-Vendor: EclipseSource
7 | Bundle-RequiredExecutionEnvironment: JavaSE-1.6
8 | Require-Bundle: org.junit;bundle-version="[4.8.2,5.0.0)",
9 | com.eclipsesource.restfuse
10 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/build.properties:
--------------------------------------------------------------------------------
1 | source.. = src/
2 | output.. = bin/
3 | bin.includes = META-INF/,\
4 | .
5 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/src/com/eclipsesource/restfuse/example/AuthenticationTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2012 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.example;
12 |
13 | import static com.eclipsesource.restfuse.Assert.assertUnauthorized;
14 | import static com.eclipsesource.restfuse.AuthenticationType.BASIC;
15 |
16 | import org.junit.Rule;
17 | import org.junit.runner.RunWith;
18 |
19 | import com.eclipsesource.restfuse.Destination;
20 | import com.eclipsesource.restfuse.HttpJUnitRunner;
21 | import com.eclipsesource.restfuse.Method;
22 | import com.eclipsesource.restfuse.Response;
23 | import com.eclipsesource.restfuse.annotation.Authentication;
24 | import com.eclipsesource.restfuse.annotation.Context;
25 | import com.eclipsesource.restfuse.annotation.HttpTest;
26 |
27 | @RunWith( HttpJUnitRunner.class )
28 | public class AuthenticationTest {
29 |
30 | @Rule
31 | public Destination destination = new Destination( this, "https://eclipsesource.com" );
32 |
33 | @Context
34 | private Response response;
35 |
36 | @HttpTest( method = Method.GET, path = "/blogs/wp_admin" )
37 | public void testAuthentication() {
38 | assertUnauthorized( response );
39 | }
40 |
41 | @HttpTest(
42 | method = Method.GET,
43 | path = "/blogs/wp_admin",
44 | authentications = { @Authentication( type = BASIC, user = "invalid", password = "invalid" ) }
45 | )
46 | public void testAuthenticationWithInvalidCredentials() {
47 | assertUnauthorized( response );
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/src/com/eclipsesource/restfuse/example/DynamicHeaderTest.java:
--------------------------------------------------------------------------------
1 | package com.eclipsesource.restfuse.example;
2 |
3 | import static com.eclipsesource.restfuse.Assert.assertOk;
4 |
5 | import org.junit.Rule;
6 | import org.junit.runner.RunWith;
7 |
8 | import com.eclipsesource.restfuse.Destination;
9 | import com.eclipsesource.restfuse.HttpJUnitRunner;
10 | import com.eclipsesource.restfuse.Method;
11 | import com.eclipsesource.restfuse.Response;
12 | import com.eclipsesource.restfuse.annotation.Context;
13 | import com.eclipsesource.restfuse.annotation.HttpTest;
14 |
15 | @RunWith(HttpJUnitRunner.class)
16 | public class DynamicHeaderTest {
17 |
18 | @Rule
19 | public Destination restfuse = getDestination();
20 | @Context
21 | private Response response;
22 |
23 | @HttpTest(method = Method.GET, path = "/")
24 | public void checkRestfuseOnlineStatus() {
25 | assertOk( response );
26 | }
27 |
28 | private Destination getDestination() {
29 | Destination destination = new Destination( this, "http://restfuse.com" );
30 | destination.getRequestContext().addHeader( "Cookie", "name:value" );
31 | return destination;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/src/com/eclipsesource/restfuse/example/DynamicPathTest.java:
--------------------------------------------------------------------------------
1 | package com.eclipsesource.restfuse.example;
2 |
3 | import static com.eclipsesource.restfuse.Assert.assertOk;
4 |
5 | import org.junit.Rule;
6 | import org.junit.runner.RunWith;
7 |
8 | import com.eclipsesource.restfuse.Destination;
9 | import com.eclipsesource.restfuse.HttpJUnitRunner;
10 | import com.eclipsesource.restfuse.Method;
11 | import com.eclipsesource.restfuse.RequestContext;
12 | import com.eclipsesource.restfuse.Response;
13 | import com.eclipsesource.restfuse.annotation.Context;
14 | import com.eclipsesource.restfuse.annotation.HttpTest;
15 |
16 | @RunWith(HttpJUnitRunner.class)
17 | public class DynamicPathTest {
18 |
19 | @Rule
20 | public Destination restfuse = getDestination();
21 |
22 | @Context
23 | private Response response;
24 |
25 | @HttpTest(method = Method.GET, path = "/{file}.jar")
26 | public void checkRestfuseDownloadJarStatus() {
27 | assertOk( response );
28 | }
29 |
30 | @HttpTest(method = Method.GET, path = "/{file}-javadoc.jar")
31 | public void checkRestfuseDownloadDocsStatus() {
32 | assertOk( response );
33 | }
34 |
35 | private Destination getDestination() {
36 | Destination destination = new Destination( this,
37 | "http://search.maven.org/remotecontent?filepath="
38 | + "com/restfuse/com.eclipsesource.restfuse/{version}/" );
39 | RequestContext context = destination.getRequestContext();
40 | context.addPathSegment( "file", "com.eclipsesource.restfuse-1.1.1" ).addPathSegment( "version", "1.1.1" );
41 | return destination;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/src/com/eclipsesource/restfuse/example/PollTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.example;
12 |
13 | import static com.eclipsesource.restfuse.Assert.assertOk;
14 |
15 | import org.junit.Rule;
16 | import org.junit.runner.RunWith;
17 |
18 | import com.eclipsesource.restfuse.Destination;
19 | import com.eclipsesource.restfuse.HttpJUnitRunner;
20 | import com.eclipsesource.restfuse.Method;
21 | import com.eclipsesource.restfuse.PollState;
22 | import com.eclipsesource.restfuse.Response;
23 | import com.eclipsesource.restfuse.annotation.Context;
24 | import com.eclipsesource.restfuse.annotation.Header;
25 | import com.eclipsesource.restfuse.annotation.HttpTest;
26 | import com.eclipsesource.restfuse.annotation.Poll;
27 |
28 |
29 | @RunWith( HttpJUnitRunner.class )
30 | public class PollTest {
31 |
32 | @Rule
33 | public Destination restfuse = new Destination( this, "http://restfuse.com" );
34 |
35 | @Context
36 | private PollState pollState;
37 |
38 | @Context
39 | private Response response;
40 |
41 | @HttpTest( method = Method.GET, path = "/" )
42 | @Poll( times = 3, interval = 2000 )
43 | public void checkRestfuseOnlineStatus() {
44 | System.out.println( "Attemt " + pollState.getTimes() );
45 | System.out.println( pollState.getTimes() + ". Responsecode = " + pollState.getResponse( pollState.getTimes() ).getStatus() );
46 | }
47 |
48 | @HttpTest( method = Method.GET,
49 | path = "/",
50 | headers = { @Header( name = "Accepted-Language", value = "en-en" ) } )
51 | public void checkRestfuseOnlineStatusWithHeader() {
52 | assertOk( response );
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.example/src/com/eclipsesource/restfuse/example/SimpleHttpTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.example;
12 |
13 | import static com.eclipsesource.restfuse.Assert.assertOk;
14 |
15 | import org.junit.Rule;
16 | import org.junit.runner.RunWith;
17 |
18 | import com.eclipsesource.restfuse.Destination;
19 | import com.eclipsesource.restfuse.HttpJUnitRunner;
20 | import com.eclipsesource.restfuse.Method;
21 | import com.eclipsesource.restfuse.Response;
22 | import com.eclipsesource.restfuse.annotation.Context;
23 | import com.eclipsesource.restfuse.annotation.HttpTest;
24 |
25 |
26 | @RunWith( HttpJUnitRunner.class )
27 | public class SimpleHttpTest {
28 |
29 | @Rule
30 | public Destination restfuse = new Destination( this, "http://restfuse.com" );
31 |
32 | @Context
33 | private Response response;
34 |
35 | @HttpTest( method = Method.GET, path = "/" )
36 | public void checkRestfuseOnlineStatus() {
37 | assertOk( response );
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.eclipsesource.restfuse.test
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.pde.ManifestBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.pde.SchemaBuilder
20 |
21 |
22 |
23 |
24 |
25 | org.eclipse.pde.PluginNature
26 | org.eclipse.jdt.core.javanature
27 |
28 |
29 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | #Thu Oct 20 13:01:05 CEST 2011
2 | eclipse.preferences.version=1
3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5 | org.eclipse.jdt.core.compiler.compliance=1.6
6 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
7 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
8 | org.eclipse.jdt.core.compiler.source=1.6
9 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/.settings/org.eclipse.pde.core.prefs:
--------------------------------------------------------------------------------
1 | #Thu Oct 20 13:01:05 CEST 2011
2 | eclipse.preferences.version=1
3 | pluginProject.equinox=false
4 | pluginProject.extensions=false
5 | resolve.requirebundle=false
6 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Bundle-ManifestVersion: 2
3 | Bundle-Name: restfuse tests
4 | Bundle-SymbolicName: com.eclipsesource.restfuse.test
5 | Bundle-Version: 1.2.0.qualifier
6 | Bundle-Vendor: EclipseSource
7 | Fragment-Host: com.eclipsesource.restfuse;bundle-version="1.2.0"
8 | Bundle-RequiredExecutionEnvironment: JavaSE-1.6
9 | Require-Bundle: org.junit;bundle-version="[4.9.0,5.0.0)",
10 | org.mockito.mockito-all;bundle-version="1.9.0"
11 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/build.properties:
--------------------------------------------------------------------------------
1 | source.. = src/
2 | output.. = bin/
3 | bin.includes = META-INF/,\
4 | .
5 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.eclipsesource
7 | restfuse
8 | 1.0.0-SNAPSHOT
9 | ../build/com.eclipsesource.restfuse.releng
10 |
11 |
12 | com.eclipsesource
13 | com.eclipsesource.restfuse.test
14 | 1.2.0-SNAPSHOT
15 | eclipse-plugin
16 |
17 |
18 |
19 |
20 | org.apache.maven.plugins
21 | maven-surefire-plugin
22 | ${surefire-version}
23 |
24 |
25 | test
26 | test
27 |
28 | ${project.build.outputDirectory}
29 | ${test-arguments}
30 |
31 |
32 | test
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 | junit
43 | junit
44 | ${junit-version}
45 | test
46 |
47 |
48 | org.mockito
49 | mockito-core
50 | ${mockito-version}
51 | test
52 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/CallbackResource_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import static org.junit.Assert.assertEquals;
14 | import static org.junit.Assert.assertFalse;
15 | import static org.junit.Assert.assertNotSame;
16 | import static org.junit.Assert.assertNull;
17 | import static org.junit.Assert.assertTrue;
18 |
19 | import java.util.HashMap;
20 | import java.util.List;
21 | import java.util.Map;
22 |
23 | import org.junit.Test;
24 |
25 |
26 | public class CallbackResource_Test {
27 |
28 | private static final String URL = "http://localhost";
29 |
30 | @Test
31 | public void testCreateResponse() {
32 | Map> headers = new HashMap>();
33 |
34 | Response response
35 | = CallbackResource.createResponse( URL, Status.OK, MediaType.WILDCARD, "body", headers );
36 |
37 | assertEquals( Status.OK.getStatusCode(), response.getStatus() );
38 | assertEquals( MediaType.WILDCARD, response.getType() );
39 | assertEquals( "body", response.getBody() );
40 | assertEquals( headers, response.getHeaders() );
41 | }
42 |
43 | @Test
44 | public void testCreateResponseCreatesHeaderSaveCopy() {
45 | Map> headers = new HashMap>();
46 |
47 | Response response
48 | = CallbackResource.createResponse( URL, Status.OK, MediaType.WILDCARD, "body", headers );
49 |
50 | assertNotSame( headers, response.getHeaders() );
51 | }
52 |
53 | @Test
54 | public void testCreateResponseHasBody() {
55 | Map> headers = new HashMap>();
56 |
57 | Response response
58 | = CallbackResource.createResponse( URL, Status.OK, MediaType.WILDCARD, "body", headers );
59 |
60 | assertTrue( response.hasBody() );
61 | }
62 |
63 | @Test
64 | public void testCreateResponseHasNoBody() {
65 | Response response
66 | = CallbackResource.createResponse( URL, Status.OK, MediaType.WILDCARD, null, null );
67 |
68 | assertFalse( response.hasBody() );
69 | }
70 |
71 | @Test
72 | public void testCreateResponseHasNoHeaders() {
73 | Response response
74 | = CallbackResource.createResponse( URL, Status.OK, MediaType.WILDCARD, null, null );
75 |
76 | assertNull( response.getHeaders() );
77 | }
78 |
79 | @Test
80 | public void testCreateResponseCreatesNewInstance() {
81 | Map> headers = new HashMap>();
82 |
83 | Response response
84 | = CallbackResource.createResponse( URL, Status.OK, MediaType.WILDCARD, "body", headers );
85 | Response response2
86 | = CallbackResource.createResponse( URL, Status.OK, MediaType.WILDCARD, "body", headers );
87 |
88 | assertNotSame( response, response2 );
89 | }
90 |
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/DefaultCallbackResource_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import static com.eclipsesource.restfuse.Assert.assertNoContent;
14 | import static org.junit.Assert.assertEquals;
15 | import static org.junit.Assert.assertFalse;
16 | import static org.junit.Assert.assertNull;
17 |
18 | import org.junit.Before;
19 | import org.junit.Test;
20 |
21 |
22 | public class DefaultCallbackResource_Test {
23 |
24 | DefaultCallbackResource resource;
25 |
26 | @Before
27 | public void setUp() {
28 | resource = new DefaultCallbackResource();
29 | }
30 |
31 | @Test
32 | public void testGetCreatesDefaultResource() {
33 | Response response = resource.get( null );
34 |
35 | checkResponse( response );
36 | }
37 |
38 | @Test
39 | public void testPutCreatesDefaultResource() {
40 | Response response = resource.put( null );
41 |
42 | checkResponse( response );
43 | }
44 |
45 | @Test
46 | public void testDeleteCreatesDefaultResource() {
47 | Response response = resource.delete( null );
48 |
49 | checkResponse( response );
50 | }
51 |
52 | @Test
53 | public void testHeadCreatesDefaultResource() {
54 | Response response = resource.head( null );
55 |
56 | checkResponse( response );
57 | }
58 |
59 | @Test
60 | public void testOptionsCreatesDefaultResource() {
61 | Response response = resource.options( null );
62 |
63 | checkResponse( response );
64 | }
65 |
66 | @Test
67 | public void testPostCreatesDefaultResource() {
68 | Response response = resource.post( null );
69 |
70 | checkResponse( response );
71 | }
72 |
73 | private void checkResponse( Response response ) {
74 | assertNoContent( response );
75 | assertEquals( MediaType.WILDCARD, response.getType() );
76 | assertNull( response.getHeaders() );
77 | assertNull( response.getBody() );
78 | assertFalse( response.hasBody() );
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/Destination_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import static org.junit.Assert.assertEquals;
14 | import static org.junit.Assert.assertTrue;
15 | import static org.mockito.Mockito.mock;
16 |
17 | import org.junit.Before;
18 | import org.junit.Rule;
19 | import org.junit.Test;
20 | import org.junit.rules.TestWatcher;
21 | import org.junit.runner.Description;
22 | import org.junit.runners.model.Statement;
23 |
24 | import com.eclipsesource.restfuse.annotation.HttpTest;
25 | import com.eclipsesource.restfuse.internal.HttpTestStatement;
26 |
27 |
28 | public class Destination_Test {
29 |
30 | private Destination destination;
31 | private Destination watchmanDestination;
32 |
33 | @Before
34 | public void setUp() {
35 | destination = new Destination( this, "http://localhost" );
36 | }
37 |
38 | @Test( expected = IllegalArgumentException.class )
39 | public void testBaseUrlCannotBeNull() {
40 | new Destination( this, null );
41 | }
42 |
43 | @Test( expected = IllegalArgumentException.class )
44 | public void testBaseUrlHasToBeAnUrl() {
45 | new Destination( this, "foo" );
46 | }
47 |
48 | @Test( expected = IllegalArgumentException.class )
49 | public void testTestObjectIsNull() throws Exception {
50 | new Destination( null, "http://localhost" );
51 | }
52 |
53 | @Test
54 | public void testApplyReturnsBaseWhenNoAnnotationsPresent() {
55 | Statement base = mock( Statement.class );
56 | Description description = mock( Description.class );
57 |
58 | Statement statement = destination.apply( base, description );
59 |
60 | assertEquals( base, statement );
61 | }
62 |
63 | @Rule
64 | public TestWatcher watcher = new TestWatcher() {
65 |
66 | @Override
67 | public void starting( Description description ) {
68 | HttpTest annotation = description.getAnnotation( HttpTest.class );
69 | if( annotation != null ) {
70 | Statement base = mock( Statement.class );
71 | watchmanDestination = new Destination( new Object(), "http://localhost" );
72 | Statement statement = watchmanDestination.apply( base, description );
73 |
74 | checkStatement( statement );
75 | }
76 | }
77 | };
78 |
79 | protected void checkStatement( Statement statement ) {
80 | assertTrue( statement instanceof HttpTestStatement );
81 | }
82 |
83 | @Test
84 | @HttpTest( method = Method.GET, path = "/" )
85 | public void testReturnsHttpTestStatement() {
86 | // nothing to do because the TestWatcher tests the Statement
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/HttpJUnitRunner_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import static org.junit.Assert.assertEquals;
14 |
15 | import java.util.List;
16 |
17 | import org.junit.Test;
18 | import org.junit.runners.model.FrameworkMethod;
19 | import org.junit.runners.model.InitializationError;
20 |
21 | import com.eclipsesource.restfuse.annotation.HttpTest;
22 |
23 |
24 | public class HttpJUnitRunner_Test {
25 |
26 | @Test
27 | public void testFindsTestMethods() throws InitializationError {
28 | HttpJUnitRunner runner = new HttpJUnitRunner( getClass() );
29 |
30 | List methods = runner.computeTestMethods();
31 |
32 | assertEquals( 3, methods.size() );
33 | }
34 |
35 | @HttpTest( method = Method.GET, path = "/" )
36 | public void fakeTestMethod() {
37 | // this method only exists to test that the runner finds HttpTest annotated methods
38 | }
39 |
40 | @Test
41 | @HttpTest( method = Method.GET, path = "/" )
42 | public void fakeTestMethodWithTwoTestAnnotations() {
43 | // this method only exists to test that the runner does not add this method twice
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/Poll_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import static com.eclipsesource.restfuse.Assert.assertNoContent;
14 | import static org.junit.Assert.assertEquals;
15 | import static org.junit.Assert.assertTrue;
16 | import static org.mockito.Mockito.mock;
17 |
18 | import org.eclipse.jetty.server.Server;
19 | import org.eclipse.jetty.servlet.ServletContextHandler;
20 | import org.eclipse.jetty.servlet.ServletHolder;
21 | import org.junit.After;
22 | import org.junit.AfterClass;
23 | import org.junit.BeforeClass;
24 | import org.junit.Rule;
25 | import org.junit.Test;
26 |
27 | import com.eclipsesource.restfuse.annotation.Context;
28 | import com.eclipsesource.restfuse.annotation.HttpTest;
29 | import com.eclipsesource.restfuse.annotation.Poll;
30 | import com.eclipsesource.restfuse.internal.callback.CallbackSerlvet;
31 | import com.eclipsesource.restfuse.internal.callback.CallbackStatement;
32 |
33 |
34 | public class Poll_Test {
35 |
36 | private static final int TIMEOUT = 10;
37 | private static Server server;
38 | private static int COUNT = 0;
39 |
40 | @Rule
41 | public Destination destination = new Destination( this, "http://localhost:10044/test" );
42 |
43 | @Context
44 | private PollState pollState;
45 |
46 | @Context
47 | private Response response;
48 |
49 | private static class TestResource extends DefaultCallbackResource {
50 | @Override
51 | public Response get( Request request ) {
52 | COUNT++;
53 | return super.get( request );
54 | }
55 | }
56 |
57 | @BeforeClass
58 | public static void setUp() throws Exception {
59 | server = new Server( 10044 );
60 | ServletContextHandler context = new ServletContextHandler( server, "/", ServletContextHandler.SESSIONS );
61 | CallbackStatement statement = mock( CallbackStatement.class );
62 | CallbackSerlvet servlet = new CallbackSerlvet( new TestResource(), statement );
63 | context.addServlet( new ServletHolder( servlet ), "/" );
64 | server.start();
65 | int timer = 0;
66 | while( !server.isRunning() && timer < TIMEOUT ) {
67 | Thread.sleep( 1000 );
68 | timer++;
69 | }
70 | }
71 |
72 | @AfterClass
73 | public static void tearDown() throws Exception {
74 | server.stop();
75 | int timer = 0;
76 | while( !server.isStopped() && timer < TIMEOUT ) {
77 | Thread.sleep( 1000 );
78 | timer++;
79 | }
80 | }
81 |
82 | @After
83 | public void resetCounter() {
84 | if( COUNT >= 5 ) {
85 | COUNT = 0;
86 | }
87 | }
88 |
89 | @Test
90 | @HttpTest( method = Method.GET, path = "/" )
91 | @Poll( times = 5, interval = 500 )
92 | public void testAbort() {
93 | assertNoContent( pollState.getResponse( pollState.getTimes() ) );
94 | if( COUNT == 3 ) {
95 | pollState.abort();
96 | COUNT = 0;
97 | }
98 | assertTrue( pollState.getTimes() <= 3 );
99 | }
100 |
101 | @Test
102 | @HttpTest( method = Method.GET, path = "/" )
103 | @Poll( times = 5, interval = 500 )
104 | public void testSendsRequest() {
105 | assertNoContent( pollState.getResponse( pollState.getTimes() ) );
106 | assertEquals( pollState.getTimes(), COUNT );
107 | }
108 |
109 | @Test
110 | @HttpTest( method = Method.GET, path = "/" )
111 | @Poll( times = 5, interval = 500 )
112 | public void testInjectsResponse() {
113 | Response currentResponse = pollState.getResponse( pollState.getTimes() );
114 |
115 | assertEquals( currentResponse, response );
116 | }
117 |
118 | }
119 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/Status_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import static org.junit.Assert.assertEquals;
14 |
15 | import org.junit.Test;
16 |
17 | import com.eclipsesource.restfuse.Status.Family;
18 |
19 |
20 | public class Status_Test {
21 |
22 | @Test
23 | public void testAssignsFamily() {
24 | assertEquals( Family.INFORMATIONAL, Status.CONTINUE.getFamily() );
25 | assertEquals( Family.CLIENT_ERROR, Status.NOT_FOUND.getFamily() );
26 | assertEquals( Family.SERVER_ERROR, Status.INTERNAL_SERVER_ERROR.getFamily() );
27 | assertEquals( Family.SUCCESSFUL, Status.NO_CONTENT.getFamily() );
28 | assertEquals( Family.REDIRECTION, Status.MOVED_PERMANENTLY.getFamily() );
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/AuthenticationInfo_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import static org.junit.Assert.assertEquals;
14 |
15 | import org.junit.Before;
16 | import org.junit.Test;
17 |
18 | import com.eclipsesource.restfuse.AuthenticationType;
19 |
20 |
21 | public class AuthenticationInfo_Test {
22 |
23 | private AuthenticationInfo info;
24 |
25 | @Before
26 | public void setUp() {
27 | info = new AuthenticationInfo( AuthenticationType.BASIC, "user", "password" );
28 | }
29 |
30 | @Test
31 | public void testGetType() {
32 | assertEquals( AuthenticationType.BASIC, info.getType() );
33 | }
34 |
35 | @Test
36 | public void testGetUser() {
37 | assertEquals( "user", info.getUser() );
38 | }
39 |
40 | @Test
41 | public void testGetPassword() {
42 | assertEquals( "password", info.getPassword() );
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/HttpTestStatementOrder_Test.java:
--------------------------------------------------------------------------------
1 | package com.eclipsesource.restfuse.internal;
2 |
3 | import static org.junit.Assert.assertEquals;
4 | import static org.junit.Assert.assertNotNull;
5 | import static org.mockito.Mockito.mock;
6 |
7 | import org.eclipse.jetty.server.Server;
8 | import org.eclipse.jetty.servlet.ServletContextHandler;
9 | import org.eclipse.jetty.servlet.ServletHolder;
10 | import org.junit.AfterClass;
11 | import org.junit.BeforeClass;
12 | import org.junit.Rule;
13 | import org.junit.runner.RunWith;
14 |
15 | import com.eclipsesource.restfuse.DefaultCallbackResource;
16 | import com.eclipsesource.restfuse.Destination;
17 | import com.eclipsesource.restfuse.HttpJUnitRunner;
18 | import com.eclipsesource.restfuse.Method;
19 | import com.eclipsesource.restfuse.Response;
20 | import com.eclipsesource.restfuse.Status;
21 | import com.eclipsesource.restfuse.annotation.Context;
22 | import com.eclipsesource.restfuse.annotation.HttpTest;
23 | import com.eclipsesource.restfuse.internal.callback.CallbackSerlvet;
24 | import com.eclipsesource.restfuse.internal.callback.CallbackStatement;
25 |
26 | @RunWith(HttpJUnitRunner.class)
27 | public class HttpTestStatementOrder_Test {
28 |
29 | private static int orderCheck = 0;
30 |
31 | private static final int TIMEOUT = 10;
32 | private static Server server;
33 | @Rule
34 | public Destination destination = new Destination( this, "http://localhost:10042/test" );
35 | @Context
36 | private Response response;
37 |
38 | @BeforeClass
39 | public static void setUp() throws Exception {
40 | server = new Server( 10042 );
41 | ServletContextHandler context = new ServletContextHandler( server,
42 | "/",
43 | ServletContextHandler.SESSIONS );
44 | CallbackStatement statement = mock( CallbackStatement.class );
45 | CallbackSerlvet servlet = new CallbackSerlvet( new DefaultCallbackResource(),
46 | statement );
47 | context.addServlet( new ServletHolder( servlet ), "/" );
48 | server.start();
49 | int timer = 0;
50 | while( !server.isRunning() && timer < TIMEOUT ) {
51 | Thread.sleep( 1000 );
52 | timer++;
53 | }
54 | }
55 |
56 | @AfterClass
57 | public static void tearDown() throws Exception {
58 | server.stop();
59 | int timer = 0;
60 | while( !server.isStopped() && timer < TIMEOUT ) {
61 | Thread.sleep( 1000 );
62 | timer++;
63 | }
64 | }
65 |
66 | @HttpTest(method = Method.GET, path = "/")
67 | public void testNoOrder() {
68 | assertNotNull( response );
69 | assertEquals( Status.NO_CONTENT.getStatusCode(), response.getStatus() );
70 | }
71 |
72 | @HttpTest(method = Method.GET, path = "/", order = 2)
73 | public void testOrderSecond() {
74 | assertNotNull( response );
75 | assertEquals( Status.NO_CONTENT.getStatusCode(), response.getStatus() );
76 |
77 | orderCheck++;
78 |
79 | assertEquals( 2, orderCheck );
80 | }
81 |
82 | @HttpTest(method = Method.GET, path = "/", order = 3)
83 | public void testOrderThird() {
84 | assertNotNull( response );
85 | assertEquals( Status.NO_CONTENT.getStatusCode(), response.getStatus() );
86 |
87 | orderCheck++;
88 |
89 | assertEquals( 3, orderCheck );
90 | }
91 |
92 | @HttpTest(method = Method.GET, path = "/", order = 1)
93 | public void testOrderFirst() {
94 | assertNotNull( response );
95 | assertEquals( Status.NO_CONTENT.getStatusCode(), response.getStatus() );
96 |
97 | orderCheck++;
98 |
99 | assertEquals( 1, orderCheck );
100 | }
101 |
102 | @HttpTest(method = Method.GET, path = "/")
103 | public void testNoOrder_2() {
104 | assertNotNull( response );
105 | assertEquals( Status.NO_CONTENT.getStatusCode(), response.getStatus() );
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/HttpTestStatement_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import static org.junit.Assert.assertEquals;
14 | import static org.junit.Assert.assertNotNull;
15 | import static org.junit.Assert.assertNull;
16 | import static org.mockito.Mockito.mock;
17 | import static org.mockito.Mockito.when;
18 |
19 | import java.lang.annotation.Annotation;
20 |
21 | import org.eclipse.jetty.server.Server;
22 | import org.eclipse.jetty.servlet.ServletContextHandler;
23 | import org.eclipse.jetty.servlet.ServletHolder;
24 | import org.junit.AfterClass;
25 | import org.junit.BeforeClass;
26 | import org.junit.Rule;
27 | import org.junit.Test;
28 | import org.junit.runner.Description;
29 | import org.junit.runners.model.Statement;
30 |
31 | import com.eclipsesource.restfuse.DefaultCallbackResource;
32 | import com.eclipsesource.restfuse.Destination;
33 | import com.eclipsesource.restfuse.MediaType;
34 | import com.eclipsesource.restfuse.Method;
35 | import com.eclipsesource.restfuse.Response;
36 | import com.eclipsesource.restfuse.Status;
37 | import com.eclipsesource.restfuse.annotation.Authentication;
38 | import com.eclipsesource.restfuse.annotation.Context;
39 | import com.eclipsesource.restfuse.annotation.Header;
40 | import com.eclipsesource.restfuse.annotation.HttpTest;
41 | import com.eclipsesource.restfuse.internal.callback.CallbackSerlvet;
42 | import com.eclipsesource.restfuse.internal.callback.CallbackStatement;
43 |
44 |
45 | public class HttpTestStatement_Test {
46 |
47 | private static final int TIMEOUT = 10;
48 |
49 | private static Server server;
50 |
51 | @Rule
52 | public Destination destination = new Destination( this, "http://localhost:10042/test" );
53 |
54 | @Context
55 | private Response response;
56 |
57 |
58 | @BeforeClass
59 | public static void setUp() throws Exception {
60 | server = new Server( 10042 );
61 | ServletContextHandler context = new ServletContextHandler( server, "/", ServletContextHandler.SESSIONS );
62 | CallbackStatement statement = mock( CallbackStatement.class );
63 | CallbackSerlvet servlet = new CallbackSerlvet( new DefaultCallbackResource(), statement );
64 | context.addServlet( new ServletHolder( servlet ), "/" );
65 | server.start();
66 | int timer = 0;
67 | while( !server.isRunning() && timer < TIMEOUT ) {
68 | Thread.sleep( 1000 );
69 | timer++;
70 | }
71 | }
72 |
73 | @AfterClass
74 | public static void tearDown() throws Exception {
75 | server.stop();
76 | int timer = 0;
77 | while( !server.isStopped() && timer < TIMEOUT ) {
78 | Thread.sleep( 1000 );
79 | timer++;
80 | }
81 | }
82 |
83 | @Test
84 | @HttpTest( method = Method.GET, path = "/" )
85 | public void testInjectsResponse() {
86 | assertNotNull( response );
87 | assertEquals( Status.NO_CONTENT.getStatusCode(), response.getStatus() );
88 | }
89 |
90 | @Test
91 | public void testRemovesProxyProperties() throws Throwable {
92 | Statement base = mock( Statement.class );
93 | Description description = mock( Description.class );
94 | HttpTest annotation = createAnnotation();
95 | when( description.getAnnotation( HttpTest.class ) ).thenReturn( annotation );
96 | Object target = new Object();
97 | HttpTestStatement statement
98 | = new HttpTestStatement( base, description, target, "http://localhost", "http://proxy.com", 8080, null );
99 |
100 | statement.evaluate();
101 |
102 | assertNull( System.getProperty( HttpTestStatement.HTTP_PROXY_HOST ) );
103 | assertNull( System.getProperty( HttpTestStatement.HTTP_PROXY_PORT ) );
104 | }
105 |
106 | private HttpTest createAnnotation() {
107 | HttpTest annotation = new HttpTest() {
108 |
109 | @Override
110 | public Class extends Annotation> annotationType() {
111 | return null;
112 | }
113 |
114 | @Override
115 | public MediaType type() {
116 | return MediaType.APPLICATION_JSON;
117 | }
118 |
119 | @Override
120 | public String path() {
121 | return "/";
122 | }
123 |
124 | @Override
125 | public Method method() {
126 | return Method.GET;
127 | }
128 |
129 | @Override
130 | public Header[] headers() {
131 | return null;
132 | }
133 |
134 | @Override
135 | public String file() {
136 | return "";
137 | }
138 |
139 | @Override
140 | public String content() {
141 | return "content";
142 | }
143 |
144 | @Override
145 | public Authentication[] authentications() {
146 | return null;
147 | }
148 |
149 | @Override
150 | public int order() {
151 | return 0;
152 | }
153 | };
154 | return annotation;
155 | }
156 | }
157 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/InternalRequest_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import static org.junit.Assert.assertEquals;
14 | import static org.junit.Assert.assertTrue;
15 | import static org.mockito.Mockito.mock;
16 |
17 | import java.io.ByteArrayInputStream;
18 | import java.io.UnsupportedEncodingException;
19 | import java.util.List;
20 | import java.util.Map;
21 |
22 | import org.eclipse.jetty.server.Server;
23 | import org.eclipse.jetty.servlet.ServletContextHandler;
24 | import org.eclipse.jetty.servlet.ServletHolder;
25 | import org.junit.AfterClass;
26 | import org.junit.BeforeClass;
27 | import org.junit.Test;
28 |
29 | import com.eclipsesource.restfuse.AuthenticationType;
30 | import com.eclipsesource.restfuse.DefaultCallbackResource;
31 | import com.eclipsesource.restfuse.MediaType;
32 | import com.eclipsesource.restfuse.Request;
33 | import com.eclipsesource.restfuse.Response;
34 | import com.eclipsesource.restfuse.internal.callback.CallbackSerlvet;
35 | import com.eclipsesource.restfuse.internal.callback.CallbackStatement;
36 |
37 |
38 | public class InternalRequest_Test {
39 |
40 | private static final int TIMEOUT = 10;
41 |
42 | private static Server server;
43 |
44 | @BeforeClass
45 | public static void setUp() throws Exception {
46 | server = new Server( 10042 );
47 | ServletContextHandler context = new ServletContextHandler( server, "/", ServletContextHandler.SESSIONS );
48 | CallbackStatement statement = mock( CallbackStatement.class );
49 | CallbackSerlvet servlet = new CallbackSerlvet( new TestResource(), statement );
50 | context.addServlet( new ServletHolder( servlet ), "/" );
51 | server.start();
52 | int timer = 0;
53 | while( !server.isRunning() && timer < TIMEOUT ) {
54 | Thread.sleep( 1000 );
55 | timer++;
56 | }
57 | }
58 |
59 | @AfterClass
60 | public static void tearDown() throws Exception {
61 | server.stop();
62 | int timer = 0;
63 | while( !server.isStopped() && timer < TIMEOUT ) {
64 | Thread.sleep( 1000 );
65 | timer++;
66 | }
67 | }
68 |
69 | private static class TestResource extends DefaultCallbackResource {
70 | @Override
71 | public Response get( Request request ) {
72 | Map> headers = request.getHeaders();
73 | assertTrue( headers.containsKey( "Authorization" ) );
74 | assertEquals( "test", headers.get( "test" ).get( 0 ) );
75 | assertEquals( MediaType.TEXT_PLAIN, request.getType() );
76 | return super.get( request );
77 | }
78 |
79 | @Override
80 | public Response post( Request request ) {
81 | assertEquals( "test", request.getBody() );
82 | Map> headers = request.getHeaders();
83 | assertTrue( headers.containsKey( "Authorization" ) );
84 | assertEquals( "test", headers.get( "test" ).get( 0 ) );
85 | assertEquals( MediaType.TEXT_PLAIN, request.getType() );
86 | return super.post( request );
87 | }
88 |
89 | @Override
90 | public Response put( Request request ) {
91 | assertEquals( "test", request.getBody() );
92 | Map> headers = request.getHeaders();
93 | assertTrue( headers.containsKey( "Authorization" ) );
94 | assertEquals( "test", headers.get( "test" ).get( 0 ) );
95 | assertEquals( MediaType.TEXT_PLAIN, request.getType() );
96 | return super.put( request );
97 | }
98 |
99 | @Override
100 | public Response delete( Request request ) {
101 | Map> headers = request.getHeaders();
102 | assertTrue( headers.containsKey( "Authorization" ) );
103 | assertEquals( "test", headers.get( "test" ).get( 0 ) );
104 | assertEquals( MediaType.TEXT_PLAIN, request.getType() );
105 | return super.delete( request );
106 | }
107 |
108 | @Override
109 | public Response options( Request request ) {
110 | Map> headers = request.getHeaders();
111 | assertTrue( headers.containsKey( "Authorization" ) );
112 | assertEquals( "test", headers.get( "test" ).get( 0 ) );
113 | assertEquals( MediaType.TEXT_PLAIN, request.getType() );
114 | return super.options( request );
115 | }
116 |
117 | @Override
118 | public Response head( Request request ) {
119 | Map> headers = request.getHeaders();
120 | assertTrue( headers.containsKey( "Authorization" ) );
121 | assertEquals( "test", headers.get( "test" ).get( 0 ) );
122 | assertEquals( MediaType.TEXT_PLAIN, request.getType() );
123 | return super.head( request );
124 | }
125 |
126 | }
127 |
128 | @Test
129 | public void testTransmitsGetData() throws UnsupportedEncodingException {
130 | InternalRequest request = createRequest();
131 |
132 | request.get();
133 | }
134 |
135 | @Test
136 | public void testTransmitsPostData() throws UnsupportedEncodingException {
137 | InternalRequest request = createRequest();
138 |
139 | request.post();
140 | }
141 |
142 | @Test
143 | public void testTransmitsPutData() throws UnsupportedEncodingException {
144 | InternalRequest request = createRequest();
145 |
146 | request.put();
147 | }
148 |
149 | @Test
150 | public void testTransmitsDeleteData() throws UnsupportedEncodingException {
151 | InternalRequest request = createRequest();
152 |
153 | request.delete();
154 | }
155 |
156 | @Test
157 | public void testTransmitsOptionsData() throws UnsupportedEncodingException {
158 | InternalRequest request = createRequest();
159 |
160 | request.options();
161 | }
162 |
163 | @Test
164 | public void testTransmitsHeadData() throws UnsupportedEncodingException {
165 | InternalRequest request = createRequest();
166 |
167 | request.head();
168 | }
169 |
170 | private InternalRequest createRequest() throws UnsupportedEncodingException {
171 | InternalRequest internalRequest = new InternalRequest( "http://localhost:10042/test" );
172 | internalRequest.setContent( new ByteArrayInputStream( "test".getBytes( "UTF-8" ) ) );
173 | internalRequest.setContentType( MediaType.TEXT_PLAIN.getMimeType() );
174 | internalRequest.addHeader( "test", "test" );
175 | AuthenticationInfo authentication
176 | = new AuthenticationInfo( AuthenticationType.BASIC, "test", "test" );
177 | internalRequest.addAuthenticationInfo( authentication );
178 | return internalRequest;
179 | }
180 | }
181 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/RequestConfiguration_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import static org.junit.Assert.assertEquals;
14 | import static org.junit.Assert.assertTrue;
15 | import static org.mockito.Mockito.mock;
16 | import static org.mockito.Mockito.when;
17 |
18 | import java.lang.annotation.Annotation;
19 | import java.util.List;
20 | import java.util.Map;
21 |
22 | import org.eclipse.jetty.server.Server;
23 | import org.eclipse.jetty.servlet.ServletContextHandler;
24 | import org.eclipse.jetty.servlet.ServletHolder;
25 | import org.junit.AfterClass;
26 | import org.junit.BeforeClass;
27 | import org.junit.Rule;
28 | import org.junit.Test;
29 | import org.junit.runner.Description;
30 |
31 | import com.eclipsesource.restfuse.AuthenticationType;
32 | import com.eclipsesource.restfuse.DefaultCallbackResource;
33 | import com.eclipsesource.restfuse.Destination;
34 | import com.eclipsesource.restfuse.MediaType;
35 | import com.eclipsesource.restfuse.Method;
36 | import com.eclipsesource.restfuse.Request;
37 | import com.eclipsesource.restfuse.RequestContext;
38 | import com.eclipsesource.restfuse.Response;
39 | import com.eclipsesource.restfuse.annotation.Authentication;
40 | import com.eclipsesource.restfuse.annotation.Header;
41 | import com.eclipsesource.restfuse.annotation.HttpTest;
42 | import com.eclipsesource.restfuse.internal.callback.CallbackSerlvet;
43 | import com.eclipsesource.restfuse.internal.callback.CallbackStatement;
44 |
45 | public class RequestConfiguration_Test {
46 |
47 | private static final int TIMEOUT = 10;
48 |
49 | private static Server server;
50 |
51 | private static TestResource resource = new TestResource();
52 |
53 | @BeforeClass
54 | public static void setUp() throws Exception {
55 | server = new Server(10043);
56 | ServletContextHandler context = new ServletContextHandler(server, "/",
57 | ServletContextHandler.SESSIONS);
58 | CallbackStatement statement = mock(CallbackStatement.class);
59 | CallbackSerlvet servlet = new CallbackSerlvet(resource, statement);
60 | context.addServlet(new ServletHolder(servlet), "/");
61 | server.start();
62 | int timer = 0;
63 | while (!server.isRunning() && timer < TIMEOUT) {
64 | Thread.sleep(1000);
65 | timer++;
66 | }
67 | }
68 |
69 | @AfterClass
70 | public static void tearDown() throws Exception {
71 | server.stop();
72 | int timer = 0;
73 | while (!server.isStopped() && timer < TIMEOUT) {
74 | Thread.sleep(1000);
75 | timer++;
76 | }
77 | }
78 |
79 | private static class TestResource extends DefaultCallbackResource {
80 |
81 | public Request request;
82 |
83 | @Override
84 | public Response post(Request request) {
85 | // store request to be used in test case
86 | this.request = request;
87 | return super.post(request);
88 | }
89 | }
90 |
91 | @Rule
92 | public Destination destination = new Destination(this, "http://localhost:10043/test");
93 |
94 | @Test
95 | @HttpTest( method = Method.POST,
96 | headers = { @Header(name = "test", value = "value") },
97 | path = "/",
98 | authentications = { @Authentication(type = AuthenticationType.BASIC, user = "test", password = "pass") },
99 | type = MediaType.TEXT_PLAIN,
100 | content = "test"
101 | )
102 | public void fakeTestMethod() {
103 | assertEquals("test", resource.request.getBody());
104 | Map> headers = resource.request.getHeaders();
105 | assertTrue(headers.containsKey("Authorization"));
106 | assertEquals("value", headers.get("test").get(0));
107 | assertEquals(MediaType.TEXT_PLAIN, resource.request.getType());
108 | }
109 |
110 | @Test
111 | public void testPathWithSegments() {
112 | Description description = mock( Description.class );
113 | HttpTest annotation = createAnnotation( "/people/{id}/{name}" );
114 | when( description.getAnnotation( HttpTest.class ) ).thenReturn( annotation );
115 | RequestConfiguration config = new RequestConfiguration( "http://www.fake.com",
116 | description,
117 | new Object() );
118 | RequestContext context = new RequestContext();
119 |
120 | context.addPathSegment( "id", "12345" );
121 | context.addPathSegment( "name", "name" );
122 | InternalRequest request = config.createRequest( context );
123 |
124 | assertEquals( "http://www.fake.com/people/12345/name", request.getUrl() );
125 | }
126 |
127 | @Test( expected = IllegalStateException.class )
128 | public void testPathWithNonExistingSegments() {
129 | Description method = mock( Description.class );
130 | HttpTest annotation = createAnnotation( "/people/{invalid}/name" );
131 | when( method.getAnnotation( HttpTest.class ) ).thenReturn( annotation );
132 | RequestConfiguration config = new RequestConfiguration( "http://www.fake.com",
133 | method,
134 | new Object() );
135 | RequestContext context = new RequestContext();
136 | context.addPathSegment( "id", "12345" );
137 | config.createRequest( context );
138 | }
139 |
140 | private HttpTest createAnnotation(final String path) {
141 | HttpTest annotation = new HttpTest() {
142 |
143 | @Override
144 | public Class extends Annotation> annotationType() {
145 | return null;
146 | }
147 |
148 | @Override
149 | public MediaType type() {
150 | return MediaType.APPLICATION_JSON;
151 | }
152 |
153 | @Override
154 | public String path() {
155 | return path;
156 | }
157 |
158 | @Override
159 | public Method method() {
160 | return Method.GET;
161 | }
162 |
163 | @Override
164 | public Header[] headers() {
165 | return null;
166 | }
167 |
168 | @Override
169 | public String file() {
170 | return "";
171 | }
172 |
173 | @Override
174 | public String content() {
175 | return "content";
176 | }
177 |
178 | @Override
179 | public Authentication[] authentications() {
180 | return null;
181 | }
182 |
183 | @Override
184 | public int order() {
185 | return 0;
186 | }
187 | };
188 | return annotation;
189 | }
190 |
191 | }
192 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/RequestContextConfiguration_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others. All rights reserved. This
3 | * program and the accompanying materials are made available under the terms of
4 | * the Eclipse Public License v1.0 which accompanies this distribution, and is
5 | * available at http://www.eclipse.org/legal/epl-v10.html Contributors: Holger
6 | * Staudacher - initial API and implementation
7 | ******************************************************************************/
8 | package com.eclipsesource.restfuse.internal;
9 |
10 | import static org.junit.Assert.assertEquals;
11 | import static org.junit.Assert.assertTrue;
12 | import static org.mockito.Mockito.mock;
13 |
14 | import java.util.List;
15 | import java.util.Map;
16 |
17 | import org.eclipse.jetty.server.Server;
18 | import org.eclipse.jetty.servlet.ServletContextHandler;
19 | import org.eclipse.jetty.servlet.ServletHolder;
20 | import org.junit.AfterClass;
21 | import org.junit.BeforeClass;
22 | import org.junit.Rule;
23 | import org.junit.Test;
24 |
25 | import com.eclipsesource.restfuse.AuthenticationType;
26 | import com.eclipsesource.restfuse.DefaultCallbackResource;
27 | import com.eclipsesource.restfuse.Destination;
28 | import com.eclipsesource.restfuse.MediaType;
29 | import com.eclipsesource.restfuse.Method;
30 | import com.eclipsesource.restfuse.Request;
31 | import com.eclipsesource.restfuse.Response;
32 | import com.eclipsesource.restfuse.annotation.Authentication;
33 | import com.eclipsesource.restfuse.annotation.Header;
34 | import com.eclipsesource.restfuse.annotation.HttpTest;
35 | import com.eclipsesource.restfuse.internal.callback.CallbackSerlvet;
36 | import com.eclipsesource.restfuse.internal.callback.CallbackStatement;
37 |
38 | public class RequestContextConfiguration_Test {
39 |
40 | private static final int TIMEOUT = 10;
41 | private static Server server;
42 | private static TestResource resource = new TestResource();
43 |
44 | @BeforeClass
45 | public static void setUp() throws Exception {
46 | server = new Server( 10045 );
47 | ServletContextHandler context = new ServletContextHandler( server, "/", ServletContextHandler.SESSIONS );
48 | CallbackStatement statement = mock( CallbackStatement.class );
49 | CallbackSerlvet servlet = new CallbackSerlvet( resource, statement );
50 | context.addServlet( new ServletHolder( servlet ), "/" );
51 | server.start();
52 | int timer = 0;
53 | while( !server.isRunning() && timer < TIMEOUT ) {
54 | Thread.sleep( 1000 );
55 | timer++;
56 | }
57 | }
58 |
59 | @AfterClass
60 | public static void tearDown() throws Exception {
61 | server.stop();
62 | int timer = 0;
63 | while( !server.isStopped() && timer < TIMEOUT ) {
64 | Thread.sleep( 1000 );
65 | timer++;
66 | }
67 | }
68 | private static class TestResource extends DefaultCallbackResource {
69 |
70 | public Request request;
71 |
72 | @Override
73 | public Response post( Request request ) {
74 | // store request to be used in test case
75 | this.request = request;
76 | return super.post( request );
77 | }
78 | }
79 |
80 | @Rule
81 | public Destination destination = getDestination();
82 |
83 | private Destination getDestination() {
84 | Destination destination = new Destination( this, "http://localhost:10045/test" );
85 | destination.getRequestContext().addHeader( "test", "value" );
86 | return destination;
87 | }
88 |
89 | @Test
90 | @HttpTest(method = Method.POST, path = "/", authentications = {
91 | @Authentication(type = AuthenticationType.BASIC, user = "test", password = "pass")
92 | }, type = MediaType.TEXT_PLAIN, content = "test")
93 | public void fakeTestMethod() {
94 | assertEquals( "test", resource.request.getBody() );
95 | Map> headers = resource.request.getHeaders();
96 | assertTrue( headers.containsKey( "Authorization" ) );
97 | assertEquals( "value", headers.get( "test" ).get( 0 ) );
98 | assertEquals( MediaType.TEXT_PLAIN, resource.request.getType() );
99 | }
100 |
101 | @Test
102 | @HttpTest(
103 | method = Method.POST,
104 | path = "/",
105 | authentications = { @Authentication(type = AuthenticationType.BASIC, user = "test", password = "pass") },
106 | type = MediaType.TEXT_PLAIN,
107 | content = "test",
108 | headers = { @Header(name = "test", value = "new-value") }
109 | )
110 | public void overrideHeaderTestMethod() {
111 | assertEquals( "test", resource.request.getBody() );
112 | Map> headers = resource.request.getHeaders();
113 | assertTrue( headers.containsKey( "Authorization" ) );
114 | assertEquals( "value,new-value", headers.get( "test" ).get( 0 ) );
115 | assertEquals( MediaType.TEXT_PLAIN, resource.request.getType() );
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/RequestImpl_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import static org.junit.Assert.assertEquals;
14 | import static org.junit.Assert.assertFalse;
15 | import static org.junit.Assert.assertTrue;
16 |
17 | import java.util.List;
18 | import java.util.Map;
19 |
20 | import org.junit.Before;
21 | import org.junit.Test;
22 |
23 | import com.eclipsesource.restfuse.MediaType;
24 |
25 |
26 | public class RequestImpl_Test {
27 |
28 | private RequestImpl request;
29 |
30 | @Before
31 | public void setUp() {
32 | request = new RequestImpl( "test", MediaType.APPLICATION_ATOM_XML );
33 | request.addHeader( "test", "test1" );
34 | request.addHeader( "test", "test2" );
35 | request.addHeader( "test2", "test" );
36 | }
37 |
38 | @Test
39 | public void testHasBody() {
40 | RequestImpl noBodyRequest = new RequestImpl( null, MediaType.WILDCARD );
41 |
42 | assertFalse( noBodyRequest.hasBody() );
43 | assertTrue( request.hasBody() );
44 | }
45 |
46 | @Test
47 | public void testGetBody() {
48 | assertEquals( "test", request.getBody() );
49 | }
50 |
51 | @Test
52 | public void testGetMediaType() {
53 | assertEquals( MediaType.APPLICATION_ATOM_XML, request.getType() );
54 | }
55 |
56 | @Test
57 | public void testGetHeader() {
58 | Map> headers = request.getHeaders();
59 |
60 | assertEquals( "test1", headers.get( "test" ).get( 0 ) );
61 | assertEquals( "test2", headers.get( "test" ).get( 1 ) );
62 | assertEquals( "test", headers.get( "test2" ).get( 0 ) );
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/Response_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import static org.junit.Assert.assertEquals;
14 | import static org.junit.Assert.assertTrue;
15 | import static org.mockito.Mockito.mock;
16 | import static org.mockito.Mockito.when;
17 |
18 | import java.net.HttpURLConnection;
19 | import java.net.MalformedURLException;
20 | import java.net.URL;
21 | import java.util.ArrayList;
22 | import java.util.HashMap;
23 | import java.util.List;
24 |
25 | import org.junit.Before;
26 | import org.junit.Test;
27 |
28 | import com.eclipsesource.restfuse.MediaType;
29 | import com.github.kevinsawicki.http.HttpRequest;
30 |
31 |
32 | public class Response_Test {
33 |
34 | private ResponseImpl response;
35 |
36 | @Before
37 | public void setUp() throws MalformedURLException {
38 | HttpRequest httpRequest = mockRequest();
39 | response = new ResponseImpl( httpRequest );
40 | }
41 |
42 | private HttpRequest mockRequest() throws MalformedURLException {
43 | HttpRequest httpRequest = mock( HttpRequest.class );
44 | HttpURLConnection connection = mock( HttpURLConnection.class );
45 | when( connection.getURL() ).thenReturn( new URL( "http://test.com" ) );
46 | when( httpRequest.getConnection() ).thenReturn( connection );
47 | when( httpRequest.body() ).thenReturn( "test" );
48 | when( httpRequest.code() ).thenReturn( 200 );
49 | when( httpRequest.contentType() ).thenReturn( MediaType.TEXT_PLAIN.toString() );
50 | HashMap> headers = new HashMap>();
51 | List values = new ArrayList();
52 | values.add( "test" );
53 | headers.put( "test", values );
54 | when( httpRequest.headers() ).thenReturn( headers );
55 | return httpRequest;
56 | }
57 |
58 | @Test
59 | public void testResponseCode() {
60 | assertEquals( 200, response.getStatus() );
61 | }
62 |
63 | @Test
64 | public void testHasBody() {
65 | assertTrue( response.hasBody() );
66 | }
67 |
68 | @Test
69 | public void testGetBody() {
70 | assertEquals( "test", response.getBody() );
71 | }
72 |
73 | @Test
74 | public void testGetMediaType() {
75 | assertEquals( MediaType.TEXT_PLAIN, response.getType() );
76 | }
77 |
78 | @Test
79 | public void testGetHeaders() {
80 | assertEquals( "test", response.getHeaders().get( "test" ).get( 0 ) );
81 | }
82 |
83 | @Test
84 | public void testGetUrl() {
85 | assertEquals( "http://test.com", response.getUrl() );
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/callback/CallbackServer_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal.callback;
12 |
13 | import static org.junit.Assert.assertEquals;
14 | import static org.junit.Assert.assertTrue;
15 | import static org.junit.Assert.fail;
16 | import static org.mockito.Mockito.mock;
17 |
18 | import org.junit.Rule;
19 | import org.junit.Test;
20 | import org.junit.rules.TestWatcher;
21 | import org.junit.runner.Description;
22 |
23 | import com.eclipsesource.restfuse.DefaultCallbackResource;
24 | import com.eclipsesource.restfuse.Response;
25 | import com.eclipsesource.restfuse.Status;
26 | import com.eclipsesource.restfuse.annotation.Callback;
27 | import com.eclipsesource.restfuse.internal.ResponseImpl;
28 | import com.github.kevinsawicki.http.HttpRequest;
29 |
30 |
31 | public class CallbackServer_Test {
32 |
33 | private CallbackServer server;
34 |
35 | @Rule
36 | public TestWatcher watchman = new TestWatcher() {
37 |
38 | @Override
39 | public void starting( Description description ) {
40 | Callback callbackAnnotation = description.getAnnotation( Callback.class );
41 | CallbackStatement statement = mock( CallbackStatement.class );
42 | server = new CallbackServer( callbackAnnotation, CallbackServer_Test.this, statement );
43 | server.start();
44 |
45 | Response response = sendRequest();
46 | assertEquals( Status.NO_CONTENT.getStatusCode(), response.getStatus() );
47 | assertEquals( 10000, server.getTimeout() );
48 | }
49 |
50 | @Override
51 | public void finished( Description description ) {
52 | try {
53 | sendRequest();
54 | fail();
55 | } catch( Exception expected ) {}
56 | }
57 |
58 | private Response sendRequest() {
59 | HttpRequest request = HttpRequest.get( "http://localhost:10042/test" );
60 | request.code();
61 | return new ResponseImpl( request );
62 | }
63 |
64 | };
65 |
66 | private class TestResource extends DefaultCallbackResource {
67 | // no content
68 | }
69 |
70 | @Test
71 | @Callback( port = 10042, path = "/test", timeout = 10000, resource = TestResource.class )
72 | public void fakeTestMethod() {
73 | assertTrue( server.wasCalled() );
74 | server.stop();
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/callback/CallbackServlet_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal.callback;
12 |
13 | import static org.junit.Assert.assertFalse;
14 | import static org.junit.Assert.assertTrue;
15 | import static org.mockito.Matchers.any;
16 | import static org.mockito.Mockito.mock;
17 | import static org.mockito.Mockito.verify;
18 | import static org.mockito.Mockito.when;
19 |
20 | import java.io.BufferedReader;
21 | import java.io.IOException;
22 | import java.io.StringReader;
23 |
24 | import javax.servlet.ServletException;
25 | import javax.servlet.http.HttpServletRequest;
26 | import javax.servlet.http.HttpServletResponse;
27 |
28 | import org.junit.Before;
29 | import org.junit.Test;
30 | import org.junit.runner.RunWith;
31 | import org.mockito.Mock;
32 | import org.mockito.runners.MockitoJUnitRunner;
33 |
34 | import com.eclipsesource.restfuse.CallbackResource;
35 | import com.eclipsesource.restfuse.Request;
36 | import com.eclipsesource.restfuse.Response;
37 | import com.eclipsesource.restfuse.internal.callback.CallbackSerlvet;
38 | import com.eclipsesource.restfuse.internal.callback.CallbackStatement;
39 |
40 |
41 | @RunWith( MockitoJUnitRunner.class )
42 | public class CallbackServlet_Test {
43 |
44 | private CallbackSerlvet callbackSerlvet;
45 | @Mock
46 | private CallbackResource resource;
47 | @Mock
48 | private CallbackStatement statement;
49 |
50 | @Before
51 | public void setUp() {
52 | callbackSerlvet = new CallbackSerlvet( resource, statement );
53 | mockResponse();
54 | }
55 |
56 | @Test
57 | public void testWasCalledIsFalse() {
58 | assertFalse( callbackSerlvet.wasCalled() );
59 | }
60 |
61 | @Test
62 | public void testDelegatesGet() throws ServletException, IOException {
63 | HttpServletRequest req = mockHttpRequest();
64 | HttpServletResponse resp = mock( HttpServletResponse.class );
65 |
66 | callbackSerlvet.doGet( req, resp );
67 |
68 | verify( resource ).get( any( Request.class ) );
69 | assertTrue( callbackSerlvet.wasCalled() );
70 | }
71 |
72 | @Test
73 | public void testDelegatesPostt() throws ServletException, IOException {
74 | HttpServletRequest req = mockHttpRequest();
75 | HttpServletResponse resp = mock( HttpServletResponse.class );
76 |
77 | callbackSerlvet.doPost( req, resp );
78 |
79 | verify( resource ).post( any( Request.class ) );
80 | assertTrue( callbackSerlvet.wasCalled() );
81 | }
82 |
83 | @Test
84 | public void testDelegatesDelete() throws ServletException, IOException {
85 | HttpServletRequest req = mockHttpRequest();
86 | HttpServletResponse resp = mock( HttpServletResponse.class );
87 |
88 | callbackSerlvet.doDelete( req, resp );
89 |
90 | verify( resource ).delete( any( Request.class ) );
91 | assertTrue( callbackSerlvet.wasCalled() );
92 | }
93 |
94 | @Test
95 | public void testDelegatesPut() throws ServletException, IOException {
96 | HttpServletRequest req = mockHttpRequest();
97 | HttpServletResponse resp = mock( HttpServletResponse.class );
98 |
99 | callbackSerlvet.doPut( req, resp );
100 |
101 | verify( resource ).put( any( Request.class ) );
102 | assertTrue( callbackSerlvet.wasCalled() );
103 | }
104 |
105 | @Test
106 | public void testDelegatesOptions() throws ServletException, IOException {
107 | HttpServletRequest req = mockHttpRequest();
108 | HttpServletResponse resp = mock( HttpServletResponse.class );
109 |
110 | callbackSerlvet.doOptions( req, resp );
111 |
112 | verify( resource ).options( any( Request.class ) );
113 | assertTrue( callbackSerlvet.wasCalled() );
114 | }
115 |
116 | @Test
117 | public void testDelegatesHead() throws ServletException, IOException {
118 | HttpServletRequest req = mockHttpRequest();
119 | HttpServletResponse resp = mock( HttpServletResponse.class );
120 |
121 | callbackSerlvet.doHead( req, resp );
122 |
123 | verify( resource ).head( any( Request.class ) );
124 | assertTrue( callbackSerlvet.wasCalled() );
125 | }
126 |
127 | @Test
128 | public void testDelegatesFailure() throws ServletException, IOException {
129 | when( resource.get( any( Request.class ) ) ).thenThrow( new IllegalStateException() );
130 | HttpServletRequest req = mockHttpRequest();
131 | HttpServletResponse resp = mock( HttpServletResponse.class );
132 |
133 | callbackSerlvet.doGet( req, resp );
134 |
135 | verify( statement ).failWithinCallback( any( Throwable.class ) );
136 | }
137 |
138 | private void mockResponse() {
139 | Response response = mock( Response.class );
140 | when( resource.get( any( Request.class ) ) ).thenReturn( response );
141 | when( resource.post( any( Request.class ) ) ).thenReturn( response );
142 | when( resource.delete( any( Request.class ) ) ).thenReturn( response );
143 | when( resource.put( any( Request.class ) ) ).thenReturn( response );
144 | when( resource.head( any( Request.class ) ) ).thenReturn( response );
145 | when( resource.options( any( Request.class ) ) ).thenReturn( response );
146 | }
147 |
148 | private HttpServletRequest mockHttpRequest() throws IOException {
149 | HttpServletRequest req = mock( HttpServletRequest.class );
150 | when( req.getReader() ).thenReturn( new BufferedReader( new StringReader( "test" ) ) );
151 | return req;
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/internal/poll/PollStateImpl_Test.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal.poll;
12 |
13 | import static org.junit.Assert.assertEquals;
14 | import static org.junit.Assert.assertSame;
15 | import static org.junit.Assert.assertTrue;
16 | import static org.mockito.Mockito.mock;
17 |
18 | import java.util.List;
19 |
20 | import org.junit.Before;
21 | import org.junit.Test;
22 |
23 | import com.eclipsesource.restfuse.Response;
24 |
25 |
26 | public class PollStateImpl_Test {
27 |
28 | private PollStateImpl state;
29 |
30 | @Before
31 | public void setUp() {
32 | state = new PollStateImpl();
33 | }
34 |
35 | @Test
36 | public void testGetTimesFirstTimes() {
37 | assertEquals( 0, state.getTimes() );
38 | }
39 |
40 | @Test
41 | public void testGetTimesAfterResponse() {
42 | Response response = mock( Response.class );
43 |
44 | state.addResponse( response );
45 |
46 | assertEquals( 1, state.getTimes() );
47 | }
48 |
49 | @Test
50 | public void testGetTimesAfterTwoResponses() {
51 | state.addResponse( mock( Response.class ) );
52 | state.addResponse( mock( Response.class ) );
53 | state.addResponse( mock( Response.class ) );
54 |
55 | assertEquals( 3, state.getTimes() );
56 | }
57 |
58 | @Test
59 | public void testGetResponse() {
60 | state.addResponse( mock( Response.class ) );
61 | Response response = mock( Response.class );
62 | state.addResponse( response );
63 | Response response2 = mock( Response.class );
64 | state.addResponse( mock( Response.class ) );
65 | state.addResponse( response2 );
66 |
67 | assertSame( response, state.getResponse( 2 ) );
68 | assertSame( response2, state.getResponse( 4 ) );
69 | }
70 |
71 | @Test( expected = IllegalArgumentException.class )
72 | public void testGetNonExsitingResponse() {
73 | state.addResponse( mock( Response.class ) );
74 | Response response = mock( Response.class );
75 | state.addResponse( response );
76 |
77 | state.getResponse( 4 );
78 | }
79 |
80 | @Test
81 | public void testGetResponses() {
82 | Response response = mock( Response.class );
83 | state.addResponse( response );
84 | Response response2 = mock( Response.class );
85 | state.addResponse( response2 );
86 |
87 | List responses = state.getResponses();
88 |
89 | assertEquals( 2, responses.size() );
90 | assertEquals( response, responses.get( 0 ) );
91 | assertEquals( response2, responses.get( 1 ) );
92 | }
93 |
94 | @Test
95 | public void testAbort() {
96 | state.abort();
97 |
98 | assertTrue( state.wasAborted() );
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse.test/src/com/eclipsesource/restfuse/test/AllRestfuseTestSuite.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.test;
12 |
13 | import org.junit.runner.RunWith;
14 | import org.junit.runners.Suite;
15 | import org.junit.runners.Suite.SuiteClasses;
16 |
17 | import com.eclipsesource.restfuse.Assert_Test;
18 | import com.eclipsesource.restfuse.CallbackResource_Test;
19 | import com.eclipsesource.restfuse.DefaultCallbackResource_Test;
20 | import com.eclipsesource.restfuse.Destination_Test;
21 | import com.eclipsesource.restfuse.HttpJUnitRunner_Test;
22 | import com.eclipsesource.restfuse.Poll_Test;
23 | import com.eclipsesource.restfuse.Status_Test;
24 | import com.eclipsesource.restfuse.internal.AuthenticationInfo_Test;
25 | import com.eclipsesource.restfuse.internal.Response_Test;
26 | import com.eclipsesource.restfuse.internal.HttpTestStatementOrder_Test;
27 | import com.eclipsesource.restfuse.internal.HttpTestStatement_Test;
28 | import com.eclipsesource.restfuse.internal.InternalRequest_Test;
29 | import com.eclipsesource.restfuse.internal.RequestConfiguration_Test;
30 | import com.eclipsesource.restfuse.internal.RequestContextConfiguration_Test;
31 | import com.eclipsesource.restfuse.internal.RequestImpl_Test;
32 | import com.eclipsesource.restfuse.internal.callback.CallbackServer_Test;
33 | import com.eclipsesource.restfuse.internal.callback.CallbackServlet_Test;
34 | import com.eclipsesource.restfuse.internal.poll.PollStateImpl_Test;
35 |
36 |
37 | @RunWith( Suite.class )
38 | @SuiteClasses( {
39 | Assert_Test.class,
40 | CallbackResource_Test.class,
41 | DefaultCallbackResource_Test.class,
42 | Destination_Test.class,
43 | HttpJUnitRunner_Test.class,
44 | AuthenticationInfo_Test.class,
45 | CallbackServer_Test.class,
46 | CallbackServlet_Test.class,
47 | HttpTestStatement_Test.class,
48 | InternalRequest_Test.class,
49 | RequestConfiguration_Test.class,
50 | RequestContextConfiguration_Test.class,
51 | RequestImpl_Test.class,
52 | PollStateImpl_Test.class,
53 | Poll_Test.class,
54 | Status_Test.class,
55 | HttpTestStatementOrder_Test.class,
56 | Response_Test.class
57 | } )
58 |
59 | public class AllRestfuseTestSuite {
60 | }
61 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.eclipsesource.restfuse
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.pde.ManifestBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.pde.SchemaBuilder
20 |
21 |
22 |
23 |
24 |
25 | org.eclipse.pde.PluginNature
26 | org.eclipse.jdt.core.javanature
27 |
28 |
29 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/.settings/org.eclipse.jdt.ui.prefs:
--------------------------------------------------------------------------------
1 | #Fri Jul 29 11:29:44 CEST 2011
2 | eclipse.preferences.version=1
3 | formatter_profile=_pe
4 | formatter_settings_version=12
5 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/.settings/org.eclipse.pde.core.prefs:
--------------------------------------------------------------------------------
1 | #Mon Jul 25 11:09:19 CEST 2011
2 | eclipse.preferences.version=1
3 | pluginProject.extensions=false
4 | resolve.requirebundle=false
5 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Bundle-ManifestVersion: 2
3 | Bundle-Name: restfuse
4 | Bundle-SymbolicName: com.eclipsesource.restfuse
5 | Bundle-Version: 1.2.0.qualifier
6 | Bundle-Vendor: EclipseSource
7 | Bundle-RequiredExecutionEnvironment: JavaSE-1.6
8 | Export-Package: com.eclipsesource.restfuse;version="1.1.0",
9 | com.eclipsesource.restfuse.annotation;version="1.1.0"
10 | Import-Package: javax.servlet;version="[2.5.0,3.0.0)",
11 | javax.servlet.http;version="[2.5.0,3.0.0)",
12 | org.eclipse.jetty.continuation;version="[8.1.0,9.0.0)",
13 | org.eclipse.jetty.http;version="[8.1.0,9.0.0)",
14 | org.eclipse.jetty.io.nio;version="[8.1.0,9.0.0)",
15 | org.eclipse.jetty.security;version="[8.1.0,9.0.0)",
16 | org.eclipse.jetty.server;version="[8.1.0,9.0.0)",
17 | org.eclipse.jetty.servlet;version="[8.1.0,9.0.0)",
18 | org.eclipse.jetty.util;version="[8.1.0,9.0.0)",
19 | org.eclipse.jetty.util.component;version="[8.1.0,9.0.0)",
20 | org.junit;version="[4.9.0,5.0.0)",
21 | org.junit.rules;version="[4.9.0,5.0.0)",
22 | org.junit.runner;version="[4.9.0,5.0.0)",
23 | org.junit.runners;version="[4.9.0,5.0.0)",
24 | org.junit.runners.model;version="[4.9.0,5.0.0)"
25 | Bundle-ActivationPolicy: lazy
26 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/about.html:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 | About
7 |
8 |
9 | About This Content
10 |
11 | June 5, 2006
12 | License
13 |
14 | EclipseSource makes available all content in this plug-in ("Content"). Unless otherwise
15 | indicated below, the Content is provided to you under the terms and conditions of the
16 | Eclipse Public License Version 1.0 ("EPL"). A copy of the EPL is available
17 | at http://www.eclipse.org/legal/epl-v10.html.
18 | For purposes of the EPL, "Program" will mean the Content.
19 |
20 | If you did not receive this Content directly from EclipseSource, the Content is
21 | being redistributed by another party ("Redistributor") and different terms and conditions may
22 | apply to your use of any object code in the Content. Check the Redistributor's license that was
23 | provided with the Content. If no such license exists, contact the Redistributor.
24 |
25 |
26 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/build.properties:
--------------------------------------------------------------------------------
1 | source.. = src/
2 | output.. = bin/
3 | bin.includes = META-INF/,\
4 | .,\
5 | about.html
6 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.eclipsesource
8 | restfuse
9 | 1.0.0-SNAPSHOT
10 | ../build/com.eclipsesource.restfuse.releng
11 |
12 |
13 | com.eclipsesource
14 | com.eclipsesource.restfuse
15 | 1.2.0-SNAPSHOT
16 | eclipse-plugin
17 |
18 |
19 |
20 | findbugs
21 |
22 | false
23 |
24 | fullBuild
25 | true
26 |
27 |
28 |
29 |
30 |
31 |
32 | org.codehaus.mojo
33 | findbugs-maven-plugin
34 | ${findbugs-version}
35 |
36 | true
37 | false
38 |
39 |
40 |
41 |
42 | check
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/AuthenticationType.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import com.eclipsesource.restfuse.annotation.Authentication;
14 |
15 |
16 | /**
17 | * An AuthenticationType
can be used with the {@link Authentication}
18 | * annotation. Currently only BASIC and DIGEST authentication are supported
19 | */
20 | public enum AuthenticationType {
21 | BASIC, DIGEST
22 | }
23 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/CallbackResource.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import java.util.HashMap;
14 | import java.util.List;
15 | import java.util.Map;
16 |
17 | import com.eclipsesource.restfuse.annotation.Callback;
18 |
19 |
20 | /**
21 | * A CallbackResource
is used by the {@link Callback}
annotation. This
22 | * class contains methods that will be called when there is an incoming request form a callback.
23 | * Subclasses need to override such methods to handle the request.
24 | *
25 | * Instead of sub classing the Callback
directly you may want to sub class the
26 | * {@link DefaultCallbackResource}
which provides a default implementation to return
27 | * default responses.
28 | *
29 | * @see Callback
30 | * @see DefaultCallbackResource
31 | */
32 | public abstract class CallbackResource {
33 |
34 | private static class CallbackResponse implements Response {
35 |
36 | private final Status status;
37 | private final MediaType contentType;
38 | private final String body;
39 | private final Map> headers;
40 | private String url;
41 |
42 | public CallbackResponse( String url,
43 | Status status,
44 | MediaType contentType,
45 | String body,
46 | Map> headers )
47 | {
48 | this.url = url;
49 | this.status = status;
50 | this.contentType = contentType;
51 | this.body = body;
52 | this.headers = headers;
53 | }
54 |
55 | @Override
56 | public boolean hasBody() {
57 | return body != null;
58 | }
59 |
60 | @Override
61 | @SuppressWarnings( "unchecked" )
62 | public T getBody( Class type ) {
63 | if( type != String.class ) {
64 | throw new IllegalStateException( "CallbackResponse can only have Strings as body" );
65 | }
66 | return ( T )body;
67 | }
68 |
69 | @Override
70 | public String getBody() {
71 | return body;
72 | }
73 |
74 | @Override
75 | public MediaType getType() {
76 | return contentType;
77 | }
78 |
79 | @Override
80 | public Map> getHeaders() {
81 | Map> result = null;
82 | if( headers != null ) {
83 | result = new HashMap>( headers );
84 | }
85 | return result;
86 | }
87 |
88 | @Override
89 | public int getStatus() {
90 | return status.getStatusCode();
91 | }
92 |
93 | @Override
94 | public String getUrl() {
95 | return url;
96 | }
97 |
98 | }
99 |
100 | static Response createResponse( String url,
101 | Status status,
102 | MediaType contentType,
103 | String body,
104 | Map> headers )
105 | {
106 | return new CallbackResponse( url, status, contentType, body, headers );
107 | }
108 |
109 | /**
110 | * Will be called during an http get request.
111 | */
112 | public abstract Response get( Request request );
113 |
114 | /**
115 | * Will be called during an http post request.
116 | */
117 | public abstract Response post( Request request );
118 |
119 | /**
120 | * Will be called during an http put request.
121 | */
122 | public abstract Response put( Request request );
123 |
124 | /**
125 | * Will be called during an http delete request.
126 | */
127 | public abstract Response delete( Request request );
128 |
129 | /**
130 | * Will be called during an http head request.
131 | */
132 | public abstract Response head( Request request );
133 |
134 | /**
135 | * Will be called during an http options request.
136 | */
137 | public abstract Response options( Request request );
138 |
139 | }
140 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/DefaultCallbackResource.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import com.eclipsesource.restfuse.annotation.Callback;
14 |
15 |
16 | /**
17 | * The DefaultCallbackResource
is a default implementation for the
18 | * {@link CallbackResource}
which can be used within an http TestCase. All the methods
19 | * return a default http response with the 204 (no content) response code.
20 | *
21 | * @see Callback
22 | */
23 | public class DefaultCallbackResource extends CallbackResource {
24 |
25 | private static Response defaultResponse
26 | = CallbackResource.createResponse( null, Status.NO_CONTENT, MediaType.WILDCARD, null, null );
27 |
28 | @Override
29 | public Response get( Request request ) {
30 | return defaultResponse;
31 | }
32 |
33 | @Override
34 | public Response post( Request request ) {
35 | return defaultResponse;
36 | }
37 |
38 | @Override
39 | public Response put( Request request ) {
40 | return defaultResponse;
41 | }
42 |
43 | @Override
44 | public Response delete( Request request ) {
45 | return defaultResponse;
46 | }
47 |
48 | @Override
49 | public Response head( Request request ) {
50 | return defaultResponse;
51 | }
52 |
53 | @Override
54 | public Response options( Request request ) {
55 | return defaultResponse;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/Destination.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import java.net.MalformedURLException;
14 | import java.net.URL;
15 |
16 | import org.junit.rules.TestRule;
17 | import org.junit.runner.Description;
18 | import org.junit.runners.model.Statement;
19 |
20 | import com.eclipsesource.restfuse.annotation.Callback;
21 | import com.eclipsesource.restfuse.annotation.HttpTest;
22 | import com.eclipsesource.restfuse.internal.HttpTestStatement;
23 |
24 | /**
25 | * A Destination
marks a requirement for http tests. Before you can use the
26 | * {@link HttpTest}
annotation you need to instantiate a Destination
in
27 | * your TestCase. This rule does manage the {@link HttpTest}
and the
28 | * {@link Callback}
annotations of your test methods.
29 | *
30 | * A Destination
needs an url during instantiation. This url will be used within a
31 | * TestCase to send requests to. If the url is not valid an IllegalArgumentException
32 | * will be thrown.
33 | *
34 | * A simple test looks like this:
35 | *
36 | * @RunWith( HttpJUnitRunner.class )
37 | * public class Example {
38 | *
39 | * @Rule
40 | * public Destination destination = new Destination( "http://localhost" );
41 | *
42 | * @Context
43 | * private Response response;
44 | *
45 | * @HttpTest( method = Method.GET, path = "/test" )
46 | * public void testMethod() {
47 | * com.eclipsesource.restfuse.Assert.assertAccepted( response );
48 | * }
49 | * }
50 | *
51 | *
52 | *
53 | * @see HttpTest
54 | * @see Callback
55 | */
56 | public class Destination implements TestRule {
57 |
58 | private HttpTestStatement requestStatement;
59 | private final String baseUrl;
60 | private String proxyHost;
61 | private int proxyPort;
62 | private RequestContext context;
63 | private Object testObject;
64 |
65 | /**
66 | * Constructs a new Destination
object. An url is needed as parameter which will
67 | * be used in the whole test to send requests to.
68 | *
69 | * @param baseUrl The url to send requests to.
70 | *
71 | * @throws IllegalArgumentException Will be thrown when the baseUrl
is null or
72 | * not a valid url.
73 | */
74 | public Destination( Object testObject, String baseUrl) {
75 | checkBaseUrl( baseUrl );
76 | checkTestObject( testObject );
77 | this.testObject = testObject;
78 | this.baseUrl = baseUrl;
79 | this.context = new RequestContext();
80 | }
81 |
82 | /**
83 | * Constructs a new Destination
object and sets some proxy properties which will
84 | * be used when sending the HTTP request. An url is needed as parameter which will
85 | * be used in the whole test to send requests to. The passed proxy properties has to conform with
86 | * the proxy properties described here
87 | * http://docs.oracle.com/javase/1.5.0/docs/guide/net/properties.html
88 | *
89 | * @param baseUrl The url to send requests to.
90 | * @param proxyHost The value of the http.proxyHost property.
91 | * @param proxyPort The value of the http.proxyPort property.
92 | *
93 | * @throws IllegalArgumentException Will be thrown when the baseUrl
is null or
94 | * not a valid url.
95 | */
96 | public Destination( Object testObject, String baseUrl, String proxyHost, int proxyPort ) {
97 | this( testObject, baseUrl );
98 | this.proxyHost = proxyHost;
99 | this.proxyPort = proxyPort;
100 | this.context = new RequestContext();
101 | }
102 |
103 | /**
104 | * Access to context to define additional request properties at runtime
105 | * @return context to be manipulated
106 | */
107 | public RequestContext getRequestContext() {
108 | return context;
109 | }
110 |
111 | private void checkBaseUrl( String baseUrl ) {
112 | if( baseUrl == null ) {
113 | throw new IllegalArgumentException( "baseUrl must not be null" );
114 | }
115 | try {
116 | new URL( baseUrl );
117 | } catch( MalformedURLException mue ) {
118 | throw new IllegalArgumentException( "baseUrl has to be an URL" );
119 | }
120 | }
121 |
122 | private void checkTestObject( Object testObject ) {
123 | if( testObject == null ) {
124 | throw new IllegalArgumentException( "testObject must not be null." );
125 | }
126 | }
127 |
128 | @Override
129 | /**
130 | * Not meant for public use. This method will be invoked by the JUnit framework.
131 | */
132 | public Statement apply( Statement base, Description description ) {
133 | Statement result;
134 | if( hasAnnotation( description ) ) {
135 | requestStatement = new HttpTestStatement( base, description, testObject, baseUrl, proxyHost, proxyPort, context );
136 | result = requestStatement;
137 | } else {
138 | result = base;
139 | }
140 | return result;
141 | }
142 |
143 | private boolean hasAnnotation( Description description ) {
144 | return description.getAnnotation( HttpTest.class ) != null;
145 | }
146 |
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/HttpJUnitRunner.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import java.util.ArrayList;
14 | import java.util.Collections;
15 | import java.util.List;
16 |
17 | import org.junit.Test;
18 | import org.junit.runners.BlockJUnit4ClassRunner;
19 | import org.junit.runners.model.FrameworkMethod;
20 | import org.junit.runners.model.InitializationError;
21 |
22 | import com.eclipsesource.restfuse.annotation.HttpTest;
23 |
24 |
25 | /**
26 | * The HttpJUnitRunner
can be used in your TestCase to avoid the double annotation
27 | * of test methods with the Test
and {@link HttpTest} annotation. The
28 | * runner detects all {@link HttpTest}
annotated methods and executes them as normal
29 | * JUnit test methods.
30 | */
31 | public class HttpJUnitRunner extends BlockJUnit4ClassRunner {
32 |
33 | public HttpJUnitRunner( Class> klass ) throws InitializationError {
34 | super( klass );
35 | }
36 |
37 | @Override
38 | protected List computeTestMethods() {
39 | ArrayList result = new ArrayList();
40 | result.addAll( getTestClass().getAnnotatedMethods(HttpTest.class) );
41 | List testAnnotatedMethods = getTestClass().getAnnotatedMethods(Test.class);
42 | for( FrameworkMethod method : testAnnotatedMethods ) {
43 | if( !result.contains( method ) ) {
44 | result.add( method );
45 | }
46 | }
47 | Collections.sort( result, new HttpOrderComparator() );
48 | return result;
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/HttpOrderComparator.java:
--------------------------------------------------------------------------------
1 | package com.eclipsesource.restfuse;
2 |
3 | import java.util.Comparator;
4 |
5 | import org.junit.runners.model.FrameworkMethod;
6 |
7 | import com.eclipsesource.restfuse.annotation.HttpTest;
8 |
9 |
10 | public class HttpOrderComparator implements Comparator {
11 |
12 | @Override
13 | public int compare( FrameworkMethod method1, FrameworkMethod method2 ) {
14 | HttpTest annotation1 = method1.getAnnotation( HttpTest.class );
15 | HttpTest annotation2 = method2.getAnnotation( HttpTest.class );
16 | if( annotation1 != null && annotation2 != null ) {
17 | return Integer.valueOf( annotation1.order() )
18 | .compareTo( Integer.valueOf( annotation2.order() ) );
19 | }
20 | return 0;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/MediaType.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 |
14 | /**
15 | * The MediaType
enumeration contains values that can be used within a request as
16 | * the request's content type.
17 | *
18 | * @see Request
19 | * @see Response
20 | */
21 | public enum MediaType {
22 |
23 | WILDCARD( "*/*" ),
24 | APPLICATION_XML( "application/xml" ),
25 | APPLICATION_ATOM_XML( "application/atom+xml" ),
26 | APPLICATION_XHTML_XML( "application/xhtml+xml" ),
27 | APPLICATION_SVG_XML( "application/svg+xml" ),
28 | APPLICATION_JSON( "application/json" ),
29 | APPLICATION_FORM_URLENCODED( "application/x-www-form-urlencoded" ),
30 | MULTIPART_FORM_DATA( "multipart/form-data" ),
31 | APPLICATION_OCTET_STREAM( "application/octet-stream" ),
32 | TEXT_PLAIN( "text/plain" ),
33 | TEXT_XML( "text/xml" ),
34 | TEXT_HTML( "text/html" );
35 |
36 | private final String mimeType;
37 |
38 | private MediaType( String mimeType ) {
39 | this.mimeType = mimeType;
40 | }
41 |
42 | public String getMimeType() {
43 | return mimeType;
44 | }
45 |
46 | /**
47 | * The fromString()
method tries to convert a mime-type to the equivalent
48 | * MediaType
.
49 | */
50 | public static MediaType fromString( String type ) {
51 | MediaType result = null;
52 | MediaType[] values = values();
53 | for( MediaType value : values ) {
54 | if( value.getMimeType().equals( type ) ) {
55 | result = value;
56 | }
57 | }
58 | return result;
59 | }
60 |
61 | @Override
62 | public String toString() {
63 | return mimeType;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/Method.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import com.eclipsesource.restfuse.annotation.HttpTest;
14 |
15 | /**
16 | * The Method
enumeration contains values that represents http methods.
17 | * Such a method needs to be set when using the {@link HttpTest}
annotation on your
18 | * test method.
19 | */
20 | public enum Method {
21 | POST, GET, DELETE, PUT, HEAD, OPTIONS
22 | }
23 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/PollState.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import java.util.List;
14 |
15 | import com.eclipsesource.restfuse.annotation.Context;
16 | import com.eclipsesource.restfuse.annotation.Poll;
17 |
18 |
19 | /**
20 | * A PollState
acts as a consistent state during a poll series. The
21 | * PollState
object will be injected into a test object when it has a field of the
22 | * type PollState
which is annotated with the {@link Context}
23 | * annotation. After the injection it can be used to test the responses of the poll series.
24 | *
25 | * @see Poll
26 | * @see Context
27 | */
28 | public interface PollState {
29 |
30 | /**
31 | * Returns the number of the last request attempt. It can be used to get the current response
32 | * when it will be passed to the {@link PollState#getResponse(int)}
method.
33 | */
34 | int getTimes();
35 |
36 | /**
37 | * Aborts a poll series. After this method was called JUnit will continue with the next test
38 | * method.
39 | */
40 | void abort();
41 |
42 | /**
43 | * Returns all responses that were received during the poll series.
44 | */
45 | List getResponses();
46 |
47 | /**
48 | * Returns a single response for a specific request attempt with a poll series.
49 | */
50 | Response getResponse( int attempt ) throws IllegalArgumentException;
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/Request.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import java.util.List;
14 | import java.util.Map;
15 |
16 |
17 | /**
18 | * A Request
acts as a wrapper for an http request.
19 | */
20 | public interface Request {
21 |
22 | boolean hasBody();
23 |
24 | String getBody();
25 |
26 | MediaType getType();
27 |
28 | Map> getHeaders();
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/RequestContext.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others. All rights reserved. This program and the
3 | * accompanying materials are made available under the terms of the Eclipse Public License v1.0
4 | * which accompanies this distribution, and is available at
5 | * http://www.eclipse.org/legal/epl-v10.html
6 | *
7 | * Contributors: drejc (https://github.com/drejc)
8 | * Holger Staudacher - ongoing development
9 | * Dennis Crissman - added dynamic path segments
10 | *
11 | ******************************************************************************/
12 | package com.eclipsesource.restfuse;
13 |
14 | import java.util.HashMap;
15 | import java.util.Map;
16 |
17 | import com.eclipsesource.restfuse.annotation.Header;
18 |
19 | /**
20 | *
21 | * RequestContext holds additional data to be added to the request before execution like headers
22 | * (cookies) or dynamic path segments. The context is used to configure requests dynamically. A
23 | * {@link RequestContext} can be created used {@link Destination#getRequestContext()}.
24 | *
25 | *
26 | * @since 1.1
27 | */
28 | public class RequestContext {
29 |
30 | /**
31 | *
32 | * Name value collection of headers.
33 | *
34 | *
35 | * @since 1.1
36 | * @deprecated use {@link RequestContext#addHeader(String, String)}. Will be removed with 1.3!
37 | */
38 | public Map headers = new HashMap();
39 |
40 | private Map dynamicPathSegments = new HashMap();
41 |
42 | /**
43 | *
44 | * Adds a header attribute to a request.
45 | *
46 | * @see Header
47 | *
48 | * @since 1.2
49 | */
50 | public RequestContext addHeader( String name, String value ) {
51 | headers.put( name, value );
52 | return this;
53 | }
54 |
55 | /**
56 | *
57 | * Dynamic path segments.Example with Given: http://localhost/{version}/{id}/
58 | *
59 | *
60 | * Destination destination = new Destination( "http://localhost/{version}/" );
61 | * RequestContext context = destination.getRequestContext();
62 | * context.addPathSegment( "id", "12345" ).addPathSegment( "version", "1.1" );
63 | *
64 | *
65 | * Produces: http://localhost/1.1/12345/
66 | *
67 | *
68 | * @since 1.2
69 | */
70 | public RequestContext addPathSegment( String segment, String replacement ) {
71 | dynamicPathSegments.put( segment, replacement );
72 | return this;
73 | }
74 |
75 | public Map getHeaders() {
76 | return new HashMap( headers );
77 | }
78 |
79 | public Map getPathSegments() {
80 | return new HashMap( dynamicPathSegments );
81 | }
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/Response.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 | import java.util.List;
14 | import java.util.Map;
15 |
16 |
17 | /**
18 | * A Response
acts as a wrapper for an http response.
19 | */
20 | public interface Response {
21 |
22 | boolean hasBody();
23 |
24 | /**
25 | * @deprecated use getBody() instead. May removed within the next iterations
26 | */
27 | T getBody( Class type );
28 |
29 | String getBody();
30 |
31 | MediaType getType();
32 |
33 | Map> getHeaders();
34 |
35 | int getStatus();
36 |
37 | String getUrl();
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/Status.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse;
12 |
13 |
14 |
15 | /**
16 | * The Status
enumeration contains values that can be used within a request/response
17 | * to represent it's status code. All values available in the official RFC 2616
18 | * (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) are implemented.
19 | *
20 | * @see Request
21 | * @see Response
22 | */
23 | public enum Status {
24 |
25 | // Informational 1xx
26 | CONTINUE( 100, "Continue" ),
27 | SWITCHING_PROTOCOLS( 101, "Switching Protocols" ),
28 | // Successful 2xx
29 | OK( 200, "OK" ),
30 | CREATED( 201, "Created" ),
31 | ACCEPTED( 202, "Accepted" ),
32 | NON_AUTHORITATIVE_INFORMATION( 203, "Non-Authoritative Information" ),
33 | NO_CONTENT( 204, "No Content" ),
34 | RESET_CONTENT( 205, "Reset Content" ),
35 | PARTIAL_CONTENT( 206, "Partial Content" ),
36 | // Redirection 3xx
37 | MULTIPLE_CHOICES( 300, "Multiple Choices" ),
38 | MOVED_PERMANENTLY( 301, "Moved Permanently" ),
39 | FOUND( 302, "Found" ),
40 | SEE_OTHER( 303, "See Other" ),
41 | NOT_MODIFIED( 304, "Not Modified" ),
42 | USE_PROXY( 305, "Use Proxy" ),
43 | TEMPORARY_REDIRECT( 307, "Temporary Redirect" ),
44 | // Client Error 4xx
45 | BAD_REQUEST( 400, "Bad Request" ),
46 | UNAUTHORIZED( 401, "Unauthorized" ),
47 | PAYMENT_REQUIRED( 402, "Payment Required" ),
48 | FORBIDDEN( 403, "Forbidden" ),
49 | NOT_FOUND( 404, "Not Found" ),
50 | METHOD_NOT_ALLOWED( 405, "Method Not Allowed" ),
51 | NOT_ACCEPTABLE( 406, "Not Acceptable" ),
52 | PROXY_AUTHENTICATION_REQUIRED( 407, "Proxy Authentication Required" ),
53 | REQUEST_TIMEOUT( 408, "Request Timeout" ),
54 | CONFLICT( 409, "Conflict" ),
55 | GONE( 410, "Gone" ),
56 | LENGTH_REQUIRED( 411, "Length Required" ),
57 | PRECONDITION_FAILED( 412, "Precondition Failed" ),
58 | REQUEST_ENTITY_TOO_LARGE( 413, "Request Entity Too Large" ),
59 | REQUEST_URI_TOO_LONG( 414, "Request-URI Too Long" ),
60 | UNSUPPORTED_MEDIA_TYPE( 415, "Unsupported Media Type" ),
61 | REQUEST_RANGE_NOT_SATISFIABLE( 416, "Requested Range Not Satisfiable" ),
62 | EXPECTATION_FAILED( 417, "Expectation Failed" ),
63 | // Server Error 5xx
64 | INTERNAL_SERVER_ERROR( 500, "Internal Server Error" ),
65 | NOT_IMPLEMENTED( 501, "Not Implemented" ),
66 | BAD_GATEWAY( 502, "Bad Gateway" ),
67 | SERVICE_UNAVAILABLE( 503, "Service Unavailable" ),
68 | GATEWAY_TIMEOUT( 504, "Gateway Timeout" ),
69 | HTTP_VERSION_NOT_SUPPORTED( 505, "HTTP Version Not Supported" );
70 |
71 | public enum Family { INFORMATIONAL, SUCCESSFUL, REDIRECTION, CLIENT_ERROR, SERVER_ERROR, UNKNOWN }
72 |
73 | private final int code;
74 | private final String reason;
75 | private Family family;
76 |
77 | Status( int statusCode, String reasonPhrase ) {
78 | this.code = statusCode;
79 | this.reason = reasonPhrase;
80 | assignFamily();
81 | }
82 |
83 | private void assignFamily() {
84 | switch( code / 100 ) {
85 | case 1:
86 | this.family = Family.INFORMATIONAL;
87 | break;
88 | case 2:
89 | this.family = Family.SUCCESSFUL;
90 | break;
91 | case 3:
92 | this.family = Family.REDIRECTION;
93 | break;
94 | case 4:
95 | this.family = Family.CLIENT_ERROR;
96 | break;
97 | case 5:
98 | this.family = Family.SERVER_ERROR;
99 | break;
100 | default:
101 | this.family = Family.UNKNOWN;
102 | break;
103 | }
104 | }
105 |
106 | public int getStatusCode() {
107 | return code;
108 | }
109 |
110 | public Family getFamily() {
111 | return family;
112 | }
113 |
114 | public static Status forStatusCode( int code ) {
115 | Status[] values = Status.values();
116 | for( Status status : values ) {
117 | if( status.getStatusCode() == code ) {
118 | return status;
119 | }
120 | }
121 | return null;
122 | }
123 |
124 | @Override
125 | public String toString() {
126 | return reason;
127 | }
128 | }
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/annotation/Authentication.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.annotation;
12 |
13 | import java.lang.annotation.ElementType;
14 | import java.lang.annotation.Retention;
15 | import java.lang.annotation.RetentionPolicy;
16 | import java.lang.annotation.Target;
17 |
18 | import com.eclipsesource.restfuse.AuthenticationType;
19 |
20 |
21 | /**
22 | * The Authentication
can be used within a {@link HttpTest}
annotation
23 | * to send authentication information within an outgoing http request. Currently only BASIC and
24 | * DIGEST authentication are supported.
25 | *
26 | * @see AuthenticationType
27 | */
28 | @Retention( RetentionPolicy.RUNTIME )
29 | @Target( { ElementType.ANNOTATION_TYPE } )
30 | public @interface Authentication {
31 |
32 | /**
33 | * The type
attribute specifies the type of the authentication method. Currently
34 | * only BASIC and DIGEST authentication are supported. This may change in future.
35 | *
36 | * @see AuthenticationType
37 | */
38 | AuthenticationType type();
39 |
40 | String user();
41 |
42 | String password();
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/annotation/Callback.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.annotation;
12 |
13 | import java.lang.annotation.ElementType;
14 | import java.lang.annotation.Retention;
15 | import java.lang.annotation.RetentionPolicy;
16 | import java.lang.annotation.Target;
17 |
18 | import com.eclipsesource.restfuse.CallbackResource;
19 | import com.eclipsesource.restfuse.DefaultCallbackResource;
20 | import com.eclipsesource.restfuse.Destination;
21 |
22 |
23 | /**
24 | * The Callback
can be used for asynchronous HTTP tests that need a callback.
25 | * This means when the tested service needs to send something back within a separate request. At
26 | * this time the client needs to act as a service to accept the incoming request. See
27 | * this wiki for more information
28 | * about webhooks.
29 | *
30 | * Please note, that the Callback
annotation only works in combination with the
31 | * {@link HttpTest}
annotation. This means it can't be used standalone and it
32 | * has the same prerequisites as the {@link HttpTest}
.
33 | *
34 | * Once a request has finished on an http test method and the method has a Callback
35 | * annotation attached, a sever will be started before the test code will be executed. On this
36 | * server a servlet will be registered on the port specified in the port
attribute
37 | * using the path specified in the path
attribute. After the test code finished the
38 | * server waits until the timeout
has reached or the callback was called. If the
39 | * callback was not done than the test method fails. In any case the server shuts itself down after
40 | * the test method execution.
41 | *
42 | * Of course you want to test something when the callback arrives. Therefore you need to register
43 | * a resource using the resource
attribute. The value of this attributes has to be a
44 | * subclass of {@link CallbackResource}
. For convenience you can also subclass the
45 | * {@link DefaultCallbackResource}
which implements all methods and returns a default
46 | * response instead of sub classing the {@link CallbackResource}
directly.
47 | *
48 | * A simple callback looks like this:
49 | *
50 | * @RunWith( HttpJUnitRunner.class )
51 | * public class Example {
52 | *
53 | * @Rule
54 | * public Destination destination = new Destination( "http://localhost" );
55 | *
56 | * @Context
57 | * private Response response;
58 | *
59 | * private class TestCallbackResource extends DefaultCallbackResource {
60 | *
61 | * @Override
62 | * public Response post( Request request ) {
63 | * assertNotNull( request.getHeaders().get( "some header" ).get( 0 ) );
64 | * return super.post( request );
65 | * }
66 | * }
67 | *
68 | * @HttpTest( method = Method.GET, path = "/test" )
69 | * @Callback( port = 9090, path = "/callback", resource = TestCallbackResource.class, timeout = 10000 )
70 | * public void testMethod() {
71 | * com.eclipsesource.restfuse.Assert.assertAccepted( response );
72 | * }
73 | * }
74 | *
75 | *
76 | *
77 | * @see CallbackResource
78 | * @see DefaultCallbackResource
79 | * @see HttpTest
80 | * @see Destination
81 | */
82 | @Retention( RetentionPolicy.RUNTIME )
83 | @Target( { ElementType.METHOD } )
84 | public @interface Callback {
85 |
86 | /**
87 | * The port
attribute specifies the port on which the callback will be reachable
88 | * during the test method execution.
89 | */
90 | int port();
91 |
92 | /**
93 | * The path
attribute specifies the path on which the callback will be reachable
94 | * during the test method execution.
95 | */
96 | String path();
97 |
98 | /**
99 | * The resource
attribute specifies the class that will be instantiated and
100 | * handles the incoming requests from a callback. Instead of sub classing
101 | * {@link CallbackResource}
directly you can also subclass
102 | * {@link DefaultCallbackResource}
which implements all methods and returns a
103 | * default response.
104 | *
105 | * @see CallbackResource
106 | * @see DefaultCallbackResource
107 | */
108 | Class extends CallbackResource> resource();
109 |
110 | /**
111 | * The timeout
attribute specifies the milliseconds of the callback timeout. This
112 | * means, that if the callback was not called within the given time frame, the test method will
113 | * fail.
114 | */
115 | int timeout();
116 | }
117 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/annotation/Context.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.annotation;
12 |
13 | import java.lang.annotation.ElementType;
14 | import java.lang.annotation.Retention;
15 | import java.lang.annotation.RetentionPolicy;
16 | import java.lang.annotation.Target;
17 |
18 | import com.eclipsesource.restfuse.Destination;
19 | import com.eclipsesource.restfuse.PollState;
20 | import com.eclipsesource.restfuse.Response;
21 |
22 |
23 | /**
24 | * The Context
annotation can be used within a TestCase to get the
25 | * {@link Response}
or a {@link PollState}
of an
26 | * HTTP request injected after it succeeds.
27 | *
28 | * A simple example looks like this:
29 | *
30 | * @RunWith( HttpJUnitRunner.class )
31 | * public class Example {
32 | *
33 | * @Rule
34 | * public Destination destination = new Destination( "http://localhost" );
35 | *
36 | * @Context
37 | * private Response response;
38 | *
39 | * @HttpTest( method = Method.GET, path = "/test" )
40 | * public void testMethod() {
41 | * com.eclipsesource.restfuse.Assert.assertAccepted( response );
42 | * }
43 | * }
44 | *
45 | *
46 | *
47 | * @see HttpTest
48 | * @see Destination
49 | */
50 | @Retention( RetentionPolicy.RUNTIME )
51 | @Target( { ElementType.FIELD } )
52 | public @interface Context {
53 | // no content
54 | }
55 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/annotation/Header.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.annotation;
12 |
13 | import java.lang.annotation.ElementType;
14 | import java.lang.annotation.Retention;
15 | import java.lang.annotation.RetentionPolicy;
16 | import java.lang.annotation.Target;
17 |
18 | /**
19 | * The Header
annotation represents a single header attribute of an http request.
20 | * This headers can be set in a {@link HttpTest}
annotation attached to a test method.
21 | *
22 | *
23 | * @see HttpTest
24 | */
25 | @Retention( RetentionPolicy.RUNTIME )
26 | @Target( { ElementType.ANNOTATION_TYPE } )
27 | public @interface Header {
28 |
29 | /**
30 | * The name
attribute represents the name of the header element which comes before
31 | * the ":" within a request header.
32 | */
33 | String name();
34 |
35 | /**
36 | * The value
attribute represents the value of the header element which comes
37 | * after the ":" within a request header.
38 | */
39 | String value();
40 | }
41 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/annotation/HttpTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.annotation;
12 |
13 | import java.lang.annotation.ElementType;
14 | import java.lang.annotation.Retention;
15 | import java.lang.annotation.RetentionPolicy;
16 | import java.lang.annotation.Target;
17 |
18 | import com.eclipsesource.restfuse.AuthenticationType;
19 | import com.eclipsesource.restfuse.Destination;
20 | import com.eclipsesource.restfuse.HttpJUnitRunner;
21 | import com.eclipsesource.restfuse.MediaType;
22 | import com.eclipsesource.restfuse.Method;
23 | import com.eclipsesource.restfuse.Response;
24 | import com.eclipsesource.restfuse.internal.AuthenticationInfo;
25 |
26 |
27 | /**
28 | * The HttpTest
annotation tells JUnit that the public void
method
29 | * to which it is attached can be run as an http test case when it's coupled with a Test
30 | * annotation. Before the test method will be executed an request will be send to the service
31 | * configured via the Destination
rule and the annotation's parameters.
32 | * Please keep in mind that an HttpTest
only works when a
33 | * {@link Destination}
Rule exists in the TestCase.
34 | * If the request fails, the test fails. When the request succeeds, the Response
35 | * will be injected into the TestCase Object. The injection looks for a field of the type
36 | * {@link Response}
which is annotated with the {@link Context}
37 | * annotation. The response object can also be pulled using the getRespone()
method
38 | * on the {@link Destination}
rule.
39 | * You don't have to combine the {@link org.junit.Test}
and HttpTest
40 | * annotation when you are using the {@link HttpJUnitRunner}
. This runner detects all
41 | * HttpTest
annotated methods and executes them as test methods.
42 | *
43 | * To run the method, JUnit first constructs a fresh instance of the class. Then the request will
44 | * be executed and JUnit invokes the annotated method afterwards. Any exceptions thrown by the
45 | * test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed
46 | * to have succeeded.
47 | *
48 | * A simple http test looks like this:
49 | *
50 | * @RunWith( HttpJUnitRunner.class )
51 | * public class Example {
52 | *
53 | * @Rule
54 | * public Destination destination = new Destination( "http://localhost" );
55 | *
56 | * @Context
57 | * private Response response;
58 | *
59 | * @HttpTest( method = Method.GET, path = "/test" )
60 | * public void testMethod() {
61 | * com.eclipsesource.restfuse.Assert.assertAccepted( response );
62 | * }
63 | * }
64 | *
65 | *
66 | *
67 | */
68 | @Retention( RetentionPolicy.RUNTIME )
69 | @Target( { ElementType.METHOD } )
70 | public @interface HttpTest {
71 |
72 | /**
73 | * The method attribute tells the request which Http Method should be used.
74 | *
75 | * @see Method
76 | */
77 | Method method();
78 |
79 | /**
80 | * The path attribute will be attached to the base url of the {@link Destination}
81 | * rule. This enables the creation of many http test method within one TestCase using different
82 | * url paths for the same host.
83 | */
84 | String path();
85 |
86 | /**
87 | * The type attribute tells the request which "Content-Type" header it should send. The default
88 | * value is the wildcard type.
89 | *
90 | * @see MediaType
91 | */
92 | MediaType type() default MediaType.WILDCARD;
93 |
94 | /**
95 | * When the request can have an entity like POST or PUT requests the content can be set by the
96 | * file
attribute. The file has to be on the classpath of the TestCase. An
97 | * alternative can be to use the content
attribute. If both are defined the
98 | * file
attribute wins.
99 | */
100 | String file() default EMPTY;
101 |
102 | /**
103 | * When the request can have an entity like POST or PUT requests the content can be set by the
104 | * content
attribute. An alternative can be to use the file
attribute.
105 | * If both are defined the file
attribute wins.
106 | */
107 | String content() default EMPTY;
108 |
109 | /**
110 | * With the headers
the request headers can be set.
111 | *
112 | * @see Header
113 | */
114 | Header[] headers() default {};
115 |
116 | /**
117 | * PROVISIONAL API, MAY CHANGE OR VANISH IN FUTURE
118 | * The {@link AuthenticationInfo}
will be used to transmit authentication data.
119 | * Currently only BASIC and DIGEST authentication is supported.
120 | *
121 | * @see AuthenticationInfo
122 | * @see AuthenticationType
123 | */
124 | Authentication[] authentications() default {};
125 |
126 | /**
127 | * Order of test in case tests need to be executed in certain order. 1 = first, 2 = second, ...
128 | * By default order is set to 0
129 | *
130 | *
131 | * @since 1.2
132 | */
133 | int order() default 0;
134 |
135 | static final String EMPTY = "";
136 |
137 | }
138 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/annotation/Poll.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.annotation;
12 |
13 | import java.lang.annotation.ElementType;
14 | import java.lang.annotation.Retention;
15 | import java.lang.annotation.RetentionPolicy;
16 | import java.lang.annotation.Target;
17 |
18 | import com.eclipsesource.restfuse.Destination;
19 | import com.eclipsesource.restfuse.PollState;
20 | import com.eclipsesource.restfuse.Response;
21 |
22 |
23 | /**
24 | * The Poll
annotation can be used for asynchronous HTTP tests that need to poll
25 | * the service more than one times.
26 | *
27 | *
Please note, that the Poll
annotation only works in combination with the
28 | * {@link HttpTest}
annotation. This means it can't be used standalone and it
29 | * has the same prerequisites as the {@link HttpTest}
.
30 | *
31 | * By annotating a test method with Poll
you tell the framework to send the request
32 | * defined in the HttpTest
more than one times. The amount of requests can be
33 | * configured via the times
attribute.
34 | *
35 | * Once a single request has finished on an HTTP test method and the method has a
36 | * Poll
annotation attached, a {@link PollState} object will be injected into the
37 | * Test object. The {@link PollState} field needs to be annotated with the
38 | * {@link Context}
annotation and can be used to get a response for a specific
39 | * request during a poll series. The last response will be also injected when a
40 | * {@link Context}
annotated {@link Response}
field is available in the
41 | * test object. After the injection, the test method will be executed.
42 | *
43 | * A simple poll looks like this:
44 | *
45 | * @RunWith( HttpJUnitRunner.class )
46 | * public class Example {
47 | *
48 | * @Rule
49 | * public Destination destination = new Destination( "http://localhost" );
50 | *
51 | * @Context
52 | * private PollState pollState;
53 | *
54 | * @Context
55 | * private Response response;
56 | *
57 | * @HttpTest( method = Method.GET, path = "/test" )
58 | * @Poll( times = 5, interval = 500 )
59 | * public void testMethod() {
60 | * Response currentResponse = pollState.getResponse( pollState.getTimes() );
61 | * com.eclipsesource.restfuse.Assert.assertAccepted( currentResponse );
62 | * assertEquals( currentResponse, response );
63 | * }
64 | * }
65 | *
66 | *
67 | *
68 | * @see PollState
69 | * @see HttpTest
70 | * @see Destination
71 | * @see Context
72 | */
73 | @Retention( RetentionPolicy.RUNTIME )
74 | @Target( { ElementType.METHOD } )
75 | public @interface Poll {
76 |
77 | /**
78 | * The times
attribute specifies the amount of requests that needs to be send
79 | * before JUnit will continue with the next test method.
80 | */
81 | int times();
82 |
83 | /**
84 | * The interval
attribute specifies the idle time in milliseconds between two
85 | * requests in a poll series.
86 | */
87 | int interval();
88 | }
89 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/AuthenticationInfo.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import com.eclipsesource.restfuse.AuthenticationType;
14 |
15 |
16 | public class AuthenticationInfo {
17 |
18 | private final AuthenticationType type;
19 | private final String user;
20 | private final String password;
21 |
22 | public AuthenticationInfo( AuthenticationType type, String user, String password ) {
23 | this.type = type;
24 | this.user = user;
25 | this.password = password;
26 | }
27 |
28 | public AuthenticationType getType() {
29 | return type;
30 | }
31 |
32 | public String getUser() {
33 | return user;
34 | }
35 |
36 | public String getPassword() {
37 | return password;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/BasicStatement.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import org.junit.runners.model.Statement;
14 |
15 | import com.eclipsesource.restfuse.Response;
16 |
17 |
18 | public class BasicStatement extends Statement {
19 |
20 | private final Statement statement;
21 | private final HttpTestStatement base;
22 |
23 | public BasicStatement( Statement statement, HttpTestStatement base ) {
24 | this.base = base;
25 | this.statement = statement;
26 | }
27 |
28 | @Override
29 | public void evaluate() throws Throwable {
30 | Response response = base.sendRequest();
31 | base.tryInjectResponse( response );
32 | statement.evaluate();
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/HttpTestStatement.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import java.lang.reflect.Field;
14 | import java.util.Properties;
15 |
16 | import org.junit.runner.Description;
17 | import org.junit.runners.model.Statement;
18 |
19 | import com.eclipsesource.restfuse.Method;
20 | import com.eclipsesource.restfuse.RequestContext;
21 | import com.eclipsesource.restfuse.Response;
22 | import com.eclipsesource.restfuse.annotation.Callback;
23 | import com.eclipsesource.restfuse.annotation.Context;
24 | import com.eclipsesource.restfuse.annotation.HttpTest;
25 | import com.eclipsesource.restfuse.annotation.Poll;
26 | import com.eclipsesource.restfuse.internal.callback.CallbackStatement;
27 | import com.eclipsesource.restfuse.internal.poll.PollStatement;
28 |
29 |
30 | public class HttpTestStatement extends Statement {
31 |
32 | static final String HTTP_PROXY_HOST = "http.proxyHost";
33 | static final String HTTP_PROXY_PORT = "http.proxyPort";
34 |
35 | private final Statement base;
36 | private final Description description;
37 | private final Object target;
38 | private final String baseUrl;
39 | private final String proxyHost;
40 | private final int proxyPort;
41 | private final RequestContext context;
42 |
43 | public HttpTestStatement( Statement base,
44 | Description description,
45 | Object target,
46 | String baseUrl,
47 | String proxyHost,
48 | int proxyPort,
49 | RequestContext context)
50 | {
51 | this.base = base;
52 | this.description = description;
53 | this.target = target;
54 | this.baseUrl = baseUrl;
55 | this.proxyHost = proxyHost;
56 | this.proxyPort = proxyPort;
57 | this.context = context;
58 | }
59 |
60 | @Override
61 | public void evaluate() throws Throwable {
62 | setProxyProperties();
63 | try {
64 | doEvaluate();
65 | } finally {
66 | unsetProxyProperties();
67 | }
68 | }
69 |
70 | private void setProxyProperties() {
71 | if( proxyHost != null ) {
72 | System.setProperty( HTTP_PROXY_HOST, proxyHost );
73 | System.setProperty( HTTP_PROXY_PORT, String.valueOf( proxyPort ) );
74 | }
75 | }
76 |
77 | private void doEvaluate() throws Throwable {
78 | Statement delegate = new BasicStatement( base, this );
79 | if( needsCallback() ) {
80 | delegate = new CallbackStatement( base, this, description, target );
81 | } else if( needsPoll() ) {
82 | delegate = new PollStatement( base, this, description, target );
83 | }
84 | delegate.evaluate();
85 | }
86 |
87 | private void unsetProxyProperties() {
88 | Properties properties = System.getProperties();
89 | properties.remove( HTTP_PROXY_HOST );
90 | properties.remove( HTTP_PROXY_PORT );
91 | }
92 |
93 | private boolean needsCallback() {
94 | Callback callbackAnnotation = description.getAnnotation( Callback.class );
95 | return callbackAnnotation != null;
96 | }
97 |
98 | private boolean needsPoll() {
99 | Poll pollAnnotation = description.getAnnotation( Poll.class );
100 | return pollAnnotation != null;
101 | }
102 |
103 | public Response sendRequest() {
104 | InternalRequest request = buildRequest();
105 | return callService( request );
106 | }
107 |
108 | private InternalRequest buildRequest() {
109 | RequestConfiguration requestConfiguration = new RequestConfiguration( baseUrl, description, target );
110 | return requestConfiguration.createRequest( context );
111 | }
112 |
113 | private Response callService( InternalRequest request ) {
114 | Method requestMethod = description.getAnnotation( HttpTest.class ).method();
115 | Response result = null;
116 | if( requestMethod.equals( Method.GET ) ) {
117 | result = request.get();
118 | } else if( requestMethod.equals( Method.POST ) ) {
119 | result = request.post();
120 | } else if( requestMethod.equals( Method.DELETE ) ) {
121 | result = request.delete();
122 | } else if( requestMethod.equals( Method.PUT ) ) {
123 | result = request.put();
124 | } else if( requestMethod.equals( Method.HEAD ) ) {
125 | result = request.head();
126 | } else if( requestMethod.equals( Method.OPTIONS ) ) {
127 | result = request.options();
128 | }
129 | return result;
130 | }
131 |
132 | public void tryInjectResponse( Response response ) {
133 | Field[] fields = target.getClass().getDeclaredFields();
134 | for( Field field : fields ) {
135 | Context contextAnnotation = field.getAnnotation( Context.class );
136 | if( contextAnnotation != null && field.getType() == Response.class ) {
137 | injectResponse( field, response );
138 | }
139 | }
140 | }
141 |
142 | private void injectResponse( Field field, Response response ) {
143 | field.setAccessible( true );
144 | try {
145 | field.set( target, response );
146 | } catch( Exception exception ) {
147 | throw new IllegalStateException( "Could not inject response.", exception );
148 | }
149 | }
150 |
151 | }
152 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/InternalRequest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import java.io.InputStream;
14 | import java.util.ArrayList;
15 | import java.util.HashMap;
16 | import java.util.List;
17 | import java.util.Map;
18 | import java.util.Set;
19 |
20 | import com.eclipsesource.restfuse.AuthenticationType;
21 | import com.eclipsesource.restfuse.Response;
22 | import com.github.kevinsawicki.http.HttpRequest;
23 |
24 |
25 | public class InternalRequest {
26 |
27 | private final Map> headers;
28 | private final List authentications;
29 | private final String url;
30 | private InputStream content;
31 | private String mediaType;
32 |
33 | public InternalRequest( String url ) {
34 | this.url = url;
35 | headers = new HashMap>();
36 | authentications = new ArrayList();
37 | }
38 |
39 | public void setContentType( String mediaType ) {
40 | this.mediaType = mediaType;
41 | }
42 |
43 | public void setContent( InputStream content ) {
44 | this.content = content;
45 | }
46 |
47 | public void addHeader( String name, String value ) {
48 | List param = headers.get( name );
49 | if( param == null ) {
50 | List params = new ArrayList();
51 | params.add( value );
52 | headers.put( name, params );
53 | } else {
54 | param.add( value );
55 | }
56 | }
57 |
58 | public void addAuthenticationInfo( AuthenticationInfo authentication ) {
59 | authentications.add( authentication );
60 | }
61 |
62 | public Response get() {
63 | HttpRequest request = HttpRequest.get( url );
64 | addContentType( request );
65 | addHeaders( request );
66 | addAuthentication( request );
67 | sendRequest( request );
68 | return new ResponseImpl( request );
69 | }
70 |
71 | public Response post() {
72 | HttpRequest request = HttpRequest.post( url );
73 | addContentType( request );
74 | addHeaders( request );
75 | addAuthentication( request );
76 | request.send( content );
77 | sendRequest( request );
78 | return new ResponseImpl( request );
79 | }
80 |
81 | public Response delete() {
82 | HttpRequest request = HttpRequest.delete( url );
83 | addContentType( request );
84 | addHeaders( request );
85 | addAuthentication( request );
86 | sendRequest( request );
87 | return new ResponseImpl( request );
88 | }
89 |
90 | public Response put() {
91 | HttpRequest request = HttpRequest.put( url );
92 | addContentType( request );
93 | addHeaders( request );
94 | addAuthentication( request );
95 | request.send( content );
96 | sendRequest( request );
97 | return new ResponseImpl( request );
98 | }
99 |
100 | public Response head() {
101 | HttpRequest request = HttpRequest.head( url );
102 | addContentType( request );
103 | addHeaders( request );
104 | addAuthentication( request );
105 | sendRequest( request );
106 | return new ResponseImpl( request );
107 | }
108 |
109 | public Response options() {
110 | HttpRequest request = HttpRequest.options( url );
111 | addContentType( request );
112 | addHeaders( request );
113 | addAuthentication( request );
114 | sendRequest( request );
115 | return new ResponseImpl( request );
116 | }
117 |
118 |
119 | private void addContentType( HttpRequest request ) {
120 | String type = mediaType != null ? mediaType : "*/*";
121 | request.contentType( type );
122 | }
123 |
124 | private void addHeaders( HttpRequest request ) {
125 | Set keySet = headers.keySet();
126 | for( String key : keySet ) {
127 | List values = headers.get( key );
128 | StringBuilder builder = new StringBuilder();
129 | for( String value : values ) {
130 | builder.append( value + "," );
131 | }
132 | request.header( key, builder.substring( 0, builder.length() - 1 ) );
133 | }
134 | request.trustAllCerts();
135 | request.trustAllHosts();
136 | }
137 |
138 | private void addAuthentication( HttpRequest request ) {
139 | for( AuthenticationInfo authentication : authentications ) {
140 | if( authentication.getType().equals( AuthenticationType.BASIC ) ) {
141 | request.basic( authentication.getUser(), authentication.getPassword() );
142 | } else if( authentication.getType().equals( AuthenticationType.DIGEST ) ) {
143 | // TODO: implement digest auth
144 | }
145 | }
146 | }
147 |
148 | private void sendRequest( HttpRequest request ) {
149 | request.code();
150 | }
151 |
152 | String getUrl(){
153 | return url;
154 | }
155 | }
156 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/RequestConfiguration.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others. All rights reserved. This program and the
3 | * accompanying materials are made available under the terms of the Eclipse Public License v1.0
4 | * which accompanies this distribution, and is available at
5 | * http://www.eclipse.org/legal/epl-v10.html
6 | *
7 | * Contributors: Holger Staudacher - initial API and implementation
8 | *
9 | ******************************************************************************/
10 | package com.eclipsesource.restfuse.internal;
11 |
12 | import java.io.ByteArrayInputStream;
13 | import java.io.InputStream;
14 | import java.io.UnsupportedEncodingException;
15 | import java.net.URL;
16 | import java.util.Map;
17 | import java.util.regex.Matcher;
18 | import java.util.regex.Pattern;
19 |
20 | import org.junit.runner.Description;
21 |
22 | import com.eclipsesource.restfuse.AuthenticationType;
23 | import com.eclipsesource.restfuse.MediaType;
24 | import com.eclipsesource.restfuse.RequestContext;
25 | import com.eclipsesource.restfuse.annotation.Authentication;
26 | import com.eclipsesource.restfuse.annotation.Header;
27 | import com.eclipsesource.restfuse.annotation.HttpTest;
28 |
29 | public class RequestConfiguration {
30 |
31 | private static final String PATH_SEPARATOR = "/";
32 | private final String baseUrl;
33 | private final Description description;
34 | private final Object target;
35 |
36 | public RequestConfiguration( String baseUrl, Description description, Object target ) {
37 | this.baseUrl = baseUrl;
38 | this.description = description;
39 | this.target = target;
40 | }
41 |
42 | public InternalRequest createRequest( RequestContext context ) {
43 | HttpTest call = description.getAnnotation( HttpTest.class );
44 | String rawPath = combineUrlAndPath( baseUrl, call.path() );
45 | InternalRequest request = new InternalRequest( substituePathSegments( rawPath, context ) );
46 | addAuthentication( call, request );
47 | addContentType( call, request );
48 | addHeader( call, request, context );
49 | addBody( call, request );
50 | return request;
51 | }
52 |
53 | private String substituePathSegments( String path, RequestContext context ) {
54 | String substitutedPath = path;
55 | Pattern pattern = Pattern.compile( ".*?\\{(.*?)\\}.*?" );
56 | Matcher matcher = pattern.matcher( path );
57 | while( matcher.find() ) {
58 | String segment = matcher.group( 1 );
59 | checkSubstitutionExists( context, segment );
60 | substitutedPath = substitutedPath.replace( "{" + segment + "}",
61 | context.getPathSegments().get( segment ) );
62 | }
63 | return substitutedPath;
64 | }
65 |
66 | private void checkSubstitutionExists( RequestContext context, String segment ) {
67 | if( !context.getPathSegments().containsKey( segment ) ) {
68 | throw new IllegalStateException( "Misconfigured Destination. Could not replace {" + segment + "}." );
69 | }
70 | }
71 |
72 | private void addAuthentication( HttpTest call, InternalRequest request ) {
73 | Authentication[] authentications = call.authentications();
74 | if( authentications != null ) {
75 | for( Authentication authentication : authentications ) {
76 | AuthenticationType type = authentication.type();
77 | String user = authentication.user();
78 | String password = authentication.password();
79 | request.addAuthenticationInfo( new AuthenticationInfo( type, user, password ) );
80 | }
81 | }
82 | }
83 |
84 | private void addContentType( HttpTest call, InternalRequest request ) {
85 | MediaType contentType = call.type();
86 | if( contentType != null ) {
87 | request.setContentType( contentType.getMimeType() );
88 | }
89 | }
90 |
91 | private void addHeader( HttpTest call, InternalRequest request, RequestContext context ) {
92 | addHeadersFromContext( request, context );
93 | addHeadersFromAnnotation( call, request );
94 | }
95 |
96 | private void addHeadersFromContext( InternalRequest request, RequestContext context ) {
97 | if( context != null && !context.getHeaders().isEmpty() ) {
98 | Map headers = context.getHeaders();
99 | for( String name : headers.keySet() )
100 | request.addHeader( name, headers.get( name ) );
101 | }
102 | }
103 |
104 | private void addHeadersFromAnnotation( HttpTest call, InternalRequest request ) {
105 | Header[] header = call.headers();
106 | if( header != null ) {
107 | for( Header parameter : header ) {
108 | request.addHeader( parameter.name(), parameter.value() );
109 | }
110 | }
111 | }
112 |
113 | private void addBody( HttpTest test, InternalRequest request ) {
114 | if( !test.file().equals( "" ) ) {
115 | request.setContent( getFileStream( test.file() ) );
116 | } else if( !test.content().equals( "" ) ) {
117 | request.setContent( getContentStream( test.content() ) );
118 | }
119 | }
120 |
121 | private InputStream getFileStream( String file ) {
122 | URL resource = target.getClass().getResource( file );
123 | try {
124 | return resource.openStream();
125 | } catch( Exception ioe ) {
126 | throw new IllegalStateException( "Could not open file "
127 | + file
128 | + ". Maybe it's not on the classpath?" );
129 | }
130 | }
131 |
132 | private InputStream getContentStream( String content ) {
133 | try {
134 | return new ByteArrayInputStream( content.getBytes( "UTF-8" ) );
135 | } catch( UnsupportedEncodingException shouldNotHappen ) {
136 | throw new IllegalStateException( shouldNotHappen );
137 | }
138 | }
139 |
140 | private String combineUrlAndPath( String url, String pathValue ) {
141 | String result;
142 | if( url.endsWith( PATH_SEPARATOR ) && pathValue.startsWith( PATH_SEPARATOR ) ) {
143 | result = url + pathValue.substring( 1, pathValue.length() );
144 | } else if( ( !url.endsWith( PATH_SEPARATOR ) && pathValue.startsWith( PATH_SEPARATOR ) )
145 | || ( url.endsWith( PATH_SEPARATOR ) && !pathValue.startsWith( PATH_SEPARATOR ) ) )
146 | {
147 | result = url + pathValue;
148 | } else if( !url.endsWith( PATH_SEPARATOR ) && !pathValue.startsWith( PATH_SEPARATOR ) ) {
149 | result = url + PATH_SEPARATOR + pathValue;
150 | } else {
151 | throw new IllegalStateException( "Invalid url format with base url "
152 | + url
153 | + " and path "
154 | + pathValue );
155 | }
156 | return result;
157 | }
158 | }
159 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/RequestImpl.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import java.util.ArrayList;
14 | import java.util.HashMap;
15 | import java.util.List;
16 | import java.util.Map;
17 |
18 | import com.eclipsesource.restfuse.MediaType;
19 | import com.eclipsesource.restfuse.Request;
20 |
21 |
22 | public class RequestImpl implements Request {
23 |
24 |
25 | private Map> headers;
26 | private final MediaType contentType;
27 | private final String body;
28 |
29 | public RequestImpl( String body, MediaType contentType ) {
30 | this.body = body;
31 | this.contentType = contentType;
32 | headers = new HashMap>();
33 | }
34 |
35 | @Override
36 | public boolean hasBody() {
37 | return body != null;
38 | }
39 |
40 | @Override
41 | public String getBody() {
42 | return body;
43 | }
44 |
45 | @Override
46 | public MediaType getType() {
47 | return contentType;
48 | }
49 |
50 | @Override
51 | public Map> getHeaders() {
52 | return headers;
53 | }
54 |
55 | public void addHeader( String name, String value ) {
56 | List param = headers.get( name );
57 | if( param == null ) {
58 | List params = new ArrayList();
59 | params.add( value );
60 | headers.put( name, params );
61 | } else {
62 | param.add( value );
63 | }
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/ResponseImpl.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal;
12 |
13 | import java.util.List;
14 | import java.util.Map;
15 |
16 | import com.eclipsesource.restfuse.MediaType;
17 | import com.eclipsesource.restfuse.Response;
18 | import com.github.kevinsawicki.http.HttpRequest;
19 |
20 |
21 | public class ResponseImpl implements Response {
22 |
23 | private final String body;
24 | private final String contentType;
25 | private final Map> headers;
26 | private final int code;
27 | private final String url;
28 |
29 | public ResponseImpl( HttpRequest request ) {
30 | body = request.body();
31 | contentType = request.contentType();
32 | headers = request.headers();
33 | code = request.code();
34 | url = request.getConnection().getURL().toString();
35 | request.disconnect();
36 | }
37 |
38 | @Override
39 | public boolean hasBody() {
40 | return body != null;
41 | }
42 |
43 | @Override
44 | @SuppressWarnings( "unchecked" )
45 | public T getBody( Class type ) {
46 | if( type != String.class ) {
47 | throw new IllegalArgumentException( "Only String is supported. Not the this method is deprecated, see getBody()." );
48 | }
49 | return ( T )body;
50 | }
51 |
52 | @Override
53 | public String getBody() {
54 | return body;
55 | }
56 |
57 | @Override
58 | public MediaType getType() {
59 | return MediaType.fromString( contentType );
60 | }
61 |
62 | @Override
63 | public Map> getHeaders() {
64 | return headers;
65 | }
66 |
67 | @Override
68 | public int getStatus() {
69 | return code;
70 | }
71 |
72 | @Override
73 | public String getUrl() {
74 | return url;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/callback/CallbackSerlvet.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal.callback;
12 |
13 | import java.io.BufferedReader;
14 | import java.io.IOException;
15 | import java.util.Enumeration;
16 | import java.util.List;
17 | import java.util.Map;
18 | import java.util.Set;
19 |
20 | import javax.servlet.ServletException;
21 | import javax.servlet.http.HttpServlet;
22 | import javax.servlet.http.HttpServletRequest;
23 | import javax.servlet.http.HttpServletResponse;
24 |
25 | import com.eclipsesource.restfuse.CallbackResource;
26 | import com.eclipsesource.restfuse.MediaType;
27 | import com.eclipsesource.restfuse.Response;
28 | import com.eclipsesource.restfuse.Request;
29 | import com.eclipsesource.restfuse.internal.RequestImpl;
30 |
31 |
32 | public class CallbackSerlvet extends HttpServlet {
33 |
34 | private boolean wasCalled;
35 | private final CallbackResource resource;
36 | private final CallbackStatement statement;
37 |
38 | public CallbackSerlvet( CallbackResource resource, CallbackStatement callbackStatement ) {
39 | this.resource = resource;
40 | this.statement = callbackStatement;
41 | }
42 |
43 | @Override
44 | protected void doGet( HttpServletRequest req, HttpServletResponse resp )
45 | throws ServletException, IOException
46 | {
47 | try {
48 | createResponse( resp, resource.get( createRequest( req ) ) );
49 | } catch( Throwable failure ) {
50 | statement.failWithinCallback( failure );
51 | }
52 | wasCalled = true;
53 | }
54 |
55 | @Override
56 | protected void doPost( HttpServletRequest req, HttpServletResponse resp )
57 | throws ServletException, IOException
58 | {
59 | try {
60 | createResponse( resp, resource.post( createRequest( req ) ) );
61 | } catch( Throwable failure ) {
62 | statement.failWithinCallback( failure );
63 | }
64 | wasCalled = true;
65 | }
66 |
67 | @Override
68 | protected void doPut( HttpServletRequest req, HttpServletResponse resp )
69 | throws ServletException, IOException
70 | {
71 | try {
72 | createResponse( resp, resource.put( createRequest( req ) ) );
73 | } catch( Throwable failure ) {
74 | statement.failWithinCallback( failure );
75 | }
76 | wasCalled = true;
77 | }
78 |
79 | @Override
80 | protected void doDelete( HttpServletRequest req, HttpServletResponse resp )
81 | throws ServletException, IOException
82 | {
83 | try {
84 | createResponse( resp, resource.delete( createRequest( req ) ) );
85 | } catch( Throwable failure ) {
86 | statement.failWithinCallback( failure );
87 | }
88 | wasCalled = true;
89 | }
90 |
91 | @Override
92 | protected void doHead( HttpServletRequest req, HttpServletResponse resp )
93 | throws ServletException, IOException
94 | {
95 | try {
96 | createResponse( resp, resource.head( createRequest( req ) ) );
97 | } catch( Throwable failure ) {
98 | statement.failWithinCallback( failure );
99 | }
100 | wasCalled = true;
101 | }
102 |
103 | @Override
104 | protected void doOptions( HttpServletRequest req, HttpServletResponse resp )
105 | throws ServletException, IOException
106 | {
107 | try {
108 | createResponse( resp, resource.options( createRequest( req ) ) );
109 | } catch( Throwable failure ) {
110 | statement.failWithinCallback( failure );
111 | }
112 | wasCalled = true;
113 | }
114 |
115 | private Request createRequest( HttpServletRequest req ) {
116 | MediaType mediaType = MediaType.fromString( req.getContentType() );
117 | String body = getBody( req );
118 | RequestImpl result = new RequestImpl( body, mediaType );
119 | addHeaderToRequest( req, result );
120 | return result;
121 | }
122 |
123 | private void addHeaderToRequest( HttpServletRequest req, RequestImpl request ) {
124 | @SuppressWarnings( "rawtypes" )
125 | Enumeration headerNames = req.getHeaderNames();
126 | if( headerNames != null ) {
127 | while( headerNames.hasMoreElements() ) {
128 | String name = ( String )headerNames.nextElement();
129 | String value = req.getHeader( name );
130 | request.addHeader( name, value );
131 | }
132 | }
133 | }
134 |
135 | private String getBody( HttpServletRequest req ) {
136 | String result = null;
137 | try {
138 | BufferedReader reader = req.getReader();
139 | String buffer = "";
140 | StringBuilder stringBuilder = new StringBuilder();
141 | while( ( buffer = reader.readLine() ) != null ) {
142 | stringBuilder.append( buffer );
143 | }
144 | reader.close();
145 | result = stringBuilder.toString();
146 | } catch( IOException shouldNotHappen ) {
147 | throw new IllegalStateException( shouldNotHappen );
148 | }
149 | return result;
150 | }
151 |
152 | private void createResponse( HttpServletResponse resp, Response response ) throws IOException {
153 | resp.setStatus( response.getStatus() );
154 | addHeadersToResponse( resp, response );
155 | addContentTypeToResponse( resp, response );
156 | addBodyToResponse( resp, response );
157 | }
158 |
159 | private void addHeadersToResponse( HttpServletResponse resp, Response response ) {
160 | Map> headers = response.getHeaders();
161 | if( headers != null ) {
162 | Set keySet = headers.keySet();
163 | for( String key : keySet ) {
164 | List values = headers.get( key );
165 | for( String value : values ) {
166 | resp.setHeader( key, value );
167 | }
168 | }
169 | }
170 | }
171 |
172 | private void addContentTypeToResponse( HttpServletResponse resp, Response response ) {
173 | MediaType type = response.getType();
174 | if( type != null ) {
175 | resp.setContentType( type.getMimeType() );
176 | }
177 | }
178 |
179 | private void addBodyToResponse( HttpServletResponse resp, Response response ) throws IOException {
180 | if( response.hasBody() ) {
181 | String body = response.getBody();
182 | resp.getWriter().write( body );
183 | resp.getWriter().close();
184 | }
185 | }
186 |
187 | public boolean wasCalled() {
188 | return wasCalled;
189 | }
190 | }
191 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/callback/CallbackServer.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal.callback;
12 |
13 | import java.lang.reflect.Constructor;
14 |
15 | import org.eclipse.jetty.server.Server;
16 | import org.eclipse.jetty.servlet.ServletContextHandler;
17 | import org.eclipse.jetty.servlet.ServletHolder;
18 |
19 | import com.eclipsesource.restfuse.CallbackResource;
20 | import com.eclipsesource.restfuse.annotation.Callback;
21 |
22 |
23 | public class CallbackServer {
24 |
25 | private CallbackResource resource;
26 | private int timeout;
27 | private int port;
28 | private String path;
29 | private Server server;
30 | private CallbackSerlvet servlet;
31 | private final CallbackStatement statement;
32 |
33 | public CallbackServer( Callback callbackAnnotation, Object target, CallbackStatement callbackStatement ) {
34 | createResource( callbackAnnotation.resource(), target );
35 | this.statement = callbackStatement;
36 | timeout = callbackAnnotation.timeout();
37 | port = callbackAnnotation.port();
38 | path = callbackAnnotation.path();
39 | }
40 |
41 | private void createResource( Class extends CallbackResource> type, Object target ) {
42 | Thread currentThread = Thread.currentThread();
43 | ClassLoader originalClassLoader = currentThread.getContextClassLoader();
44 | try {
45 | currentThread.setContextClassLoader( target.getClass().getClassLoader() );
46 | doCreateResource( type, target );
47 | } finally {
48 | currentThread.setContextClassLoader( originalClassLoader );
49 | }
50 | }
51 |
52 | private void doCreateResource( Class extends CallbackResource> type, Object target ) {
53 | try {
54 | Constructor extends CallbackResource> constructor
55 | = type.getDeclaredConstructor( new Class[] { target.getClass() } );
56 | constructor.setAccessible( true );
57 | resource = constructor.newInstance( new Object[] { target } );
58 | } catch( Exception e ) {
59 | try {
60 | Constructor extends CallbackResource> constructor = type.getDeclaredConstructor();
61 | constructor.setAccessible( true );
62 | resource = constructor.newInstance();
63 | } catch( Exception shouldNotHappen ) {
64 | throw new IllegalStateException( "Could not create resource instance of type "
65 | + type.getName() + ". Is there an default constructor?", shouldNotHappen );
66 | }
67 | }
68 | }
69 |
70 | public void start() {
71 | configureServer();
72 | doStartServer();
73 | }
74 |
75 | private void configureServer() {
76 | server = new Server( port );
77 | ServletContextHandler context = new ServletContextHandler( server, "/", ServletContextHandler.SESSIONS );
78 | servlet = new CallbackSerlvet( resource, statement );
79 | context.addServlet( new ServletHolder( servlet ), path );
80 | }
81 |
82 | private void doStartServer() {
83 | try {
84 | server.start();
85 | } catch( Exception shouldNotHappen ) {
86 | throw new IllegalStateException( "Could not start Http Server", shouldNotHappen );
87 | }
88 | }
89 |
90 | public void stop() {
91 | try {
92 | server.stop();
93 | } catch( Exception shouldNotHappen ) {
94 | throw new IllegalStateException( "Could not stop Http Server", shouldNotHappen );
95 | }
96 | }
97 |
98 | public boolean wasCalled() {
99 | return servlet.wasCalled();
100 | }
101 |
102 | public int getTimeout() {
103 | return timeout;
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/callback/CallbackStatement.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal.callback;
12 |
13 | import static org.junit.Assert.fail;
14 |
15 | import org.junit.runner.Description;
16 | import org.junit.runners.model.Statement;
17 |
18 | import com.eclipsesource.restfuse.Response;
19 | import com.eclipsesource.restfuse.annotation.Callback;
20 | import com.eclipsesource.restfuse.internal.HttpTestStatement;
21 |
22 |
23 | public class CallbackStatement extends Statement {
24 |
25 | private static final int WAIT_TIME = 100;
26 |
27 | private HttpTestStatement base;
28 | private Description description;
29 | private Object target;
30 | private CallbackServer callbackServer;
31 | private final Object lock = new Object();
32 | private volatile String errorMessage;
33 |
34 | private final Statement statement;
35 |
36 | public CallbackStatement( Statement statement,
37 | HttpTestStatement base,
38 | Description description,
39 | Object target )
40 | {
41 | this.statement = statement;
42 | this.base = base;
43 | this.description = description;
44 | this.target = target;
45 | }
46 |
47 | @Override
48 | public void evaluate() throws Throwable {
49 | try {
50 | startCallbackServerWhenAvailable();
51 | Response response = base.sendRequest();
52 | base.tryInjectResponse( response );
53 | statement.evaluate();
54 | } finally {
55 | waitForCallbackWhenAvailable();
56 | }
57 | }
58 |
59 | private void startCallbackServerWhenAvailable() {
60 | Callback callbackAnnotation = description.getAnnotation( Callback.class );
61 | if( callbackAnnotation != null ) {
62 | callbackServer = new CallbackServer( callbackAnnotation, target, this );
63 | callbackServer.start();
64 | }
65 | }
66 |
67 | private void waitForCallbackWhenAvailable() {
68 | if( callbackServer != null ) {
69 | try {
70 | int waitTime = 0;
71 | while( !callbackServer.wasCalled() && waitTime <= callbackServer.getTimeout() ) {
72 | sleep();
73 | waitTime += WAIT_TIME;
74 | }
75 | checkForFailuresDuringCallback();
76 | checkCallbackWasCalled();
77 | } finally {
78 | callbackServer.stop();
79 | }
80 | }
81 | }
82 |
83 | private void sleep() {
84 | try {
85 | Thread.sleep( WAIT_TIME );
86 | } catch( InterruptedException shouldNotHappen ) {
87 | throw new IllegalStateException( "Could not wait until callback was called",
88 | shouldNotHappen );
89 | }
90 | }
91 |
92 | private void checkForFailuresDuringCallback() {
93 | synchronized( lock ) {
94 | if( errorMessage != null ) {
95 | fail( errorMessage );
96 | }
97 | }
98 | }
99 |
100 | private void checkCallbackWasCalled() {
101 | if( !callbackServer.wasCalled() ) {
102 | fail( "Callback was not called" );
103 | }
104 | }
105 |
106 | public void failWithinCallback( Throwable cause ) {
107 | synchronized( lock ) {
108 | errorMessage = cause.getMessage();
109 | }
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/poll/PollStateImpl.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal.poll;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | import com.eclipsesource.restfuse.PollState;
17 | import com.eclipsesource.restfuse.Response;
18 |
19 |
20 | public class PollStateImpl implements PollState {
21 |
22 | private final ArrayList responses;
23 | private boolean wasAborted;
24 |
25 | public PollStateImpl() {
26 | responses = new ArrayList();
27 | }
28 |
29 | @Override
30 | public int getTimes() {
31 | return responses.size();
32 | }
33 |
34 | @Override
35 | public void abort() {
36 | wasAborted = true;
37 | }
38 |
39 | boolean wasAborted() {
40 | return wasAborted;
41 | }
42 |
43 | @Override
44 | public List getResponses() {
45 | return new ArrayList( responses );
46 | }
47 |
48 | @Override
49 | public Response getResponse( int attempt ) throws IllegalArgumentException {
50 | if( ( attempt - 1 ) > responses.size() ) {
51 | throw new IllegalArgumentException( "Response does not exist for attemt " + attempt );
52 | }
53 | return responses.get( attempt - 1 );
54 | }
55 |
56 | void addResponse( Response response ) {
57 | responses.add( response );
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/internal/poll/PollStatement.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 EclipseSource and others.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Holger Staudacher - initial API and implementation
10 | ******************************************************************************/
11 | package com.eclipsesource.restfuse.internal.poll;
12 |
13 | import java.lang.reflect.Field;
14 |
15 | import org.junit.runner.Description;
16 | import org.junit.runners.model.Statement;
17 |
18 | import com.eclipsesource.restfuse.PollState;
19 | import com.eclipsesource.restfuse.Response;
20 | import com.eclipsesource.restfuse.annotation.Context;
21 | import com.eclipsesource.restfuse.annotation.Poll;
22 | import com.eclipsesource.restfuse.internal.HttpTestStatement;
23 |
24 |
25 | public class PollStatement extends Statement {
26 |
27 | private final Statement statement;
28 | private final HttpTestStatement base;
29 | private int interval;
30 | private int times;
31 | private final Object target;
32 | private PollStateImpl pollState;
33 |
34 | public PollStatement( Statement statement,
35 | HttpTestStatement base,
36 | Description description,
37 | Object target )
38 | {
39 | this.statement = statement;
40 | this.base = base;
41 | this.target = target;
42 | Poll pollAnnotation = description.getAnnotation( Poll.class );
43 | interval = pollAnnotation.interval();
44 | times = pollAnnotation.times();
45 | pollState = new PollStateImpl();
46 | }
47 |
48 | @Override
49 | public void evaluate() throws Throwable {
50 | for( int i = 0; i < times && !pollState.wasAborted(); i++ ) {
51 | doSingleEvaluate();
52 | }
53 | }
54 |
55 | private void doSingleEvaluate() throws Throwable {
56 | Response response = base.sendRequest();
57 | base.tryInjectResponse( response );
58 | tryToInjectPollState( response );
59 | statement.evaluate();
60 | sleep();
61 | }
62 |
63 | private void tryToInjectPollState( Response response ) {
64 | pollState.addResponse( response );
65 | Field[] fields = target.getClass().getDeclaredFields();
66 | for( Field field : fields ) {
67 | Context contextAnnotation = field.getAnnotation( Context.class );
68 | if( contextAnnotation != null && field.getType() == PollState.class ) {
69 | injectPollState( field );
70 | }
71 | }
72 | }
73 |
74 | private void injectPollState( Field field ) {
75 | field.setAccessible( true );
76 | try {
77 | field.set( target, pollState );
78 | } catch( Exception exception ) {
79 | throw new IllegalStateException( "Could not inject pollstate.", exception );
80 | }
81 | }
82 |
83 | private void sleep() {
84 | try {
85 | Thread.sleep( interval );
86 | } catch( InterruptedException shouldNotHappen ) {
87 | throw new IllegalStateException( "Could not sleep until the next poll", shouldNotHappen );
88 | }
89 | }
90 | }
--------------------------------------------------------------------------------