-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[breaking][trivially] ScalatraTests support for Jetty7 and Jetty8. [f…
…ixes #129] The signature of addFilter changed incompatibly from Jetty7 to Jetty8. This is a reprehensible hack to support both with the same version of Scalatra. We don't need to add any more Cross Build Hell.™ If this broke your code, replace: import org.eclipse.jetty.server.DispatcherType with import org.scalatra.test.DispatcherType
- Loading branch information
1 parent
5e5aa82
commit bd2a038
Showing
7 changed files
with
160 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
test/jetty8/src/test/scala/org/scalatra/test/jetty8/AddFilterSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.scalatra | ||
package test.jetty8 | ||
|
||
import java.util.EnumSet | ||
import javax.servlet.{DispatcherType => ServletDispatcherType, _} | ||
import org.specs2._ | ||
import test.DispatcherType | ||
import DispatcherType._ | ||
import test.specs2.ScalatraSpec | ||
|
||
class AddFilterSpecFilter extends Filter { | ||
def init(config: FilterConfig) = {} | ||
def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain) = { | ||
res.getWriter.write(req.getDispatcherType.name) | ||
} | ||
def destroy() = {} | ||
} | ||
|
||
class AddFilterSpec extends ScalatraSpec { def is = | ||
"Adding a filter instance should" ^ | ||
"dispatch on the specified dispatcher type" ! e1^ | ||
"not dispatch on unspecified dispatcher types" ! e2^ | ||
p^ | ||
"Adding a filter by class should" ^ | ||
"dispatch on the specified dispatcher type" ! e3^ | ||
"not dispatch on unspecified dispatcher types" ! e4 | ||
|
||
addFilter(new AddFilterSpecFilter, "/instance/request", EnumSet.of(REQUEST)) | ||
addFilter(new AddFilterSpecFilter, "/instance/not-request", EnumSet.complementOf(EnumSet.of(REQUEST))) | ||
addFilter(classOf[AddFilterSpecFilter], "/class/request", EnumSet.of(REQUEST)) | ||
addFilter(classOf[AddFilterSpecFilter], "/class/not-request", EnumSet.complementOf(EnumSet.of(REQUEST))) | ||
|
||
def e1 = get("/instance/request") { | ||
body must_== "REQUEST" | ||
} | ||
|
||
def e2 = get("/instance/not-request") { | ||
status must_== 404 | ||
} | ||
|
||
def e3 = get("/class/request") { | ||
body must_== "REQUEST" | ||
} | ||
|
||
def e4 = get("/class/not-request") { | ||
status must_== 404 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.scalatra.test; | ||
|
||
import java.util.*; | ||
import org.apache.commons.lang3.reflect.MethodUtils; | ||
|
||
public enum DispatcherType { | ||
REQUEST(1), | ||
FORWARD(2), | ||
INCLUDE(4), | ||
ERROR(8), | ||
ASYNC(16); | ||
|
||
private int intValue; | ||
|
||
private DispatcherType(int intValue) { | ||
this.intValue = intValue; | ||
} | ||
|
||
public int intValue() { | ||
return intValue; | ||
} | ||
|
||
public static int intValue(Set<DispatcherType> dispatcherTypes) { | ||
int value = 0; | ||
for (DispatcherType dispatcherType : dispatcherTypes) { | ||
value |= dispatcherType.intValue(); | ||
} | ||
return value; | ||
} | ||
|
||
@SuppressWarnings(value = "unchecked") // yeah... I know | ||
public static <E extends Enum<E>> EnumSet<E> convert( | ||
Set<DispatcherType> dispatcherTypes, String className) | ||
throws Exception | ||
{ | ||
Set<E> result = new HashSet<E>(); | ||
Class<?> enumClass = Class.forName(className); | ||
for (DispatcherType dispatcherType : dispatcherTypes) { | ||
result.add((E) MethodUtils.invokeStaticMethod(Enum.class, "valueOf", | ||
enumClass, dispatcherType.name())); | ||
} | ||
return EnumSet.copyOf(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.scalatra.test | ||
|
||
import org.apache.commons.lang3.reflect.MethodUtils | ||
|
||
object Reflection { | ||
def invokeMethod(target: AnyRef, methodName: String, args: AnyRef*): Either[Throwable, AnyRef] = | ||
try { | ||
Right(MethodUtils.invokeMethod(target, methodName, args :_*)) | ||
} | ||
catch { | ||
case e => Left(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
test/src/test/scala/org/scalatra/test/DispatcherTypeSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.scalatra.test | ||
|
||
import java.util.EnumSet | ||
import org.specs2.mutable._ | ||
import DispatcherType._ | ||
|
||
class DispatcherTypeSpec extends Specification { | ||
"A set of dispatcher types" should { | ||
"have an int value equal to the bitwise or of its members" in { | ||
intValue(EnumSet.of(REQUEST, INCLUDE, ASYNC)) must_== 21 | ||
} | ||
} | ||
} |