diff --git a/pom.xml b/pom.xml index e02bdec..f12e716 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ com.github.odiszapc nginxparser - 0.9.6 + 0.9.6-1 jar ${project.groupId}:${project.artifactId} @@ -73,7 +73,22 @@ antlr4-runtime 4.5.3 - + + commons-io + commons-io + 2.5 + + + org.apache.commons + commons-lang3 + 3.5 + + + com.google.guava + guava + 20.0 + + junit junit diff --git a/src/main/java/com/github/odiszapc/common/formatter/EntryFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/EntryFormatter.java new file mode 100644 index 0000000..53dc1a2 --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/EntryFormatter.java @@ -0,0 +1,21 @@ +package com.github.odiszapc.common.formatter; + +import com.github.odiszapc.nginxparser.NgxEntry; +import com.github.odiszapc.nginxparser.NgxEntryType; + +/** + * + *

+ * formatter for NgxEntry + *

+ * @author ${Author} + * @version ${Version} + * @since 8.1.0 + * + */ +public interface EntryFormatter{ + + StringBuffer formattedText(NgxEntry entry); + + NgxEntryType[] supportedEntryTypes(); +} diff --git a/src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java b/src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java new file mode 100644 index 0000000..c9bd265 --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java @@ -0,0 +1,94 @@ +package com.github.odiszapc.common.formatter; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.github.odiszapc.common.formatter.impl.DefaultEntryFormatter; +import com.github.odiszapc.common.formatter.impl.NgxBlockFormatter; +import com.github.odiszapc.common.formatter.impl.NgxConfigFormatter; +import com.github.odiszapc.nginxparser.NgxEntry; +import com.github.odiszapc.nginxparser.NgxEntryType; + +public class FormatterRepository { + + private static final EntryFormatter NULL_INDENT_SETTABLE_FORMATTER = new NullEntryFormatter(); + private Map innerFormatters; + private Map indentSettableFormatters; + + public FormatterRepository() { + initDefalutFormatters(); + } + + private void initDefalutFormatters() { + List entryFormatters = new ArrayList(); + entryFormatters.add(new DefaultEntryFormatter(this)); + entryFormatters.add(new NgxBlockFormatter(this)); + entryFormatters.add(new NgxConfigFormatter(this)); + setFormatters(entryFormatters); + } + + public void setFormatters(Collection formatters) { + indentSettableFormatters = new HashMap(); + innerFormatters = new HashMap(); + for (EntryFormatter formatter : formatters) { + for (NgxEntryType entryType : formatter.supportedEntryTypes()) { + putFormatter(entryType, formatter); + } + } + } + + public void putFormatter(NgxEntryType entryType, EntryFormatter formatter) { + innerFormatters.put(entryType, formatter); + if (formatter instanceof IndentSettableEntryFormatter) { + indentSettableFormatters.put(entryType, ((IndentSettableEntryFormatter) formatter).copy()); + } + } + + public EntryFormatter getFormatter(Class formatterClazz) { + NgxEntryType entryType = NgxEntryType.fromClass(formatterClazz); + return getFormatter(entryType); + } + + public EntryFormatter getFormatter(NgxEntryType entryType) { + if (entryType != null) { + EntryFormatter formatter = innerFormatters.get(entryType); + if (formatter != null) { + return formatter; + } + } + return NULL_INDENT_SETTABLE_FORMATTER; + } + + public IndentSettableEntryFormatter getIndentSettableEntryFormatter(Class formatterClazz) { + NgxEntryType entryType = NgxEntryType.fromClass(formatterClazz); + return getIndentSettableEntryFormatter(entryType); + } + + public IndentSettableEntryFormatter getIndentSettableEntryFormatter(NgxEntryType entryType) { + if (entryType != null) { + IndentSettableEntryFormatter formatter = indentSettableFormatters.get(entryType); + if (formatter != null) { + return formatter.copy(); + } + } + return null; + } + + private static class NullEntryFormatter implements EntryFormatter { + + @Override + public StringBuffer formattedText(NgxEntry entry) { + return new StringBuffer(); + } + + @Override + public NgxEntryType[] supportedEntryTypes() { + return new NgxEntryType[] {}; + } + + } + +} diff --git a/src/main/java/com/github/odiszapc/common/formatter/IndentSettableEntryFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/IndentSettableEntryFormatter.java new file mode 100644 index 0000000..8369002 --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/IndentSettableEntryFormatter.java @@ -0,0 +1,55 @@ +package com.github.odiszapc.common.formatter; + +import com.github.odiszapc.nginxparser.NgxEntry; + +public abstract class IndentSettableEntryFormatter implements EntryFormatter { + + public static final int DEFAULT_INDENT = 4; + private int indent = DEFAULT_INDENT; + private FormatterRepository repository; + + public IndentSettableEntryFormatter(IndentSettableEntryFormatter formatter) { + this(formatter.repository); + this.indent = formatter.indent; + } + + public IndentSettableEntryFormatter(FormatterRepository repository) { + this.repository = repository; + } + + public void setIndent(int indent) { + this.indent = indent; + } + + public int getIndent() { + return indent; + } + + protected EntryFormatter getFormatter(Class formatterClazz) { + IndentSettableEntryFormatter insFormatter = repository.getIndentSettableEntryFormatter(formatterClazz); + if (insFormatter != null) { + insFormatter.setIndent(indent + insFormatter.indent); + return insFormatter; + } + return repository.getFormatter(formatterClazz); + } + + public StringBuffer indentText() { + StringBuffer indentBuf = new StringBuffer(); + for (int i = 0; i < indent; i++) { + indentBuf.append(' '); + } + return indentBuf; + } + + @Override + public StringBuffer formattedText(NgxEntry entry) { + StringBuffer buf = new StringBuffer(); + buf.append(indentText()); + buf.append(entry.toString()); + buf.append('\n'); + return buf; + } + + public abstract IndentSettableEntryFormatter copy(); +} diff --git a/src/main/java/com/github/odiszapc/common/formatter/impl/DefaultEntryFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/impl/DefaultEntryFormatter.java new file mode 100644 index 0000000..7f56cc7 --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/impl/DefaultEntryFormatter.java @@ -0,0 +1,27 @@ +package com.github.odiszapc.common.formatter.impl; + +import com.github.odiszapc.common.formatter.FormatterRepository; +import com.github.odiszapc.common.formatter.IndentSettableEntryFormatter; +import com.github.odiszapc.nginxparser.NgxEntryType; + +public class DefaultEntryFormatter extends IndentSettableEntryFormatter { + + public DefaultEntryFormatter(DefaultEntryFormatter defaultEntryFormatter) { + super(defaultEntryFormatter); + } + + public DefaultEntryFormatter(FormatterRepository repository) { + super(repository); + } + + @Override + public NgxEntryType[] supportedEntryTypes() { + return new NgxEntryType[] { NgxEntryType.PARAM, NgxEntryType.COMMENT }; + } + + @Override + public IndentSettableEntryFormatter copy() { + return new DefaultEntryFormatter(this); + } + +} diff --git a/src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java new file mode 100644 index 0000000..9b61b44 --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java @@ -0,0 +1,50 @@ +package com.github.odiszapc.common.formatter.impl; + +import com.github.odiszapc.common.formatter.FormatterRepository; +import com.github.odiszapc.common.formatter.IndentSettableEntryFormatter; +import com.github.odiszapc.nginxparser.NgxBlock; +import com.github.odiszapc.nginxparser.NgxEntry; +import com.github.odiszapc.nginxparser.NgxEntryType; + +public class NgxBlockFormatter extends IndentSettableEntryFormatter { + + public NgxBlockFormatter(NgxBlockFormatter ngxBlockFormatter) { + super(ngxBlockFormatter); + } + + public NgxBlockFormatter(FormatterRepository repository) { + super(repository); + } + + @Override + public StringBuffer formattedText(NgxEntry entry) { + StringBuffer buf = super.formattedText(entry); + buf.append(formattedBlockBody(entry)); + buf.append("}\n"); + return buf; + } + + protected StringBuffer formattedBlockBody(NgxEntry entry) { + StringBuffer buf = new StringBuffer(); + if (!(entry instanceof NgxBlock)) { + return buf; + } + NgxBlock block = (NgxBlock) entry; + for (NgxEntry e : block.getEntries()) { + buf.append(getFormatter(e.getClass()).formattedText(e)); + } + buf.append(indentText()); + return buf; + } + + @Override + public NgxEntryType[] supportedEntryTypes() { + return new NgxEntryType[] { NgxEntryType.BLOCK }; + } + + @Override + public IndentSettableEntryFormatter copy() { + return new NgxBlockFormatter(this); + } + +} diff --git a/src/main/java/com/github/odiszapc/common/formatter/impl/NgxConfigFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/impl/NgxConfigFormatter.java new file mode 100644 index 0000000..bd0b54c --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/impl/NgxConfigFormatter.java @@ -0,0 +1,40 @@ +package com.github.odiszapc.common.formatter.impl; + +import com.github.odiszapc.common.formatter.EntryFormatter; +import com.github.odiszapc.common.formatter.FormatterRepository; +import com.github.odiszapc.common.formatter.IndentSettableEntryFormatter; +import com.github.odiszapc.nginxparser.NgxEntry; +import com.github.odiszapc.nginxparser.NgxEntryType; + +//base config do not contains indent +public class NgxConfigFormatter extends NgxBlockFormatter { + + public NgxConfigFormatter(FormatterRepository repository) { + super(repository); + } + + @Override + public int getIndent() { + return 0; + } + + @Override + public StringBuffer formattedText(NgxEntry entry) { + return formattedBlockBody(entry); + } + + @Override + protected EntryFormatter getFormatter(Class formatterClazz) { + EntryFormatter formatter = super.getFormatter(formatterClazz); + if (formatter instanceof IndentSettableEntryFormatter) { + ((IndentSettableEntryFormatter) formatter).setIndent(0); + } + return formatter; + } + + @Override + public NgxEntryType[] supportedEntryTypes() { + return new NgxEntryType[] { NgxEntryType.CONFIG }; + } + +} diff --git a/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java b/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java new file mode 100644 index 0000000..e996fe2 --- /dev/null +++ b/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java @@ -0,0 +1,112 @@ +package com.github.odiszapc.nginxparser; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; + +import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.StringUtils; + +import com.github.odiszapc.nginxparser.annotation.NgxTokenValue; +import com.github.odiszapc.nginxparser.annotation.NgxType; +import com.google.common.base.Defaults; + +public class DefaultNgxEntrySerializer implements NgxEntrySerializer { + + private static final String UPPER_CHAR_REGEX = "(?=\\p{Upper})"; + + @Override + public NgxEntry serialize(String fieldName, Object obj) { + if (obj == null) { + throw new IllegalArgumentException("obj should not be null"); + } + if (obj instanceof NgxEntry) { + return (NgxEntry) obj; + } + Class clazz = obj.getClass(); + String realFiledName = getRealFieldName(fieldName,obj); + + // too complicated + if (isPrimaryClass(clazz)) { + NgxParam param = new NgxParam(); + param.addValue(generateToken(realFiledName)); + param.addValue(generateToken(obj.toString())); + return param; + } else { + NgxBlock block = new NgxBlock(); + block.addValue(generateToken(realFiledName)); + for (Field field : clazz.getFields()) { + NgxEntry entry; + try { + Class fieldClazz = field.getType(); + Object fieldValue= field.get(obj); + if(isDefaultValue(fieldValue,fieldClazz)) { + continue; + } + Annotation tokenAnnotation = field.getAnnotation(NgxTokenValue.class); + if (tokenAnnotation != null) { + block.addValue(fieldValue.toString()); + continue; + } + entry = serialize(field.getName(), fieldValue); + if (entry != null) { + block.addEntry(entry); + } + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + return block; + } + } + + private static String getRealFieldName(String fieldName,Object obj){ + Class clazz = obj.getClass(); + NgxType ngxType = clazz.getAnnotation(NgxType.class); + if (ngxType != null) { + return ngxType.name(); + } else if(StringUtils.isNotEmpty(fieldName)){ + return fieldName; + }else{ + return getDefaultFieldName(obj); + } + } + + private static String getDefaultFieldName(Object obj){ + Class clazz = obj.getClass(); + return clazz.getSimpleName(); + } + + private static boolean isPrimaryClass(Class clazz) { + if (CharSequence.class.isAssignableFrom(clazz)) { + return true; + } + if (ClassUtils.isPrimitiveWrapper(clazz)) { + return true; + } + return false; + } + + private static boolean isDefaultValue(Object value, Class clazz) { + if (value == null) { + return true; + } + Object defalutValue = Defaults.defaultValue(clazz); + return value.equals(defalutValue); + } + + public static String generateToken(String token) { + String[] parts = token.split(UPPER_CHAR_REGEX); + StringBuffer buf = new StringBuffer(); + for (String part : parts) { + buf.append(part.toLowerCase()); + buf.append('_'); + } + if (buf.length() > 0) { + buf.deleteCharAt(buf.length() - 1); + } + return buf.toString(); + } + +} diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxAbstractEntry.java b/src/main/java/com/github/odiszapc/nginxparser/NgxAbstractEntry.java index 968c1ae..a833fe0 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxAbstractEntry.java +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxAbstractEntry.java @@ -49,7 +49,11 @@ public String toString() { builder.append(value).append(" "); } String s = builder.toString(); - return s.substring(0, s.length()-1); + if (s.length() == 0) { + return s; + } else { + return s.substring(0, s.length() - 1); + } } public String getName() { @@ -66,7 +70,7 @@ public List getValues() { Iterator it = getTokens().iterator(); it.next(); - while(it.hasNext()) { + while (it.hasNext()) { values.add(it.next().toString()); } return values; @@ -78,7 +82,7 @@ public String getValue() { while (iterator.hasNext()) { builder.append(iterator.next()); if (iterator.hasNext()) { - builder.append(' '); + builder.append(' '); } } return builder.toString(); diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxBlock.java b/src/main/java/com/github/odiszapc/nginxparser/NgxBlock.java index 6a5fc07..8dfe478 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxBlock.java +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxBlock.java @@ -40,7 +40,6 @@ public String toString() { return super.toString() + " {"; } - @Override public Iterator iterator() { return getEntries().iterator(); @@ -51,21 +50,21 @@ public void remove(NgxEntry itemToRemove) { throw new NullPointerException("Item can not be null"); Iterator it = entries.iterator(); - while(it.hasNext()) { + while (it.hasNext()) { NgxEntry entry = it.next(); switch (NgxEntryType.fromClass(entry.getClass())) { - case PARAM: - if (entry.equals(itemToRemove)) - it.remove(); - break; - case BLOCK: - if (entry.equals(itemToRemove)) - it.remove(); - else { - NgxBlock block = (NgxBlock) entry; - block.remove(itemToRemove); - } - break; + case PARAM: + if (entry.equals(itemToRemove)) + it.remove(); + break; + case BLOCK: + if (entry.equals(itemToRemove)) + it.remove(); + else { + NgxBlock block = (NgxBlock) entry; + block.remove(itemToRemove); + } + break; } } } @@ -116,25 +115,25 @@ public List findAll(Class clazz, List 0) { + if (block.getName().equals(head)) { + res.addAll(block.findAll(clazz, result, tail)); } - break; - - case BLOCK: - NgxBlock block = (NgxBlock) entry; - if (tail.length > 0) { - if (block.getName().equals(head)) { - res.addAll(block.findAll(clazz, result, tail)); - } - } else { - if (block.getName().equals(head) && (clazz.equals(NgxBlock.class))) { - res.add(block); - } + } else { + if (block.getName().equals(head) && (clazz.equals(NgxBlock.class))) { + res.add(block); } - break; + } + break; } } diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxConfig.java b/src/main/java/com/github/odiszapc/nginxparser/NgxConfig.java index d70e378..26498d1 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxConfig.java +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxConfig.java @@ -16,6 +16,9 @@ package com.github.odiszapc.nginxparser; +import com.github.odiszapc.common.formatter.EntryFormatter; +import com.github.odiszapc.common.formatter.FormatterRepository; +import com.github.odiszapc.common.formatter.IndentSettableEntryFormatter; import com.github.odiszapc.nginxparser.antlr.NginxLexer; import com.github.odiszapc.nginxparser.antlr.NginxListenerImpl; import com.github.odiszapc.nginxparser.antlr.NginxParser; @@ -25,10 +28,13 @@ import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTreeWalker; +import org.apache.commons.io.FileUtils; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Collection; /** @@ -41,6 +47,8 @@ public class NgxConfig extends NgxBlock { public static final Class BLOCK = NgxBlock.class; public static final Class IF = NgxIfBlock.class; + private FormatterRepository formatterRespository = new FormatterRepository(); + /** * Parse an existing config * @@ -69,7 +77,6 @@ public static NgxConfig readJavaCC(InputStream input) throws IOException, ParseE return parser.parse(); } - public static NgxConfig readAntlr(InputStream in) throws IOException { ANTLRInputStream input = new ANTLRInputStream(in); NginxLexer lexer = new NginxLexer(input); @@ -84,6 +91,10 @@ public static NgxConfig readAntlr(InputStream in) throws IOException { return listener.getResult(); } + public void setFormatterRepository(FormatterRepository formatterRespository) { + this.formatterRespository = formatterRespository; + } + @Override public Collection getTokens() { throw new IllegalStateException("Not implemented"); @@ -94,8 +105,17 @@ public void addValue(NgxToken token) { throw new IllegalStateException("Not implemented"); } + @Override public String toString() { - return "Nginx Config (" + getEntries().size() + " entries)"; + return "#Nginx Config (" + getEntries().size() + " entries)"; } + public void writeTo(File file) throws IOException { + EntryFormatter formatter = formatterRespository.getFormatter(NgxConfig.class); + if (formatter instanceof IndentSettableEntryFormatter) { + ((IndentSettableEntryFormatter) formatter).setIndent(0); + } + StringBuffer buffer = formatter.formattedText(this); + FileUtils.write(file, buffer, StandardCharsets.UTF_8); + } } diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxEntrySerializer.java b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrySerializer.java new file mode 100644 index 0000000..a8722fb --- /dev/null +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrySerializer.java @@ -0,0 +1,6 @@ +package com.github.odiszapc.nginxparser; + +public interface NgxEntrySerializer { + + NgxEntry serialize(String fieldName, Object obj); +} diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxEntryType.java b/src/main/java/com/github/odiszapc/nginxparser/NgxEntryType.java index e62f6fb..b3bff86 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxEntryType.java +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxEntryType.java @@ -23,7 +23,8 @@ public enum NgxEntryType { PARAM(NgxParam.class), COMMENT(NgxComment.class), IF(NgxIfBlock.class), - BLOCK(NgxBlock.class); + BLOCK(NgxBlock.class), + CONFIG(NgxConfig.class); private final Class clazz; diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java new file mode 100644 index 0000000..2959718 --- /dev/null +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java @@ -0,0 +1,32 @@ +package com.github.odiszapc.nginxparser; + + +/** + * + *

+ * common util for NgxEntry + *

+ * @author ${Author} + * @version ${Version} + * + */ +public class NgxEntrys { + + private static final NgxEntrySerializer serializer = new DefaultNgxEntrySerializer(); + + /** + * + *

+ * parse a object to NgxEntry + *

+ * @param obj + * @return + */ + public static NgxEntry parse(Object obj) { + if (obj == null) { + throw new IllegalArgumentException("obj should not be null"); + } + return serializer.serialize(null, obj); + } + +} diff --git a/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxTokenValue.java b/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxTokenValue.java new file mode 100644 index 0000000..366b06f --- /dev/null +++ b/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxTokenValue.java @@ -0,0 +1,13 @@ +package com.github.odiszapc.nginxparser.annotation; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface NgxTokenValue { + +} diff --git a/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java b/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java new file mode 100644 index 0000000..2907726 --- /dev/null +++ b/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java @@ -0,0 +1,12 @@ +package com.github.odiszapc.nginxparser.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +public @interface NgxType { + String name() default "name"; +} diff --git a/src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java b/src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java new file mode 100644 index 0000000..65ce4ca --- /dev/null +++ b/src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java @@ -0,0 +1,36 @@ +package com.github.odiszapc.nginxparser; + +import java.util.Collection; + +import org.junit.Assert; +import org.junit.Test; + +import com.github.odiszapc.nginxparser.annotation.NgxTokenValue; + +public class NgxEntrysTest { + + @Test + public void testParse() { + Server testObj = new Server(); + testObj.realm = "*"; + testObj.listen = "80"; + testObj.serverName = "localhost"; + NgxEntry result = NgxEntrys.parse(testObj); + TestUtils.assertBlock(result, "server", testObj.realm); + NgxBlock resultBlock = (NgxBlock) result; + Collection resultEntrys = resultBlock.getEntries(); + Assert.assertEquals(resultEntrys.size(), 2); + for (NgxEntry entry : resultEntrys) { + Assert.assertTrue(entry instanceof NgxParam); + } + } + + static class Server { + + @NgxTokenValue + public String realm; + + public String listen; + public String serverName; + } +}