Committed by
Gerrit Code Review
[ONOS-3908] YANG container translator.
Change-Id: I4e239509df747238905ca0995f41019679093627
Showing
19 changed files
with
528 additions
and
108 deletions
... | @@ -26,6 +26,7 @@ import org.onosproject.yangutils.parser.ParsableDataType; | ... | @@ -26,6 +26,7 @@ import org.onosproject.yangutils.parser.ParsableDataType; |
26 | import org.onosproject.yangutils.translator.CachedFileHandle; | 26 | import org.onosproject.yangutils.translator.CachedFileHandle; |
27 | import org.onosproject.yangutils.translator.GeneratedFileType; | 27 | import org.onosproject.yangutils.translator.GeneratedFileType; |
28 | import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax; | 28 | import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax; |
29 | +import org.onosproject.yangutils.utils.UtilConstants; | ||
29 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; | 30 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
30 | /*- | 31 | /*- |
31 | * Reference RFC 6020. | 32 | * Reference RFC 6020. |
... | @@ -472,17 +473,21 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom | ... | @@ -472,17 +473,21 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom |
472 | @Override | 473 | @Override |
473 | public void generateJavaCodeEntry() throws IOException { | 474 | public void generateJavaCodeEntry() throws IOException { |
474 | YangNode parent = getParent(); | 475 | YangNode parent = getParent(); |
475 | - String modPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), getName()); | 476 | + String contPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), parent.getName()); |
476 | - setPackage(modPkg); | 477 | + setPackage(contPkg); |
477 | 478 | ||
478 | CachedFileHandle handle = null; | 479 | CachedFileHandle handle = null; |
479 | try { | 480 | try { |
480 | - FileSystemUtil.createPackage(getPackage(), getName()); | 481 | + FileSystemUtil.createPackage(UtilConstants.YANG_GEN_DIR + getPackage(), getName()); |
481 | handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL); | 482 | handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL); |
483 | + handle.setFilePath(UtilConstants.YANG_GEN_DIR + getPackage().replace(".", "/")); | ||
482 | } catch (IOException e) { | 484 | } catch (IOException e) { |
483 | throw new IOException("Failed to create the source files."); | 485 | throw new IOException("Failed to create the source files."); |
484 | } | 486 | } |
485 | setFileHandle(handle); | 487 | setFileHandle(handle); |
488 | + | ||
489 | + addLeavesAttributes(); | ||
490 | + addLeafListAttributes(); | ||
486 | addAttributeInParent(); | 491 | addAttributeInParent(); |
487 | } | 492 | } |
488 | 493 | ||
... | @@ -498,8 +503,6 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom | ... | @@ -498,8 +503,6 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom |
498 | 503 | ||
499 | @Override | 504 | @Override |
500 | public void generateJavaCodeExit() throws IOException { | 505 | public void generateJavaCodeExit() throws IOException { |
501 | - addLeavesAttributes(); | ||
502 | - addLeafListAttributes(); | ||
503 | getFileHandle().close(); | 506 | getFileHandle().close(); |
504 | return; | 507 | return; |
505 | } | 508 | } | ... | ... |
... | @@ -26,6 +26,7 @@ import org.onosproject.yangutils.translator.CachedFileHandle; | ... | @@ -26,6 +26,7 @@ import org.onosproject.yangutils.translator.CachedFileHandle; |
26 | import org.onosproject.yangutils.translator.CodeGenerator; | 26 | import org.onosproject.yangutils.translator.CodeGenerator; |
27 | import org.onosproject.yangutils.translator.GeneratedFileType; | 27 | import org.onosproject.yangutils.translator.GeneratedFileType; |
28 | import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax; | 28 | import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax; |
29 | +import org.onosproject.yangutils.utils.UtilConstants; | ||
29 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; | 30 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
30 | 31 | ||
31 | /*- | 32 | /*- |
... | @@ -558,18 +559,19 @@ public class YangModule extends YangNode | ... | @@ -558,18 +559,19 @@ public class YangModule extends YangNode |
558 | 559 | ||
559 | CachedFileHandle handle = null; | 560 | CachedFileHandle handle = null; |
560 | try { | 561 | try { |
561 | - FileSystemUtil.createPackage(getPackage(), getName()); | 562 | + FileSystemUtil.createPackage(UtilConstants.YANG_GEN_DIR + getPackage(), getName()); |
562 | handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL); | 563 | handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL); |
564 | + handle.setFilePath(UtilConstants.YANG_GEN_DIR + getPackage().replace(".", "/")); | ||
563 | } catch (IOException e) { | 565 | } catch (IOException e) { |
564 | throw new IOException("Failed to create the source files."); | 566 | throw new IOException("Failed to create the source files."); |
565 | } | 567 | } |
566 | setFileHandle(handle); | 568 | setFileHandle(handle); |
569 | + addLeavesAttributes(); | ||
570 | + addLeafListAttributes(); | ||
567 | } | 571 | } |
568 | 572 | ||
569 | @Override | 573 | @Override |
570 | public void generateJavaCodeExit() throws IOException { | 574 | public void generateJavaCodeExit() throws IOException { |
571 | - addLeavesAttributes(); | ||
572 | - addLeafListAttributes(); | ||
573 | getFileHandle().close(); | 575 | getFileHandle().close(); |
574 | return; | 576 | return; |
575 | } | 577 | } | ... | ... |
... | @@ -49,4 +49,11 @@ public interface CachedFileHandle { | ... | @@ -49,4 +49,11 @@ public interface CachedFileHandle { |
49 | * @param pkg child's package path | 49 | * @param pkg child's package path |
50 | */ | 50 | */ |
51 | void setChildsPackage(String pkg); | 51 | void setChildsPackage(String pkg); |
52 | + | ||
53 | + /** | ||
54 | + * Sets directory package path for code generation. | ||
55 | + * | ||
56 | + * @param filePath directory package path for code generation | ||
57 | + */ | ||
58 | + void setFilePath(String filePath); | ||
52 | } | 59 | } | ... | ... |
... | @@ -19,6 +19,7 @@ package org.onosproject.yangutils.translator.tojava; | ... | @@ -19,6 +19,7 @@ package org.onosproject.yangutils.translator.tojava; |
19 | import java.io.Serializable; | 19 | import java.io.Serializable; |
20 | 20 | ||
21 | import org.onosproject.yangutils.datamodel.YangType; | 21 | import org.onosproject.yangutils.datamodel.YangType; |
22 | +import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType; | ||
22 | 23 | ||
23 | /** | 24 | /** |
24 | * Maintains the attribute info corresponding to class/interface generated. | 25 | * Maintains the attribute info corresponding to class/interface generated. |
... | @@ -71,7 +72,10 @@ public class AttributeInfo implements Serializable { | ... | @@ -71,7 +72,10 @@ public class AttributeInfo implements Serializable { |
71 | * @param type the data type info of attribute. | 72 | * @param type the data type info of attribute. |
72 | */ | 73 | */ |
73 | public void setAttributeType(YangType<?> type) { | 74 | public void setAttributeType(YangType<?> type) { |
74 | - attrType = type; | 75 | + |
76 | + if (type != null) { | ||
77 | + attrType = AttributesJavaDataType.getJavaDataType(type); | ||
78 | + } | ||
75 | } | 79 | } |
76 | 80 | ||
77 | /** | 81 | /** | ... | ... |
This diff is collapsed. Click to expand it.
... | @@ -42,21 +42,22 @@ public final class JavaCodeGenerator { | ... | @@ -42,21 +42,22 @@ public final class JavaCodeGenerator { |
42 | TraversalType curTraversal = TraversalType.ROOT; | 42 | TraversalType curTraversal = TraversalType.ROOT; |
43 | 43 | ||
44 | while (!(curNode == null)) { | 44 | while (!(curNode == null)) { |
45 | - if (curTraversal != TraversalType.PARENT || curTraversal == TraversalType.SIBILING) { | 45 | + if (curTraversal != TraversalType.PARENT) { |
46 | curNode.generateJavaCodeEntry(); | 46 | curNode.generateJavaCodeEntry(); |
47 | } | 47 | } |
48 | - if (curTraversal != TraversalType.PARENT && !(curNode.getChild() == null)) { | 48 | + if (curTraversal != TraversalType.PARENT && (curNode.getChild() != null)) { |
49 | curTraversal = TraversalType.CHILD; | 49 | curTraversal = TraversalType.CHILD; |
50 | curNode = curNode.getChild(); | 50 | curNode = curNode.getChild(); |
51 | - } else if (curTraversal == TraversalType.PARENT && !(curNode.getNextSibling() == null)) { | 51 | + } else if ((curNode.getNextSibling() != null)) { |
52 | curNode.generateJavaCodeExit(); | 52 | curNode.generateJavaCodeExit(); |
53 | curTraversal = TraversalType.SIBILING; | 53 | curTraversal = TraversalType.SIBILING; |
54 | curNode = curNode.getNextSibling(); | 54 | curNode = curNode.getNextSibling(); |
55 | } else { | 55 | } else { |
56 | - curNode.generateJavaCodeExit(); | ||
57 | curTraversal = TraversalType.PARENT; | 56 | curTraversal = TraversalType.PARENT; |
57 | + curNode.generateJavaCodeExit(); | ||
58 | curNode = curNode.getParent(); | 58 | curNode = curNode.getParent(); |
59 | } | 59 | } |
60 | } | 60 | } |
61 | + | ||
61 | } | 62 | } |
62 | } | 63 | } | ... | ... |
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.utils; | ||
18 | + | ||
19 | +import org.onosproject.yangutils.datamodel.YangDataTypes; | ||
20 | +import org.onosproject.yangutils.datamodel.YangType; | ||
21 | +import org.onosproject.yangutils.translator.tojava.AttributeInfo; | ||
22 | +import org.onosproject.yangutils.utils.UtilConstants; | ||
23 | + | ||
24 | +/** | ||
25 | + * Provides java data types corresponding to YANG type. | ||
26 | + */ | ||
27 | +public final class AttributesJavaDataType { | ||
28 | + | ||
29 | + /** | ||
30 | + * Default constructor. | ||
31 | + */ | ||
32 | + private AttributesJavaDataType() { | ||
33 | + } | ||
34 | + | ||
35 | + /** | ||
36 | + * Returns YANG type. | ||
37 | + * | ||
38 | + * @param yangType YANG type | ||
39 | + * @return YANG type | ||
40 | + */ | ||
41 | + public static YangType<?> getJavaDataType(YangType<?> yangType) { | ||
42 | + yangType.setDataTypeName(yangType.getDataTypeName().replace("\"", "")); | ||
43 | + if (yangType.getDataType() != null) { | ||
44 | + yangType.setDataTypeName(parseYangDataType(yangType.getDataType())); | ||
45 | + } | ||
46 | + return yangType; | ||
47 | + } | ||
48 | + | ||
49 | + /** | ||
50 | + * Returns list string as attribute name when attribute is a list. | ||
51 | + * | ||
52 | + * @param attr attribute info. | ||
53 | + * @return list attribute | ||
54 | + */ | ||
55 | + @SuppressWarnings("rawtypes") | ||
56 | + public static YangType<?> getListString(AttributeInfo attr) { | ||
57 | + String listString = JavaCodeSnippetGen.getListAttribute(attr.getAttributeType().getDataTypeName()); | ||
58 | + YangType<?> type = new YangType(); | ||
59 | + type.setDataTypeName(listString); | ||
60 | + attr.setAttributeType(type); | ||
61 | + return type; | ||
62 | + } | ||
63 | + | ||
64 | + /** | ||
65 | + * Parses YANG data type and returns corresponding java data type. | ||
66 | + * | ||
67 | + * @param type YANG data type | ||
68 | + * @return java data type | ||
69 | + */ | ||
70 | + private static String parseYangDataType(YangDataTypes type) { | ||
71 | + if (type.equals(YangDataTypes.INT8)) { | ||
72 | + return UtilConstants.BYTE; | ||
73 | + } else if (type.equals(YangDataTypes.INT16)) { | ||
74 | + return UtilConstants.SHORT; | ||
75 | + } else if (type.equals(YangDataTypes.INT32)) { | ||
76 | + return UtilConstants.INT; | ||
77 | + } else if (type.equals(YangDataTypes.INT64)) { | ||
78 | + return UtilConstants.LONG; | ||
79 | + } else if (type.equals(YangDataTypes.UINT8)) { | ||
80 | + return UtilConstants.SHORT; | ||
81 | + } else if (type.equals(YangDataTypes.UINT16)) { | ||
82 | + return UtilConstants.INT; | ||
83 | + } else if (type.equals(YangDataTypes.UINT32)) { | ||
84 | + return UtilConstants.LONG; | ||
85 | + } else if (type.equals(YangDataTypes.UINT64)) { | ||
86 | + //TODO: BIGINTEGER. | ||
87 | + } else if (type.equals(YangDataTypes.DECIMAL64)) { | ||
88 | + //TODO: DECIMAL64. | ||
89 | + } else if (type.equals(YangDataTypes.STRING)) { | ||
90 | + return UtilConstants.STRING; | ||
91 | + } else if (type.equals(YangDataTypes.BOOLEAN)) { | ||
92 | + return UtilConstants.BOOLEAN; | ||
93 | + } else if (type.equals(YangDataTypes.ENUMERATION)) { | ||
94 | + //TODO: ENUMERATION. | ||
95 | + } else if (type.equals(YangDataTypes.BITS)) { | ||
96 | + //TODO:BITS | ||
97 | + } else if (type.equals(YangDataTypes.BINARY)) { | ||
98 | + //TODO:BINARY | ||
99 | + } else if (type.equals(YangDataTypes.LEAFREF)) { | ||
100 | + //TODO:LEAFREF | ||
101 | + } else if (type.equals(YangDataTypes.IDENTITYREF)) { | ||
102 | + //TODO:IDENTITYREF | ||
103 | + } else if (type.equals(YangDataTypes.EMPTY)) { | ||
104 | + //TODO:EMPTY | ||
105 | + } else if (type.equals(YangDataTypes.UNION)) { | ||
106 | + //TODO:UNION | ||
107 | + } else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) { | ||
108 | + //TODO:INSTANCE_IDENTIFIER | ||
109 | + } else if (type.equals(YangDataTypes.DERIVED)) { | ||
110 | + //TODO:DERIVED | ||
111 | + } | ||
112 | + return null; | ||
113 | + } | ||
114 | +} |
... | @@ -16,9 +16,6 @@ | ... | @@ -16,9 +16,6 @@ |
16 | 16 | ||
17 | package org.onosproject.yangutils.translator.tojava.utils; | 17 | package org.onosproject.yangutils.translator.tojava.utils; |
18 | 18 | ||
19 | -import java.util.List; | ||
20 | -import java.util.SortedSet; | ||
21 | - | ||
22 | import org.onosproject.yangutils.datamodel.YangType; | 19 | import org.onosproject.yangutils.datamodel.YangType; |
23 | import org.onosproject.yangutils.translator.GeneratedFileType; | 20 | import org.onosproject.yangutils.translator.GeneratedFileType; |
24 | import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes; | 21 | import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes; |
... | @@ -50,17 +47,6 @@ public final class JavaCodeSnippetGen { | ... | @@ -50,17 +47,6 @@ public final class JavaCodeSnippetGen { |
50 | } | 47 | } |
51 | 48 | ||
52 | /** | 49 | /** |
53 | - * reorder the import list based on the ONOS import rules. | ||
54 | - * | ||
55 | - * @param importInfo the set of classes/interfaces to be imported. | ||
56 | - * @return string of import info. | ||
57 | - */ | ||
58 | - public List<ImportInfo> sortImportOrder(SortedSet<ImportInfo> importInfo) { | ||
59 | - /* TODO: reorder the import list based on the ONOS import rules. */ | ||
60 | - return null; | ||
61 | - } | ||
62 | - | ||
63 | - /** | ||
64 | * Get the textual java code information corresponding to the import list. | 50 | * Get the textual java code information corresponding to the import list. |
65 | * | 51 | * |
66 | * @param importInfo import info. | 52 | * @param importInfo import info. | ... | ... |
This diff is collapsed. Click to expand it.
... | @@ -50,7 +50,7 @@ public final class JavaIdentifierSyntax { | ... | @@ -50,7 +50,7 @@ public final class JavaIdentifierSyntax { |
50 | pkg = pkg + UtilConstants.PERIOD; | 50 | pkg = pkg + UtilConstants.PERIOD; |
51 | pkg = pkg + getYangRevisionStr(revision); | 51 | pkg = pkg + getYangRevisionStr(revision); |
52 | 52 | ||
53 | - return pkg; | 53 | + return pkg.toLowerCase(); |
54 | } | 54 | } |
55 | 55 | ||
56 | /** | 56 | /** |
... | @@ -126,11 +126,11 @@ public final class JavaIdentifierSyntax { | ... | @@ -126,11 +126,11 @@ public final class JavaIdentifierSyntax { |
126 | * Get the package from parent's package and string. | 126 | * Get the package from parent's package and string. |
127 | * | 127 | * |
128 | * @param parentPkg parent's package. | 128 | * @param parentPkg parent's package. |
129 | - * @param childName child's name. | 129 | + * @param parentName parent's name. |
130 | * @return package string. | 130 | * @return package string. |
131 | */ | 131 | */ |
132 | - public static String getPackageFromParent(String parentPkg, String childName) { | 132 | + public static String getPackageFromParent(String parentPkg, String parentName) { |
133 | - return parentPkg + UtilConstants.PERIOD + getSubPkgFromName(childName); | 133 | + return (parentPkg + UtilConstants.PERIOD + getSubPkgFromName(parentName)).toLowerCase(); |
134 | } | 134 | } |
135 | 135 | ||
136 | /** | 136 | /** | ... | ... |
... | @@ -109,6 +109,7 @@ public final class MethodsGenerator { | ... | @@ -109,6 +109,7 @@ public final class MethodsGenerator { |
109 | * @param returnType return type of method | 109 | * @param returnType return type of method |
110 | * @return constructed method impl | 110 | * @return constructed method impl |
111 | */ | 111 | */ |
112 | + @SuppressWarnings("rawtypes") | ||
112 | public static String constructMethodInfo(GeneratedFileType genFileTypes, String yangName, | 113 | public static String constructMethodInfo(GeneratedFileType genFileTypes, String yangName, |
113 | GeneratedMethodTypes methodTypes, YangType<?> returnType) { | 114 | GeneratedMethodTypes methodTypes, YangType<?> returnType) { |
114 | 115 | ||
... | @@ -187,8 +188,7 @@ public final class MethodsGenerator { | ... | @@ -187,8 +188,7 @@ public final class MethodsGenerator { |
187 | attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType()); | 188 | attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType()); |
188 | String setterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS, | 189 | String setterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS, |
189 | attr.getAttributeName(), GeneratedMethodTypes.SETTER, attr.getAttributeType()); | 190 | attr.getAttributeName(), GeneratedMethodTypes.SETTER, attr.getAttributeType()); |
190 | - return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString | 191 | + return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString; |
191 | - + UtilConstants.NEW_LINE; | ||
192 | } | 192 | } |
193 | 193 | ||
194 | /** | 194 | /** |
... | @@ -201,8 +201,7 @@ public final class MethodsGenerator { | ... | @@ -201,8 +201,7 @@ public final class MethodsGenerator { |
201 | 201 | ||
202 | return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE | 202 | return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE |
203 | + UtilConstants.NEW_LINE + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS, | 203 | + UtilConstants.NEW_LINE + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS, |
204 | - attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType()) | 204 | + attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType()); |
205 | - + UtilConstants.NEW_LINE; | ||
206 | } | 205 | } |
207 | 206 | ||
208 | /** | 207 | /** |
... | @@ -400,8 +399,7 @@ public final class MethodsGenerator { | ... | @@ -400,8 +399,7 @@ public final class MethodsGenerator { |
400 | } | 399 | } |
401 | getAttrInfo().clear(); | 400 | getAttrInfo().clear(); |
402 | } | 401 | } |
403 | - return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET | 402 | + return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET; |
404 | - + UtilConstants.NEW_LINE; | ||
405 | } | 403 | } |
406 | 404 | ||
407 | /** | 405 | /** | ... | ... |
... | @@ -54,7 +54,6 @@ public final class FileSystemUtil { | ... | @@ -54,7 +54,6 @@ public final class FileSystemUtil { |
54 | return false; | 54 | return false; |
55 | } | 55 | } |
56 | 56 | ||
57 | - | ||
58 | /** | 57 | /** |
59 | * Create a package structure with package info java file if not present. | 58 | * Create a package structure with package info java file if not present. |
60 | * | 59 | * | ... | ... |
... | @@ -170,8 +170,8 @@ public final class JavaDocGen { | ... | @@ -170,8 +170,8 @@ public final class JavaDocGen { |
170 | * @return javaDocs. | 170 | * @return javaDocs. |
171 | */ | 171 | */ |
172 | private static String generateForBuilderClass(String className) { | 172 | private static String generateForBuilderClass(String className) { |
173 | - return (UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_CLASS_JAVA_DOC | 173 | + return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_CLASS_JAVA_DOC + className |
174 | - + className + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE); | 174 | + + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE); |
175 | } | 175 | } |
176 | 176 | ||
177 | /** | 177 | /** |
... | @@ -181,8 +181,8 @@ public final class JavaDocGen { | ... | @@ -181,8 +181,8 @@ public final class JavaDocGen { |
181 | * @return javaDocs. | 181 | * @return javaDocs. |
182 | */ | 182 | */ |
183 | private static String generateForInterface(String interfaceName) { | 183 | private static String generateForInterface(String interfaceName) { |
184 | - return (UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC | 184 | + return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC + interfaceName |
185 | - + interfaceName + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE); | 185 | + + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE); |
186 | } | 186 | } |
187 | 187 | ||
188 | /** | 188 | /** | ... | ... |
... | @@ -18,6 +18,7 @@ package org.onosproject.yangutils.utils.io.impl; | ... | @@ -18,6 +18,7 @@ package org.onosproject.yangutils.utils.io.impl; |
18 | 18 | ||
19 | import java.io.BufferedInputStream; | 19 | import java.io.BufferedInputStream; |
20 | import java.io.BufferedOutputStream; | 20 | import java.io.BufferedOutputStream; |
21 | +import java.io.File; | ||
21 | import java.io.FileInputStream; | 22 | import java.io.FileInputStream; |
22 | import java.io.FileNotFoundException; | 23 | import java.io.FileNotFoundException; |
23 | import java.io.FileOutputStream; | 24 | import java.io.FileOutputStream; |
... | @@ -32,19 +33,19 @@ import java.util.ArrayList; | ... | @@ -32,19 +33,19 @@ import java.util.ArrayList; |
32 | import java.util.List; | 33 | import java.util.List; |
33 | 34 | ||
34 | /** | 35 | /** |
35 | - * Provides storage for serialized data while traversing data model tree for code generation. | 36 | + * Provides storage for Temp data while traversing data model tree for code generation. |
36 | */ | 37 | */ |
37 | -public final class SerializedDataStore { | 38 | +public final class TempDataStore { |
38 | 39 | ||
39 | /** | 40 | /** |
40 | * Data Store types. | 41 | * Data Store types. |
41 | */ | 42 | */ |
42 | - public static enum SerializedDataStoreType { | 43 | + public static enum TempDataStoreType { |
43 | 44 | ||
44 | /** | 45 | /** |
45 | * Methods. | 46 | * Methods. |
46 | */ | 47 | */ |
47 | - INTERFACE_METHODS, | 48 | + GETTER_METHODS, |
48 | 49 | ||
49 | /** | 50 | /** |
50 | * Methods. | 51 | * Methods. |
... | @@ -73,42 +74,42 @@ public final class SerializedDataStore { | ... | @@ -73,42 +74,42 @@ public final class SerializedDataStore { |
73 | } | 74 | } |
74 | 75 | ||
75 | /** | 76 | /** |
76 | - * File name string for serialized files of methods. | 77 | + * File name string for Temp files of methods. |
77 | */ | 78 | */ |
78 | - private static final String INTERFACE_METHOD_FILE_NAME = "SerializedInterfaceMethodDataStore"; | 79 | + private static final String GETTER_METHOD_FILE_NAME = "TempGetterMethodDataStore"; |
79 | 80 | ||
80 | /** | 81 | /** |
81 | - * File name string for serialized files of methods. | 82 | + * File name string for Temp files of methods. |
82 | */ | 83 | */ |
83 | - private static final String BUILDER_METHOD_FILE_NAME = "SerializedBuilderMethodDataStore"; | 84 | + private static final String BUILDER_METHOD_FILE_NAME = "TempBuilderMethodDataStore"; |
84 | 85 | ||
85 | /** | 86 | /** |
86 | - * File name string for serialized files of methods. | 87 | + * File name string for Temp files of methods. |
87 | */ | 88 | */ |
88 | - private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "SerializedBuilderInterfaceMethodDataStore"; | 89 | + private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "TempBuilderInterfaceMethodDataStore"; |
89 | 90 | ||
90 | /** | 91 | /** |
91 | - * File name string for serialized files of methods. | 92 | + * File name string for Temp files of methods. |
92 | */ | 93 | */ |
93 | - private static final String IMPL_METHOD_FILE_NAME = "SerializedImplMethodDataStore"; | 94 | + private static final String IMPL_METHOD_FILE_NAME = "TempImplMethodDataStore"; |
94 | 95 | ||
95 | /** | 96 | /** |
96 | - * File name string for serialized files of attributes. | 97 | + * File name string for Temp files of attributes. |
97 | */ | 98 | */ |
98 | - private static final String ATTRIBUTE_FILE_NAME = "SerializedAttributeDataStore"; | 99 | + private static final String ATTRIBUTE_FILE_NAME = "TempAttributeDataStore"; |
99 | 100 | ||
100 | /** | 101 | /** |
101 | - * File name string for serialized files of imports. | 102 | + * File name string for Temp files of imports. |
102 | */ | 103 | */ |
103 | - private static final String IMPORT_FILE_NAME = "SerializedImportDataStore"; | 104 | + private static final String IMPORT_FILE_NAME = "TempImportDataStore"; |
104 | 105 | ||
105 | /** | 106 | /** |
106 | - * File extension of serialized files. | 107 | + * File extension of Temp files. |
107 | */ | 108 | */ |
108 | - private static final String SERIALIZE_FILE_EXTENSION = ".ser"; | 109 | + private static final String FILE_EXTENSION = ".tmp"; |
109 | 110 | ||
110 | /** | 111 | /** |
111 | - * Directory for generating Serialized files. | 112 | + * Directory for generating Temp files. |
112 | */ | 113 | */ |
113 | private static final String GEN_DIR = "target/"; | 114 | private static final String GEN_DIR = "target/"; |
114 | 115 | ||
... | @@ -120,35 +121,38 @@ public final class SerializedDataStore { | ... | @@ -120,35 +121,38 @@ public final class SerializedDataStore { |
120 | /** | 121 | /** |
121 | * Default constructor. | 122 | * Default constructor. |
122 | */ | 123 | */ |
123 | - private SerializedDataStore() { | 124 | + private TempDataStore() { |
124 | } | 125 | } |
125 | 126 | ||
126 | /** | 127 | /** |
127 | - * Writes specific info to a serialized file. | 128 | + * Writes specific info to a Temp file. |
128 | * | 129 | * |
129 | * @param data data to be stored | 130 | * @param data data to be stored |
130 | - * @param type type of serialized data store | 131 | + * @param type type of Temp data store |
131 | - * @throws IOException when fails to create a serialized data file. | 132 | + * @param className class name |
133 | + * @throws IOException when fails to create a Temp data file. | ||
132 | */ | 134 | */ |
133 | - public static void setSerializeData(String data, SerializedDataStoreType type) throws IOException { | 135 | + public static void setTempData(String data, TempDataStoreType type, String className) throws IOException { |
134 | 136 | ||
135 | String fileName = ""; | 137 | String fileName = ""; |
136 | - if (type.equals(SerializedDataStoreType.ATTRIBUTE)) { | 138 | + if (type.equals(TempDataStoreType.ATTRIBUTE)) { |
137 | fileName = ATTRIBUTE_FILE_NAME; | 139 | fileName = ATTRIBUTE_FILE_NAME; |
138 | - } else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) { | 140 | + } else if (type.equals(TempDataStoreType.GETTER_METHODS)) { |
139 | - fileName = INTERFACE_METHOD_FILE_NAME; | 141 | + fileName = GETTER_METHOD_FILE_NAME; |
140 | - } else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) { | 142 | + } else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) { |
141 | fileName = BUILDER_INTERFACE_METHOD_FILE_NAME; | 143 | fileName = BUILDER_INTERFACE_METHOD_FILE_NAME; |
142 | - } else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) { | 144 | + } else if (type.equals(TempDataStoreType.BUILDER_METHODS)) { |
143 | fileName = BUILDER_METHOD_FILE_NAME; | 145 | fileName = BUILDER_METHOD_FILE_NAME; |
144 | - } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) { | 146 | + } else if (type.equals(TempDataStoreType.IMPL_METHODS)) { |
145 | fileName = IMPL_METHOD_FILE_NAME; | 147 | fileName = IMPL_METHOD_FILE_NAME; |
146 | } else { | 148 | } else { |
147 | fileName = IMPORT_FILE_NAME; | 149 | fileName = IMPORT_FILE_NAME; |
148 | } | 150 | } |
149 | 151 | ||
152 | + File dir = new File(GEN_DIR + className + File.separator); | ||
153 | + dir.mkdirs(); | ||
150 | try { | 154 | try { |
151 | - OutputStream file = new FileOutputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION); | 155 | + OutputStream file = new FileOutputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION); |
152 | OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE); | 156 | OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE); |
153 | 157 | ||
154 | ObjectOutput output = new ObjectOutputStream(buffer); | 158 | ObjectOutput output = new ObjectOutputStream(buffer); |
... | @@ -163,33 +167,34 @@ public final class SerializedDataStore { | ... | @@ -163,33 +167,34 @@ public final class SerializedDataStore { |
163 | } | 167 | } |
164 | 168 | ||
165 | /** | 169 | /** |
166 | - * Get the serialized data. | 170 | + * Get the Temp data. |
167 | * | 171 | * |
168 | - * @param type type of serialized data store | 172 | + * @param type type of Temp data store |
173 | + * @param className name of the class. | ||
169 | * @return list of attribute info. | 174 | * @return list of attribute info. |
170 | * @throws IOException when fails to read from the file. | 175 | * @throws IOException when fails to read from the file. |
171 | * @throws ClassNotFoundException when class is missing. | 176 | * @throws ClassNotFoundException when class is missing. |
172 | * @throws FileNotFoundException when file is missing. | 177 | * @throws FileNotFoundException when file is missing. |
173 | */ | 178 | */ |
174 | - public static List<String> getSerializeData(SerializedDataStoreType type) | 179 | + public static List<String> getTempData(TempDataStoreType type, String className) |
175 | throws IOException, FileNotFoundException, ClassNotFoundException { | 180 | throws IOException, FileNotFoundException, ClassNotFoundException { |
176 | 181 | ||
177 | String fileName = ""; | 182 | String fileName = ""; |
178 | - if (type.equals(SerializedDataStoreType.ATTRIBUTE)) { | 183 | + if (type.equals(TempDataStoreType.ATTRIBUTE)) { |
179 | fileName = ATTRIBUTE_FILE_NAME; | 184 | fileName = ATTRIBUTE_FILE_NAME; |
180 | - } else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) { | 185 | + } else if (type.equals(TempDataStoreType.GETTER_METHODS)) { |
181 | - fileName = INTERFACE_METHOD_FILE_NAME; | 186 | + fileName = GETTER_METHOD_FILE_NAME; |
182 | - } else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) { | 187 | + } else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) { |
183 | fileName = BUILDER_INTERFACE_METHOD_FILE_NAME; | 188 | fileName = BUILDER_INTERFACE_METHOD_FILE_NAME; |
184 | - } else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) { | 189 | + } else if (type.equals(TempDataStoreType.BUILDER_METHODS)) { |
185 | fileName = BUILDER_METHOD_FILE_NAME; | 190 | fileName = BUILDER_METHOD_FILE_NAME; |
186 | - } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) { | 191 | + } else if (type.equals(TempDataStoreType.IMPL_METHODS)) { |
187 | fileName = IMPL_METHOD_FILE_NAME; | 192 | fileName = IMPL_METHOD_FILE_NAME; |
188 | } else { | 193 | } else { |
189 | fileName = IMPORT_FILE_NAME; | 194 | fileName = IMPORT_FILE_NAME; |
190 | } | 195 | } |
191 | try { | 196 | try { |
192 | - InputStream file = new FileInputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION); | 197 | + InputStream file = new FileInputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION); |
193 | InputStream buffer = new BufferedInputStream(file); | 198 | InputStream buffer = new BufferedInputStream(file); |
194 | ObjectInput input = new ObjectInputStream(buffer); | 199 | ObjectInput input = new ObjectInputStream(buffer); |
195 | try { | 200 | try { |
... | @@ -204,7 +209,7 @@ public final class SerializedDataStore { | ... | @@ -204,7 +209,7 @@ public final class SerializedDataStore { |
204 | } catch (FileNotFoundException ex) { | 209 | } catch (FileNotFoundException ex) { |
205 | throw new FileNotFoundException("No such file or directory."); | 210 | throw new FileNotFoundException("No such file or directory."); |
206 | } catch (ClassNotFoundException ex) { | 211 | } catch (ClassNotFoundException ex) { |
207 | - throw new ClassNotFoundException("failed to fetch the serialized data file."); | 212 | + throw new ClassNotFoundException("failed to fetch the Temp data file."); |
208 | } | 213 | } |
209 | } | 214 | } |
210 | } | 215 | } | ... | ... |
... | @@ -33,7 +33,6 @@ public final class YangFileScanner { | ... | @@ -33,7 +33,6 @@ public final class YangFileScanner { |
33 | private YangFileScanner() { | 33 | private YangFileScanner() { |
34 | } | 34 | } |
35 | 35 | ||
36 | - | ||
37 | /** | 36 | /** |
38 | * Returns the list of java files. | 37 | * Returns the list of java files. |
39 | * | 38 | * | ... | ... |
... | @@ -69,10 +69,9 @@ public final class YangIoUtils { | ... | @@ -69,10 +69,9 @@ public final class YangIoUtils { |
69 | public static void addPackageInfo(File path, String classInfo, String pack) throws IOException { | 69 | public static void addPackageInfo(File path, String classInfo, String pack) throws IOException { |
70 | 70 | ||
71 | if (pack.contains(UtilConstants.YANG_GEN_DIR)) { | 71 | if (pack.contains(UtilConstants.YANG_GEN_DIR)) { |
72 | - String[] strArray = pack.split(UtilConstants.YANG_GEN_DIR + UtilConstants.SLASH); | 72 | + String[] strArray = pack.split(UtilConstants.YANG_GEN_DIR); |
73 | - pack = strArray[1]; | 73 | + pack = strArray[1]; |
74 | - } | 74 | + } |
75 | - | ||
76 | try { | 75 | try { |
77 | 76 | ||
78 | File packageInfo = new File(path + File.separator + "package-info.java"); | 77 | File packageInfo = new File(path + File.separator + "package-info.java"); | ... | ... |
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 | +} |
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.utils; | ||
18 | + | ||
19 | +import org.junit.Test; | ||
20 | +import org.onosproject.yangutils.datamodel.YangDataTypes; | ||
21 | +import org.onosproject.yangutils.datamodel.YangType; | ||
22 | +import org.onosproject.yangutils.translator.GeneratedFileType; | ||
23 | +import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes; | ||
24 | +import org.onosproject.yangutils.translator.tojava.ImportInfo; | ||
25 | +import org.onosproject.yangutils.utils.UtilConstants; | ||
26 | + | ||
27 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
28 | +import static org.hamcrest.core.Is.is; | ||
29 | + | ||
30 | +/** | ||
31 | + * Unit test cases for java code snippet generator. | ||
32 | + */ | ||
33 | +public class JavaCodeSnippetGenTest { | ||
34 | + | ||
35 | + private static final String PKG_INFO = "org.onosproject.unittest"; | ||
36 | + private static final String CLASS_INFO = "JavaCodeSnippetGenTest"; | ||
37 | + private static final GeneratedFileType FILE_GEN_TYPE = GeneratedFileType.INTERFACE; | ||
38 | + private static final GeneratedMethodTypes METHOD_GEN_TYPE = GeneratedMethodTypes.GETTER; | ||
39 | + private static final String YANG_NAME = "Test"; | ||
40 | + private static final String STRING = "String"; | ||
41 | + | ||
42 | + /** | ||
43 | + * Unit test case for import text. | ||
44 | + */ | ||
45 | + @Test | ||
46 | + public void testForImportText() { | ||
47 | + ImportInfo importInfo = new ImportInfo(); | ||
48 | + importInfo.setPkgInfo(PKG_INFO); | ||
49 | + importInfo.setClassInfo(CLASS_INFO); | ||
50 | + | ||
51 | + String imports = JavaCodeSnippetGen.getImportText(importInfo); | ||
52 | + | ||
53 | + assertThat(true, is(imports.equals(UtilConstants.IMPORT + PKG_INFO + UtilConstants.PERIOD + CLASS_INFO | ||
54 | + + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE))); | ||
55 | + } | ||
56 | + | ||
57 | + /** | ||
58 | + * Unit test case for java class definition start. | ||
59 | + */ | ||
60 | + @Test | ||
61 | + public void testForJavaClassDefStart() { | ||
62 | + String classDef = JavaCodeSnippetGen.getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME); | ||
63 | + assertThat(true, | ||
64 | + is(classDef.equals(UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE | ||
65 | + + UtilConstants.SPACE + YANG_NAME + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET | ||
66 | + + UtilConstants.NEW_LINE))); | ||
67 | + } | ||
68 | + | ||
69 | + /** | ||
70 | + * Unit test case for java attribute info. | ||
71 | + */ | ||
72 | + @SuppressWarnings("rawtypes") | ||
73 | + @Test | ||
74 | + public void testForJavaAttributeInfo() { | ||
75 | + | ||
76 | + String attributeWithType = JavaCodeSnippetGen.getJavaAttributeInfo(FILE_GEN_TYPE, YANG_NAME, getType()); | ||
77 | + assertThat(true, is(attributeWithType.equals(UtilConstants.PRIVATE + UtilConstants.SPACE | ||
78 | + + getType().getDataTypeName() + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN))); | ||
79 | + | ||
80 | + String attributeWithoutType = JavaCodeSnippetGen.getJavaAttributeInfo(FILE_GEN_TYPE, YANG_NAME, null); | ||
81 | + assertThat(true, | ||
82 | + is(attributeWithoutType.equals( | ||
83 | + UtilConstants.PRIVATE + UtilConstants.SPACE + JavaIdentifierSyntax.getCaptialCase(YANG_NAME) | ||
84 | + + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN))); | ||
85 | + | ||
86 | + } | ||
87 | + | ||
88 | + /** | ||
89 | + * Unit test case for list attribute. | ||
90 | + */ | ||
91 | + @Test | ||
92 | + public void testForListAttribute() { | ||
93 | + String listAttribute = JavaCodeSnippetGen.getListAttribute(STRING); | ||
94 | + assertThat(true, is(listAttribute.equals(UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + STRING | ||
95 | + + UtilConstants.DIAMOND_CLOSE_BRACKET))); | ||
96 | + } | ||
97 | + | ||
98 | + /** | ||
99 | + * Unit test case for java method info. | ||
100 | + */ | ||
101 | + @Test | ||
102 | + public void testForJavaMethodInfo() { | ||
103 | + | ||
104 | + String method = JavaCodeSnippetGen.getJavaMethodInfo(FILE_GEN_TYPE, YANG_NAME, METHOD_GEN_TYPE, getType()); | ||
105 | + assertThat(true, | ||
106 | + is(method.equals(UtilConstants.FOUR_SPACE_INDENTATION | ||
107 | + + JavaIdentifierSyntax.getCaptialCase(getType().getDataTypeName()) + UtilConstants.SPACE | ||
108 | + + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(YANG_NAME) | ||
109 | + + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS | ||
110 | + + UtilConstants.SEMI_COLAN))); | ||
111 | + } | ||
112 | + | ||
113 | + /** | ||
114 | + * Unit test case for java class definition close. | ||
115 | + */ | ||
116 | + @Test | ||
117 | + public void testForJavaClassDefClose() { | ||
118 | + String classDef = JavaCodeSnippetGen.getJavaClassDefClose(FILE_GEN_TYPE, YANG_NAME); | ||
119 | + assertThat(true, is(classDef.equals(UtilConstants.CLOSE_CURLY_BRACKET))); | ||
120 | + } | ||
121 | + | ||
122 | + /** | ||
123 | + * Returns YANG type. | ||
124 | + * | ||
125 | + * @return type | ||
126 | + */ | ||
127 | + @SuppressWarnings("rawtypes") | ||
128 | + private YangType<?> getType() { | ||
129 | + YangType<?> type = new YangType(); | ||
130 | + type.setDataTypeName(STRING); | ||
131 | + type.setDataType(YangDataTypes.STRING); | ||
132 | + return type; | ||
133 | + } | ||
134 | +} |
... | @@ -19,7 +19,7 @@ package org.onosproject.yangutils.utils.io.impl; | ... | @@ -19,7 +19,7 @@ package org.onosproject.yangutils.utils.io.impl; |
19 | import org.junit.Test; | 19 | import org.junit.Test; |
20 | import org.junit.Rule; | 20 | import org.junit.Rule; |
21 | import org.junit.rules.ExpectedException; | 21 | import org.junit.rules.ExpectedException; |
22 | -import org.onosproject.yangutils.utils.io.impl.SerializedDataStore.SerializedDataStoreType; | 22 | +import org.onosproject.yangutils.utils.io.impl.TempDataStore.TempDataStoreType; |
23 | 23 | ||
24 | import java.io.FileNotFoundException; | 24 | import java.io.FileNotFoundException; |
25 | import java.io.IOException; | 25 | import java.io.IOException; |
... | @@ -36,11 +36,12 @@ import static org.junit.Assert.assertThat; | ... | @@ -36,11 +36,12 @@ import static org.junit.Assert.assertThat; |
36 | import static org.junit.Assert.assertNotNull; | 36 | import static org.junit.Assert.assertNotNull; |
37 | 37 | ||
38 | /** | 38 | /** |
39 | - * Unit tests for the serialized data store for its contents. | 39 | + * Unit tests for the Tempd data store for its contents. |
40 | */ | 40 | */ |
41 | -public final class SerializedDataStoreTest { | 41 | +public final class TempDataStoreTest { |
42 | 42 | ||
43 | private final Logger log = getLogger(getClass()); | 43 | private final Logger log = getLogger(getClass()); |
44 | + private static final String CLASS_NAME = "YANG"; | ||
44 | 45 | ||
45 | @Rule | 46 | @Rule |
46 | public ExpectedException thrown = ExpectedException.none(); | 47 | public ExpectedException thrown = ExpectedException.none(); |
... | @@ -59,7 +60,7 @@ public final class SerializedDataStoreTest { | ... | @@ -59,7 +60,7 @@ public final class SerializedDataStoreTest { |
59 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, | 60 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
60 | InstantiationException, IllegalAccessException, InvocationTargetException { | 61 | InstantiationException, IllegalAccessException, InvocationTargetException { |
61 | 62 | ||
62 | - Class<?>[] classesToConstruct = {SerializedDataStore.class }; | 63 | + Class<?>[] classesToConstruct = {TempDataStore.class }; |
63 | for (Class<?> clazz : classesToConstruct) { | 64 | for (Class<?> clazz : classesToConstruct) { |
64 | Constructor<?> constructor = clazz.getDeclaredConstructor(); | 65 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
65 | constructor.setAccessible(true); | 66 | constructor.setAccessible(true); |
... | @@ -74,12 +75,12 @@ public final class SerializedDataStoreTest { | ... | @@ -74,12 +75,12 @@ public final class SerializedDataStoreTest { |
74 | public void insertAttributeDataTest() throws IOException, ClassNotFoundException, FileNotFoundException { | 75 | public void insertAttributeDataTest() throws IOException, ClassNotFoundException, FileNotFoundException { |
75 | 76 | ||
76 | String attributeData = "attribute content lists this"; | 77 | String attributeData = "attribute content lists this"; |
77 | - SerializedDataStore.setSerializeData(attributeData, SerializedDataStoreType.ATTRIBUTE); | 78 | + TempDataStore.setTempData(attributeData, TempDataStoreType.ATTRIBUTE, CLASS_NAME); |
78 | - List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.ATTRIBUTE); | 79 | + List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, CLASS_NAME); |
79 | List<String> expectedinfo = new LinkedList<>(); | 80 | List<String> expectedinfo = new LinkedList<>(); |
80 | expectedinfo.add(attributeData); | 81 | expectedinfo.add(attributeData); |
81 | assertThat(true, is(attributeInfo.equals(expectedinfo))); | 82 | assertThat(true, is(attributeInfo.equals(expectedinfo))); |
82 | - SerializedDataStoreType.valueOf(SerializedDataStoreType.ATTRIBUTE.toString()); | 83 | + TempDataStoreType.valueOf(TempDataStoreType.ATTRIBUTE.toString()); |
83 | } | 84 | } |
84 | 85 | ||
85 | /** | 86 | /** |
... | @@ -89,10 +90,8 @@ public final class SerializedDataStoreTest { | ... | @@ -89,10 +90,8 @@ public final class SerializedDataStoreTest { |
89 | public void insertBuilderInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException { | 90 | public void insertBuilderInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException { |
90 | 91 | ||
91 | String builderInterfaceMethodsData = "builder interface methods content lists this"; | 92 | String builderInterfaceMethodsData = "builder interface methods content lists this"; |
92 | - SerializedDataStore.setSerializeData(builderInterfaceMethodsData, | 93 | + TempDataStore.setTempData(builderInterfaceMethodsData, TempDataStoreType.BUILDER_INTERFACE_METHODS, CLASS_NAME); |
93 | - SerializedDataStoreType.BUILDER_INTERFACE_METHODS); | 94 | + List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.BUILDER_INTERFACE_METHODS, CLASS_NAME); |
94 | - List<String> attributeInfo = SerializedDataStore | ||
95 | - .getSerializeData(SerializedDataStoreType.BUILDER_INTERFACE_METHODS); | ||
96 | List<String> expectedinfo = new LinkedList<>(); | 95 | List<String> expectedinfo = new LinkedList<>(); |
97 | expectedinfo.add(builderInterfaceMethodsData); | 96 | expectedinfo.add(builderInterfaceMethodsData); |
98 | assertThat(true, is(attributeInfo.equals(expectedinfo))); | 97 | assertThat(true, is(attributeInfo.equals(expectedinfo))); |
... | @@ -105,8 +104,8 @@ public final class SerializedDataStoreTest { | ... | @@ -105,8 +104,8 @@ public final class SerializedDataStoreTest { |
105 | public void insertBuilderMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException { | 104 | public void insertBuilderMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException { |
106 | 105 | ||
107 | String builderMethodsData = "builder methods content lists this"; | 106 | String builderMethodsData = "builder methods content lists this"; |
108 | - SerializedDataStore.setSerializeData(builderMethodsData, SerializedDataStoreType.BUILDER_METHODS); | 107 | + TempDataStore.setTempData(builderMethodsData, TempDataStoreType.BUILDER_METHODS, CLASS_NAME); |
109 | - List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.BUILDER_METHODS); | 108 | + List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.BUILDER_METHODS, CLASS_NAME); |
110 | List<String> expectedinfo = new LinkedList<>(); | 109 | List<String> expectedinfo = new LinkedList<>(); |
111 | expectedinfo.add(builderMethodsData); | 110 | expectedinfo.add(builderMethodsData); |
112 | assertThat(true, is(attributeInfo.equals(expectedinfo))); | 111 | assertThat(true, is(attributeInfo.equals(expectedinfo))); |
... | @@ -119,8 +118,8 @@ public final class SerializedDataStoreTest { | ... | @@ -119,8 +118,8 @@ public final class SerializedDataStoreTest { |
119 | public void insertImplMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException { | 118 | public void insertImplMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException { |
120 | 119 | ||
121 | String implMethodsData = "impl methods content lists this"; | 120 | String implMethodsData = "impl methods content lists this"; |
122 | - SerializedDataStore.setSerializeData(implMethodsData, SerializedDataStoreType.IMPL_METHODS); | 121 | + TempDataStore.setTempData(implMethodsData, TempDataStoreType.IMPL_METHODS, CLASS_NAME); |
123 | - List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.IMPL_METHODS); | 122 | + List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.IMPL_METHODS, CLASS_NAME); |
124 | List<String> expectedinfo = new LinkedList<>(); | 123 | List<String> expectedinfo = new LinkedList<>(); |
125 | expectedinfo.add(implMethodsData); | 124 | expectedinfo.add(implMethodsData); |
126 | assertThat(true, is(attributeInfo.equals(expectedinfo))); | 125 | assertThat(true, is(attributeInfo.equals(expectedinfo))); |
... | @@ -133,8 +132,8 @@ public final class SerializedDataStoreTest { | ... | @@ -133,8 +132,8 @@ public final class SerializedDataStoreTest { |
133 | public void insertImportTest() throws IOException, ClassNotFoundException, FileNotFoundException { | 132 | public void insertImportTest() throws IOException, ClassNotFoundException, FileNotFoundException { |
134 | 133 | ||
135 | String importData = "interface methods content lists this"; | 134 | String importData = "interface methods content lists this"; |
136 | - SerializedDataStore.setSerializeData(importData, SerializedDataStoreType.IMPORT); | 135 | + TempDataStore.setTempData(importData, TempDataStoreType.IMPORT, CLASS_NAME); |
137 | - List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.IMPORT); | 136 | + List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.IMPORT, CLASS_NAME); |
138 | List<String> expectedinfo = new LinkedList<>(); | 137 | List<String> expectedinfo = new LinkedList<>(); |
139 | expectedinfo.add(importData); | 138 | expectedinfo.add(importData); |
140 | assertThat(true, is(attributeInfo.equals(expectedinfo))); | 139 | assertThat(true, is(attributeInfo.equals(expectedinfo))); |
... | @@ -147,8 +146,8 @@ public final class SerializedDataStoreTest { | ... | @@ -147,8 +146,8 @@ public final class SerializedDataStoreTest { |
147 | public void insertInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException { | 146 | public void insertInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException { |
148 | 147 | ||
149 | String interfaceMethodsData = "interface methods content lists this"; | 148 | String interfaceMethodsData = "interface methods content lists this"; |
150 | - SerializedDataStore.setSerializeData(interfaceMethodsData, SerializedDataStoreType.INTERFACE_METHODS); | 149 | + TempDataStore.setTempData(interfaceMethodsData, TempDataStoreType.GETTER_METHODS, CLASS_NAME); |
151 | - List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.INTERFACE_METHODS); | 150 | + List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.GETTER_METHODS, CLASS_NAME); |
152 | List<String> expectedinfo = new LinkedList<>(); | 151 | List<String> expectedinfo = new LinkedList<>(); |
153 | expectedinfo.add(interfaceMethodsData); | 152 | expectedinfo.add(interfaceMethodsData); |
154 | assertThat(true, is(attributeInfo.equals(expectedinfo))); | 153 | assertThat(true, is(attributeInfo.equals(expectedinfo))); | ... | ... |
-
Please register or login to post a comment