View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.lang3.SerializationUtils;
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  /**
26   * Sanity check that a custom enum can be properly serialized and deserialized on a give JRE.
27   *
28   * @since 2.1
29   */
30  public class FileTypeTest {
31  
32      private static class Fixture implements Serializable {
33          private static final long serialVersionUID = 1L;
34          private final FileType fileType = FileType.FILE;
35  
36          public FileType getFileType() {
37              return this.fileType;
38          }
39  
40      }
41  
42      private void assertEquals(final FileType expected, final FileType actualFileType) {
43          Assert.assertEquals(expected.getName(), actualFileType.getName());
44          Assert.assertEquals(expected.hasAttributes(), actualFileType.hasAttributes());
45          Assert.assertEquals(expected.hasChildren(), actualFileType.hasChildren());
46          Assert.assertEquals(expected.hasContent(), actualFileType.hasContent());
47      }
48  
49      private void test(final FileType expected) {
50          final byte[] serialized = SerializationUtils.serialize(expected);
51          final FileType actualFileType = (FileType) SerializationUtils.deserialize(serialized);
52          assertEquals(expected, actualFileType);
53      }
54  
55      @Test
56      public void testSerializationContainer() {
57          final Fixture expectedFixture = new Fixture();
58          final byte[] serialized = SerializationUtils.serialize(expectedFixture);
59          final Fixture actualFixture = (Fixture) SerializationUtils.deserialize(serialized);
60          assertEquals(expectedFixture.getFileType(), actualFixture.getFileType());
61      }
62  
63      @Test
64      public void testSerializationFile() {
65          test(FileType.FILE);
66      }
67  
68      @Test
69      public void testSerializationFileOrFolder() {
70          test(FileType.FILE_OR_FOLDER);
71      }
72  
73      @Test
74      public void testSerializationFolder() {
75          test(FileType.FOLDER);
76      }
77  
78      @Test
79      public void testSerializationImaginary() {
80          test(FileType.IMAGINARY);
81      }
82  }