    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package javax.jdo;
    19
    20
    21  import java.lang.instrument.IllegalClassFormatException;
    22  import java.security.ProtectionDomain;
    23  import java.util.ArrayList;
    24  import java.util.Arrays;
    25  import java.util.List;
    26  import java.util.Properties;
    27
    28  import static javax.jdo.Constants.PROPERTY_ENHANCER_VENDOR_NAME;
    29  import static javax.jdo.Constants.PROPERTY_ENHANCER_VERSION_NUMBER;
    30
    31  import javax.jdo.metadata.JDOMetadata;
    32
    33
    34  /**
    35   * Tests class javax.jdo.Enhancer (main class).
    36   * <p>
    37   */
    38  public class MockEnhancer implements JDOEnhancer {
    39
    40      static Properties props = new Properties();
    41      static {
    42          props.put(PROPERTY_ENHANCER_VENDOR_NAME, "Mock Enhancer");
    43          props.put(PROPERTY_ENHANCER_VERSION_NUMBER, "2.3.0");
    44          props.put("MockKey", "MockValue");
    45      }
    46      @SuppressWarnings("unused")
    47      private boolean verbose;
    48      private int numberOfElements;
    49      private List<String> classNames = new ArrayList<String>();
    50      private List<String> jarNames = new ArrayList<String>();
    51      private List<String> jdoNames = new ArrayList<String>();
    52      private List<String> puNames = new ArrayList<String>();
    53      @SuppressWarnings("unused")
    54      private String outputDirectory = null;
    55
    56      public MockEnhancer(){
    57      }
    58
    59      public Properties getProperties() {
    60          return props;
    61      }
    62
    63      public JDOEnhancer setVerbose(boolean flag) {
    64          this.verbose = flag;
    65          return this;
    66      }
    67
    68      public JDOEnhancer setOutputDirectory(String dirName) {
    69          outputDirectory = dirName;
    70          return this;
    71      }
    72
    73      public JDOEnhancer setClassLoader(ClassLoader loader) {
    74          // check to see if JDOHelper is loadable from the loader
    75          try {
    76          loader.loadClass("javax.jdo.JDOHelper");
    77          } catch (ClassNotFoundException ex) {
    78              // bad
    79              throw new JDOFatalInternalException("Should be able to load JDOHelper from the class loader");
    80          }
    81          return this;
    82      }
    83
    84      public JDOEnhancer addPersistenceUnit(String persistenceUnit) {
    85          numberOfElements++;
    86          this.puNames.add(persistenceUnit);
    87          return this;
    88      }
    89
    90      public JDOEnhancer addClass(String className, byte[] bytes) {
    91          throw new UnsupportedOperationException("Not supported yet.");
    92      }
    93
    94      public JDOEnhancer addClasses(String... classNames) {
    95          numberOfElements += classNames.length;
    96          this.classNames.addAll(Arrays.asList(classNames));
    97          return this;
    98      }
    99
    100     public JDOEnhancer addFiles(String... metadataFiles) {
    101         numberOfElements += metadataFiles.length;
    102         this.jdoNames.addAll(Arrays.asList(metadataFiles));
    103         return this;
    104     }
    105
    106     public JDOEnhancer addJar(String jarFileName) {
    107         numberOfElements++;
    108         this.jarNames.add(jarFileName);
    109         return this;
    110     }
    111
    112     public int enhance() {
    113         return numberOfElements;
    114     }
    115
    116     public int validate() {
    117         return numberOfElements;
    118     }
    119
    120     public byte[] getEnhancedBytes(String className) {
    121         throw new UnsupportedOperationException("Not supported yet.");
    122     }
    123
    124     public void registerMetadata(JDOMetadata metadata) {
    125         throw new UnsupportedOperationException("Not supported yet.");
    126     }
    127
    128     public JDOMetadata newMetadata() {
    129         throw new UnsupportedOperationException("Not supported yet.");
    130     }
    131
    132     public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    133         throw new UnsupportedOperationException("Not supported yet.");
    134     }
    135 }
    136