|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.mnemonic.service.memoryservice.internal; |
| 19 | + |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Paths; |
| 25 | + |
| 26 | +import org.testng.annotations.Test; |
| 27 | +import static org.testng.Assert.assertTrue; |
| 28 | + |
| 29 | +/** |
| 30 | + * test the functionalities of JavaVMemServiceImpl class |
| 31 | + * |
| 32 | + */ |
| 33 | +public class JavaVMemServiceImplNGTest { |
| 34 | + private String testFile = "./jvmstest.dat"; |
| 35 | + /** |
| 36 | + * test to verify the initial of memory service successes when isnew = true |
| 37 | + * regardless of whether the file exists or not (file exists) |
| 38 | + */ |
| 39 | + @Test |
| 40 | + public void testToInitByCreateNewFile1() throws IOException, ClassNotFoundException { |
| 41 | + //Ensure it doesn't impact the test file |
| 42 | + String dest = "./jvmstestCopy.dat"; |
| 43 | + Files.copy(Paths.get(testFile), Paths.get(dest)); |
| 44 | + |
| 45 | + JavaVMemServiceImpl jvms = new JavaVMemServiceImpl(); |
| 46 | + long cap = 10 * 1024 * 1024; |
| 47 | + long idx = jvms.init(10 * 1024 * 1024, dest, true); |
| 48 | + assertTrue(idx != -1); |
| 49 | + assertTrue(idx == 0); |
| 50 | + assertTrue(jvms.getMInfo().get(idx).equals(cap)); |
| 51 | + jvms.close(idx); |
| 52 | + Files.delete(Paths.get(dest)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * test to verify the initial of memory service successes when isnew = true |
| 57 | + * regardless of whether the file exists or not (file doesn't exist) |
| 58 | + */ |
| 59 | + @Test |
| 60 | + public void testToInitByCreateNewFile2() throws IOException, ClassNotFoundException { |
| 61 | + JavaVMemServiceImpl jvms = new JavaVMemServiceImpl(); |
| 62 | + long cap = 10 * 1024 * 1024; |
| 63 | + long idx = jvms.init(10 * 1024 * 1024, "./jvmstestnotexist.dat", true); |
| 64 | + assertTrue(idx != -1); |
| 65 | + assertTrue(idx == 0); |
| 66 | + assertTrue(jvms.getMInfo().get(idx).equals(cap)); |
| 67 | + |
| 68 | + //Delete the new created test file |
| 69 | + File file = new File("./jvmstestnotexist.dat"); |
| 70 | + file.delete(); |
| 71 | + jvms.close(idx); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + /** |
| 77 | + * test to verify the initial of memory service fails when isnew = true |
| 78 | + * and the specifiled uri is not a file |
| 79 | + */ |
| 80 | + @Test |
| 81 | + public void testToInitFailWhenNofile() throws IOException, ClassNotFoundException { |
| 82 | + long idx = -1; |
| 83 | + boolean thrown = false; |
| 84 | + JavaVMemServiceImpl jvms = new JavaVMemServiceImpl(); |
| 85 | + try { |
| 86 | + idx = jvms.init(10 * 1024 * 1024, ".", true); |
| 87 | + } catch (Exception e) { |
| 88 | + thrown = true; |
| 89 | + } finally { |
| 90 | + assertTrue(thrown); |
| 91 | + if (idx >= 0) { |
| 92 | + jvms.close(idx); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * test to verify the initial of memory service successes when isnew = false |
| 99 | + * and the specifiled file exists. |
| 100 | + */ |
| 101 | + @Test |
| 102 | + public void testToInitWhenFileExists() throws IOException, ClassNotFoundException { |
| 103 | + JavaVMemServiceImpl jvms = new JavaVMemServiceImpl(); |
| 104 | + File file = new File(testFile); |
| 105 | + long cap = file.length(); |
| 106 | + long idx = jvms.init(10 * 1024 * 1024, testFile, false); |
| 107 | + assertTrue(idx != -1); |
| 108 | + assertTrue(idx == 0); |
| 109 | + assertTrue(jvms.getMInfo().get(idx).equals(cap)); |
| 110 | + jvms.close(idx); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * test to verify the initial of memory service fails when isnew = false |
| 115 | + * and the specifiled file doesn't exist. |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void testToInitFailWhenFileNotExists() throws IOException, ClassNotFoundException { |
| 119 | + long idx = -1; |
| 120 | + boolean thrown = false; |
| 121 | + JavaVMemServiceImpl jvms = new JavaVMemServiceImpl(); |
| 122 | + try { |
| 123 | + idx = jvms.init(10 * 1024 * 1024, "./jvmstestnotexist.dat", false); |
| 124 | + } catch (Exception e) { |
| 125 | + thrown = true; |
| 126 | + } finally { |
| 127 | + assertTrue(thrown); |
| 128 | + if (idx >= 0) { |
| 129 | + jvms.close(idx); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * test to verify the initial of memory service fails when isnew = false |
| 136 | + * and the specifiled uri is not a file. |
| 137 | + */ |
| 138 | + @Test |
| 139 | + public void testToInitFailWhenNotAFile() throws IOException, ClassNotFoundException { |
| 140 | + long idx = -1; |
| 141 | + boolean thrown = false; |
| 142 | + JavaVMemServiceImpl jvms = new JavaVMemServiceImpl(); |
| 143 | + try { |
| 144 | + idx = jvms.init(10 * 1024 * 1024, ".", false); |
| 145 | + } catch (Exception e) { |
| 146 | + thrown = true; |
| 147 | + } finally { |
| 148 | + assertTrue(thrown); |
| 149 | + if (idx >= 0) { |
| 150 | + jvms.close(idx); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments