001: /*
002: * @(#)AnnotationType.java
003: *
004: * Copyright 1999-2004 by Taleo Corporation,
005: * 330 St-Vallier East, Suite 400, Quebec city, Quebec, G1K 9C5, CANADA
006: * All rights reserved
007: */
008: package org.testng.internal;
009:
010: import java.io.Serializable;
011:
012: import org.testng.internal.version.VersionInfo;
013: import org.testng.log4testng.Logger;
014:
015: /**
016: * <code>AnnotationTypeEnum</code> is an enumeration value representing the annotation type.
017: * @author cquezel
018: */
019: public final class AnnotationTypeEnum implements Serializable {
020: /** This class's log4testng Logger. */
021: private static final Logger LOGGER = Logger
022: .getLogger(AnnotationTypeEnum.class);
023:
024: /** The JDK50 annotation type ID ("JDK5").*/
025: private static final String JDK_ANNOTATION_TYPE = "JDK";
026:
027: /** The JavaDoc annotation type ID ("javadoc"). */
028: private static final String JAVADOC_ANNOTATION_TYPE = "javadoc";
029:
030: /** javadoc annotation type */
031: public static final AnnotationTypeEnum JAVADOC = new AnnotationTypeEnum(
032: JAVADOC_ANNOTATION_TYPE);
033:
034: /** JDK5 annotation type */
035: public static final AnnotationTypeEnum JDK = new AnnotationTypeEnum(
036: JDK_ANNOTATION_TYPE);
037:
038: /** The enumeration name (one of JAVADOC_ANNOTATION_TYPE or JDK5_ANNOTATION_TYPE) */
039: private String m_name;
040:
041: /**
042: * Returns an <code>AnnotationTypeEnum</code> object holding the value of the
043: * specified <code>pAnnotationType</code>. This method throws an IllegalArgumentException
044: * if pAnnotationType is an illegal value or if this is version 14 and JDK5 annotations
045: * are specified.
046: *
047: * @param pAnnotationType the annotation type. This is one of the two constants
048: * (AnnotationTypeEnum.JAVADOC_ANNOTATION_TYPE or AnnotationTypeEnum.JDK5_ANNOTATION_TYPE).
049: * For backward compatibility we accept "1.4", "1.5". Any other value will default to
050: * AnnotationTypeEnum.JDK5 if this is the 1.5 version of TestNG or
051: * AnnotationTypeEnum.JAVADOC if this is the 1.4 version of TestNG.
052: *
053: * @return an <code>AnnotationTypeEnum</code> object holding the value of the
054: * specified <code>pAnnotationType</code>.
055: */
056: public static AnnotationTypeEnum valueOf(String pAnnotationType) {
057: return valueOf(pAnnotationType, true);
058: }
059:
060: /**
061: * Returns an <code>AnnotationTypeEnum</code> object holding the value of the
062: * specified <code>pAnnotationType</code>. This method throws an IllegalArgumentException
063: * if pAnnotationType is an illegal value or if this is version 14 and JDK5 annotations
064: * are specified.
065: *
066: * @param pAnnotationType the annotation type. This is one of the two constants
067: * (AnnotationTypeEnum.JAVADOC_ANNOTATION_TYPE or AnnotationTypeEnum.JDK5_ANNOTATION_TYPE).
068: * For backward compatibility we accept "1.4", "1.5". Any other value will default to
069: * AnnotationTypeEnum.JDK5 if this is the 1.5 version of TestNG or
070: * AnnotationTypeEnum.JAVADOC if this is the 1.4 version of TestNG.
071: * @param strict flag indicating if compatibility check should be performed
072: *
073: * @return an <code>AnnotationTypeEnum</code> object holding the value of the
074: * specified <code>pAnnotationType</code>.
075: */
076: public static AnnotationTypeEnum valueOf(String pAnnotationType,
077: boolean strict) {
078: if (pAnnotationType == null) {
079: throw new IllegalArgumentException("annotation is null");
080: }
081:
082: AnnotationTypeEnum annotationType;
083: if (pAnnotationType.equals(JAVADOC_ANNOTATION_TYPE)) {
084: annotationType = JAVADOC;
085: } else if (pAnnotationType.equals(JDK_ANNOTATION_TYPE)) {
086: annotationType = JDK;
087: } else if (pAnnotationType.equals("1.4")
088: || pAnnotationType.toLowerCase().equals(
089: JAVADOC_ANNOTATION_TYPE.toLowerCase())) {
090: // For backward compatibility only
091: annotationType = JAVADOC;
092: log(2, pAnnotationType, annotationType);
093: } else if ("1.5".equals(pAnnotationType)
094: || pAnnotationType.toLowerCase().equals(
095: JDK_ANNOTATION_TYPE.toLowerCase())) {
096: // For backward compatibility only
097: annotationType = JDK;
098: log(2, pAnnotationType, annotationType);
099: } else if ("jdk1.5".equals(pAnnotationType.toLowerCase())
100: || "jdk5".equals(pAnnotationType.toLowerCase())) {
101: // For backward compatibility only
102: annotationType = JDK;
103: log(2, pAnnotationType, annotationType);
104: } else {
105: // For backward compatibility only
106: // TODO should we make this an error?
107: annotationType = VersionInfo.getDefaultAnnotationType();
108: log(1, pAnnotationType, annotationType);
109: }
110:
111: if (strict) {
112: if (VersionInfo.IS_JDK14 && annotationType == JDK) {
113: throw new IllegalArgumentException("Cannot specify \""
114: + pAnnotationType
115: + "\" with 1.4 version of TestNG");
116: }
117: }
118:
119: return annotationType;
120: }
121:
122: /**
123: * Returns a human readable representation of the enum, suitable to be converted
124: * back to the enumeration by the valueOf method.
125: * @return a human readable representation of the enum, suitable to be converted
126: * back to the enumeration by the valueOf method.
127: */
128: public String getName() {
129: return m_name;
130: }
131:
132: /**
133: * Returns the m_name of the annotation type.
134: * {@inheritDoc}
135: */
136: @Override
137: public String toString() {
138: return m_name;
139: }
140:
141: private static void log(int level, String pAnnotationType,
142: AnnotationTypeEnum pDefault) {
143: final String msg = "Illegal annotation type '"
144: + pAnnotationType + "' defaulting to '" + pDefault
145: + "'";
146: LOGGER.info(msg);
147: Utils.log("AnnotationTypeEnum", level, "[WARN] " + msg);
148: }
149:
150: private AnnotationTypeEnum(String pName) {
151: m_name = pName;
152: }
153: }
|