1   /*
2    * Copyright 2006 - 2013 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.misc;
17  
18  import java.io.IOException;
19  
20  import org.junit.Assert;
21  import org.junit.Test;
22  
23  import p3j.pppm.ProjectionModel;
24  import p3j.pppm.SubPopulation;
25  import p3j.pppm.SubPopulationModel;
26  
27  /**
28   * Tests for {@link Serializer}.
29   * 
30   * Created on 01.08.2013
31   * 
32   * @author Christina Bohk
33   * @author Roland Ewald
34   */
35  public class TestSerializer {
36  
37    private static final String TEMPORARY_MODEL_FILE = "./testModel.p3j";
38  
39    final SubPopulationModel subPopModel = new SubPopulationModel();
40    {
41      subPopModel.getSubPopulations().add(
42          new SubPopulation("TestPop", true, true, true));
43      subPopModel.getSubPopulations().add(
44          new SubPopulation("TestPop2", false, false, false));
45    }
46  
47    final ProjectionModel testProjectionModel = new ProjectionModel(
48        "scenarioName", "scenarioDescription", 3, 50, 100, 2011, subPopModel);
49  
50    @Test
51    public void testSubPoopulationStorage() throws IOException,
52        ClassNotFoundException {
53      (new Serializer()).save(testProjectionModel, TEMPORARY_MODEL_FILE);
54      ProjectionModel projectionModel = (ProjectionModel) (new Serializer())
55          .load(TEMPORARY_MODEL_FILE);
56      SubPopulationModel loadedModel = projectionModel.getSubPopulationModel();
57  
58      Assert.assertEquals("Number of sub-populations should match.", subPopModel
59          .getSubPopulations().size(), loadedModel.getSubPopulations().size());
60  
61      for (int i = 0; i < subPopModel.getSubPopulations().size(); i++) {
62        SubPopulation expectedSubPop = subPopModel.getSubPopulations().get(i);
63        Assert.assertEquals("Sub-populations should be equal", expectedSubPop,
64            loadedModel.getSubPopulations().get(i));
65      }
66    }
67  
68  }