Common.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package hello;
  2. import java.io.IOException;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.concurrent.ThreadLocalRandom;
  10. import com.fasterxml.jackson.core.JsonGenerator;
  11. import com.fasterxml.jackson.databind.*;
  12. import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
  13. import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
  14. /**
  15. * Some common functionality and constants used by the Servlet tests.
  16. */
  17. public class Common {
  18. private static final int DB_ROWS = 10000;
  19. // Constants for setting the content type.
  20. protected static final String HEADER_CONTENT_TYPE = "Content-Type";
  21. protected static final String CONTENT_TYPE_JSON = "application/json";
  22. protected static final String CONTENT_TYPE_TEXT = "text/plain";
  23. protected static final String CONTENT_TYPE_HTML = "text/html";
  24. // Jackson encoder, reused for each response.
  25. protected static final ObjectMapper MAPPER = new ObjectMapper();
  26. // Jackson encoder with AfterBurner module
  27. protected static final ObjectMapper AF_MAPPER = new ObjectMapper().registerModule(new AfterburnerModule());
  28. private static final String DB_QUERY = "SELECT * FROM world";
  29. // Response message class.
  30. public static class HelloMessage {
  31. public final String message = "Hello, World!";
  32. }
  33. // Response message class with custom Jackson serializer
  34. public static class HelloMessageCJS implements JsonSerializable {
  35. public final String message = "Hello, World!";
  36. @Override
  37. public void serialize(JsonGenerator jg, SerializerProvider sp) throws IOException {
  38. jg.writeStartObject();
  39. jg.writeStringField("message", this.message);
  40. jg.writeEndObject();
  41. }
  42. @Override
  43. public void serializeWithType(JsonGenerator jg, SerializerProvider sp,
  44. TypeSerializer ts) throws IOException {
  45. throw new UnsupportedOperationException("Not needed so far.");
  46. }
  47. }
  48. // Request parameter checking and normalisation
  49. public static int normalise(String param) {
  50. int count = 1;
  51. try {
  52. count = Integer.parseInt(param);
  53. // Bounds checks
  54. if (count > 500) {
  55. return 500;
  56. } else if (count < 1) {
  57. return 1;
  58. }
  59. } catch (NumberFormatException nfexc) {
  60. }
  61. return count;
  62. }
  63. // Preload all the data for the cache query test
  64. public static Map<Integer, CachedWorld> loadAll(Connection connection) throws SQLException{
  65. // Fetch all rows from the database.
  66. final Map<Integer, CachedWorld> worlds = new HashMap<Integer, CachedWorld>();
  67. try (Connection conn = connection;
  68. PreparedStatement statement = conn.prepareStatement(DB_QUERY,
  69. ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
  70. ResultSet results = statement.executeQuery()) {
  71. while (results.next()) {
  72. CachedWorld some = new CachedWorld(results.getInt("id"), results.getInt("randomNumber"));
  73. worlds.put(new Integer(some.getId()), some);
  74. }
  75. }
  76. return worlds;
  77. }
  78. // Modify the SQL connection settings
  79. public static void modifySQLConnectionSettings(Connection connection) throws SQLException {
  80. connection.setAutoCommit(true);
  81. }
  82. public static int getRandom() {
  83. return 1 + ThreadLocalRandom.current().nextInt(DB_ROWS);
  84. }
  85. }