Package com.j256.ormlite.field

Source Code of com.j256.ormlite.field.DatabaseFieldConfigTest$BadUnknownVal

package com.j256.ormlite.field;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.Serializable;
import java.lang.reflect.Field;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import org.junit.Test;

import com.j256.ormlite.db.BaseDatabaseType;
import com.j256.ormlite.db.DatabaseType;

public class DatabaseFieldConfigTest {

  @Test
  public void testGetSet() {
    DatabaseFieldConfig config = new DatabaseFieldConfig();
    String str;

    assertNull(config.getColumnName());
    str = "name";
    config.setColumnName(str);
    assertEquals(str, config.getColumnName());

    assertNull(config.getDefaultValue());
    str = "default";
    config.setDefaultValue(str);
    assertEquals(str, config.getDefaultValue());

    assertNull(config.getGeneratedIdSequence());
    str = "seq";
    config.setGeneratedIdSequence(str);
    assertEquals(str, config.getGeneratedIdSequence());

    assertNull(config.getDataPersister());
    DataPersister jdbcType = DataType.DOUBLE.getDataPersister();
    config.setDataPersister(jdbcType);
    assertEquals(jdbcType, config.getDataPersister());

    assertEquals(0, config.getWidth());
    int width = 21312312;
    config.setWidth(width);
    assertEquals(width, config.getWidth());

    assertFalse(config.isCanBeNull());
    config.setCanBeNull(true);
    assertTrue(config.isCanBeNull());

    assertFalse(config.isForeign());
    config.setForeign(true);
    assertTrue(config.isForeign());

    assertFalse(config.isGeneratedId());
    config.setGeneratedId(true);
    assertTrue(config.isGeneratedId());

    assertFalse(config.isId());
    config.setId(true);
    assertTrue(config.isId());

    assertFalse(config.isUseGetSet());
    config.setUseGetSet(true);
    assertTrue(config.isUseGetSet());
  }

  @Test
  public void testFromDbField() throws Exception {
    Field[] fields = Foo.class.getDeclaredFields();
    DatabaseType databaseType = new StubDatabaseType();
    assertTrue(fields.length >= 1);
    DatabaseFieldConfig config = DatabaseFieldConfig.fromField(databaseType, "foo", fields[0]);
    assertNotNull(config);
    assertTrue(config.isCanBeNull());
    assertEquals(fields[0].getName(), config.getFieldName());
  }

  private final static String STUFF_FIELD_NAME = "notstuff";
  private final static int LENGTH_LENGTH = 100;

  @Test
  public void testJavaxAnnotations() throws Exception {
    DatabaseType databaseType = new StubDatabaseType();
    Field[] fields = JavaxAnno.class.getDeclaredFields();
    assertTrue(fields.length >= 8);

    // not a column
    assertNull(DatabaseFieldConfig.fromField(databaseType, "foo", fields[0]));

    DatabaseFieldConfig config = DatabaseFieldConfig.fromField(databaseType, "foo", fields[1]);
    assertNotNull(config);
    assertFalse(config.isId());
    assertTrue(config.isGeneratedId());
    assertFalse(config.isUseGetSet());
    assertEquals(fields[1].getName(), config.getFieldName());

    config = DatabaseFieldConfig.fromField(databaseType, "foo", fields[2]);
    assertNotNull(config);
    assertFalse(config.isUseGetSet());
    assertEquals(STUFF_FIELD_NAME, config.getColumnName());

    config = DatabaseFieldConfig.fromField(databaseType, "foo", fields[3]);
    assertNotNull(config);
    assertEquals(LENGTH_LENGTH, config.getWidth());
    assertFalse(config.isUseGetSet());
    assertEquals(fields[3].getName(), config.getFieldName());

    config = DatabaseFieldConfig.fromField(databaseType, "foo", fields[4]);
    assertNotNull(config);
    assertFalse(config.isCanBeNull());
    assertFalse(config.isUseGetSet());
    assertEquals(fields[4].getName(), config.getFieldName());

    config = DatabaseFieldConfig.fromField(databaseType, "foo", fields[5]);
    assertNotNull(config);
    assertTrue(config.isForeign());
    assertNull(config.getDataPersister());
    assertEquals(fields[5].getName(), config.getFieldName());

    config = DatabaseFieldConfig.fromField(databaseType, "foo", fields[6]);
    assertNotNull(config);
    assertTrue(config.isForeign());
    assertNull(config.getDataPersister());
    assertEquals(fields[6].getName(), config.getFieldName());
  }

  @Test
  public void testJavaxJustId() throws Exception {
    DatabaseType databaseType = new StubDatabaseType();
    Field[] fields = JavaxAnnoJustId.class.getDeclaredFields();
    assertTrue(fields.length >= 1);

    DatabaseFieldConfig config = DatabaseFieldConfig.fromField(databaseType, "foo", fields[0]);
    assertNotNull(config);
    assertTrue(config.isId());
    assertFalse(config.isGeneratedId());
    assertFalse(config.isUseGetSet());
    assertEquals(fields[0].getName(), config.getFieldName());
  }

  @Test
  public void testJavaxGetSet() throws Exception {
    DatabaseType databaseType = new StubDatabaseType();
    Field[] fields = JavaxGetSet.class.getDeclaredFields();
    assertTrue(fields.length >= 1);

    DatabaseFieldConfig config = DatabaseFieldConfig.fromField(databaseType, "foo", fields[0]);
    assertNotNull(config);
    assertTrue(config.isUseGetSet());
    assertEquals(fields[0].getName(), config.getFieldName());
  }

  @Test
  public void testJavaxUnique() throws Exception {
    DatabaseType databaseType = new StubDatabaseType();
    Field[] fields = JavaxUnique.class.getDeclaredFields();
    assertTrue(fields.length >= 1);

    DatabaseFieldConfig config = DatabaseFieldConfig.fromField(databaseType, "foo", fields[0]);
    assertNotNull(config);
    assertTrue(config.isUnique());
    assertEquals(fields[0].getName(), config.getFieldName());
  }

  @Test(expected = IllegalArgumentException.class)
  public void testUnknownEnumVal() throws Exception {
    DatabaseType databaseType = new StubDatabaseType();
    Field[] fields = BadUnknownVal.class.getDeclaredFields();
    assertTrue(fields.length >= 1);
    DatabaseFieldConfig.fromField(databaseType, "foo", fields[0]);
  }

  @Test
  public void testIndex() throws Exception {
    DatabaseType databaseType = new StubDatabaseType();
    Field[] fields = Index.class.getDeclaredFields();
    assertTrue(fields.length >= 1);
    String tableName = "foo";
    DatabaseFieldConfig fieldConfig = DatabaseFieldConfig.fromField(databaseType, tableName, fields[0]);
    assertEquals(tableName + "_" + fields[0].getName() + "_idx", fieldConfig.getIndexName());
  }

  @Test
  public void testComboIndex() throws Exception {
    DatabaseType databaseType = new StubDatabaseType();
    Field[] fields = ComboIndex.class.getDeclaredFields();
    assertTrue(fields.length >= 2);
    DatabaseFieldConfig fieldConfig = DatabaseFieldConfig.fromField(databaseType, "foo", fields[0]);
    assertEquals(ComboIndex.INDEX_NAME, fieldConfig.getIndexName());
    fieldConfig = DatabaseFieldConfig.fromField(databaseType, "foo", fields[1]);
    assertEquals(ComboIndex.INDEX_NAME, fieldConfig.getIndexName());
  }

  @Test
  public void testDefaultValue() throws Exception {
    DatabaseType databaseType = new StubDatabaseType();
    DatabaseFieldConfig fieldConfig =
        DatabaseFieldConfig.fromField(databaseType, "defaultstring",
            DefaultString.class.getDeclaredField("stuff"));
    assertNotNull(fieldConfig.getDefaultValue());
    assertEquals(DefaultString.STUFF_DEFAULT, fieldConfig.getDefaultValue());
    fieldConfig =
        DatabaseFieldConfig.fromField(databaseType, "defaultstring",
            DefaultString.class.getDeclaredField("junk"));
    assertNotNull(fieldConfig.getDefaultValue());
    assertEquals(DefaultString.JUNK_DEFAULT, fieldConfig.getDefaultValue());
    fieldConfig =
        DatabaseFieldConfig.fromField(databaseType, "defaultstring",
            DefaultString.class.getDeclaredField("none"));
    assertNull(fieldConfig.getDefaultValue());
  }

  protected class Foo {
    @DatabaseField(canBeNull = true)
    String field;
  }

  protected class Serial implements Serializable {
    private static final long serialVersionUID = 6826474171714263950L;
    @DatabaseField(canBeNull = true)
    String field;
  }

  protected class JavaxAnno {
    // no annotations so is not a configured column
    int notColumn;
    @Column
    @Id
    @GeneratedValue
    int id;
    @Column(name = STUFF_FIELD_NAME)
    String stuff;
    @Column(length = LENGTH_LENGTH)
    String length;
    @Column(nullable = false)
    String nullable;
    @ManyToOne
    Foo foo;
    @OneToOne
    Foo foo2;
  }

  protected class JavaxAnnoJustId {
    @Column
    @Id
    String id;
  }

  protected class JavaxGetSet {
    @Column
    String id;
    public String getId() {
      return id;
    }
    public void setId(String id) {
      this.id = id;
    }
  }

  protected class JavaxUnique {
    @Column(unique = true)
    String id;
  }

  protected class BadUnknownVal {
    // not a valid enum name
    @DatabaseField(unknownEnumName = "THIRD")
    OurEnum ourEnum;
  }

  private enum OurEnum {
    FIRST,
    SECOND, ;
  }

  private static class StubDatabaseType extends BaseDatabaseType {
    @Override
    public String getDriverClassName() {
      return "foo.bar.baz";
    }
    @Override
    public String getDatabaseName() {
      return "fake";
    }
    public boolean isDatabaseUrlThisType(String url, String dbTypePart) {
      return false;
    }
  }

  protected static class Index {
    @DatabaseField(index = true)
    String stuff;
    public Index() {
    }
  }

  protected static class ComboIndex {
    @DatabaseField(indexName = INDEX_NAME)
    String stuff;
    @DatabaseField(indexName = INDEX_NAME)
    long junk;
    public ComboIndex() {
    }
    public static final String INDEX_NAME = "stuffjunk";
  }

  protected static class DefaultString {
    public static final String STUFF_DEFAULT = "";
    public static final String JUNK_DEFAULT = "xyzzy";
    @DatabaseField(defaultValue = STUFF_DEFAULT)
    String stuff;
    @DatabaseField(defaultValue = JUNK_DEFAULT)
    String junk;
    @DatabaseField
    String none;
    public DefaultString() {
    }
  }
}
TOP

Related Classes of com.j256.ormlite.field.DatabaseFieldConfigTest$BadUnknownVal

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
script>