//////////////////////////////////////////////////////////////////////////////
// 
//                    Copyright 2020, Cornutum Project
//                             www.cornutum.org
//
//////////////////////////////////////////////////////////////////////////////

package org.cornutum.tcases.openapi.moco;

import java.util.Optional;

import org.cornutum.tcases.io.IndentedWriter;

/**
 * POJO configuration for a <a href="https://github.com/dreamhead/moco/blob/master/moco-doc/junit.md">Moco server</a>.
 */
public class MocoServerConfigPojo extends MocoServerConfig
  {  
  /**
   * Creates a new MocoServerConfigPojo instance.
   */
  public MocoServerConfigPojo( PojoWriterFactory factory)
    {
    setFactory( factory);
    }
  
  /**
   * Creates a new MocoServerConfigPojo instance.
   */
  public MocoServerConfigPojo( Class<PojoWriterFactory> factoryClass) throws InstantiationException, IllegalAccessException
    {
    this( factoryClass.newInstance());
    }
  
  /**
   * Creates a new MocoServerConfigPojo instance.
   */
  @SuppressWarnings("unchecked")
  public MocoServerConfigPojo( String factoryClassName) throws InstantiationException, IllegalAccessException, ClassNotFoundException
    {
    this( (Class<PojoWriterFactory>) Class.forName( factoryClassName));
    }
  
  /**
   * Creates a new MocoServerConfigPojo instance.
   */
  public MocoServerConfigPojo( PojoWriter pojoWriter)
    {
    pojoWriter_ = pojoWriter;
    }

  /**
   * Returns the {@link PojoWriter} for this server configuration.
   */
  public PojoWriter getPojoWriter()
    {
    if( pojoWriter_ == null)
      {
      pojoWriter_ =
        Optional.ofNullable( getFactory())
        .map( PojoWriterFactory::createPojoWriter)
        .orElse( null);
      }
    
    return pojoWriter_;
    }

  /**
   * Changes the {@link PojoWriterFactory} for this server configuration.
   */
  public void setFactory( PojoWriterFactory factory)
    {
    factory_ = factory;
    }

  /**
   * Returns the {@link PojoWriterFactory} for this server configuration.
   */
  public PojoWriterFactory getFactory()
    {
    return factory_;
    }

  /**
   * Implements the Visitor pattern for this server configuration.
   */
  public void accept( ConfigVisitor visitor)
    {
    visitor.visit( this);
    }

  private PojoWriter pojoWriter_;
  private PojoWriterFactory factory_;

  /**
   * Writes the Mojo API code to configure the given server.
   */
  public interface PojoWriter
    {
    /**
     * Writes the Mojo API code to configure the given server.
     */
    public void writePojo( String serverName, IndentedWriter targetWriter);
    }
  
  /**
   * Writes the Mojo API code to configure the given server.
   */
  public interface PojoWriterFactory
    {
    /**
     * Writes the Mojo API code to configure the given server.
     */
    public PojoWriter createPojoWriter();
    }

  }

@@ -305,16 +305,68 @@
 .moco;%0A%0A
+import java.lang.reflect.InvocationTargetException;%0A
 import j
@@ -962,32 +962,127 @@
 lAccessException
+, InvocationTargetException, IllegalArgumentException, NoSuchMethodException, SecurityException
 %0A    %7B%0A    this(
@@ -1095,16 +1095,41 @@
 ryClass.
+getDeclaredConstructor().
 newInsta
@@ -1365,32 +1365,127 @@
 otFoundException
+, InvocationTargetException, IllegalArgumentException, NoSuchMethodException, SecurityException
 %0A    %7B%0A    this(

Fix ClassNewInstance errors