com.ifeng.computing.data.DataFactoryTest.java Source code

Java tutorial

Introduction

Here is the source code for com.ifeng.computing.data.DataFactoryTest.java

Source

/*
 * Copyright 2010-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ifeng.computing.data;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ifeng.computing.commons.util.TDateUtils;
import com.ifeng.computing.data.EventLogDataDocument.EventLogData;

/**
 * @version 0.1
 *
 * @author Hefei Li
 *
 * @since Sep 28, 2014
 */
public class DataFactoryTest {
    private static final Logger log = LoggerFactory.getLogger(DataFactoryTest.class);

    @Test
    public void genEventLogData() {
        String fileName = TDateUtils.getTime("yyMMddHHmmss");
        PrintWriter pw = null;

        try {
            pw = new PrintWriter(new File("src\\test\\eventlog-" + fileName + ".json"));

            Random random = new Random();

            for (int i = 0; i < MAX_ITEM_SIZE; i++) {
                int id = (random.nextInt(100) + 1);

                EventLogDataDocument doc = EventLogDataDocument.Factory.newInstance();
                EventLogData data = EventLogData.Factory.newInstance();

                doc.setEventLogData(data);

                ObjectMapper mapper = new ObjectMapper();

                mapper.writeValue(new File("src\\test\\VideoModel.json"), doc);

                //pw.append(""+id+" ------- ------------  -------------");
                pw.println();
            }

        } /*catch (JsonGenerationException e) {
           log.error(e.getMessage(), e);
          } catch (JsonMappingException e) {
           log.error(e.getMessage(), e);
          }*/ catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (pw != null) {
                pw.flush();
                pw.close();
            }
        }

    }

    private static final int MAX_ITEM_SIZE = 100;
}