001/* 002 * Copyright (C) 2016 Hobrasoft s.r.o. 003 * 004 * This program is free software: you can redistribute it and/or modify 005 * it under the terms of the GNU Affero General Public License as published by 006 * the Free Software Foundation, either version 3 of the License, or 007 * (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU Affero General Public License for more details. 013 * 014 * You should have received a copy of the GNU Affero General Public License 015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 016 */ 017package cz.hobrasoft.pdfmu; 018 019import com.tngtech.java.junit.dataprovider.DataProvider; 020import com.tngtech.java.junit.dataprovider.DataProviderRunner; 021import com.tngtech.java.junit.dataprovider.UseDataProvider; 022import cz.hobrasoft.pdfmu.jackson.Inspect; 023import cz.hobrasoft.pdfmu.operation.OperationException; 024import cz.hobrasoft.pdfmu.operation.OperationInspect; 025import java.io.File; 026import java.io.IOException; 027import java.util.ArrayList; 028import java.util.List; 029import org.junit.Test; 030import org.junit.contrib.java.lang.system.Assertion; 031import org.junit.rules.TemporaryFolder; 032import org.junit.runner.RunWith; 033 034/** 035 * @author Filip Bartek 036 */ 037@RunWith(DataProviderRunner.class) 038public class MainUpdateVersionTest extends MainTest { 039 040 public enum PdfVersion { 041 V12, V13, V14, V15, V16, V17; 042 043 static PdfVersion DEFAULT = V16; 044 045 private char toChar() { 046 switch (this) { 047 case V12: 048 return '2'; 049 case V13: 050 return '3'; 051 case V14: 052 return '4'; 053 case V15: 054 return '5'; 055 case V16: 056 return '6'; 057 case V17: 058 return '7'; 059 } 060 assert false; 061 return 0; 062 } 063 064 @Override 065 public String toString() { 066 return String.format("1.%1$c", toChar()); 067 } 068 069 public String getBlankResourceName() { 070 return String.format("blank-1%1$c.pdf", toChar()); 071 } 072 073 public File getBlankFile(TemporaryFolder folder) throws IOException { 074 FileResource fileResource = new FileResource(getBlankResourceName()); 075 return fileResource.getFile(folder); 076 } 077 } 078 079 public enum OnlyIfLower { 080 No, 081 Yes; 082 083 public boolean toBoolean() { 084 return this == Yes; 085 } 086 } 087 088 public enum Force { 089 No, 090 Yes; 091 092 public boolean toBoolean() { 093 return this == Yes; 094 } 095 } 096 097 public static class UpdateVersionInput { 098 099 public UpdateVersionInput(Force force, PdfVersion inputVersion, 100 PdfVersion requestedVersion, OnlyIfLower onlyIfLower) { 101 this.force = force; 102 this.inputVersion = inputVersion; 103 this.requestedVersion = requestedVersion; 104 this.onlyIfLower = onlyIfLower; 105 } 106 107 public Force force; 108 public PdfVersion inputVersion; 109 public PdfVersion requestedVersion; 110 public OnlyIfLower onlyIfLower; 111 112 @Override 113 public String toString() { 114 List<String> argsList = new ArrayList<>(); 115 argsList.add("update-version"); 116 final String inputFileName = inputVersion.getBlankResourceName(); 117 argsList.add(inputFileName); 118 if (force.toBoolean()) { 119 argsList.add("--force"); 120 } else { 121 argsList.add("--out"); 122 final String outFileName = "out.pdf"; 123 assert !outFileName.equals(inputFileName); 124 argsList.add(outFileName); 125 } 126 if (requestedVersion != null) { 127 argsList.add("--version"); 128 argsList.add(requestedVersion.toString()); 129 } 130 if (onlyIfLower.toBoolean()) { 131 argsList.add("--only-if-lower"); 132 } 133 return String.join(" ", argsList); 134 } 135 } 136 137 public static List<UpdateVersionInput> updateVersionInputs() { 138 List<UpdateVersionInput> result = new ArrayList<>(); 139 for (Force force : Force.values()) { 140 for (PdfVersion inputVersion : PdfVersion.values()) { 141 for (OnlyIfLower onlyIfLower : OnlyIfLower.values()) { 142 for (PdfVersion requestedVersion : PdfVersion.values()) { 143 result.add(new UpdateVersionInput(force, inputVersion, requestedVersion, onlyIfLower)); 144 } 145 } 146 } 147 } 148 return result; 149 } 150 151 @DataProvider 152 public static Object[][] dataProviderUpdateVersion() { 153 List<Object[]> result = new ArrayList<>(); 154 for (final UpdateVersionInput updateVersionInput : updateVersionInputs()) { 155 final Force force = updateVersionInput.force; 156 assert force != null; 157 final PdfVersion inputVersion = updateVersionInput.inputVersion; 158 assert inputVersion != null; 159 final PdfVersion requestedVersion = updateVersionInput.requestedVersion; 160 final OnlyIfLower onlyIfLower = updateVersionInput.onlyIfLower; 161 assert onlyIfLower != null; 162 PdfVersion expectedVersion = requestedVersion; 163 if (expectedVersion == null) { 164 expectedVersion = PdfVersion.DEFAULT; 165 } 166 if (!force.toBoolean() && onlyIfLower.toBoolean() && inputVersion.compareTo(expectedVersion) >= 0) { 167 // Discard combinations that do not create an output file 168 continue; 169 } 170 if (onlyIfLower.toBoolean() && inputVersion.compareTo(expectedVersion) > 0) { 171 expectedVersion = inputVersion; 172 } 173 Inspect expectedInspect = newInspect(expectedVersion.toString()); 174 result.add(new Object[]{updateVersionInput, expectedInspect}); 175 } 176 return result.toArray(new Object[][]{}); 177 } 178 179 private File outFile; 180 181 @Test 182 @UseDataProvider 183 public void testUpdateVersion(final UpdateVersionInput updateVersionInput, 184 final Inspect expectedInspect) throws IOException { 185 final Force force = updateVersionInput.force; 186 final PdfVersion inputVersion = updateVersionInput.inputVersion; 187 final PdfVersion requestedVersion = updateVersionInput.requestedVersion; 188 final OnlyIfLower onlyIfLower = updateVersionInput.onlyIfLower; 189 final File document = inputVersion.getBlankFile(folder); 190 outFile = document; 191 List<String> argsList = new ArrayList<>(); 192 argsList.add("update-version"); 193 argsList.add(document.getAbsolutePath()); 194 if (force.toBoolean()) { 195 argsList.add("--force"); 196 } else { 197 argsList.add("--out"); 198 final String outFileName = "out.pdf"; 199 assert !outFileName.equals(inputVersion.getBlankResourceName()); 200 outFile = newFile(outFileName, false); 201 assert !outFile.exists(); 202 argsList.add(outFile.getAbsolutePath()); 203 } 204 if (requestedVersion != null) { 205 argsList.add("--version"); 206 argsList.add(requestedVersion.toString()); 207 } 208 if (onlyIfLower.toBoolean()) { 209 argsList.add("--only-if-lower"); 210 } 211 exit.expectSystemExitWithStatus(0); 212 exit.checkAssertionAfterwards(new Assertion() { 213 @Override 214 public void checkAssertion() throws OperationException, IOException { 215 final Inspect actualInspect = OperationInspect.getInstance().execute(outFile); 216 assertEquals(expectedInspect, actualInspect); 217 } 218 }); 219 Main.main(argsList.toArray(new String[]{})); 220 assert false; 221 } 222 223}