Junit Testing Framework for google app engine - google-app-engine

// My test class
I want to do junit test of the below code but it is not performing and throwing some errors. I have imported all the jar files but still the junit testing is not doing its task.
package com.full.notetakingapplicationtest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import static org.mockito.Mockito.atLeastOnce;
import com.full.notetakingapplication.RegistrationClass;
import com.full.notetakingapplication.UserDetailsClass;
import static org.mockito.Mockito.verify;
import com.google.appengine.tools.development.testing.LocalAppIdentityServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
import junit.framework.TestCase;
public class UserDetailsClassTest extends TestCase {
public final LocalServiceTestHelper helper = new LocalServiceTestHelper(
new LocalDatastoreServiceTestConfig(),
new LocalTaskQueueTestConfig().setDisableAutoTaskExecution(true),
new LocalTaskQueueTestConfig().setShouldCopyApiProxyEnvironment(true),
new LocalAppIdentityServiceTestConfig());
#Before
public void setUp(){
helper.setUp();
}
#After
public void tearDown(){
helper.tearDown();
}
#InjectMocks
UserDetailsClass user = Mockito.mock(UserDetailsClass.class);
#InjectMocks
RegistrationClass register = new RegistrationClass();
#SuppressWarnings("static-access")
#Test
public void testRegisterUser_usingMockito(){
String userName = "Pravin";
String userEmailId = "pravin.bala#adaptavantcloud.com";
String userPassword = "success";
register.setUserName(userName);
register.setUserEmailId(userEmailId);
register.setUserPassword(userPassword);
Assert.assertTrue(user.registerUser(register));
verify(user, atLeastOnce()).registerUser(register);
}
}
// My Error on Testing the method.
UserDetailsClassTest.testRegisterUser_usingMockito
testRegisterUser_usingMockito(com.full.notetakingapplicationtest.UserDetailsClassTest)
java.lang.NoSuchMethodError: com.google.appengine.repackaged.com.google.io.protocol.ProtocolSupport.freezeString(Ljava/lang/Object;)Ljava/lang/Object;
at com.google.storage.onestore.v3.OnestoreEntity$Reference.freeze(OnestoreEntity.java:7656)
at com.google.storage.onestore.v3.OnestoreEntity$Reference$1.<init>(OnestoreEntity.java:7555)
at com.google.storage.onestore.v3.OnestoreEntity$Reference.<clinit>(OnestoreEntity.java:7552)
at com.google.appengine.api.datastore.KeyTranslator.convertToPb(KeyTranslator.java:48)
at com.google.appengine.api.datastore.EntityTranslator.convertToPb(EntityTranslator.java:51)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$4.toPb(AsyncDatastoreServiceImpl.java:178)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$4.toPb(AsyncDatastoreServiceImpl.java:155)
at com.google.appengine.api.datastore.Batcher$BatchIterator.<init>(Batcher.java:180)
at com.google.appengine.api.datastore.Batcher$2.<init>(Batcher.java:317)
at com.google.appengine.api.datastore.Batcher.getBatches(Batcher.java:317)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.doBatchPut(AsyncDatastoreServiceImpl.java:365)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:293)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl$4.runInternal(BaseAsyncDatastoreServiceImpl.java:261)
at com.google.appengine.api.datastore.TransactionRunner.runWriteInTransaction(TransactionRunner.java:53)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:263)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:234)
at com.google.appengine.api.datastore.DatastoreServiceImpl.put(DatastoreServiceImpl.java:56)
at com.full.notetakingapplication.UserDetailsClass.registerUser(UserDetailsClass.java:25)
at com.full.notetakingapplicationtest.UserDetailsClassTest.testRegisterUser_usingMockito(UserDetailsClassTest.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Related

Integrating SQLServer and H2 in Spring Boot Application

I'm trying to integrate SQL Server 2016 and H2 in-memory database for one of my Spring Boot project. All the configuration is set and I'm able to load persistence-unit for both database without any problem. Also, I can query for SQL Server Database and I'm getting my data too. Sql Server JPA configuration is my primary configuration. But, somehow JPA is not able to create entity in H2 database. And after seeing log, I came into the conclusion that the entity that I'm trying to create in H2 is actually trying to find that in SqlServer Datbase. Some part of log for this complain is below:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'person'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:348)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy152.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 32 common frames omitted
More detail log is posted below. Below are the classes that I've done and configured till now.
application.properties
#Profile Properties
spring.profiles.active=local
# DataSource Properties for SQL Server
app.sqlserver.datasource.url=jdbc:sqlserver://XYZ;database=PQR
app.sqlserver.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
app.sqlserver.jpa.properties.hibernate.ddl-auto=update
app.sqlserver.jpa.properties.hibernate.format_sql=true
app.sqlserver.jpa.properties.hibernate.dialect=<custom_dialect_class>
app.sqlserver.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
app.sqlserver.jpa.show-sql=true
# DataSource Properties for H2
app.h2.datasource.url=jdbc:h2:mem:~/h2/TMV
app.h2.datasource.platform=h2
app.h2.datasource.username=sa
app.h2.datasource.password=
app.h2.datasource.driver-class-name=org.h2.Driver
app.h2.jpa.properties.hibernate.hbm2ddl.auto=create #here I tried using ddl-auto=create
app.h2.jpa.properties.hibernate.format_sql=true
app.h2.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
app.h2.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
app.h2.jpa.show-sql=true
spring.h2.console.enabled=true
spring.h2.console.path=/h2_console
SQLServerDataSourceConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
#Configuration
#EnableJpaRepositories(
entityManagerFactoryRef = "sqlServerEntityManager",
basePackages = "com.xyz.persistence.dao",
transactionManagerRef = "sqlServerTransactionManager"
)
public class SQLServerDataSourceConfig {
#Autowired(required = false)
private PersistenceUnitManager persistenceUnitManager;
#Bean
#ConfigurationProperties("app.sqlserver.jpa")
public JpaProperties sqlServerJpaProperties(){
return new JpaProperties();
}
#Bean
#ConfigurationProperties("app.sqlserver.datasource")
public DataSource sqlServerDataSource(){
return DataSourceBuilder.create().build();
}
#Bean
#Primary
public LocalContainerEntityManagerFactoryBean sqlServerEntityManager(
JpaProperties sqlServerJpaProperties) {
EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(sqlServerJpaProperties);
return builder
.dataSource(sqlServerDataSource())
.packages("com.xyz.persistence.domain")
.persistenceUnit("SQLServer-PU")
.build();
}
#Bean
#Primary
public JpaTransactionManager sqlServerTransactionManager(EntityManagerFactory sqlServerEntityManager) {
return new JpaTransactionManager(sqlServerEntityManager);
}
private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties sqlServerJpaProperties) {
JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(sqlServerJpaProperties);
return new EntityManagerFactoryBuilder(jpaVendorAdapter,
sqlServerJpaProperties.getProperties(), this.persistenceUnitManager);
}
private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(jpaProperties.isShowSql());
adapter.setDatabase(jpaProperties.getDatabase());
adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
return adapter;
}
}
H2DataSourceConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
#Configuration
#EnableJpaRepositories(
entityManagerFactoryRef = "h2EntityManager",
basePackages = "com.xyz.persistence.daoh2",
transactionManagerRef = "h2TransactionManager"
)
#EntityScan(basePackages = {"com.xyz.persistence.domainh2"})
public class H2DataSourceConfig {
#Autowired(required = false)
private PersistenceUnitManager persistenceUnitManager;
#Bean
#ConfigurationProperties("app.h2.jpa")
public JpaProperties sqlServerJpaProperties() {
return new JpaProperties();
}
#Bean
#ConfigurationProperties("app.h2.datasource")
public DataSource sqlServerDataSource() {
return DataSourceBuilder.create().build();
}
#Bean
public LocalContainerEntityManagerFactoryBean h2EntityManager(
JpaProperties h2JpaProperties) {
EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(h2JpaProperties);
return builder
.dataSource(sqlServerDataSource())
.packages("com.xyz.persistence.domainh2")
.persistenceUnit("H2-PU")
.build();
}
#Bean
public JpaTransactionManager h2TransactionManager(EntityManagerFactory h2EntityManager) {
return new JpaTransactionManager(h2EntityManager);
}
private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties h2JpaProperties) {
JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(h2JpaProperties);
return new EntityManagerFactoryBuilder(jpaVendorAdapter,
h2JpaProperties.getProperties(), this.persistenceUnitManager);
}
private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(jpaProperties.isShowSql());
adapter.setDatabase(jpaProperties.getDatabase());
adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
return adapter;
}
}
PersonDao.java is the class that is inside “com.xyz.persistence.daoh2”
import com.xyz.persistence.domainh2.Person;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface PersonDao extends CrudRepository<Person, Long> {
}
CustomerDao.java is the class that is inside “com.xyz.persistence.dao”
import com.xyz.persistence.domain.Customer;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface CustomerDao extends CrudRepository<Customer, Long> {
}
Person.class
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
#Table(name = "PERSON")
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#Column(name = "FIRST_NAME", nullable = false)
private String firstName;
#Column(name = "LAST_NAME",nullable = false)
private String lastName;
}
Customer.class
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
#Table(name = "CUSTOMER")
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#Column(name = "FIRST_NAME", nullable = false)
private String firstName;
#Column(name = "LAST_NAME",nullable = false)
private String lastName;
}
This is my H2DataLoader.java class which implements CommandLineRunner to run this class at the time of deployment.
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
#Configuration
#Slf4j
public class H2DataLoader implements CommandLineRunner {
#PersistenceContext(name = "h2EntityManager")
private EntityManager entityManager;
#Override
public void run(String... args) throws Exception {
try {
Query query = entityManager.createNativeQuery("INSERT INTO PERSON (ID, FIRST_NAME, LAST_NAME) VALUES (?, ?, ?)");
query.setParameter(1, 1L);
query.setParameter(2, "Hello");
query.setParameter(3, "World");
query.executeUpdate();
} catch (Exception e) {
log.error("Error Occurred: {}", e);
}
}
}
Spring Boot's main ApiApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
#SpringBootApplication(
scanBasePackages = { "com.xyz.persistence" },
exclude = HibernateJpaAutoConfiguration.class
)
#EnableTransactionManagement
#EnableAsync
#EnableAspectJAutoProxy(proxyTargetClass = true)
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
Finally this is my build.gradle contents:
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('org.springframework.boot:spring-boot-devtools')
compile('com.microsoft.sqlserver:mssql-jdbc')
compileOnly('org.projectlombok:lombok')
runtime('com.h2database:h2')
}
While running this application it gives following information/errors in console:
.....
[INFO ] 2018-01-18 11:38:41.984 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'H2-PU'
[INFO ] 2018-01-18 11:38:44.950 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'H2-PU'
[INFO ] 2018-01-18 11:38:45.043 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'SQLServer-PU'
[INFO ] 2018-01-18 11:38:46.059 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'SQLServer-PU'
[INFO ] 2018-01-18 11:38:48.122 [restartedMain] o.s.a.f.CglibAopProxy - Final method [public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest)] cannot get proxied via CGLIB: Calls to this method will NOT be routed to the target instance and might lead to NPEs against uninitialized fields in the proxy instance.
……
Hiding endpoints mapping details, because this is my official project.
……
o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat started on port(s): 8084 (http)
Hibernate:
INSERT
INTO
person
(ID, FIRST_NAME, LAST_NAME)
VALUES
(?, ?, ?)
[WARN ] 2018-01-18 11:38:54.600 [restartedMain] o.h.e.j.s.SqlExceptionHelper - SQL Error: 208, SQLState: S0002
[ERROR] 2018-01-18 11:38:54.600 [restartedMain] o.h.e.j.s.SqlExceptionHelper - Invalid object name 'person'.
[ERROR] 2018-01-18 11:38:54.615 [restartedMain] c.h.m.p.c.H2DataLoader - Error Occurred: {}
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1700)
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:70)
at com.hms.matching.persistence.config.H2DataLoader.run(H2DataLoader.java:34)
at com.hms.matching.persistence.config.H2DataLoader$$FastClassBySpringCGLIB$$28072180.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at com.hms.matching.persistence.config.H2DataLoader$$EnhancerBySpringCGLIB$$d87b2678.run(<generated>)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at com…...api.ApiApplication.main(ApiApplication.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.engine.query.spi.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:194)
at org.hibernate.internal.SessionImpl.executeNativeUpdate(SessionImpl.java:1373)
at org.hibernate.internal.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:373)
at org.hibernate.jpa.internal.QueryImpl.internalExecuteUpdate(QueryImpl.java:405)
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:61)
... 27 common frames omitted
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'person'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:348)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy152.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 32 common frames omitted
[INFO ] 2018-01-18 11:38:54.631 [restartedMain] o.s.b.a.l.AutoConfigurationReportLoggingInitializer -
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
[ERROR] 2018-01-18 11:38:54.662 [restartedMain] o.s.b.SpringApplication - Application startup failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at com……….api.ApiApplication.main(ApiApplication.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at com.hms.matching.persistence.config.H2DataLoader$$EnhancerBySpringCGLIB$$d87b2678.run(<generated>)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732)
... 11 common frames omitted
Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:58)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
... 22 common frames omitted
[INFO ] 2018-01-18 11:38:54.662 [restartedMain] o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#7ab1fbb9: startup date [Thu Jan 18 11:38:31 CST 2018]; root of context hierarchy
I don’t know why it is trying to look for Person object in SQL Server. Do anyone have some idea on this. I'm looking after this issue for last 24 hrs. But, didn't find anything. Person table should be created in H2 database. Everything is fine SQL Server side.
I don't know if this will solve your problem but :
This is not the way JPA is supposed to work. Instead create a new Person() object, which should be an #Entity, and set its attributes, then entityManager.persist(person).

Getting NullPointeException while running the Selenium test script

I am getting the NullPointerException after executing the below testng test script. After launching the URL when it comes inside the test script method then, it is throwing the exception. Can you please help me out in this.
(Object Repo) LakesAndMountainsHomePage.java :-
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.actitime.genericlib.WebDriverCommonLib;
public class LakesAndMountainsHomePage extends WebDriverCommonLib{
WebDriver driver;
public LakesAndMountainsHomePage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(driver, this);
}
#FindBy(xpath= "//*[#id='the-main-menu']//*[contains(text() , 'LAKES &
MOUNTAINS')]")
WebElement LandM;
public WebElement LandMHeader()
{
System.out.println("came inside the method");
//LandM.isDisplayed();
return LandM;
}
}
BaseTest.java:-
public abstract class BaseTest {
ExcelLib eLib;
WebDriverCommonLib wLib;
WebDriver driver;
HomePage homepage;
#BeforeClass
public void baseBeforeClass()
{
eLib = new ExcelLib();
wLib = new WebDriverCommonLib();
driver=Driver.getBrowser();
driver.manage().window().maximize();
System.out.println("Browser is launched");
}
#BeforeMethod
public void launchURL()
{
wLib.homePage();
//loginPage.loginToAPP();
System.out.println("Navigated to the URL");
}
}
Testscript:-
public class LakesAndMountainsHomePageTest extends BaseTest{
LakesAndMountainsHomePage lm = new LakesAndMountainsHomePage(this.driver);
//TC TC131409 [New Lakes & Mountains Tab] : Verify New Lakes & Mountains
Tab is displayed in header.
#Test(priority=0)
public void lakesAndMountainsHeader()
{
boolean a= lm.LandMHeader().isDisplayed(); //getting exception here
if(a==true)
{
System.out.println("Lakes And Mountains tab is present in the HomePage");
}
else
{
System.out.println("Lakes And Mountains tab is not present in the
HomePage");
}
lm.LandMHeader().click();
System.out.println("It has clicked the tab");
}
}
And below is the exception stack trace:-
below is the exception stacktrace:-
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy9.isDisplayed(Unknown Source)
at com.acttime.usertest.LakesAndMountainsHomePageTest.lakesAndMountainsHeader(LakesAndMountainsHomePageTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:776)
at org.testng.TestRunner.run(TestRunner.java:634)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
at org.testng.SuiteRunner.run(SuiteRunner.java:334)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
at org.testng.TestNG.runSuites(TestNG.java:1161)
at org.testng.TestNG.run(TestNG.java:1129)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
I am getting the NullPointerException after executing the below testng test script. After launching the URL when it comes inside the test script method then, it is throwing the exception. Can you please help me out in this.
This instantiation is illegal.
LakesAndMountainsHomePage lm = new LakesAndMountainsHomePage(this.driver);
It is the root cause of Null pointer exception.
Move this statement to BaseTest class, create a class field and instantiate inside #BeforeClass method . Also, remove this operator.
Well i'am unable to see the implementation of a driver class specifying the type of driver(firefox,chrome,IE) being initialized.

Null Pointer Exception using TestNG

I am trying to automate and have integrated Selenium with Appium and I am using Eclipse with TestNG for execution.
Now within a project I have a package with two classes; the first class is for login into the App and the second class is for logout
First class code -
package com.gma.test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class LoginApp {
public AppiumDriver driver;
#BeforeSuite
public void beforeMethod() throws InterruptedException {
File app = new File("C:\\Users\\mc30058\\Downloads\\gma-QA-RELEASE-QRCode-07022015_v4.5.4028.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.Button[#resource-id='com.mcdonalds.app:id/button_choose_closest']")).click();
driver.findElement(By.xpath("//android.widget.Button[#text='Continue']")).click();
driver.findElement(By.xpath("//android.widget.Button[#text='OK']")).click();
}
#Test (priority=1)
public void Login() {
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.TextView[#text='Sign In']")).click();
driver.findElement(By.xpath("//android.widget.EditText[#resource-id='com.mcdonalds.app:id/signin_edittext_email']")).sendKeys("anuj.shrivastava#us.mcd.com");
driver.findElement(By.xpath("//android.widget.EditText[#resource-id='com.mcdonalds.app:id/signin_edittext_password']")).sendKeys("Anujtest2");
currentTime();
driver.findElement(By.xpath("//android.widget.Button[#text='Sign In']")).click();
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.Button[#text='Yeah, count me in!']")).isDisplayed();
currentTime();
driver.findElement(By.xpath("//android.widget.Button[#text='No, thanks. I like to be out of the loop']")).click();
}
private void currentTime() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println( sdf.format(cal.getTime()) );
}
}
Second class code:
package com.gma.test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Logout {
public AppiumDriver driver;
#Test (priority=3)
public void LogoutApp() {
driver.findElement(By.xpath("//android.widget.TextView[#content-desc='Navigation Button']")).click();
driver.findElement(By.xpath("//android.widget.TextView[#text='Sign Out']")).click();
driver.findElement(By.xpath("//android.widget.Button[#text='Sign Out']")).click();
}
}
Contents from Testng.xml
<suite name = "GMA Automation">
<test name = "GMA Automation">
<classes>
<class name = "com.gma.test.LoginApp"/>
<class name = "com.gma.test.Logout"/>
</classes>
</test>
</suite>
I receive Null Pointer exception after execution of first class. Appium server stops saying no more commands received. Please help.
You are getting NPE b'coz you have declared one more driver reference public AppiumDriver driver; in your Logout class.
And you are using this driver reference without instantiation.
In logout class you forget to instantiate your AppiumDriver driver.
try this:
public AppiumDriver driver;
driver = new AndroidDriver();

Display Oracle Database content in JavaFX TableView

I want to display content of an Oracle Database in my TableView "FilmTable".
The structure of the Oracle Databade is: a table column named "FILMTITEL" in the table "LUKA1". The are 4 Film names like Fast and Furious, ...
Hope you can help me and i hope i understand your Solutions to solve my Problem.
I made this code and i got this Errors:
javafx.fxml.LoadException:
/D:/Users/muellerl/workspace/Table_DB/bin/application/gui.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at application.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl$8.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$7.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at controller.table_controller.initialize(table_controller.java:39)
... 20 more
If I remove all Table-Code-Contents the error isn't there.
Here is my code i hope you can help me to Display this things.
Main.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource(
"/application/gui.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
table_controller.java:
package controller;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import oracle.jdbc.*;
import model.filmtable_data;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class table_controller implements Initializable {
Connection conn;
String output;
int zaehler = 0;
#FXML
private TableView<filmtable_data> FilmTable;
#FXML
private TableColumn<filmtable_data, String> Filmtitel_col;
#FXML
private TableColumn<filmtable_data, Integer> ID_col;
#Override
public void initialize(URL location, ResourceBundle resources) {
// Observable List
final ObservableList<filmtable_data> data = FXCollections.observableArrayList();
ID_col.setCellValueFactory(new PropertyValueFactory<filmtable_data, Integer>("rID"));
Filmtitel_col.setCellValueFactory(new PropertyValueFactory<filmtable_data, String>("rFilmtitel"));
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:lukas/1234#10.140.79.39:1521:OTTO");
Statement statement = conn.createStatement();
ResultSet resultset = statement.executeQuery("SELECT FILMTITEL FROM LUKA1");
while (resultset.next()) {
output = resultset.getString(1);
zaehler++;
filmtable_data entry = new filmtable_data(zaehler, output);
data.add(entry);
System.out.println (resultset.getString(1));
System.out.println(output);
}
FilmTable.setItems(data);
statement.close();
} catch (SQLException e) {
System.out.println("Login fehlgeschlagen.");
e.printStackTrace();
}
}
}
filmtable_data.java:
package model;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class filmtable_data {
private final SimpleIntegerProperty rID;
private final SimpleStringProperty rFilmtitel;
public filmtable_data (Integer sID, String sFilmtitel) {
this.rID = new SimpleIntegerProperty(sID);
this.rFilmtitel = new SimpleStringProperty(sFilmtitel);
}
public Integer getRID() {
return rID.get();
}
public void setRID(int set) {
rID.set(set);
}
public String getRFilmtitel() {
return rFilmtitel.get();
}
public void setRFilmtitel(String set) {
rFilmtitel.set(set);
}
}

Error while connecting to MySql database using mapreduce job

I was trying the following code for connecting to a mysql database using a map-reduce job. I am facing the following error which is posted below. I have placed checkpoints in my code
which indicate that the part of the job till the job is actually run is run correctly, afterwards the job fails...
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.SequenceFileOutputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.mapred.lib.db.DBConfiguration;
//import org.apache.hadoop.mapred.lib.db.DBInputFormat;
import org.apache.hadoop.mapred.lib.db.DBInputFormat;
import org.apache.hadoop.mapred.lib.db.DBOutputFormat;
import org.apache.hadoop.mapred.lib.db.DBWritable;
public class TweetWordCount {
public static class TweetWordCountMapper extends MapReduceBase implements
Mapper<LongWritable, GetTweets, Text, IntWritable> {
private final static IntWritable intTwordsCount = new IntWritable(1);
private Text strTwoken = new Text();
public void map(LongWritable key, GetTweets value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
System.out.println("checkpoint4");
GetTweets tweets = new GetTweets();
tweets.strTweet = value.strTweet;
//TwitterTokenizer twokenizer = new TwitterTokenizer();
//List<String> twokens = twokenizer.twokenize(value.strTweet.toString());
output.collect(new Text(value.strTweet.toString()), intTwordsCount);
System.out.println("checkpoint5");
}
}
public static class TweetWordCountReducer extends MapReduceBase implements
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
System.out.println("checkpoint6");
int intTwokenCount = 0;
while (values.hasNext()) {
intTwokenCount += values.next().get();
}
output.collect(key, new IntWritable(intTwokenCount));
System.out.println("checkpoint6");
}
}
public static void main(String[] args) throws Exception {
System.out.println("checkpoint1");
JobConf twokenJobConf = new JobConf(new Configuration(),TweetWordCount.class);
//JobConf twokenJobConf = new JobConf(TweetWordCount.class);
twokenJobConf.setJobName("twoken_count");
twokenJobConf.setInputFormat(DBInputFormat.class); //Set input format here
twokenJobConf.setOutputFormat(TextOutputFormat.class);// Sets the output format
Object out = new Path("twokens");
twokenJobConf.setMapperClass(TweetWordCountMapper.class);
twokenJobConf.setCombinerClass(TweetWordCountReducer.class);
twokenJobConf.setReducerClass(TweetWordCountReducer.class);
twokenJobConf.setOutputKeyClass(Text.class);
twokenJobConf.setOutputValueClass(IntWritable.class);
DBConfiguration.configureDB(twokenJobConf, "com.mysql.jdbc.Driver",
"jdbc:mysql://localhost/test", "root", "root"); //Specifies the DB configuration
String[] fields = {"Tweet"}; //Specifies the Fields to be fetched from DB
DBInputFormat.setInput(twokenJobConf, GetTweets.class, "NewGamil",
null /* conditions */, "Tweet", fields); // Specifies the DB table and fields
//SequenceFileOutputFormat.setOutputPath(twokenJobConf, (Path) out);
FileOutputFormat.setOutputPath(twokenJobConf, (Path) out);
System.out.println("checkpoint2");
JobClient.runJob(twokenJobConf);
System.out.println("checkpoint3");
}
public static class GetTweets implements Writable, DBWritable {
String strTweet;
public GetTweets() {
}
public void readFields(DataInput in) throws IOException {
System.out.println("checkpoint 2a");
this.strTweet = Text.readString(in);
}
public void readFields(ResultSet resultSet) throws SQLException {
System.out.println("checkpoint 3a");
// this.id = resultSet.getLong(1);
this.strTweet = resultSet.getString(1);
}
public void write(DataOutput out) throws IOException {
}
public void write(PreparedStatement stmt) throws SQLException {
}
}
}
rv#ramanujan:~$ hadoop jar Twit.jar
Warning: $HADOOP_HOME is deprecated.
checkpoint1
checkpoint2
13/03/22 17:16:12 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
13/03/22 17:16:12 INFO mapred.JobClient: Cleaning up the staging area hdfs://localhost:54310/home/rv/hadoopfiles/mapred/staging/rv/.staging/job_201303221600_0008
Exception in thread "main" java.lang.RuntimeException: Error in configuring object
at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:93)
at org.apache.hadoop.util.ReflectionUtils.setConf(ReflectionUtils.java:64)
at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:117)
at org.apache.hadoop.mapred.JobConf.getInputFormat(JobConf.java:575)
at org.apache.hadoop.mapred.JobClient.writeOldSplits(JobClient.java:981)
at org.apache.hadoop.mapred.JobClient.writeSplits(JobClient.java:973)
at org.apache.hadoop.mapred.JobClient.access$600(JobClient.java:172)
at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:889)
at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:842)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:416)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1059)
at org.apache.hadoop.mapred.JobClient.submitJobInternal(JobClient.java:842)
at org.apache.hadoop.mapred.JobClient.submitJob(JobClient.java:816)
at org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:1253)
at TweetWordCount.main(TweetWordCount.java:107)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.hadoop.util.RunJar.main(RunJar.java:156)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:88)
... 20 more
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.hadoop.mapred.lib.db.DBInputFormat.configure(DBInputFormat.java:271)
... 25 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at org.apache.hadoop.mapred.lib.db.DBConfiguration.getConnection(DBConfiguration.java:123)
at org.apache.hadoop.mapred.lib.db.DBInputFormat.configure(DBInputFormat.java:266)
... 25 more
Make sure you use -libjars in commands. like the following :
bin/hadoop jar wordcount.jar org.myorg.DBCountPageView -libjars mysql-connector-java-5.1.26-bin.jar
Please reference http://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html's usage part
Not sure what the scope of your app is (learning, dev, etc.) but I would suggest to use Sqoop to interact with a relational database such as MySQL.

Resources