|
| 1 | +package com.genexus; |
| 2 | + |
| 3 | +import com.genexus.specific.java.Connect; |
| 4 | +import com.genexus.specific.java.ImagesPath; |
| 5 | +import com.genexus.specific.java.LogManager; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Rule; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.rules.TemporaryFolder; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.nio.file.Paths; |
| 15 | + |
| 16 | +public class TestGxImageUtil { |
| 17 | + |
| 18 | + private String FILE_NAME = "bird-thumbnail.jpg"; |
| 19 | + private String FILE_NAME_COPY = "bird-thumbnail-%s-%s.jpg"; |
| 20 | + |
| 21 | + private int IMAGE_HEIGHT = 900; |
| 22 | + private int IMAGE_WIDTH = 720; |
| 23 | + |
| 24 | + @Rule |
| 25 | + public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testImageWidth() |
| 29 | + { |
| 30 | + int imageWidth = GxImageUtil.getImageWidth(initialize("imageWidth")); |
| 31 | + Assert.assertEquals(IMAGE_WIDTH, imageWidth); |
| 32 | + } |
| 33 | + |
| 34 | + private String initialize(String name) |
| 35 | + { |
| 36 | + Connect.init(); |
| 37 | + LogManager.initialize("."); |
| 38 | + |
| 39 | + String copiedFileName = String.format(FILE_NAME_COPY, name, java.util.UUID.randomUUID().toString()); |
| 40 | + File resourcesDirectory = new File("src/test/resources"); |
| 41 | + Path originalFileLocation = Paths.get(resourcesDirectory.getPath(), FILE_NAME); |
| 42 | + Path copyFileLocation = Paths.get(tempFolder.getRoot().getPath(), copiedFileName); |
| 43 | + |
| 44 | + try { |
| 45 | + Files.copy(originalFileLocation, copyFileLocation); |
| 46 | + } |
| 47 | + catch (Exception e) { |
| 48 | + e.printStackTrace(); |
| 49 | + } |
| 50 | + |
| 51 | + System.out.println(copyFileLocation.toString()); |
| 52 | + return copyFileLocation.toString(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testImageHeight() |
| 57 | + { |
| 58 | + String fileName = initialize("testHeight"); |
| 59 | + int imageHeight = GxImageUtil.getImageHeight(fileName); |
| 60 | + Assert.assertEquals(IMAGE_HEIGHT, imageHeight); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testImageScale() |
| 65 | + { |
| 66 | + String fileName = initialize("scaled"); |
| 67 | + short scale = 50; |
| 68 | + |
| 69 | + String imagePath = GxImageUtil.scale(fileName, scale); |
| 70 | + |
| 71 | + int imageHeight = GxImageUtil.getImageHeight(imagePath); |
| 72 | + Assert.assertEquals(IMAGE_HEIGHT * scale / 100, imageHeight); |
| 73 | + |
| 74 | + int imageWidth = GxImageUtil.getImageWidth(imagePath); |
| 75 | + Assert.assertEquals(IMAGE_WIDTH * scale / 100, imageWidth); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testImageCrop() |
| 80 | + { |
| 81 | + String fileName = initialize("croped"); |
| 82 | + String imagePath = GxImageUtil.crop(fileName, 10, 10, 300, 400); |
| 83 | + |
| 84 | + int imageHeight = GxImageUtil.getImageHeight(imagePath); |
| 85 | + Assert.assertEquals(400, imageHeight); |
| 86 | + |
| 87 | + int imageWidth = GxImageUtil.getImageWidth(imagePath); |
| 88 | + Assert.assertEquals(300, imageWidth); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testImageResize() |
| 94 | + { |
| 95 | + String fileName = initialize("resized"); |
| 96 | + String imagePath = GxImageUtil.resize(fileName, 300, 400, false); |
| 97 | + |
| 98 | + int imageHeight = GxImageUtil.getImageHeight(imagePath); |
| 99 | + Assert.assertEquals(400, imageHeight); |
| 100 | + |
| 101 | + int imageWidth = GxImageUtil.getImageWidth(imagePath); |
| 102 | + Assert.assertEquals(300, imageWidth); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testImageFlipHorizontally() |
| 108 | + { |
| 109 | + String fileName = initialize("flippedHorizontally"); |
| 110 | + String imagePath = GxImageUtil.flipHorizontally(fileName); |
| 111 | + |
| 112 | + int imageHeight = GxImageUtil.getImageHeight(imagePath); |
| 113 | + Assert.assertEquals(IMAGE_HEIGHT, imageHeight); |
| 114 | + |
| 115 | + int imageWidth = GxImageUtil.getImageWidth(imagePath); |
| 116 | + Assert.assertEquals(IMAGE_WIDTH, imageWidth); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testImageFlipVertically() |
| 121 | + { |
| 122 | + String fileName = initialize("flippedVertically"); |
| 123 | + String imagePath = GxImageUtil.flipVertically(fileName); |
| 124 | + |
| 125 | + int imageHeight = GxImageUtil.getImageHeight(imagePath); |
| 126 | + Assert.assertEquals(IMAGE_HEIGHT, imageHeight); |
| 127 | + |
| 128 | + int imageWidth = GxImageUtil.getImageWidth(imagePath); |
| 129 | + Assert.assertEquals(IMAGE_WIDTH, imageWidth); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testImageFileSize() |
| 134 | + { |
| 135 | + String fileName = initialize("imageSize"); |
| 136 | + long fileSize = GxImageUtil.getFileSize(fileName); |
| 137 | + Assert.assertEquals(113974, fileSize); |
| 138 | + |
| 139 | + } |
| 140 | +} |
0 commit comments