Best Mockito code snippet using org.mockito.moduletest.ModuleUtil.open
Source:ModuleUtilTest.java
1/**2 * This Source Code Form is subject to the terms of the Mozilla Public License,3 * v. 2.0. If a copy of the MPL was not distributed with this file, You can4 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under5 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.6 *7 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS8 * graphic logo is a trademark of OpenMRS Inc.9 */10package org.openmrs.module;11import org.apache.commons.io.IOUtils;12import org.junit.After;13import org.junit.Assert;14import org.junit.Test;15import org.mockito.Mock;16import org.openmrs.GlobalProperty;17import org.openmrs.api.AdministrationService;18import org.openmrs.api.context.Context;19import org.openmrs.messagesource.MessageSourceService;20import org.openmrs.test.BaseContextMockTest;21import org.openmrs.test.Verifies;22import java.io.File;23import java.io.FileOutputStream;24import java.io.IOException;25import java.io.InputStream;26import java.io.OutputStream;27import java.util.Arrays;28import java.util.Properties;29import java.util.jar.JarFile;30import static org.hamcrest.Matchers.contains;31import static org.hamcrest.Matchers.empty;32import static org.junit.Assert.assertThat;33import static org.mockito.Mockito.when;34/**35 * Tests methods on the {@link org.openmrs.module.ModuleUtil} class36 */37public class ModuleUtilTest extends BaseContextMockTest {38 39 @Mock40 MessageSourceService messageSourceService;41 42 @Mock43 AdministrationService administrationService;44 45 Properties initialRuntimeProperties;46 47 /**48 * @see {@link org.openmrs.module.ModuleUtil#checkMandatoryModulesStarted()}49 */50 @Test(expected = MandatoryModuleException.class)51 @Verifies(value = "should throw ModuleException if a mandatory module is not started", method = "checkMandatoryModulesStarted()")52 public void checkMandatoryModulesStarted_shouldThrowModuleExceptionIfAMandatoryModuleIsNotStarted() throws Exception {53 //given54 assertThat(ModuleFactory.getStartedModules(), empty());55 56 GlobalProperty gp1 = new GlobalProperty("module1.mandatory", "true");57 when(administrationService.getGlobalPropertiesBySuffix(".mandatory")).thenReturn(Arrays.asList(gp1));58 59 //when60 ModuleUtil.checkMandatoryModulesStarted();61 //then exception62 }63 64 @After65 public void revertRuntimeProperties() {66 if (initialRuntimeProperties != null) {67 Context.setRuntimeProperties(initialRuntimeProperties);68 initialRuntimeProperties = null;69 }70 }71 72 /**73 * @see {@link org.openmrs.module.ModuleUtil#getMandatoryModules()}74 */75 @Test76 @Verifies(value = "should return mandatory module ids", method = "getMandatoryModules()")77 public void getMandatoryModules_shouldReturnMandatoryModuleIds() throws Exception {78 //given79 GlobalProperty gp1 = new GlobalProperty("firstmodule.mandatory", "true");80 GlobalProperty gp2 = new GlobalProperty("secondmodule.mandatory", "false");81 82 when(administrationService.getGlobalPropertiesBySuffix(".mandatory")).thenReturn(Arrays.asList(gp1, gp2));83 84 //when85 //then86 assertThat(ModuleUtil.getMandatoryModules(), contains("firstmodule"));87 }88 89 /**90 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}91 */92 @Test93 @Verifies(value = "should allow ranged required version", method = "matchRequiredVersions(String,String)")94 public void matchRequiredVersions_shouldAllowRangedRequiredVersion() throws Exception {95 String openmrsVersion = "1.4.3";96 String requiredOpenmrsVersion = "1.2.3 - 1.4.4";97 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));98 }99 100 /**101 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}102 */103 @Test104 @Verifies(value = "should allow ranged required version with wild card", method = "matchRequiredVersions(String,String)")105 public void matchRequiredVersions_shouldAllowRangedRequiredVersionWithWildCard() throws Exception {106 String openmrsVersion = "1.4.3";107 String requiredOpenmrsVersion = "1.2.* - 1.4.*";108 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));109 }110 111 /**112 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}113 */114 @Test115 @Verifies(value = "should allow ranged required version with wild card on one end", method = "matchRequiredVersions(String,String)")116 public void matchRequiredVersions_shouldAllowRangedRequiredVersionWithWildCardOnOneEnd() throws Exception {117 String openmrsVersion = "1.4.3";118 String requiredOpenmrsVersion = "1.2.3 - 1.4.*";119 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));120 requiredOpenmrsVersion = "1.4.* - 1.4.5";121 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));122 }123 124 /**125 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}126 */127 @Test128 @Verifies(value = "should allow single entry for required version", method = "matchRequiredVersions(String,String)")129 public void matchRequiredVersions_shouldAllowSingleEntryForRequiredVersion() throws Exception {130 String openmrsVersion = "1.4.3";131 String requiredOpenmrsVersion = "1.4.2";132 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));133 }134 135 /**136 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}137 */138 @Test139 @Verifies(value = "should allow required version with wild card", method = "matchRequiredVersions(String,String)")140 public void matchRequiredVersions_shouldAllowRequiredVersionWithWildCard() throws Exception {141 String openmrsVersion = "1.4.3";142 String requiredOpenmrsVersion = "1.4.*";143 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));144 }145 146 /**147 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}148 */149 @Test150 @Verifies(value = "should allow non numeric character required version", method = "matchRequiredVersions(String,String)")151 public void matchRequiredVersions_shouldAllowNonNumericCharacterRequiredVersion() throws Exception {152 String openmrsVersion = "1.4.3";153 String requiredOpenmrsVersion = "1.4.3a";154 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));155 }156 157 /**158 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}159 */160 @Test161 @Verifies(value = "should allow ranged non numeric character required version", method = "matchRequiredVersions(String,String)")162 public void matchRequiredVersions_shouldAllowRangedNonNumericCharacterRequiredVersion() throws Exception {163 String openmrsVersion = "1.4.3";164 String requiredOpenmrsVersion = "1.4.1a - 1.4.3a";165 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));166 }167 168 /**169 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}170 */171 @Test172 @Verifies(value = "should allow ranged non numeric character with wild card", method = "matchRequiredVersions(String,String)")173 public void matchRequiredVersions_shouldAllowRangedNonNumericCharacterWithWildCard() throws Exception {174 String openmrsVersion = "1.4.3";175 String requiredOpenmrsVersion = "1.3.*a - 1.4.*a";176 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));177 }178 179 /**180 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}181 */182 @Test183 @Verifies(value = "should allow ranged non numeric character with wild card on one end", method = "matchRequiredVersions(String,String)")184 public void matchRequiredVersions_shouldAllowRangedNonNumericCharacterWithWildCardOnOneEnd() throws Exception {185 String openmrsVersion = "1.4.3";186 String requiredOpenmrsVersion = "1.2.3 - 1.4.*a";187 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));188 requiredOpenmrsVersion = "1.4.*a - 1.4.5a";189 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));190 }191 192 /**193 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}194 */195 @Test196 @Verifies(value = "should return false when openmrs version beyond wild card range", method = "matchRequiredVersions(String,String)")197 public void matchRequiredVersions_shouldReturnFalseWhenOpenmrsVersionBeyondWildCardRange() throws Exception {198 String openmrsVersion = "1.4.3";199 String requiredOpenmrsVersion = "1.3.*";200 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));201 requiredOpenmrsVersion = "1.5.*";202 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));203 }204 205 /**206 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}207 */208 @Test209 @Verifies(value = "should return false when required version beyond openmrs version", method = "matchRequiredVersions(String,String)")210 public void matchRequiredVersions_shouldReturnFalseWhenRequiredVersionBeyondOpenmrsVersion() throws Exception {211 String openmrsVersion = "1.4.3";212 String requiredOpenmrsVersion = "1.5.*";213 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));214 }215 216 /**217 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}218 */219 @Test220 @Verifies(value = "should return false when required version with wild card beyond openmrs version", method = "matchRequiredVersions(String,String)")221 public void matchRequiredVersions_shouldReturnFalseWhenRequiredVersionWithWildCardBeyondOpenmrsVersion()222 throws Exception {223 String openmrsVersion = "1.4.3";224 String requiredOpenmrsVersion = "1.5.* - 1.6.*";225 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));226 }227 228 /**229 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}230 */231 @Test232 @Verifies(value = "should return false when required version with wild card on one end beyond openmrs version", method = "matchRequiredVersions(String,String)")233 public void matchRequiredVersions_shouldReturnFalseWhenRequiredVersionWithWildCardOnOneEndBeyondOpenmrsVersion()234 throws Exception {235 String openmrsVersion = "1.4.3";236 String requiredOpenmrsVersion = "1.4.5 - 1.5.*";237 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));238 requiredOpenmrsVersion = "1.5.* - 1.6.0";239 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));240 }241 242 /**243 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}244 */245 @Test246 @Verifies(value = "should return false when single entry required version beyond openmrs version", method = "matchRequiredVersions(String,String)")247 public void matchRequiredVersions_shouldReturnFalseWhenSingleEntryRequiredVersionBeyondOpenmrsVersion() throws Exception {248 String openmrsVersion = "1.4.3";249 String requiredOpenmrsVersion = "1.5.0";250 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));251 }252 253 /**254 * @see {@link org.openmrs.module.ModuleUtil#matchRequiredVersions(String,String)}255 */256 @Test257 @Verifies(value = "should allow release type in the version", method = "matchRequiredVersions(String,String)")258 public void matchRequiredVersions_shouldAllowReleaseTypeInTheVersion() throws Exception {259 String openmrsVersion = "1.4.3";260 String requiredOpenmrsVersion = "1.4.1-dev - 1.5.*-alpha";261 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));262 requiredOpenmrsVersion = "1.5.*-dev - 1.6.0-dev";263 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredOpenmrsVersion));264 }265 266 /**267 * @see {@link ModuleUtil#matchRequiredVersions(String,String)}268 */269 @Test270 @Verifies(value = "should match when revision number is below maximum revision number", method = "matchRequiredVersions(String version, String versionRange)")271 public void matchRequiredVersions_shouldMatchWhenRevisionNumberIsBelowMaximumRevisionNumber() {272 String openmrsVersion = "1.4.1111";273 String requiredVersion = "1.4.*";274 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredVersion));275 }276 277 /**278 * @see {@link ModuleUtil#matchRequiredVersions(String,String)}279 */280 @Test281 @Verifies(value = "should not match when revision number is above maximum revision number", method = "matchRequiredVersions(String version, String versionRange)")282 public void matchRequiredVersions_shouldNotMatchWhenRevisionNumberIsAboveMaximumRevisionNumber() {283 Long revisionNumber = (long) Integer.MAX_VALUE + 2;284 String openmrsVersion = "1.4." + revisionNumber;285 String requiredVersion = "1.4.*";286 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredVersion));287 }288 289 /**290 * @see {@link ModuleUtil#matchRequiredVersions(String,String)}291 */292 @Test293 @Verifies(value = "should not match when version has wild card and is outside boundary", method = "matchRequiredVersions(String version, String versionRange)")294 public void matchRequiredVersions_shouldNotMatchWhenVersionHasWildCardAndIsOutsideBoundary() {295 String openmrsVersion = "1.*.4";296 String requiredVersion = "1.4.0 - 1.4.10";297 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredVersion));298 }299 300 /**301 * @see {@link ModuleUtil#matchRequiredVersions(String,String)}302 */303 @Test304 @Verifies(value = "should match when version has wild card and is within boundary", method = "matchRequiredVersions(String version, String versionRange)")305 public void matchRequiredVersions_shouldMatchWhenVersionHasWildCardAndIsWithinBoundary() {306 String openmrsVersion = "1.4.*";307 String requiredVersion = "1.4.0 - 1.4.10";308 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredVersion));309 }310 311 /**312 * @see {@link ModuleUtil#matchRequiredVersions(String,String)}313 */314 @Test315 @Verifies(value = "should not match when version has wild card plus qualifier and is outside boundary", method = "matchRequiredVersions(String version, String versionRange)")316 public void matchRequiredVersions_shouldNotMatchWhenVersionHasWildPlusQualifierCardAndIsOutsideBoundary() {317 String openmrsVersion = "1.*.4-SNAPSHOT";318 String requiredVersion = "1.4.0 - 1.4.10";319 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredVersion));320 }321 322 /**323 * @see {@link ModuleUtil#matchRequiredVersions(String,String)}324 */325 @Test326 @Verifies(value = "should match when version has wild card plus qualifier and is within boundary", method = "matchRequiredVersions(String version, String versionRange)")327 public void matchRequiredVersions_shouldMatchWhenVersionHasWildCardPlusQualifierAndIsWithinBoundary() {328 String openmrsVersion = "1.4.*-SNAPSHOT";329 String requiredVersion = "1.4.0 - 1.4.10";330 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredVersion));331 }332 333 /**334 * @see {@link ModuleUtil#matchRequiredVersions(String,String)}335 */336 @Test337 @Verifies(value = "should correctly set upper and lower limit for versionRange with qualifiers and wild card", method = "matchRequiredVersions(String version, String versionRange)")338 public void matchRequiredVersions_shouldCorrectlySetUpperAndLoweLimitForVersionRangeWithQualifiersAndWildCard() {339 String openmrsVersion = "1.4.11111";340 String requiredVersion = "1.4.200 - 1.4.*-SNAPSHOT";341 Long revisionNumber = (long) Integer.MAX_VALUE + 2;342 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredVersion));343 requiredVersion = "1.4.*-SNAPSHOT - 1.4.*";344 Assert.assertTrue(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredVersion));345 openmrsVersion = "1.4." + revisionNumber;346 Assert.assertFalse(ModuleUtil.matchRequiredVersions(openmrsVersion, requiredVersion));347 }348 349 /**350 * @see {@link org.openmrs.module.ModuleUtil#getPathForResource(org.openmrs.module.Module,String)}351 */352 @Test353 @Verifies(value = "should handle ui springmvc css ui dot css example", method = "getPathForResource(Module,String)")354 public void getPathForResource_shouldHandleUiSpringmvcCssUiDotCssExample() throws Exception {355 Module module = new Module("Unit test");356 module.setModuleId("ui.springmvc");357 String path = "/ui/springmvc/css/ui.css";358 Assert.assertEquals("/css/ui.css", ModuleUtil.getPathForResource(module, path));359 }360 361 /**362 * @see {@link org.openmrs.module.ModuleUtil#getModuleForPath(String)}363 */364 @Test365 @Verifies(value = "should handle ui springmvc css ui dot css when ui dot springmvc module is running", method = "getModuleForPath(String)")366 public void getModuleForPath_shouldHandleUiSpringmvcCssUiDotCssWhenUiDotSpringmvcModuleIsRunning() throws Exception {367 ModuleFactory.getStartedModulesMap().clear();368 Module module = new Module("For Unit Test");369 module.setModuleId("ui.springmvc");370 ModuleFactory.getStartedModulesMap().put(module.getModuleId(), module);371 372 String path = "/ui/springmvc/css/ui.css";373 Assert.assertEquals(module, ModuleUtil.getModuleForPath(path));374 }375 376 /**377 * @see {@link org.openmrs.module.ModuleUtil#getModuleForPath(String)}378 */379 @Test380 @Verifies(value = "should handle ui springmvc css ui dot css when ui module is running", method = "getModuleForPath(String)")381 public void getModuleForPath_shouldHandleUiSpringmvcCssUiDotCssWhenUiModuleIsRunning() throws Exception {382 ModuleFactory.getStartedModulesMap().clear();383 Module module = new Module("For Unit Test");384 module.setModuleId("ui");385 ModuleFactory.getStartedModulesMap().put(module.getModuleId(), module);386 387 String path = "/ui/springmvc/css/ui.css";388 Assert.assertEquals(module, ModuleUtil.getModuleForPath(path));389 }390 391 /**392 * @see {@link org.openmrs.module.ModuleUtil#getModuleForPath(String)}393 */394 @Test395 @Verifies(value = "should return null for ui springmvc css ui dot css when no relevant module is running", method = "getModuleForPath(String)")396 public void getModuleForPath_shouldReturnNullForUiSpringmvcCssUiDotCssWhenNoRelevantModuleIsRunning() throws Exception {397 ModuleFactory.getStartedModulesMap().clear();398 String path = "/ui/springmvc/css/ui.css";399 Assert.assertNull(ModuleUtil.getModuleForPath(path));400 }401 402 /**403 * @see {@link org.openmrs.module.ModuleUtil#checkRequiredVersion(String, String)}404 */405 @Test(expected = ModuleException.class)406 @Verifies(value = "should throw ModuleException if openmrs version beyond wild card range", method = "checkRequiredVersion(String, String)")407 public void checkRequiredVersion_shouldThrowModuleExceptionIfOpenmrsVersionBeyondWildCardRange() throws Exception {408 String openmrsVersion = "1.4.3";409 String requiredOpenmrsVersion = "1.3.*";410 ModuleUtil.checkRequiredVersion(openmrsVersion, requiredOpenmrsVersion);411 }412 413 /**414 * @see {@link org.openmrs.module.ModuleUtil#checkRequiredVersion(String, String)}415 */416 @Test(expected = ModuleException.class)417 @Verifies(value = "should throw ModuleException if required version beyond openmrs version", method = "checkRequiredVersion(String, String)")418 public void checkRequiredVersion_shouldThrowModuleExceptionIfRequiredVersionBeyondOpenmrsVersion() throws Exception {419 String openmrsVersion = "1.4.3";420 String requiredOpenmrsVersion = "1.5.*";421 ModuleUtil.checkRequiredVersion(openmrsVersion, requiredOpenmrsVersion);422 }423 424 /**425 * @see {@link org.openmrs.module.ModuleUtil#checkRequiredVersion(String, String)}426 */427 @Test(expected = ModuleException.class)428 @Verifies(value = "should throw ModuleException if required version with wild card beyond openmrs version", method = "checkRequiredVersion(String, String)")429 public void checkRequiredVersion_shouldThrowModuleExceptionIfRequiredVersionWithWildCardBeyondOpenmrsVersion()430 throws Exception {431 String openmrsVersion = "1.4.3";432 String requiredOpenmrsVersion = "1.5.* - 1.6.*";433 ModuleUtil.checkRequiredVersion(openmrsVersion, requiredOpenmrsVersion);434 }435 436 /**437 * @see {@link org.openmrs.module.ModuleUtil#checkRequiredVersion(String, String)}438 */439 @Test(expected = ModuleException.class)440 @Verifies(value = "should throw ModuleException if required version with wild card on one end beyond openmrs version", method = "checkRequiredVersion(String, String)")441 public void checkRequiredVersion_shouldThrowModuleExceptionIfRequiredVersionWithWildCardOnOneEndBeyondOpenmrsVersion()442 throws Exception {443 String openmrsVersion = "1.4.3";444 String requiredOpenmrsVersion = "1.4.5 - 1.5.*";445 ModuleUtil.checkRequiredVersion(openmrsVersion, requiredOpenmrsVersion);446 }447 448 /**449 * @see {@link org.openmrs.module.ModuleUtil#checkRequiredVersion(String, String)}450 */451 @Test(expected = ModuleException.class)452 @Verifies(value = "should throw ModuleException if single entry required version beyond openmrs version", method = "checkRequiredVersion(String, String)")453 public void checkRequiredVersion_shouldThrowModuleExceptionIfSingleEntryRequiredVersionBeyondOpenmrsVersion()454 throws Exception {455 String openmrsVersion = "1.4.3";456 String requiredOpenmrsVersion = "1.5.0";457 ModuleUtil.checkRequiredVersion(openmrsVersion, requiredOpenmrsVersion);458 }459 460 /**461 * @see {@link org.openmrs.module.ModuleUtil#compareVersion(String,String)}462 */463 @Test464 @Verifies(value = "should correctly comparing two version numbers", method = "compareVersion(String,String)")465 public void compareVersion_shouldCorrectlyComparingTwoVersionNumbers() throws Exception {466 String olderVersion = "2.1.1";467 String newerVersion = "2.1.10";468 Assert.assertTrue(ModuleUtil.compareVersion(olderVersion, newerVersion) < 0);469 }470 471 /**472 * @see {@link org.openmrs.module.ModuleUtil#compareVersion(String,String)}473 */474 @Test475 @Verifies(value = "treat SNAPSHOT as earliest version", method = "compareVersion(String,String)")476 public void compareVersion_shouldTreatSNAPSHOTAsEarliestVersion() throws Exception {477 String olderVersion = "1.8.3";478 String newerVersion = "1.8.4-SNAPSHOT";479 Assert.assertTrue(ModuleUtil.compareVersion(newerVersion, olderVersion) > 0);480 //should still return the correct value if the arguments are switched481 Assert.assertTrue(ModuleUtil.compareVersion(olderVersion, newerVersion) < 0);482 }483 484 /**485 * @see {@link org.openmrs.module.ModuleUtil#checkRequiredVersion(String, String)}486 */487 @Test(expected = ModuleException.class)488 @Verifies(value = "should throw ModuleException if SNAPSHOT not handled correctly", method = "checkRequiredVersion(String, String)")489 public void checkRequiredVersion_shouldThrowModuleExceptionIfSNAPSHOTNotHandledCorrectly() throws Exception {490 String openmrsVersion = "1.4.3";491 String requiredOpenmrsVersion = "1.4.5-SNAPSHOT";492 ModuleUtil.checkRequiredVersion(openmrsVersion, requiredOpenmrsVersion);493 }494 495 /**496 * @see {@link org.openmrs.module.ModuleUtil#checkRequiredVersion(String, String)}497 */498 @Test499 @Verifies(value = "Should handle SNAPSHOT versions ", method = "checkRequiredVersion(String, String)")500 public void checkRequiredVersion_shouldHandleSnapshotVersion() throws Exception {501 String openMRSVersion = "1.9.2-SNAPSHOT";502 String requiredOpenmrsVersion = "1.9.2-SNAPSHOT";503 ModuleUtil.checkRequiredVersion(openMRSVersion, requiredOpenmrsVersion);504 }505 506 /**507 * @see {@link org.openmrs.module.ModuleUtil#checkRequiredVersion(String, String)}508 */509 @Test510 @Verifies(value = "Should handle UUID suffix versions ", method = "checkRequiredVersion(String, String)")511 public void checkRequiredVersion_shouldHandleUuidSuffixVersion() throws Exception {512 String openMRSVersion = "1.9.9-f4927f";513 String requiredOpenmrsVersion = "1.9.9-SNAPSHOT";514 ModuleUtil.checkRequiredVersion(openMRSVersion, requiredOpenmrsVersion);515 }516 517 @Test518 @Verifies(value = "Should handle ALPHA versions ", method = "checkRequiredVersion(String, String)")519 public void checkRequiredVersion_shouldHandleAlphaVersion() throws Exception {520 String openMRSVersion = "1.9.2-ALPHA";521 String requiredOpenmrsVersion = "1.9.2-ALPHA";522 ModuleUtil.checkRequiredVersion(openMRSVersion, requiredOpenmrsVersion);523 }524 525 private JarFile loadModuleJarFile(String moduleId, String version) throws IOException {526 InputStream moduleStream = null;527 File tempFile = null;528 OutputStream tempFileStream = null;529 JarFile jarFile = null;530 try {531 moduleStream = getClass().getClassLoader().getResourceAsStream(532 "org/openmrs/module/include/" + moduleId + "-" + version + ".omod");533 Assert.assertNotNull(moduleStream);534 tempFile = File.createTempFile("moduleTest", "omod");535 tempFileStream = new FileOutputStream(tempFile);536 IOUtils.copy(moduleStream, tempFileStream);537 jarFile = new JarFile(tempFile);538 }539 finally {540 IOUtils.closeQuietly(moduleStream);541 IOUtils.closeQuietly(tempFileStream);542 if (tempFile != null) {543 tempFile.delete();544 }545 }546 return jarFile;...
Source:ModuleHandlingTest.java
...36 public ModuleHandlingTest(boolean namedModules) {37 this.namedModules = namedModules;38 }39 @Test40 public void can_define_class_in_open_reading_module() throws Exception {41 assumeThat(Plugins.getMockMaker() instanceof InlineByteBuddyMockMaker, is(false));42 Path jar = modularJar(true, true, true);43 ModuleLayer layer = layer(jar, true, namedModules);44 ClassLoader loader = layer.findLoader("mockito.test");45 Class<?> type = loader.loadClass("sample.MyCallable");46 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();47 Thread.currentThread().setContextClassLoader(loader);48 try {49 Class<?> mockito = loader.loadClass(Mockito.class.getName());50 @SuppressWarnings("unchecked")51 Callable<String> mock = (Callable<String>) mockito.getMethod("mock", Class.class).invoke(null, type);52 Object stubbing = mockito.getMethod("when", Object.class).invoke(null, mock.call());53 loader.loadClass(OngoingStubbing.class.getName()).getMethod("thenCallRealMethod").invoke(stubbing);54 assertThat(mock.getClass().getName()).startsWith("sample.MyCallable$MockitoMock$");55 assertThat(mock.call()).isEqualTo("foo");56 } finally {57 Thread.currentThread().setContextClassLoader(contextLoader);58 }59 }60 @Test61 public void can_define_class_in_open_java_util_module() throws Exception {62 assumeThat(Plugins.getMockMaker() instanceof InlineByteBuddyMockMaker, is(false));63 Path jar = modularJar(true, true, true);64 ModuleLayer layer = layer(jar, true, namedModules);65 ClassLoader loader = layer.findLoader("mockito.test");66 Class<?> type = loader.loadClass("java.util.concurrent.locks.Lock");67 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();68 Thread.currentThread().setContextClassLoader(loader);69 try {70 Class<?> mockito = loader.loadClass(Mockito.class.getName());71 @SuppressWarnings("unchecked")72 Lock mock = (Lock) mockito.getMethod("mock", Class.class).invoke(null, type);73 Object stubbing = mockito.getMethod("when", Object.class).invoke(null, mock.tryLock());74 loader.loadClass(OngoingStubbing.class.getName()).getMethod("thenReturn", Object.class).invoke(stubbing, true);75 boolean relocated = !Boolean.getBoolean("org.mockito.internal.noUnsafeInjection") && ClassInjector.UsingReflection.isAvailable();76 String prefix = relocated ? "org.mockito.codegen.Lock$MockitoMock$" : "java.util.concurrent.locks.Lock$MockitoMock$";77 assertThat(mock.getClass().getName()).startsWith(prefix);78 assertThat(mock.tryLock()).isEqualTo(true);79 } finally {80 Thread.currentThread().setContextClassLoader(contextLoader);81 }82 }83 @Test84 public void inline_mock_maker_can_mock_closed_modules() throws Exception {85 assumeThat(Plugins.getMockMaker() instanceof InlineByteBuddyMockMaker, is(true));86 Path jar = modularJar(false, false, false);87 ModuleLayer layer = layer(jar, false, namedModules);88 ClassLoader loader = layer.findLoader("mockito.test");89 Class<?> type = loader.loadClass("sample.MyCallable");90 Class<?> mockito = loader.loadClass(Mockito.class.getName());91 @SuppressWarnings("unchecked")92 Callable<String> mock = (Callable<String>) mockito.getMethod("mock", Class.class).invoke(null, type);93 Object stubbing = mockito.getMethod("when", Object.class).invoke(null, mock.call());94 loader.loadClass(OngoingStubbing.class.getName()).getMethod("thenCallRealMethod").invoke(stubbing);95 assertThat(mock.getClass().getName()).isEqualTo("sample.MyCallable");96 assertThat(mock.call()).isEqualTo("foo");97 }98 @Test99 public void can_define_class_in_open_reading_private_module() throws Exception {100 assumeThat(Plugins.getMockMaker() instanceof InlineByteBuddyMockMaker, is(false));101 Path jar = modularJar(false, true, true);102 ModuleLayer layer = layer(jar, true, namedModules);103 ClassLoader loader = layer.findLoader("mockito.test");104 Class<?> type = loader.loadClass("sample.MyCallable");105 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();106 Thread.currentThread().setContextClassLoader(loader);107 try {108 Class<?> mockito = loader.loadClass(Mockito.class.getName());109 @SuppressWarnings("unchecked")110 Callable<String> mock = (Callable<String>) mockito.getMethod("mock", Class.class).invoke(null, type);111 Object stubbing = mockito.getMethod("when", Object.class).invoke(null, mock.call());112 loader.loadClass(OngoingStubbing.class.getName()).getMethod("thenCallRealMethod").invoke(stubbing);113 assertThat(mock.getClass().getName()).startsWith("sample.MyCallable$MockitoMock$");114 assertThat(mock.call()).isEqualTo("foo");115 } finally {116 Thread.currentThread().setContextClassLoader(contextLoader);117 }118 }119 @Test120 public void can_define_class_in_open_non_reading_module() throws Exception {121 assumeThat(Plugins.getMockMaker() instanceof InlineByteBuddyMockMaker, is(false));122 Path jar = modularJar(true, true, true);123 ModuleLayer layer = layer(jar, false, namedModules);124 ClassLoader loader = layer.findLoader("mockito.test");125 Class<?> type = loader.loadClass("sample.MyCallable");126 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();127 Thread.currentThread().setContextClassLoader(loader);128 try {129 Class<?> mockito = loader.loadClass(Mockito.class.getName());130 @SuppressWarnings("unchecked")131 Callable<String> mock = (Callable<String>) mockito.getMethod("mock", Class.class).invoke(null, type);132 Object stubbing = mockito.getMethod("when", Object.class).invoke(null, mock.call());133 loader.loadClass(OngoingStubbing.class.getName()).getMethod("thenCallRealMethod").invoke(stubbing);134 assertThat(mock.getClass().getName()).startsWith("sample.MyCallable$MockitoMock$");135 assertThat(mock.call()).isEqualTo("foo");136 } finally {137 Thread.currentThread().setContextClassLoader(contextLoader);138 }139 }140 @Test141 public void can_define_class_in_open_non_reading_non_exporting_module() throws Exception {142 assumeThat(Plugins.getMockMaker() instanceof InlineByteBuddyMockMaker, is(false));143 Path jar = modularJar(true, false, true);144 ModuleLayer layer = layer(jar, false, namedModules);145 ClassLoader loader = layer.findLoader("mockito.test");146 Class<?> type = loader.loadClass("sample.MyCallable");147 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();148 Thread.currentThread().setContextClassLoader(loader);149 try {150 Class<?> mockito = loader.loadClass(Mockito.class.getName());151 @SuppressWarnings("unchecked")152 Callable<String> mock = (Callable<String>) mockito.getMethod("mock", Class.class).invoke(null, type);153 Object stubbing = mockito.getMethod("when", Object.class).invoke(null, mock.call());154 loader.loadClass(OngoingStubbing.class.getName()).getMethod("thenCallRealMethod").invoke(stubbing);155 assertThat(mock.getClass().getName()).startsWith("sample.MyCallable$MockitoMock$");156 assertThat(mock.call()).isEqualTo("foo");157 } finally {158 Thread.currentThread().setContextClassLoader(contextLoader);159 }160 }161 @Test162 public void can_define_class_in_closed_module() throws Exception {163 assumeThat(Plugins.getMockMaker() instanceof InlineByteBuddyMockMaker, is(false));164 Path jar = modularJar(true, true, false);165 ModuleLayer layer = layer(jar, false, namedModules);166 ClassLoader loader = layer.findLoader("mockito.test");167 Class<?> type = loader.loadClass("sample.MyCallable");168 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();169 Thread.currentThread().setContextClassLoader(loader);170 try {171 Class<?> mockito = loader.loadClass(Mockito.class.getName());172 @SuppressWarnings("unchecked")173 Callable<String> mock = (Callable<String>) mockito.getMethod("mock", Class.class).invoke(null, type);174 Object stubbing = mockito.getMethod("when", Object.class).invoke(null, mock.call());175 loader.loadClass(OngoingStubbing.class.getName()).getMethod("thenCallRealMethod").invoke(stubbing);176 boolean relocated = !Boolean.getBoolean("org.mockito.internal.noUnsafeInjection") && ClassInjector.UsingReflection.isAvailable();177 String prefix = relocated ? "sample.MyCallable$MockitoMock$" : "org.mockito.codegen.MyCallable$MockitoMock$";178 assertThat(mock.getClass().getName()).startsWith(prefix);179 assertThat(mock.call()).isEqualTo("foo");180 } finally {181 Thread.currentThread().setContextClassLoader(contextLoader);182 }183 }184 @Test185 public void cannot_define_class_in_non_opened_non_exported_module_if_lookup_injection() throws Exception {186 assumeThat(Plugins.getMockMaker() instanceof InlineByteBuddyMockMaker, is(false));187 assumeThat(!Boolean.getBoolean("org.mockito.internal.noUnsafeInjection") && ClassInjector.UsingReflection.isAvailable(), is(true));188 Path jar = modularJar(false, false, false);189 ModuleLayer layer = layer(jar, false, namedModules);190 ClassLoader loader = layer.findLoader("mockito.test");191 Class<?> type = loader.loadClass("sample.MyCallable");192 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();193 Thread.currentThread().setContextClassLoader(loader);194 try {195 Class<?> mockito = loader.loadClass(Mockito.class.getName());196 @SuppressWarnings("unchecked")197 Callable<String> mock = (Callable<String>) mockito.getMethod("mock", Class.class).invoke(null, type);198 Object stubbing = mockito.getMethod("when", Object.class).invoke(null, mock.call());199 loader.loadClass(OngoingStubbing.class.getName()).getMethod("thenCallRealMethod").invoke(stubbing);200 assertThat(mock.getClass().getName()).startsWith("sample.MyCallable$MockitoMock$");201 assertThat(mock.call()).isEqualTo("foo");202 } finally {203 Thread.currentThread().setContextClassLoader(contextLoader);204 }205 }206 @Test207 public void can_define_class_in_non_opened_non_exported_module_if_unsafe_injection() throws Exception {208 assumeThat(Plugins.getMockMaker() instanceof InlineByteBuddyMockMaker, is(false));209 assumeThat(!Boolean.getBoolean("org.mockito.internal.noUnsafeInjection") && ClassInjector.UsingReflection.isAvailable(), is(false));210 Path jar = modularJar(false, false, false);211 ModuleLayer layer = layer(jar, false, namedModules);212 ClassLoader loader = layer.findLoader("mockito.test");213 Class<?> type = loader.loadClass("sample.MyCallable");214 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();215 Thread.currentThread().setContextClassLoader(loader);216 try {217 Class<?> mockito = loader.loadClass(Mockito.class.getName());218 try {219 mockito.getMethod("mock", Class.class).invoke(null, type);220 fail("Expected mocking to fail");221 } catch (InvocationTargetException e) {...
Source:ModuleUtil.java
...113 .packages(new HashSet<>(Arrays.asList(packages)))114 .build();115 ModuleReference reference = new ModuleReference(descriptor, null) {116 @Override117 public ModuleReader open() {118 return new ModuleReader() {119 @Override120 public Optional<URI> find(String name) {121 return Optional.empty();122 }123 @Override124 public Stream<String> list() {125 return Stream.empty();126 }127 @Override128 public void close() {129 }130 };131 }...
open
Using AI Code Generation
1package com.example;2import org.mockito.moduletest.ModuleUtil;3public class 1 {4 public static void main(String[] args) {5 ModuleUtil.open("com.example", "com.example");6 }7}8package com.example;9import org.mockito.moduletest.ModuleUtil;10public class 2 {11 public static void main(String[] args) {12 ModuleUtil.open("com.example", "com.example", "com.example");13 }14}15package com.example;16import org.mockito.moduletest.ModuleUtil;17public class 3 {18 public static void main(String[] args) {19 ModuleUtil.open("com.example", "com.example", "com.example", "com.example");20 }21}22package com.example;23import org.mockito.moduletest.ModuleUtil;24public class 4 {25 public static void main(String[] args) {26 ModuleUtil.open("com.example", "com.example", "com.example", "com.example", "com.example");27 }28}29package com.example;30import org.mockito.moduletest.ModuleUtil;31public class 5 {32 public static void main(String[] args) {33 ModuleUtil.open("com.example", "com.example", "com.example", "com.example", "com.example", "com.example");34 }35}36package com.example;37import org.mockito.moduletest.ModuleUtil;38public class 6 {39 public static void main(String[] args) {40 ModuleUtil.open("com.example", "com.example", "com.example", "com.example", "com.example", "com.example", "com.example");41 }42}43package com.example;44import org.mockito.moduletest.ModuleUtil;45public class 7 {46 public static void main(String[] args) {47 ModuleUtil.open("com.example", "com.example", "com.example", "com.example", "com.example", "com.example
open
Using AI Code Generation
1import org.mockito.moduletest.ModuleUtil;2public class Test {3 public static void main(String[] args) {4 ModuleUtil.open("org.mockito.moduletest");5 }6}7import org.mockito.moduletest.ModuleUtil;8public class Test {9 public static void main(String[] args) {10 ModuleUtil.open("org.mockito.moduletest");11 }12}13import org.mockito.moduletest.ModuleUtil;14public class Test {15 public static void main(String[] args) {16 ModuleUtil.open("org.mockito.moduletest");17 }18}19import org.mockito.moduletest.ModuleUtil;20public class Test {21 public static void main(String[] args) {22 ModuleUtil.open("org.mockito.moduletest");23 }24}25import org.mockito.moduletest.ModuleUtil;26public class Test {27 public static void main(String[] args) {28 ModuleUtil.open("org.mockito.moduletest");29 }30}31import org.mockito.moduletest.ModuleUtil;32public class Test {33 public static void main(String[] args) {34 ModuleUtil.open("org.mockito.moduletest");35 }36}37import org.mockito.moduletest.ModuleUtil;38public class Test {39 public static void main(String[] args) {40 ModuleUtil.open("org.mockito.moduletest");41 }42}43import org.mockito.moduletest.ModuleUtil;44public class Test {45 public static void main(String[] args) {46 ModuleUtil.open("org.mockito.moduletest");47 }48}
open
Using AI Code Generation
1public class Test1 {2 public static void main(String[] args) {3 System.out.println("Hello World!");4 ModuleUtil.open("org.mockito.moduletest");5 }6}7public class Test2 {8 public static void main(String[] args) {9 System.out.println("Hello World!");10 ModuleUtil.open("org.mockito.moduletest");11 }12}
open
Using AI Code Generation
1package org.mockito.moduletest;2import java.lang.reflect.Method;3import java.lang.reflect.InvocationTargetException;4public class ModuleUtil {5 public static void open(String moduleName, Class<?>... packages) {6 try {7 Class<?> moduleClass = Class.forName("java.lang.Module");8 Method getModule = Class.class.getMethod("getModule");9 Method addOpens = moduleClass.getMethod("addOpens", String.class, moduleClass);10 Object thisModule = getModule.invoke(ModuleUtil.class);11 for (Class<?> p : packages) {12 Object thatModule = getModule.invoke(p);13 addOpens.invoke(thisModule, p.getPackageName(), thatModule);14 }15 } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {16 throw new RuntimeException(e);17 }18 }19}20package org.mockito.moduletest;21import java.lang.reflect.Method;22import java.lang.reflect.InvocationTargetException;23public class ModuleUtil {24 public static void open(String moduleName, Class<?>... packages) {25 try {26 Class<?> moduleClass = Class.forName("java.lang.Module");27 Method getModule = Class.class.getMethod("getModule");28 Method addOpens = moduleClass.getMethod("addOpens", String.class, moduleClass);29 Object thisModule = getModule.invoke(ModuleUtil.class);30 for (Class<?> p : packages) {31 Object thatModule = getModule.invoke(p);32 addOpens.invoke(thisModule, p.getPackageName(), thatModule);33 }34 } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {35 throw new RuntimeException(e);36 }37 }38}39package org.mockito.moduletest;40import java.lang.reflect.Method;41import java.lang.reflect.InvocationTargetException;42public class ModuleUtil {43 public static void open(String moduleName, Class<?>... packages) {44 try {45 Class<?> moduleClass = Class.forName("java.lang.Module");46 Method getModule = Class.class.getMethod("getModule");47 Method addOpens = moduleClass.getMethod("addOpens", String.class, moduleClass);48 Object thisModule = getModule.invoke(ModuleUtil.class);49 for (Class<?> p : packages) {
open
Using AI Code Generation
1package org.mockito.moduletest;2import org.mockito.moduletest.ModuleUtil;3import java.lang.reflect.*;4import java.util.*;5import java.io.*;6public class ModuleTest {7 public static void main(String[] args) {8 try {9 ModuleUtil.open("java.base", "java.lang");10 Module javaBase = ModuleUtil.getModule("java.base");11 Module javaLang = ModuleUtil.getModule("java.lang");12 System.out.println("Module java.base is open to java.lang: " 13 + javaBase.isOpen("java.lang"));14 System.out.println("Module java.lang is open to java.base: " 15 + javaLang.isOpen("java.base"));16 } catch (Exception e) {17 System.out.println(e);18 }19 }20}21package org.mockito.moduletest;22import java.lang.reflect.*;23import java.util.*;24import java.io.*;25public class ModuleTest {26 public static void main(String[] args) {27 try {28 ModuleUtil.open("java.base", "java.lang");29 Module javaBase = ModuleUtil.getModule("java.base");30 Module javaLang = ModuleUtil.getModule("java.lang");31 System.out.println("Module java.base is open to java.lang: " 32 + javaBase.isOpen("java.lang"));33 System.out.println("Module java.lang is open to java.base: " 34 + javaLang.isOpen("java.base"));35 Class<?> clazz = Class.forName("java.lang.String");36 Field field = clazz.getDeclaredField("value");37 field.setAccessible(true);38 Object obj = field.get("Hello");39 System.out.println("Field value: " + obj);40 } catch (Exception e) {41 System.out.println(e);42 }43 }44}
open
Using AI Code Generation
1package org.mockito.moduletest;2import org.mockito.moduletest.internal.ModuleUtil;3public class ModuleTest {4 public static void main(String[] args) {5 ModuleUtil moduleUtil = new ModuleUtil();6 moduleUtil.open();7 }8}9package org.mockito.moduletest.internal;10public class ModuleUtil {11 public void open() {12 Module module = new Module();13 module.moduleMethod();14 }15}16package org.mockito.moduletest.internal;17public class Module {18 public void moduleMethod() {19 System.out.println("Module method invoked");20 }21}
open
Using AI Code Generation
1import org.mockito.moduletest.ModuleUtil;2import org.mockito.moduletest.Module;3public class Test {4 public static void main(String[] args) {5 ModuleUtil moduleUtil = new ModuleUtil();6 Module module = moduleUtil.open("org.mockito.moduletest.Module");7 module.print();8 }9}10import org.mockito.moduletest.ModuleUtil;11import org.mockito.moduletest.Module;12public class Test {13 public static void main(String[] args) {14 ModuleUtil moduleUtil = new ModuleUtil();15 Module module = moduleUtil.open(Module.class);16 module.print();17 }18}
open
Using AI Code Generation
1import org.mockito.moduletest.ModuleUtil;2public class ModuleTest {3 public static void main(String[] args) {4 ModuleUtil.open("com.foo", "com.bar");5 }6}7 at ModuleTest.main(ModuleTest.java:6)8 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)9 at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)10 at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!