Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | |
| 22 | import org.junit.Test; |
| 23 | |
| 24 | import static org.hamcrest.MatcherAssert.assertThat; |
| 25 | import static org.hamcrest.core.Is.is; |
| 26 | |
| 27 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 28 | import org.onosproject.yangutils.datamodel.YangType; |
| 29 | import org.onosproject.yangutils.translator.CachedFileHandle; |
| 30 | import org.onosproject.yangutils.translator.GeneratedFileType; |
| 31 | import org.onosproject.yangutils.utils.UtilConstants; |
| 32 | import org.onosproject.yangutils.utils.io.impl.CopyrightHeader; |
| 33 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
| 34 | |
| 35 | /** |
| 36 | * Unit test case for cached java file handle. |
| 37 | */ |
| 38 | public class CachedJavaFileHandleTest { |
| 39 | |
| 40 | private static final String DIR_PKG = "target/unit/cachedfile/"; |
| 41 | private static final String PKG = "org.onosproject.unittest"; |
| 42 | private static final String CHILD_PKG = "target/unit/cachedfile/child"; |
| 43 | private static final String YANG_NAME = "Test1"; |
| 44 | private static final GeneratedFileType GEN_TYPE = GeneratedFileType.ALL; |
| 45 | |
| 46 | /** |
| 47 | * Unit test case for add attribute info. |
| 48 | * |
| 49 | * @throws IOException when fails to add an attribute. |
| 50 | */ |
| 51 | @Test |
| 52 | public void testForAddAttributeInfo() throws IOException { |
| 53 | |
| 54 | AttributeInfo attr = getAttr(); |
| 55 | attr.setListAttr(false); |
| 56 | getFileHandle().addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr()); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Unit test case for close of cached files. |
| 61 | * |
| 62 | * @throws IOException when fails to generate files. |
| 63 | */ |
| 64 | @Test |
| 65 | public void testForClose() throws IOException { |
| 66 | |
| 67 | CopyrightHeader.parseCopyrightHeader(); |
| 68 | |
| 69 | AttributeInfo attr = getAttr(); |
| 70 | attr.setListAttr(false); |
| 71 | CachedFileHandle handle = getFileHandle(); |
| 72 | handle.addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr()); |
| 73 | handle.close(); |
| 74 | |
| 75 | assertThat(true, is(getStubDir().exists())); |
| 76 | assertThat(true, is(getStubPkgInfo().exists())); |
| 77 | assertThat(true, is(getStubInterfaceFile().exists())); |
| 78 | assertThat(true, is(getStubBuilderFile().exists())); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Unit test case for setting child's package. |
| 83 | * |
| 84 | * @throws IOException when fails to add child's package |
| 85 | */ |
| 86 | @Test |
| 87 | public void testForSetChildsPackage() throws IOException { |
| 88 | |
| 89 | AttributeInfo attr = getAttr(); |
| 90 | attr.setListAttr(false); |
| 91 | CachedFileHandle handle = getFileHandle(); |
| 92 | handle.addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr()); |
| 93 | |
| 94 | handle.setChildsPackage(CHILD_PKG); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns attribute info. |
| 99 | * |
| 100 | * @return attribute info |
| 101 | */ |
| 102 | @SuppressWarnings("rawtypes") |
| 103 | private AttributeInfo getAttr() { |
| 104 | YangType<?> type = new YangType(); |
| 105 | YangDataTypes dataType = YangDataTypes.STRING; |
| 106 | |
| 107 | type.setDataTypeName("string"); |
| 108 | type.setDataType(dataType); |
| 109 | |
| 110 | AttributeInfo attr = new AttributeInfo(); |
| 111 | |
| 112 | attr.setAttributeName("testAttr"); |
| 113 | attr.setAttributeType(type); |
| 114 | return attr; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns cached java file handle. |
| 119 | * |
| 120 | * @return java file handle. |
| 121 | */ |
| 122 | private CachedFileHandle getFileHandle() throws IOException { |
| 123 | CopyrightHeader.parseCopyrightHeader(); |
| 124 | FileSystemUtil.createPackage(DIR_PKG + File.separator + PKG, YANG_NAME); |
| 125 | CachedFileHandle fileHandle = FileSystemUtil.createSourceFiles(PKG, YANG_NAME, GEN_TYPE); |
| 126 | fileHandle.setFilePath(DIR_PKG + PKG.replace(".", "/")); |
| 127 | |
| 128 | return fileHandle; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns stub directory file object. |
| 133 | * |
| 134 | * @return stub directory file |
| 135 | */ |
| 136 | private File getStubDir() { |
| 137 | return new File(DIR_PKG); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns stub package-info file object. |
| 142 | * |
| 143 | * @return stub package-info file |
| 144 | */ |
| 145 | private File getStubPkgInfo() { |
| 146 | return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator |
| 147 | + "package-info.java"); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns stub interface file object. |
| 152 | * |
| 153 | * @return stub interface file |
| 154 | */ |
| 155 | private File getStubInterfaceFile() { |
| 156 | return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME |
| 157 | + ".java"); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns stub builder file. |
| 162 | * |
| 163 | * @return stub builder file |
| 164 | */ |
| 165 | private File getStubBuilderFile() { |
| 166 | return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME |
| 167 | + "Builder.java"); |
| 168 | } |
| 169 | |
| 170 | } |