use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class ExportedKeyLoaderIT method testExportedKeyLoad.
@Test
public void testExportedKeyLoad() throws Exception {
boolean supportsFK = accessStackAdapter.supportsFKConstraints();
if (!supportsFK) {
return;
}
createEntity(nameForDb("ARTIST"));
createEntity(nameForDb("GALLERY"));
createEntity(nameForDb("PAINTING"));
DbEntity artist = getDbEntity("ARTIST");
DbAttribute artistId = new DbAttribute("ARTIST_ID");
artist.addAttribute(artistId);
DbEntity gallery = getDbEntity("GALLERY");
DbAttribute galleryId = new DbAttribute("GALLERY_ID");
gallery.addAttribute(galleryId);
DbEntity painting = getDbEntity("PAINTING");
DbAttribute paintingId = new DbAttribute("PAINTING_ID");
DbAttribute paintingArtistId = new DbAttribute("ARTIST_ID");
DbAttribute paintingGalleryId = new DbAttribute("GALLERY_ID");
painting.addAttribute(paintingId);
painting.addAttribute(paintingArtistId);
painting.addAttribute(paintingGalleryId);
ExportedKeyLoader loader = new ExportedKeyLoader(EMPTY_CONFIG, new DefaultDbLoaderDelegate());
loader.load(connection.getMetaData(), store);
assertEquals(2, store.getExportedKeysEntrySet().size());
ExportedKey artistIdFk = findArtistExportedKey();
assertNotNull(artistIdFk);
assertEquals("ARTIST", artistIdFk.getPk().getTable().toUpperCase());
assertEquals("ARTIST_ID", artistIdFk.getPk().getColumn().toUpperCase());
assertEquals("PAINTING", artistIdFk.getFk().getTable().toUpperCase());
assertEquals("ARTIST_ID", artistIdFk.getFk().getColumn().toUpperCase());
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class PrimaryKeyLoaderIT method testPrimaryKeyLoad.
@Test
public void testPrimaryKeyLoad() throws Exception {
createDbEntities();
DbEntity artist = getDbEntity(nameForDb("ARTIST"));
DbAttribute artistId = new DbAttribute(nameForDb("ARTIST_ID"));
DbAttribute artistName = new DbAttribute(nameForDb("ARTIST_NAME"));
DbAttribute artistId1 = new DbAttribute(nameForDb("ARTIST_ID1"));
artist.addAttribute(artistId);
artist.addAttribute(artistName);
artist.addAttribute(artistId1);
assertFalse(artistId.isPrimaryKey());
assertFalse(artistName.isPrimaryKey());
assertFalse(artistId1.isPrimaryKey());
PrimaryKeyLoader loader = new PrimaryKeyLoader(EMPTY_CONFIG, new DefaultDbLoaderDelegate());
loader.load(connection.getMetaData(), store);
assertTrue(artistId.isPrimaryKey());
assertFalse(artistId1.isPrimaryKey());
assertFalse(artistName.isPrimaryKey());
Collection<DbAttribute> pk = artist.getPrimaryKeys();
assertEquals(1, pk.size());
assertEquals(artistId, pk.iterator().next());
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class CryptoRowReaderFactoryDecorator method encryptedRowDescriptor.
protected RowDescriptor encryptedRowDescriptor(RowDescriptor descriptor, ExtendedTypeMap typeMap) {
// need to tweak the original descriptor to ensure binary columns are read as binary, eben if the plain Java
// type is not a byte[]
ColumnDescriptor[] originalColumns = descriptor.getColumns();
int len = originalColumns.length;
ExtendedType[] originalConverters = descriptor.getConverters();
ExtendedType[] encryptedConverters = new ExtendedType[len];
for (int i = 0; i < len; i++) {
DbAttribute attribute = originalColumns[i].getAttribute();
ExtendedType t = originalConverters[i];
if (attribute != null && columnMapper.isEncrypted(attribute)) {
// only char or binary columns can store encrypted data
if (TypesMapping.isBinary(attribute.getType())) {
t = typeMap.getRegisteredType(byte[].class);
} else if (TypesMapping.isCharacter(attribute.getType())) {
t = typeMap.getRegisteredType(String.class);
}
// else - warning?
}
encryptedConverters[i] = t;
}
return new RowDescriptor(originalColumns, encryptedConverters);
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class DefaultValueTransformerFactoryIT method testGetJavaType.
@Test
public void testGetJavaType() {
DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
assertEquals("java.lang.String", f.getJavaType(t1_ct));
DbAttribute t2_cb = t2.getAttribute("CRYPTO_BYTES");
assertEquals("byte[]", f.getJavaType(t2_cb));
DbEntity fakeEntity = mock(DbEntity.class);
when(fakeEntity.getDataMap()).thenReturn(t1.getDataMap());
DbAttribute fakeA1 = mock(DbAttribute.class);
when(fakeA1.getName()).thenReturn("fake1");
when(fakeA1.getEntity()).thenReturn(fakeEntity);
when(fakeA1.getType()).thenReturn(Types.VARBINARY);
assertEquals("byte[]", f.getJavaType(fakeA1));
DbAttribute fakeA2 = mock(DbAttribute.class);
when(fakeA2.getName()).thenReturn("fake2");
when(fakeA2.getEntity()).thenReturn(fakeEntity);
when(fakeA2.getType()).thenReturn(Types.VARCHAR);
assertEquals("java.lang.String", f.getJavaType(fakeA2));
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class DefaultValueTransformerFactoryIT method testCreateDecryptor.
@Test
public void testCreateDecryptor() {
DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
ValueDecryptor t1 = f.createDecryptor(t1_ct);
assertNotNull(t1);
assertTrue(t1 instanceof DefaultValueDecryptor);
assertSame(Base64StringConverter.INSTANCE, ((DefaultValueDecryptor) t1).getPreConverter());
assertSame(Utf8StringConverter.INSTANCE, ((DefaultValueDecryptor) t1).getPostConverter());
DbAttribute t2_cb = t2.getAttribute("CRYPTO_BYTES");
ValueDecryptor t2 = f.createDecryptor(t2_cb);
assertNotNull(t2);
assertTrue(t2 instanceof DefaultValueDecryptor);
assertSame(BytesToBytesConverter.INSTANCE, ((DefaultValueDecryptor) t2).getPreConverter());
assertSame(BytesToBytesConverter.INSTANCE, ((DefaultValueDecryptor) t2).getPostConverter());
DbAttribute t3_cb = t3.getAttribute("CRYPTO_BYTES");
ValueDecryptor t3 = f.createDecryptor(t3_cb);
assertNotNull(t3);
assertTrue(t3 instanceof DefaultValueDecryptor);
assertSame(BytesToBytesConverter.INSTANCE, ((DefaultValueDecryptor) t3).getPreConverter());
assertSame(Utf8StringConverter.INSTANCE, ((DefaultValueDecryptor) t3).getPostConverter());
}
Aggregations