Search in sources :

Example 41 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class PatternRuleTest method testRegex.

@Test
public void testRegex() {
    when(config.isIncludeJsr303Annotations()).thenReturn(true);
    final String patternValue = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$";
    when(node.asText()).thenReturn(patternValue);
    when(fieldVar.annotate(patternClass)).thenReturn(annotation);
    when(fieldVar.type().boxify().fullName()).thenReturn(fieldClass.getTypeName());
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, times(isApplicable ? 1 : 0)).annotate(patternClass);
    verify(annotation, times(isApplicable ? 1 : 0)).param("regexp", patternValue);
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) Test(org.junit.Test)

Example 42 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class MinLengthMaxLengthRuleTest method testMinLength.

@Test
public void testMinLength() {
    when(config.isIncludeJsr303Annotations()).thenReturn(true);
    final int minValue = new Random().nextInt();
    when(subNode.asInt()).thenReturn(minValue);
    when(node.get("minLength")).thenReturn(subNode);
    when(fieldVar.annotate(sizeClass)).thenReturn(annotation);
    when(node.has("minLength")).thenReturn(true);
    when(fieldVar.type().boxify().fullName()).thenReturn(fieldClass.getTypeName());
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, times(isApplicable ? 1 : 0)).annotate(sizeClass);
    verify(annotation, times(isApplicable ? 1 : 0)).param("min", minValue);
    verify(annotation, never()).param(eq("max"), anyString());
}
Also used : Random(java.util.Random) JFieldVar(com.sun.codemodel.JFieldVar) Test(org.junit.Test)

Example 43 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class MinLengthMaxLengthRuleTest method testMaxLength.

@Test
public void testMaxLength() {
    when(config.isIncludeJsr303Annotations()).thenReturn(true);
    final int maxValue = new Random().nextInt();
    when(subNode.asInt()).thenReturn(maxValue);
    when(node.get("maxLength")).thenReturn(subNode);
    when(fieldVar.annotate(sizeClass)).thenReturn(annotation);
    when(node.has("maxLength")).thenReturn(true);
    when(fieldVar.type().boxify().fullName()).thenReturn(fieldClass.getTypeName());
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, times(isApplicable ? 1 : 0)).annotate(sizeClass);
    verify(annotation, times(isApplicable ? 1 : 0)).param("max", maxValue);
    verify(annotation, never()).param(eq("min"), anyInt());
}
Also used : Random(java.util.Random) JFieldVar(com.sun.codemodel.JFieldVar) Test(org.junit.Test)

Example 44 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class MinLengthMaxLengthRuleTest method testMaxAndMinLength.

@Test
public void testMaxAndMinLength() {
    when(config.isIncludeJsr303Annotations()).thenReturn(true);
    final int minValue = new Random().nextInt();
    final int maxValue = new Random().nextInt();
    JsonNode maxSubNode = Mockito.mock(JsonNode.class);
    when(subNode.asInt()).thenReturn(minValue);
    when(maxSubNode.asInt()).thenReturn(maxValue);
    when(node.get("minLength")).thenReturn(subNode);
    when(node.get("maxLength")).thenReturn(maxSubNode);
    when(fieldVar.annotate(sizeClass)).thenReturn(annotation);
    when(node.has("minLength")).thenReturn(true);
    when(node.has("maxLength")).thenReturn(true);
    when(fieldVar.type().boxify().fullName()).thenReturn(fieldClass.getTypeName());
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, times(isApplicable ? 1 : 0)).annotate(sizeClass);
    verify(annotation, times(isApplicable ? 1 : 0)).param("min", minValue);
    verify(annotation, times(isApplicable ? 1 : 0)).param("max", maxValue);
}
Also used : Random(java.util.Random) JFieldVar(com.sun.codemodel.JFieldVar) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 45 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class EnumRule method addQuickLookupMap.

protected JFieldVar addQuickLookupMap(EnumDefinition enumDefinition, JDefinedClass _enum) {
    JType backingType = enumDefinition.getBackingType();
    JClass lookupType = _enum.owner().ref(Map.class).narrow(backingType.boxify(), _enum);
    JFieldVar lookupMap = _enum.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, lookupType, "CONSTANTS");
    JClass lookupImplType = _enum.owner().ref(HashMap.class).narrow(backingType.boxify(), _enum);
    lookupMap.init(JExpr._new(lookupImplType));
    JForEach forEach = _enum.init().forEach(_enum, "c", JExpr.invoke("values"));
    JInvocation put = forEach.body().invoke(lookupMap, "put");
    put.arg(forEach.var().ref("value"));
    put.arg(forEach.var());
    return lookupMap;
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) HashMap(java.util.HashMap) JClass(com.sun.codemodel.JClass) JInvocation(com.sun.codemodel.JInvocation) JForEach(com.sun.codemodel.JForEach) HashMap(java.util.HashMap) Map(java.util.Map) JType(com.sun.codemodel.JType)

Aggregations

JFieldVar (com.sun.codemodel.JFieldVar)74 JMethod (com.sun.codemodel.JMethod)33 JVar (com.sun.codemodel.JVar)28 JClass (com.sun.codemodel.JClass)24 Test (org.junit.Test)24 JBlock (com.sun.codemodel.JBlock)21 JInvocation (com.sun.codemodel.JInvocation)19 JDefinedClass (com.sun.codemodel.JDefinedClass)16 Random (java.util.Random)12 ByteString (com.linkedin.data.ByteString)10 HashMap (java.util.HashMap)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 JExpression (com.sun.codemodel.JExpression)9 JConditional (com.sun.codemodel.JConditional)8 JType (com.sun.codemodel.JType)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)6 JFieldRef (com.sun.codemodel.JFieldRef)5 JCodeModel (com.sun.codemodel.JCodeModel)3 JPackage (com.sun.codemodel.JPackage)3