åä½ãã¹ã㧠ImagesService#applyTransform ã使ãæ¹æ³
slim3ã§ã¯GAEã®ç°å¢ã«ä¾åãããããªåä½ãã¹ããããªãç°¡åã«è¡ãäºãã§ãã¾ããä¾ãã°ãBigtableã¸ã®ã¢ã¯ã»ã¹ã¯ãã¹ãã®å®è¡æ¯ã«ãªã»ããããããã°ãã¼ãã«ãã©ã³ã¶ã¯ã·ã§ã³ã«å¯¾å¿ãªã©è³ããã¤ãããã§ãã
ã¨ããããç»åãå å·¥ãã¦ãµã ãã¤ã«ãä½ãæãªã©ã«ä½¿ç¨ãã ImagesServiceã§ããããããåä½ãã¹ãã§å®è¡ãããã¨ããã¨ä¾å¤ãçºçãã¾ããå é¨çã«ã¯ãç»åã®ä½æãä¸é¨ã®è»½ãå¦çã¯Implã§è¡ã£ã¦ããããã§ãããæ¡å¤§ç¸®å°ãªã©ã®å¤æå¦çã¯åã¢ããªã§ããã®ã§ã¯ãªããå¤é¨ãµã¼ãã¹ã¨ãã¦å¼ã³åºãã¦ããããã§ãããããã®ãã¹ãç°å¢ã§ã®ã¨ãã¥ã¬ã¼ãã¯ãorg.slim3.tester.AppEngineTesterã®makeSyncCallã§è¡ã£ã¦ãã¾ãã®ã§ããããå°ãã«ã¹ã¿ãã¤ãºãããã¨ã§ImagesServiceãã¨ãã¥ã¬ã¼ãã§ããããã«ãªãã¾ãã
以ä¸ãç°¡åãªãµã³ãã«ã«ãªãã¾ãã
public class MyTester extends org.slim3.tester.ControllerTester { private static final String IMAGE_SERVICE = "images"; public final ImageServiceStub imageServiceStub = new ImageServiceStub(); @Override public byte[] makeSyncCall(Environment env, String service, String method, byte[] requestBuf) throws ApiProxyException { if (service.equals(IMAGE_SERVICE)) { Queue<byte[]> queue = imageServiceStub.imageStore.get(method); if (queue == null || queue.isEmpty()) throw new ApiProxyException("service=" + service + ", method=" + method); ImagesServicePb.ImagesTransformResponse res = new ImagesServicePb.ImagesTransformResponse(); ImagesServicePb.ImageData data = new ImagesServicePb.ImageData(); data.setContentAsBytes(queue.peek()); res.setImage(data); return res.toByteArray(); } return super.makeSyncCall(env, service, method, requestBuf); } public class ImageServiceStub { Map<String, Queue<byte[]>> imageStore = new HashMap<String, Queue<byte[]>>(); public void register(String method, byte[] imageData) { Queue<byte[]> queue = imageStore.get(method); if (queue == null) { queue = new LinkedList<byte[]>(); imageStore.put(method, queue); } queue.add(imageData); } } }
makeSyncCallã¯ãµã¼ãã¹åã¨å®è¡ã¡ã½ããåãå¼æ°ã«å¼ã³åºããã¾ããImageServiceã®å ´åimagesãªã®ã§ãããããã³ãã«å¦çãä¸æ¸ããã¾ããããrequestBufã¯å¼æ°ã®æ
å ±ãªã©ãæ ¼ç´ããã¦ããã®ã§ãå¿
è¦ãªå ´åãé©åãªãªãã¸ã§ã¯ãã«æ»ãã¾ãï¼ããã§ã¯ImagesServicePb.ImagesTransformRequestï¼ãåæ§ã«æ»ãå¤ãbyteã«å¤æãã¾ãããåæ§ã«ImagesServicePb.ImagesTransformResponseãä½æããtoByteArrayã§byteã«æ»ãã¦ããè¿ãã¾ãã
å¾ã¯å¤æå¦çã®ã¤ã¡ã¼ã¸ã«ã¤ãã¦ãé©å½ã«ç»é²ã§ããããã«ããã¡ã½ããã®å¼ã³åºãé ã«è¿ãããã«ããã ãã§ãã
å®éã«ãã¹ãããå ´åã¯ãããªæãã
@Test public void upload_post() throws Exception { // ImageServiceã§è¿å´ãããç»åãç»é² tester.imageServiceStub.register("Transform", BinLoader.load("thumbnail.jpg")); FileItem item = new FileItem("sample.jpg", "image/jpg", BinLoader.load("sample.jpg")); tester.request.setMethod("post"); tester.request.setAttribute("file", item); tester.start("/_mng/media/upload"); assertThat(tester.response.getStatus(), is(HttpServletResponse.SC_MOVED_TEMPORARILY)); assertThat(tester.isRedirect(), is(true)); assertThat(tester.getDestinationPath(), is("/_mng/media/")); }
ãã®è¾ºã¯ã @shin1ogawa ããã®エントリーãé常ã«åèã«ãªãã¾ãã®ã§ããããã¦åç §ãã ããã