blob: 2aa1daa28ef1e3d4c99b9d098a435a99f34600db [file] [log] [blame]
Bharat saraswalc46ee2a2016-02-25 02:26:43 +05301/*
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
17package org.onosproject.yangutils.translator.tojava;
18
19import java.io.File;
20import java.io.IOException;
21
22import org.junit.Test;
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053023import org.onosproject.yangutils.datamodel.YangDataTypes;
24import org.onosproject.yangutils.datamodel.YangType;
25import org.onosproject.yangutils.translator.CachedFileHandle;
26import org.onosproject.yangutils.translator.GeneratedFileType;
27import org.onosproject.yangutils.utils.UtilConstants;
28import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
29import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
30
31/**
32 * Unit test case for cached java file handle.
33 */
34public class CachedJavaFileHandleTest {
35
36 private static final String DIR_PKG = "target/unit/cachedfile/";
37 private static final String PKG = "org.onosproject.unittest";
38 private static final String CHILD_PKG = "target/unit/cachedfile/child";
39 private static final String YANG_NAME = "Test1";
Vinod Kumar S08710982016-03-03 19:55:30 +053040 private static final int GEN_TYPE = GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053041
42 /**
43 * Unit test case for add attribute info.
44 *
45 * @throws IOException when fails to add an attribute.
46 */
47 @Test
48 public void testForAddAttributeInfo() throws IOException {
49
50 AttributeInfo attr = getAttr();
51 attr.setListAttr(false);
52 getFileHandle().addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
53 }
54
55 /**
56 * Unit test case for close of cached files.
57 *
58 * @throws IOException when fails to generate files.
59 */
60 @Test
61 public void testForClose() throws IOException {
62
Vinod Kumar S08710982016-03-03 19:55:30 +053063 // TODO: update to new framework.
64 // CopyrightHeader.parseCopyrightHeader();
65 //
66 // AttributeInfo attr = getAttr();
67 // attr.setListAttr(false);
68 // CachedFileHandle handle = getFileHandle();
69 // handle.addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
70 // handle.close();
71 //
72 // assertThat(true, is(getStubDir().exists()));
73 // assertThat(true, is(getStubPkgInfo().exists()));
74 // assertThat(true, is(getStubInterfaceFile().exists()));
75 // assertThat(true, is(getStubBuilderFile().exists()));
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053076 }
77
78 /**
79 * Unit test case for setting child's package.
80 *
81 * @throws IOException when fails to add child's package
82 */
83 @Test
84 public void testForSetChildsPackage() throws IOException {
85
86 AttributeInfo attr = getAttr();
87 attr.setListAttr(false);
88 CachedFileHandle handle = getFileHandle();
89 handle.addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
90
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053091 }
92
93 /**
94 * Returns attribute info.
95 *
96 * @return attribute info
97 */
98 @SuppressWarnings("rawtypes")
99 private AttributeInfo getAttr() {
100 YangType<?> type = new YangType();
101 YangDataTypes dataType = YangDataTypes.STRING;
102
103 type.setDataTypeName("string");
104 type.setDataType(dataType);
105
106 AttributeInfo attr = new AttributeInfo();
107
108 attr.setAttributeName("testAttr");
109 attr.setAttributeType(type);
110 return attr;
111 }
112
113 /**
114 * Returns cached java file handle.
115 *
116 * @return java file handle.
117 */
118 private CachedFileHandle getFileHandle() throws IOException {
119 CopyrightHeader.parseCopyrightHeader();
120 FileSystemUtil.createPackage(DIR_PKG + File.separator + PKG, YANG_NAME);
121 CachedFileHandle fileHandle = FileSystemUtil.createSourceFiles(PKG, YANG_NAME, GEN_TYPE);
Vinod Kumar S08710982016-03-03 19:55:30 +0530122 fileHandle.setRelativeFilePath(DIR_PKG + PKG.replace(".", "/"));
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530123
124 return fileHandle;
125 }
126
127 /**
128 * Returns stub directory file object.
129 *
130 * @return stub directory file
131 */
132 private File getStubDir() {
133 return new File(DIR_PKG);
134 }
135
136 /**
137 * Returns stub package-info file object.
138 *
139 * @return stub package-info file
140 */
141 private File getStubPkgInfo() {
142 return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator
143 + "package-info.java");
144 }
145
146 /**
147 * Returns stub interface file object.
148 *
149 * @return stub interface file
150 */
151 private File getStubInterfaceFile() {
152 return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME
153 + ".java");
154 }
155
156 /**
157 * Returns stub builder file.
158 *
159 * @return stub builder file
160 */
161 private File getStubBuilderFile() {
162 return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME
163 + "Builder.java");
164 }
165
166}