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; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 23 | |
| 24 | import static org.hamcrest.MatcherAssert.assertThat; |
| 25 | import static org.hamcrest.core.Is.is; |
| 26 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 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 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 40 | private static final String DIR_PKG = "target/unit/cachedfile/yangmodel/"; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 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"; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 44 | private static final int GEN_TYPE = GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Unit test case for add attribute info. |
| 48 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 49 | * @throws IOException when fails to add an attribute |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 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 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 62 | * @throws IOException when fails to generate files |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 63 | */ |
| 64 | @Test |
| 65 | public void testForClose() throws IOException { |
| 66 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 67 | CopyrightHeader.parseCopyrightHeader(); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 68 | |
| 69 | AttributeInfo attr = getAttr(); |
| 70 | attr.setListAttr(false); |
| 71 | CachedFileHandle handle = getFileHandle(); |
| 72 | handle.addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr()); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 73 | handle.close(); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 74 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 75 | assertThat(true, is(getStubDir().exists())); |
| 76 | assertThat(true, is(getStubPkgInfo().exists())); |
| 77 | assertThat(true, is(getStubInterfaceFile().exists())); |
| 78 | assertThat(true, is(getStubBuilderFile().exists())); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns attribute info. |
| 83 | * |
| 84 | * @return attribute info |
| 85 | */ |
| 86 | @SuppressWarnings("rawtypes") |
| 87 | private AttributeInfo getAttr() { |
| 88 | YangType<?> type = new YangType(); |
| 89 | YangDataTypes dataType = YangDataTypes.STRING; |
| 90 | |
| 91 | type.setDataTypeName("string"); |
| 92 | type.setDataType(dataType); |
| 93 | |
| 94 | AttributeInfo attr = new AttributeInfo(); |
| 95 | |
| 96 | attr.setAttributeName("testAttr"); |
| 97 | attr.setAttributeType(type); |
| 98 | return attr; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns cached java file handle. |
| 103 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 104 | * @return java file handle |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 105 | */ |
| 106 | private CachedFileHandle getFileHandle() throws IOException { |
| 107 | CopyrightHeader.parseCopyrightHeader(); |
| 108 | FileSystemUtil.createPackage(DIR_PKG + File.separator + PKG, YANG_NAME); |
| 109 | CachedFileHandle fileHandle = FileSystemUtil.createSourceFiles(PKG, YANG_NAME, GEN_TYPE); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 110 | fileHandle.setRelativeFilePath(PKG.replace(".", "/")); |
| 111 | fileHandle.setCodeGenFilePath(DIR_PKG); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 112 | return fileHandle; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns stub directory file object. |
| 117 | * |
| 118 | * @return stub directory file |
| 119 | */ |
| 120 | private File getStubDir() { |
| 121 | return new File(DIR_PKG); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns stub package-info file object. |
| 126 | * |
| 127 | * @return stub package-info file |
| 128 | */ |
| 129 | private File getStubPkgInfo() { |
| 130 | return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator |
| 131 | + "package-info.java"); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns stub interface file object. |
| 136 | * |
| 137 | * @return stub interface file |
| 138 | */ |
| 139 | private File getStubInterfaceFile() { |
| 140 | return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME |
| 141 | + ".java"); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns stub builder file. |
| 146 | * |
| 147 | * @return stub builder file |
| 148 | */ |
| 149 | private File getStubBuilderFile() { |
| 150 | return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME |
| 151 | + "Builder.java"); |
| 152 | } |
| 153 | |
| 154 | } |