Skip to content

Commit

Permalink
Feature/#120 (#121)
Browse files Browse the repository at this point in the history
* preparation for upcoming sql reimplementation ( #101 )

* implementation of settings for observer properties

* implementation of observer properties

* implementation of JsonSerializationObserver

* added unit test; update documentation

* review fixes
  • Loading branch information
Michael Remme authored and sschmittBt committed Jul 27, 2017
1 parent 2d18736 commit 07840f4
Show file tree
Hide file tree
Showing 32 changed files with 620 additions and 104 deletions.
24 changes: 24 additions & 0 deletions jomnigate-mysql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
*.class
/target/
/pom.xml~
/.settings/
/.classpath
/.project
/.yardoc
/tmp
/pombak/
/.sonar_lock

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

.DS_Store

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/bin/
24 changes: 24 additions & 0 deletions jomnigate-sql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
*.class
/target/
/pom.xml~
/.settings/
/.classpath
/.project
/.yardoc
/tmp
/pombak/
/.sonar_lock

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

.DS_Store

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/bin/
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
TestStoreObject.class, TestQuery.class, TestQueryHelper.class, TestIndex.class, TestRoundtrip.class,
TestOnlyIdMapper.class, TestTrigger.class, TestMassInsert.class, TestKeyGenerator.class, TestGeoSearch.class,
TestEncoder.class, TestListExtrems.class, TestReferenced.class, TestFieldConditionCache.class,
TestQueryInterator.class, DataTypesTestSuite.class, ObserverVersioningSuite.class, TestClearDatastore.class,
TestQueryInterator.class, DataTypesTestSuite.class, ObserverSuite.class, TestClearDatastore.class,
TestIndexedFields.class, TestUpdate.class })
public class AllTestsCommon {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
*/
@RunWith(Suite.class)
@SuiteClasses({ TestObserverHandler.class, TestDataVersioning.class })
public class ObserverVersioningSuite {
@SuiteClasses({ TestObserverHandler.class, TestDataVersioning.class, TestJsonSerializationObserver.class })
public class ObserverSuite {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* #%L
* vertx-pojo-mapper-common-test
* %%
* Copyright (C) 2017 Braintags GmbH
* %%
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
* #L%
*/
package de.braintags.vertx.jomnigate.testdatastore;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import org.junit.Test;

import de.braintags.vertx.jomnigate.init.DataStoreSettings;
import de.braintags.vertx.jomnigate.init.ObserverDefinition;
import de.braintags.vertx.jomnigate.observer.ObserverEventType;
import de.braintags.vertx.jomnigate.observer.impl.JsonSerializationObserver;
import de.braintags.vertx.jomnigate.testdatastore.mapper.SimpleMapper;
import io.vertx.core.json.Json;
import io.vertx.ext.unit.TestContext;

/**
* Tests to improve correct mapping information for defined observers ( or by settings or annotation )
*
* @author Michael Remme
*
*/
public class TestJsonSerializationObserver extends AbstractObserverTest {
private static final io.vertx.core.logging.Logger LOGGER = io.vertx.core.logging.LoggerFactory
.getLogger(TestJsonSerializationObserver.class);

@Test
public void testAfterInsertAndUpdate(TestContext context) throws IOException {
File logDir = getLogDir();
DataStoreSettings settings = getDataStore(context).getSettings();
ObserverDefinition<JsonSerializationObserver> os = new ObserverDefinition<>(JsonSerializationObserver.class);
os.getEventTypeList().add(ObserverEventType.AFTER_INSERT);
os.getEventTypeList().add(ObserverEventType.AFTER_UPDATE);
os.getObserverProperties().setProperty(JsonSerializationObserver.DIRECTORY_PROPERTY, logDir.getAbsolutePath());
settings.getObserverSettings().add(os);

SimpleMapper sm = new SimpleMapper("testname", "nix");
sm.intValue = -1;
saveRecord(context, sm);
sm.intValue = 5;
sm.name = "newNAME";
saveRecord(context, sm);
checkResult(context, logDir, 2, 500);
}

@Test
public void testAfterInsert(TestContext context) throws IOException {
File logDir = getLogDir();
DataStoreSettings settings = getDataStore(context).getSettings();
ObserverDefinition<JsonSerializationObserver> os = new ObserverDefinition<>(JsonSerializationObserver.class);
os.getEventTypeList().add(ObserverEventType.AFTER_INSERT);
os.getObserverProperties().setProperty(JsonSerializationObserver.DIRECTORY_PROPERTY, logDir.getAbsolutePath());
settings.getObserverSettings().add(os);
LOGGER.debug(Json.encodePrettily(os));
SimpleMapper sm = new SimpleMapper("testname", "nix");
sm.intValue = -1;
saveRecord(context, sm);
checkResult(context, logDir, 1, 500);
}

@Test
public void testDirectoryNotSet(TestContext context) throws IOException {
File logDir = getLogDir();
DataStoreSettings settings = getDataStore(context).getSettings();
ObserverDefinition<JsonSerializationObserver> os = new ObserverDefinition<>(JsonSerializationObserver.class);
os.getEventTypeList().add(ObserverEventType.AFTER_INSERT);
settings.getObserverSettings().add(os);
SimpleMapper sm = new SimpleMapper("testname", "nix");
sm.intValue = -1;
try {
saveRecord(context, sm);
context.fail("we are expecting an exception, cause the log directory is not set");
} catch (Throwable e) {
// we expect this
}
}

private void checkResult(TestContext context, File logDir, int fileCount, long waitTicks) {
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < waitTicks) {
File[] files = logDir.listFiles((dir, fileName) -> fileName.endsWith("json"));
if (files.length >= fileCount) {
break;
}
}
context.assertEquals(fileCount, logDir.listFiles((dir, fileName) -> fileName.endsWith("json")).length,
"expected files to be created");
}

private File getLogDir() throws IOException {
File logDir = Files.createTempDirectory("jsonSerDirTmp").toFile();
File[] fl = logDir.listFiles();
for (File f : fl) {
f.delete();
}
if (logDir.listFiles().length > 0) {
throw new IllegalArgumentException("the directory was not cleaned");
}
logDir.mkdir();
LOGGER.debug("FILE CREATION IN " + logDir.getAbsolutePath());
return logDir;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class TestObserverHandler extends AbstractObserverTest {

@Test
public void test_AfterMapping_SingleRecord(TestContext context) {
AfterMappingObserver.executed = false;
AfterMappingObserver.reset();
DataStoreSettings settings = getDataStore(context).getSettings();
ObserverDefinition<AfterMappingObserver> os = new ObserverDefinition<>(AfterMappingObserver.class);
os.getEventTypeList().add(ObserverEventType.AFTER_MAPPING);
Expand All @@ -54,6 +54,10 @@ public void test_AfterMapping_SingleRecord(TestContext context) {
sm.intValue = -1;
IMapper<SimpleMapper> mapper = getDataStore(context).getMapperFactory().getMapper(SimpleMapper.class);
context.assertTrue(AfterMappingObserver.executed, "Observer wasn't executed");
context.assertTrue(AfterMappingObserver.executedTypes.contains(ObserverEventType.AFTER_MAPPING),
"Observer wasn't executed");
context.assertFalse(AfterMappingObserver.executedTypes.contains(ObserverEventType.BEFORE_MAPPING),
"Observer should not be executed for event BEFORE_MAPPING");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import de.braintags.vertx.jomnigate.observer.IObserver;
import de.braintags.vertx.jomnigate.observer.ObserverEventType;
import de.braintags.vertx.jomnigate.testdatastore.mapper.ObserverAnnotatedMapper;
import de.braintags.vertx.jomnigate.testdatastore.mapper.ObserverAnnotatedMapperWithProperties;
import de.braintags.vertx.jomnigate.testdatastore.mapper.ObserverAnnotatedMapper_TwoEvents;
import de.braintags.vertx.jomnigate.testdatastore.mapper.Person;
import de.braintags.vertx.jomnigate.testdatastore.mapper.PolyMapper;
Expand All @@ -36,6 +37,7 @@
import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver2;
import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver3;
import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver4;
import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver5;
import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver_NoDefaultConstructor;
import io.vertx.ext.unit.TestContext;

Expand All @@ -49,6 +51,48 @@ public class TestObserverMapping extends AbstractObserverTest {
private static final io.vertx.core.logging.Logger LOGGER = io.vertx.core.logging.LoggerFactory
.getLogger(TestObserverMapping.class);

/**
* Defines an observer with properties by annotation
*
* @param context
*/
@Test
public void testObserverWithPropertiesByAnnotation(TestContext context) {

IMapper<ObserverAnnotatedMapperWithProperties> mapper = getDataStore(context).getMapperFactory()
.getMapper(ObserverAnnotatedMapperWithProperties.class);
List<IObserver> ol = mapper.getObserverHandler().getObserver(ObserverEventType.AFTER_LOAD);
context.assertFalse(ol.isEmpty(), "observer not found");
context.assertFalse(ol.get(0).getObserverProperties().isEmpty(), "observer properties not found");
}

/**
* Defines an observer with properties
*
* @param context
*/
@Test
public void testObserverWithProperties(TestContext context) {
DataStoreSettings settings = getDataStore(context).getSettings();
ObserverDefinition<TestObserver5> os = new ObserverDefinition<>(TestObserver5.class);
os.getObserverProperties().setProperty("testProperty", "testValue");
settings.getObserverSettings().add(os);

IMapper<ObserverAnnotatedMapper> mapper = getDataStore(context).getMapperFactory()
.getMapper(ObserverAnnotatedMapper.class);
List<IObserver> ol = mapper.getObserverHandler().getObserver(ObserverEventType.AFTER_LOAD);
context.assertFalse(ol.isEmpty(), "observer not found");

TestObserver5 os5 = null;
for (IObserver tmp : ol) {
if (tmp.getClass() == TestObserver5.class) {
os5 = (TestObserver5) tmp;
}
}
context.assertNotNull(os5, "required ovbserver not found");
context.assertFalse(os5.getObserverProperties().isEmpty(), "observ er properties not found");
}

/**
* Defines an observer, which should be executed for any event instanceof BaseRecord. TriggerMapper should not be
* handled by this mapper
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* #%L
* vertx-pojo-mapper-common-test
* %%
* Copyright (C) 2017 Braintags GmbH
* %%
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
* #L%
*/
package de.braintags.vertx.jomnigate.testdatastore.mapper;

import de.braintags.vertx.jomnigate.annotation.Observer;
import de.braintags.vertx.jomnigate.annotation.ObserverOption;
import de.braintags.vertx.jomnigate.testdatastore.mapper.typehandler.BaseRecord;
import de.braintags.vertx.jomnigate.testdatastore.observer.TestObserver;

/**
*
*
* @author Michael Remme
*
*/
@Observer(observerClass = TestObserver.class, priority = 600, observerOptions = {
@ObserverOption(key = "testKey", value = "testValue") })
public class ObserverAnnotatedMapperWithProperties extends BaseRecord {

/**
*
*/
public ObserverAnnotatedMapperWithProperties() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*/
package de.braintags.vertx.jomnigate.testdatastore.observer;

import java.util.ArrayList;
import java.util.List;

import de.braintags.vertx.jomnigate.mapping.IMapper;
import de.braintags.vertx.jomnigate.observer.IObserver;
import de.braintags.vertx.jomnigate.observer.IObserverContext;
import de.braintags.vertx.jomnigate.observer.IObserverEvent;
import de.braintags.vertx.jomnigate.observer.ObserverEventType;
import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver;
import io.vertx.core.Future;

/**
Expand All @@ -25,8 +28,14 @@
* @author Michael Remme
*
*/
public class AfterMappingObserver implements IObserver {
public class AfterMappingObserver extends AbstractObserver {
public static boolean executed = false;
public static List<ObserverEventType> executedTypes = new ArrayList<>();

public static void reset() {
executed = false;
executedTypes = new ArrayList<>();
}

/*
* (non-Javadoc)
Expand All @@ -36,14 +45,10 @@ public class AfterMappingObserver implements IObserver {
*/
@Override
public Future<Void> handleEvent(IObserverEvent event, IObserverContext context) {
executedTypes.add(event.getEventType());
IMapper<?> mapper = (IMapper<?>) event.getSource();
AfterMappingObserver.executed = true;
return Future.succeededFuture();
}

@Override
public boolean canHandleEvent(IObserverEvent event, IObserverContext context) {
return event.getEventType().equals(ObserverEventType.AFTER_MAPPING);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
package de.braintags.vertx.jomnigate.testdatastore.observer;

import de.braintags.vertx.jomnigate.dataaccess.delete.IDelete;
import de.braintags.vertx.jomnigate.observer.IObserver;
import de.braintags.vertx.jomnigate.observer.IObserverContext;
import de.braintags.vertx.jomnigate.observer.IObserverEvent;
import de.braintags.vertx.jomnigate.observer.impl.AbstractObserver;
import io.vertx.core.Future;

/**
Expand All @@ -24,7 +24,7 @@
* @author Michael Remme
*
*/
public class BeforeDeleteObserver implements IObserver {
public class BeforeDeleteObserver extends AbstractObserver {
public static boolean executed = false;

/*
Expand All @@ -40,9 +40,4 @@ public Future<Void> handleEvent(IObserverEvent event, IObserverContext context)
return Future.succeededFuture();
}

@Override
public boolean canHandleEvent(IObserverEvent event, IObserverContext context) {
return true;
}

}
Loading

0 comments on commit 07840f4

Please sign in to comment.